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

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

  /**
   * @augments {adventurejs.Verb}
   * @class untie_from
   * @ajsnode game.dictionary.verbs.untie_from
   * @ajsconstruct MyGame.createVerb({ "name": "untie_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; untie leash from tree</span>
   * You untie the leash from the tree, releasing your pal Fido.
   * Fido runs across the field and disappears into the trees,
   * chasing rabbits and barking all the way.
   * </pre>
   * <p>
   * <strong>Untie</strong> one {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} from another.
   * Requires that the untied Asset has
   * asset.dov.untie.enabled set to true.
   * </p>
   */
  A.Preverbs.untie_from = {
    name: "untie_from",
    prettyname: "untie",
    synonyms: [],
    verb_noun_prep_noun: ["untie 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 direct_object = input.getAsset(1);
      var indirect_object = input.getAsset(2);
      var player = this.game.getPlayer();

      // example: untie rope from table
      // rope is direct_object
      // table is indirect_object

      // is the rope tied to the thing?
      if (
        direct_object.dov.tie?.with_params.connections.indexOf(
          indirect_object.id
        ) === -1
      ) {
        var msg = direct_object.Articlename + " isn't";
        if (!direct_object.dov.tie || !indirect_object.iov.tie) {
          (", and can't be,");
        }
        msg += " tied to " + indirect_object.articlename + ". ";
        this.handleFailure(msg);
        return null;
      }

      // can't be untied, which might be true even if it's a rope
      if (false === direct_object.isDOV("untie")) {
        var msg = "$(We) can't untie " + direct_object.articlename + ". ";
        this.handleFailure(msg);
        return null;
      }

      return true;
    },

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

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

      results = direct_object.onUntieThisFromThat(indirect_object);
      if ("undefined" !== typeof results) return results;

      if (
        this.game.settings
          .on_untie_rope_from_object_outside_inventory_take_rope &&
        false === direct_object.isIn(player) &&
        0 === direct_object.dov.tie?.with_params.connections.length
      ) {
        // set thing's new location to player
        results = player.onMoveThatToThis(direct_object, "in");
        if ("undefined" !== typeof results) return results;

        msg += ", and take " + direct_object.articlename;
      }

      msg += ". ";

      this.handleSuccess(msg, direct_object);

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