Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// screw.js

(function () {
  /*global adventurejs A*/

  /**
   * @augments {adventurejs.Verb}
   * @class screw
   * @ajsnode game.dictionary.verbs.screw
   * @ajsconstruct MyGame.createVerb({ "name": "screw", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @summary Verb meaning screw, as in "screw rod into socket".
   * @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; screw suppressor to pistol</span>
   * You screw the suppressor to the barrel of the pistol, carefully
   * guiding the threads into place. The seam closes tightly, squeezing
   * out a bit of grease. You wipe it away with a bit of chamois from
   * your pocket. As you refold the chamois, greasy side in, you consider
   * the chain of events that brought you here. A late phone call, a
   * veiled threat, an untrustworthy promise. You certainly have no
   * loyalties to betray here, but neither do you care to be manipulated.
   * </pre>
   * <p>
   * <strong>Screw</strong> 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>
   * @ajsverbreactions doRemoveThisFromThat(), doRemoveThatFromThis(), doMoveThisToThat(), doMoveThatToThis()
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.screw = {
    name: "screw",
    prettyname: "screw",
    past_tense: "screwed",
    synonyms: ["screw"],
    // verb_noun_prep_noun: ["screw to", "screw onto", "screw into"],
    // verb_noun_prep_prep_noun: ["screw on to", "screw in to"],
    state: "screwed",
    //unstate: "unscrewed",
    gerund: "screwing",

    /**
     * @ajsverbstructures
     * @memberof screw
     */
    accepts_structures: [
      "verb noun", // screw thing
      "verb preposition noun", // screw in lightbulb
      "verb noun preposition noun", // screw thing with thing, screw thing into thing
      "verb noun preposition noun preposition noun", // screw thing into thing with thing
    ],

    /**
     * USES
     * screw in lightbulb
     * screw cap onto bottle
     * screw off cap (unscrew)
     * screw on lid
     * screw lid onto jar
     * screw screw with screwdriver
     * screw in screw with screwdriver
     */

    /**
     * @memberof screw
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun:true,
     *   noun_must_be:
     *   {
     *     in_inventory_if_takeable: true,
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      noun_must_be: {
        in_inventory_if_takeable: true,
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
      accepts_preposition: true,
      accepts_these_prepositions: ["in", "on", "into", "onto", "off", "out"],
    },

    /**
     * @memberof screw
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun:true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   accepts_these_prepositions: [ 'to', 'in', 'into', 'onto', 'with' ],
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: {
        in_inventory_if_takeable: true,
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      accepts_these_prepositions: ["to", "in", "into", "onto", "with"],
    },

    /**
     * @memberof screw
     * @ajsverbphrase
     * phrase3:
     * {
     *   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: ["with"],
     * },
     */
    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"],
    },

    /**
     * @memberof screw
     * @ajsverbparams
     * with_params: {
     *   max_turns: 1,
     * },
     */
    with_params: {
      max_turns: 1,
    },

    doTry: function () {
      var input = this.game.getInput();
      var verb_phrase = input.verb_phrase;
      var direct_object = input.getAsset(1);
      var direct_preposition = input.getPreposition(1);
      var indirect_object = input.getAsset(2);
      var indirect_preposition = input.getPreposition(2);
      var indirect_object2 = input.getAsset(3);
      var indirect_preposition2 = input.getPreposition(3);
      var target_object, target_preposition;
      var tool_preposition = "with";
      var tool_object;
      var tool_inferred;

      var player = this.game.getPlayer();
      var results;
      var msg = "";

      if (verb_phrase === "screw to") {
        //
      }
      if (verb_phrase === "screw in to" || verb_phrase === "screw into") {
        //
      }
      if (verb_phrase === "screw on to" || verb_phrase === "screw onto") {
        //
      }

      if (
        direct_preposition &&
        (direct_preposition === "off" || direct_preposition === "out")
      ) {
        if (direct_object.isDOV("unscrew")) {
          return this.game.dictionary.doVerb("unscrew");
        }
        // @TODO handle something like "screw off cap"?
        this.game.debug(
          `D2155 | ${this.name}.js | not handling ${this.name} ${direct_preposition}`
        );
        msg += `$(We) don't know how to  ${this.name} ${direct_preposition} ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return false;
      }

      if (indirect_preposition === "with") {
        tool_object = indirect_object;
      }
      if (indirect_preposition2 === "with") {
        tool_object = indirect_object2;
      }
      if (indirect_preposition && "with" !== indirect_preposition) {
        target_object = indirect_object;
        target_preposition = indirect_preposition;
      }

      // verb enabled?
      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `D2136 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.enabled is false `
        );
        msg += `${direct_object.Articlename} can't be ${this.past_tense}. `;
        this.handleFailure(msg);
        return false;
      }

      // single use direct object?
      if (
        direct_object.allowVerbOnce(this.name, "dov") &&
        direct_object.didVerb(this.name, "dov")
      ) {
        this.game.debug(
          `D2139 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.did.${this.name}.directly `
        );
        msg += `${direct_object.Articlename} has already been ${this.past_tense}. `;
        this.handleFailure(msg);
        return false;
      }

      // verb state?
      if (this.hasState() && direct_object.isVerbState(this.name)) {
        this.game.debug(
          `D1396 | ${this.name}.js | ${
            direct_object.id
          }.is.${this.getState()} is ${direct_object.isVerbState(this.name)}`
        );
        msg += `${direct_object.Articlename_is} already ${this.getState()}. `;
        this.handleFailure(msg);
        return null;
      }

      // sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // can verb act without an indirect object?
        if (direct_object.allowVerbWithNothing(this.name, "dov")) {
          return true;
        }

        // indirect objects available?
        if (!direct_object.hasIndirectObjects(this.name)) {
          this.game.debug(
            `D2149 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing is false `
          );
          msg += `$(We) don't know of a way to ${this.name} ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return false;
        }

        // infer indirect object?
        results = this.tryToInferIndirectObject({
          direct_object: direct_object,
          context: player,
          handle_input: true,
        });
        if (results.prompt) {
          this.game.debug(`D2150 | ${this.name}.js | soft prompt for noun2 `);
          msg += `What would $(we) like to ${this.name} ${direct_object.articlename} with? `;
          this.handleFailure(msg);
          return false;
        } else if (results.success) {
          tool_inferred = true;
          tool_object = results.indirect_object;
          tool_preposition = "with";
          this.game.printInferred(
            `${tool_preposition} ${tool_object.articlename}`
          );
        }
      } // verb noun

      // tool_object
      // acting as sentence structure: verb noun preposition noun
      if (tool_object) {
        input.verb_params.tool_object = tool_object;
        input.verb_params.tool_preposition = tool_preposition;
        // is player holding asset?
        // only error if tool_object was specified, not inferred
        if (
          !this.game.parser.selectInHands(tool_object.id).length &&
          !tool_inferred
        ) {
          this.game.debug(
            `D2151 | ${this.name}.js | ${tool_object.id}.$is("inhands") is false `
          );
          msg += `$(We're) not holding ${tool_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // works with any indirect object?
        if (direct_object.allowVerbWithAnything(this.name, "dov")) {
          return true;
        }

        // indirect object not required?
        if (direct_object.allowVerbWithNothing(this.name, "dov")) {
          this.game.debug(
            `D2152 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing `
          );
          msg += `$(We) don't need ${tool_object.articlename} to ${this.name} ${
            direct_preposition ? direct_preposition : ""
          } ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // indirect object usable with direct object?
        if (!direct_object.allowVerbWithAsset(this.name, tool_object, "dov")) {
          this.game.debug(
            `D2153 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_assets/with_classes does not include ${tool_object.id} `
          );
          msg += `$(We) can't ${this.name} ${direct_object.articlename} ${tool_preposition} ${tool_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // can indirect object be used?
        if (!tool_object.isIOV(this.name)) {
          this.game.debug(
            `D2154 | ${this.name}.js | ${tool_object.id}.iov.${this.name}.enabled is false `
          );
          msg += `$(We) can't ${this.name} ${
            direct_preposition ? direct_preposition : ""
          } anything ${tool_preposition} ${tool_object.articlename}. `;
          this.handleFailure(msg);
          return false;
        }

        // single use indirect object?
        if (
          tool_object.allowVerbOnce(this.name, "iov") &&
          tool_object.iDidVerb(this.name, "iov")
        ) {
          this.game.debug(
            `D1395 | ${this.name}.js | ${tool_object.id}.iov.${
              this.name
            }.once and ${tool_object.id}.did.${this.name}.indirectly is ${
              tool_object.did[this.name].indirectly
            } `
          );
          msg += `${tool_object.Articlename} has already been used to ${
            this.name
          } ${direct_preposition ? direct_preposition : ""} something. `;
          this.handleFailure(msg);
          return null;
        }
      } // tool_object

      // target_object
      if (target_object) {
        input.verb_params.target_object = target_object;
        input.verb_params.target_preposition = target_preposition;
        // is this already in that?
        if (
          direct_object.getPlaceAssetId === target_object.id &&
          direct_object.isPlacedAtAspect("attached")
        ) {
          if (
            direct_object.is.screwed >=
            direct_object.dov[this.name].with_params.max_turns
          ) {
            this.game.debug(
              `D1394 | ${this.name}.js | ${direct_object.id} is already screwed  ${target_preposition} ${target_object.id}`
            );
            msg += `${direct_object.Articlename_is} already screwed ${target_preposition}    } to ${indirect_object.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
        } // already attached

        // can we attach this particular thing to that particular thing?
        else {
          results = this.tryToPutThisInThatAspect(
            direct_object,
            target_preposition,
            target_object
          );
          if (results.fail) {
            msg = results.msg;
            this.handleFailure(msg);
            if (results.end_turn) return false;
            return null;
          }
        }
      } // target_object

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var verb_phrase = input.verb_phrase;
      var player = this.game.getPlayer();
      var direct_object = input.getAsset(1);
      var direct_preposition = input.getPreposition(1);

      var tool_object = input.verb_params.tool_object;
      var tool_preposition = input.verb_params.tool_preposition;
      var target_object = input.verb_params.target_object;
      var target_preposition = input.verb_params.target_preposition;

      var doParent = direct_object.getPlaceAsset();
      // var indirect_object = input.getAsset(2);
      var msg = "";
      var results;

      if (target_object && target_object.id !== doParent.id) {
        // remove thing from current parent
        results = direct_object.moveFrom(doParent);
        if ("undefined" !== typeof results) return results;

        // add thing to new parent
        results = direct_object.moveTo("attached", target_object);
        if ("undefined" !== typeof results) return results;
      }

      // apply state changes
      //this.setState(direct_object, true);
      direct_object.is.screwed = Math.min(
        direct_object.is.screwed + 1,
        direct_object.dov[this.name].with_params.max_turns
      );

      // compose output
      msg +=
        `$(We) ${this.name}` +
        `${direct_preposition ? " " + direct_preposition : ""}` +
        `${direct_object ? " " + direct_object.articlename : ""}` +
        `${target_preposition ? " " + target_preposition : ""}` +
        `${target_object ? " " + target_object.articlename : ""}` +
        `${tool_preposition ? " " + tool_preposition : ""}` +
        `${tool_object ? " " + tool_object.articlename : ""}` +
        `. `;

      // print output
      return this.handleSuccess(msg, direct_object);
    },
  };
})(); // screw