Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// attach.js
// OK 09 2023
(function () {
  /*global adventurejs A*/
  "use strict";

  /**
   * @augments {adventurejs.Verb}
   * @class attach
   * @ajsnode game.dictionary.verbs.attach
   * @ajsconstruct MyGame.createVerb({ "name": "attach", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @summary Verb meaning attach one tangible asset to another.
   * @tutorial Scripting_VerbSubscriptions
   * @tutorial Verbs_VerbAnatomy
   * @tutorial Verbs_VerbProcess
   * @tutorial Verbs_ModifyVerbs
   * @tutorial Verbs_WriteVerbs
   * @classdesc
   * <pre class="display border outline">
   * <span class="input">&gt; attach crank to chicken</span>
   * You attach the crank to the chicken, creating an eggan grinder.
   * </pre>
   * <p>
   * Attach one {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} to another.
   * Requires that the receiving Asset has an <code>attach</code>
   * {@link adventurejs.Aspect|Aspect}.
   * To learn about Aspects, see
   * <a href="/doc/Tangibles_Aspects.html">How to Use Aspects</a>.
   * </p>
   * <h3>Logic Notes</h3>
   * <code>Attach</code> tries to move an asset into an aspect
   * (aka aspects.attached) of another asset.
   * @ajseventhooks
   * tryAttachThis
   * tryAttachThis[Preposition]That
   * tryAttachThat[Preposition]This
   * tryAttachThis[Preposition]That[Preposition]That
   * tryAttachThat[Preposition]This[Preposition]That
   * tryAttachThat[Preposition]That[Preposition]This
   * doAttachThis
   * doAttachThis[Preposition]That
   * doAttachThat[Preposition]This
   * doAttachThis[Preposition]That[Preposition]That
   * doAttachThat[Preposition]This[Preposition]That
   * doAttachThat[Preposition]That[Preposition]This
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */

  A.Preverbs.attach = {
    name: "attach",
    prettyname: "attach",
    past_tense: "attached",
    synonyms: ["attach", "connect"],
    //state: "attached", // state should be determined by place

    /**
     * @ajsverbstructures
     * @memberof attach
     */
    accepts_structures: [
      "verb noun",
      "verb noun preposition noun",
      "verb noun preposition noun preposition noun",
    ],

    /**
     * @memberof attach
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   // accepts_plural_noun: true, // @TODO allow "attach A and B"
     *   noun_must_be:
     *   {
     *     in_inventory_if_takeable: true,
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     reachable: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        in_inventory_if_takeable: true,
        known: true,
        tangible: true,
        present: true,
        reachable: true,
      },
    },

    /**
     * @memberof attach
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     in_inventory_if_takeable: true,
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     reachable: true,
     *   },
     *   accepts_preposition:true,
     *   requires_preposition: true,
     *   accepts_these_prepositions: ["to","with"],
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: {
        in_inventory_if_takeable: true,
        known: true,
        tangible: true,
        present: true,
        reachable: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      accepts_these_prepositions: ["to", "with"],
    },

    /**
     * @memberof attach
     * @ajsverbphrase
     * phrase3:
     * {
     * },
     */
    phrase3: {
      accepts_noun: true,
      noun_must_be: {
        in_inventory_if_takeable: true,
        known: true,
        tangible: true,
        present: true,
        reachable: true,
        //visible: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      accepts_these_prepositions: ["with", "to"],
    },

    /**
     * @memberof attach
     * @ajsverbparams
     * Unlike other verbs that make attachments, like plug and tie,
     * attach doesn't use verb param connections. Instead it results
     * in putting one asset inside another asset.aspects.attached.
     * with_params: {},
     */
    with_params: {},

    doTry: function () {
      var input = this.game.getInput();
      var player = this.game.getPlayer();

      var object1 = input.getAsset(1);
      var object2, object2_preposition;
      var tool, tool_preposition;

      var results, results2;
      var msg = "";

      // can direct_object be tied?
      if (object1.isDOV("tie")) {
        this.game.debug(`F1234 | ${this.name}.js | infer verb tie `);
        this.game.print(msg);
        this.game.dictionary.doVerb("tie");
        return null;
      }

      // parsed sentence structure: verb
      if (input.hasStructure("verb")) {
      }

      // parsed sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // prompt for object2, object to attach to
        input.setPreposition(2, "to");
        input.setSoftPrompt({
          noun2: true,
          structure: "verb noun preposition noun",
        });
        this.game.debug(`F1233 | ${this.name}.js | soft prompt for noun2 `);
        msg += `What would $(we) like to ${this.name} ${object1.articlename} to? `;
        this.handleFailure(msg);
        return null;
      } // verb noun

      // did player input with/with or to/to ?
      if (input.getPreposition(2) === input.getPreposition(3)) {
        // we don't understand that
        this.game.debug(
          `F1638 | ${this.name}.js | expected A to B with C, received two identical prepositions `,
        );
        msg += this.game.parser.getUnparsedMessage(this.game.getInput().input);
        this.handleFailure(msg);
        return null;
      }

      // did player input 'attach A with B to C'?
      if (
        "with" === input.getPreposition(2) &&
        "to" === input.getPreposition(3)
      ) {
        // swap B with C
        input.swapPhrases(2, 3);
      }

      // did player input 'attach A with B'?
      if ("with" === input.getPreposition(2) && !input.hasPhrase(3)) {
        // change 'with B' to 'with C' and prompt for 'to B'
        input.swapPhrases(2, 3);
        input.setPreposition(2, "to");
        input.setSoftPrompt({
          noun2: true,
          structure: "verb noun preposition noun preposition noun",
        });
        this.game.debug(`F1523 | ${this.name}.js | soft prompt for noun2 `);
        msg += `What would $(we) like to ${this.name} ${object1.articlename} to? `;
        this.handleFailure(msg);
        return null;
      }

      // at this point we should have 'attach A to B' or 'attach A to B with C'
      object2 = input.getAsset(2);
      object2_preposition = input.getPreposition(2);

      // these things apply to 'verb noun preposition noun' and 'verb noun preposition noun preposition noun'

      // verb enabled for one or the other?
      if (!object1.isDOV(this.name) && !object2.isDOV(this.name)) {
        this.game.debug(
          `F1349 | ${this.name}.js | neither ${object1.id} nor ${object2.id}.dov.${this.name}.enabled `,
        );
        msg += `${object1.Articlename} and ${object2.articlename} can't be ${this.past_tense}. `;
        this.handleFailure(msg);
        return null;
      }

      // can either of these assets be attached to the other?
      if (
        !object1.canBePut("attached", object2) &&
        !object2.canBePut("attached", object1)
      ) {
        this.game.debug(
          `F1235 | ${this.name}.js | neither asset is listed in the other's .aspects.attached.with_assets/with_classes `,
        );
        msg += `${object1.Articlename} and ${object2.articlename} can't be ${this.past_tense}. `;
        this.handleFailure(msg);
        return null;
      }

      // does object2 actually go in object1?
      if (
        !object1.canBePut("attached", object2) &&
        object2.canBePut("attached", object1)
      ) {
        input.swapNouns(1, 2);
        object1 = input.getAsset(1);
        object2 = input.getAsset(2);
      }

      // sentence structure: verb noun preposition noun
      // we put this way down here after all the object swapping is done
      if (input.hasStructure("verb noun preposition noun")) {
        // call actions
      }

      // already attached to something (else?)
      // TODO multiple attachments
      if (object1.isPlacedAtAspectAndAsset("attached", object2.id)) {
        this.game.debug(
          `F1236 | ${this.name}.js | ${object1.id} is attached to ${object2.id} `,
        );
        msg = `${object1.Articlename} is already attached to ${object2.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // tool, maybe?
      tool = input.getAsset(3);
      tool_preposition = input.getPreposition(3);

      // no tool?
      if (!tool) {
        // is a tool needed?
        if (
          !object1.DOVallowWithNothing(this.name) &&
          !object2.DOVallowWithNothing(this.name)
        ) {
          // tool needed
          results = this.tryToInferIndirectObject(object1);
          results2 = this.tryToInferIndirectObject(object2);
          if (results.success || results2.success) {
            // found a tool
            var either_one = results.success
              ? results.success
              : results2.success;
            tool = either_one.indirect_object;
            tool_preposition = "with";
            input.setNewPhrase({
              asset: tool,
              preposition: tool_preposition,
            });
            input.setStructure("verb noun preposition noun preposition noun");
          } else if (results.prompt || results2.prompt) {
            // prompt for tool
            // restructure sentence for next turn
            input.setPreposition(3, "with");
            input.setSoftPrompt({
              noun3: true,
              structure: "verb noun preposition noun preposition noun",
            });
            this.game.debug(`F1672 | ${this.name}.js | soft prompt for noun3 `);
            msg += `What would $(we) like to ${this.name} ${object1.articlename} to ${object2.articlename} with? `;
            this.handleFailure(msg);
            return false;
          }
        } // tool needed?
      } // no tool

      // by now we should have an input tool or an inferred tool or no tool needed

      // sentence structure: verb noun preposition noun preposition noun
      // ie: 'attach male pipe to female pipe with wrench'
      if (input.hasStructure("verb noun preposition noun preposition noun")) {
        // works with any indirect object?
        if (
          object1.DOVallowWithAnything(this.name) ||
          object2.DOVallowWithAnything(this.name)
        ) {
          return true;
        }

        // indirect object not required?
        if (
          object1.DOVallowWithNothing(this.name) &&
          object2.DOVallowWithNothing(this.name)
        ) {
          this.game.debug(
            `F1753 | ${this.name}.js | ${object1.id} and ${object2.id}.dov.${this.name}.with_nothing `,
          );
          msg += `${object1.Articlename} and ${object2.articlename} don't need another object to ${this.name} them. `;
          this.handleFailure(msg);
          return null;
        }

        // indirect object usable with direct object?
        if (
          !object1.DOVallowWithAsset(this.name, tool) &&
          !object2.DOVallowWithAsset(this.name, tool)
        ) {
          this.game.debug(
            `F1750 | ${this.name}.js | neither ${object1.id} nor ${object2.id}.dov.${this.name}.with_assets/with_classes includes ${tool.id} `,
          );
          msg += `${tool.Articlename} can't be used to ${this.name} ${object1.articlename} to ${object2.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // single use indirect object?
        if (tool && tool.IOVallowOnce(this.name) && tool.IOVdidDo(this.name)) {
          this.game.debug(
            `F1820 | ${this.name}.js | ${tool.id}.iov.${this.name}.once and ${
              tool.id
            }.iov.${this.name}.do_count is ${tool.iov[this.name].do_count} `,
          );
          msg += `${tool.Articlename} has already been used to ${this.name} something. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb noun preposition noun preposition noun

      return true;
    }, // doTry

    doSuccess: function () {
      var input = this.game.getInput();
      var player = this.game.getPlayer();

      var object1 = input.getAsset(1);
      var object1_parent = object1.getPlaceAsset();

      var object2 = input.getAsset(2);
      var object2_preposition = input.getPreposition(2);

      var tool = input.getAsset(3);
      var tool_preposition = input.getPreposition(3);

      var results;
      var msg = "";

      this.game.debug(`F1237 | ${this.name}.js | print doSuccess `);

      // set direct object place
      if (object1_parent.id !== object2.id) {
        // remove direct object from current parent
        results = object1.moveFrom(object1_parent);
        if ("undefined" !== typeof results) return results;

        // add direct object to new parent
        results = object1.moveTo("attached", object2);
        if ("undefined" !== typeof results) return results;
      }

      // apply state changes
      // object1.is[this.getState()] = true;

      // compose output
      msg += `$(We) ${this.name} ${object1.articlename}`;
      msg += object2 ? ` to ${object2.articlename}` : ``;
      msg += tool ? ` with ${tool.articlename}` : ``;
      msg += `. `;

      // print output
      this.handleSuccess(msg, object1);
      return true;
    },
  };
})(); // attach