Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// unscrew_from.js

(function () {
  /*global adventurejs A*/
  "use strict";

  /**
   * @augments {adventurejs.Verb}
   * @class unscrew_from
   * @ajsnode game.dictionary.verbs.unscrew_from
   * @ajsconstruct MyGame.createVerb({ "name": "unscrew_from", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading DeprecatedVerbs
   * @summary Summary.
   * @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; unscrew widget from gewgaw</span>
   * You unscrew the widget from the gewgaw. The framistat starts
   * blinking.
   * </pre>
   * <p>
   * <strong>Unscrew</strong> one {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} from another.
   * Requires that the unscrewed Asset has
   * asset.dov.unscrew.enabled set to true.
   * </p>
   */
  A.Preverbs.unscrew_from = {
    name: "unscrew_from",
    synonyms: ["unscrew"],
    prettyname: "unscrew",
    verb_noun_prep_noun: ["unscrew from"],

    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
    },

    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      //accepts_preposition: true,
      //requires_preposition: true,
      //accepts_these_prepositions: [ 'from' ], /* @todo */
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
    },

    doTry: function () {
      var input = this.game.getInput();
      var player = this.game.getPlayer();
      var direct_object = input.getAsset(1);
      var indirect_object = input.getAsset(2);

      // player didn't enter a second noun
      if (!indirect_object) {
        var parent = direct_object.getPlaceAsset();
        input.setAsset(2, parent);
        input.setAssumed(2);
      }

      // can't unscrew
      if (!direct_object.isDOV("unscrew")) {
        var msg = "$(We) can't unscrew " + direct_object.articlename;
        if (!input.getAssumed(2)) {
          msg += " from " + indirect_object.articlename;
        }
        msg += ". ";

        this.handleFailure(msg);
        return null;
      }

      // not attached to anything
      if (
        !direct_object.is.screwed ||
        !direct_object.isPlacedAtAspect("attached")
      ) {
        var msg = direct_object.Articlename + " isn't screwed ";
        if (input.getAssumed(2)) {
          msg += " to anything";
        } else {
          // ... to specified thing
          msg += " to " + indirect_object.articlename;
        }
        msg += ". ";
        this.handleFailure(msg);
        return null;
      }

      // not screwed to specified thing
      if (
        direct_object.isPlacedAtAspect("attached") &&
        !input.getAssumed(2) &&
        direct_object.getPlaceAssetId() !== indirect_object.id
      ) {
        var msg =
          direct_object.Articlename +
          " isn't screwed to " +
          indirect_object.articlename +
          ". ";
        this.handleFailure(msg);
        return null;
      }

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var player = this.game.getPlayer();
      var msg = "";
      var direct_object = input.getAsset(1);
      var indirect_object = input.getAsset(2);
      var doParent = direct_object.getPlaceAsset();
      var results;

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

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

      var msg =
        "$(We) unscrew " +
        direct_object.articlename +
        " from " +
        indirect_object.articlename +
        ". ";

      direct_object.is.screwed = false;
      direct_object.incrementDoVerbCount("unscrew");

      this.handleSuccess(msg, direct_object);

      return true;
    },
  };
})(); // unscrew_from