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

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

  /**
   * @augments {adventurejs.Verb}
   * @class give
   * @ajsnode game.dictionary.verbs.give
   * @ajsconstruct MyGame.createVerb({ "name": "give", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @summary Verb meaning give asset to character.
   * @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; give incriminating evidence to butler</span>
   * You give the piece of incriminating evidence to the butler.
   * He blanches, then turns to flee. The chase is on!
   * </pre>
   * <strong>Give</strong> a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} to an
   * {@link adventurejs.NPC|NPC} Asset. Requires
   * that the Tangible Asset is in player's inventory.
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.give = {
    name: "give",
    prettyname: "give",
    past_tense: "gave",
    synonyms: [],
    //verb_noun_prep_noun: ["give to"],

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

    /**
     * @memberof give
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun:true,
     *   requires_noun:true,
     *   noun_must_be:
     *   {
     *     in_inventory: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        in_inventory: true,
      },
    },

    /**
     * @memberof give
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun:true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   accepts_these_prepositions: ["to"],
     * },
     */
    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        character: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      accepts_these_prepositions: ["to"],
    },

    /**
     * @memberof give
     * @ajsverbparams
     * with_params: {},
     */
    with_params: {},

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

      // can't give this thing
      if (!direct_object.isDOV("give")) {
        this.game.debug(
          `F1292 | ${this.name}.js | ${direct_object.id}.dov.give.enabled is false`,
        );
        msg += `$(We) can't give ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // can't remove this thing
      if (direct_object.is.worn && !direct_object.isDOV("remove")) {
        this.game.debug(
          `F1294 | ${this.name}.js | ${direct_object.id}.dov.remove.enabled is false`,
        );
        msg += `$(We) can't remove ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // parsed sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // find a character in the room
        let chars = this.game.getCurrentRoom().findClassInThis("Character", {
          exclude: [player.id],
        });
        for (let i = chars.length - 1; i > -1; i--) {
          if (!this.game.getAsset(chars[i]).isIOV("give")) {
            chars.splice(i, 1);
          }
        }
        switch (chars.length) {
          case 1:
            indirect_object = this.game.getAsset(chars[0]);
            indirect_preposition = "to";
            input.setAsset(2, indirect_object);
            input.setPreposition(2, "to");
            input.setAssumed(2);
            input.setStructure("verb noun preposition noun");
            break;
          default:
            input.setPreposition(2, "to");
            input.setSoftPrompt({
              noun2: true,
              structure: "verb noun preposition noun",
            });
            this.game.debug(`F1924 | ${this.name}.js | soft prompt for noun2 `);
            msg += `To whom would $(we) like to ${this.name} ${direct_object.articlename}? `;
            this.handleFailure(msg);
            return null;
        }
      } // verb noun

      // parsed sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        // give to only works with characters
        if (!(indirect_object instanceof adventurejs.Character)) {
          this.game.debug(
            `F1290 | ${this.name}.js | ${indirect_object.id} is not class Character`,
          );
          msg += `$(We) can't give anything to ${indirect_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // can't give to this character
        if (!indirect_object.isIOV("give")) {
          this.game.debug(
            `F1291 | ${this.name}.js | ${indirect_object.id}.iov.give is unset`,
          );
          msg += `$(We) can't give anything to ${indirect_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }
      }
      return true;
    },

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

      this.game.debug(`F1293 | ${this.name}.js | print doSuccess`);
      // if player is giving thing that is nested in inventory,
      // we'll auto-open any containers if necessary
      if (direct_object.isIn(player) && direct_object.areAnscestorsClosed()) {
        closedAnscestors = direct_object.getClosedAnscestors();
        for (var i = 0; i < closedAnscestors.length; i++) {
          this.game.getAsset(closedAnscestors[i]).is.closed = false;
          closedAnscestors[i] = this.game.getAsset(closedAnscestors[i]).name;
        }
      }

      if (closedAnscestors.length > 0) {
        msg += "$(We) open the ";
        for (var i = 0; i < closedAnscestors.length; i++) {
          if (closedAnscestors.length > 2 && i < closedAnscestors.length - 2) {
            msg += ", ";
          }
          if (
            closedAnscestors.length > 1 &&
            i === closedAnscestors.length - 1
          ) {
            msg += " and the ";
          }
          msg += closedAnscestors[i];
        }
        msg += ". ";
      }

      // if it's an article of clothing, remove it
      if (direct_object.is.worn) {
        results = direct_object.unfasten();
        msg += results ? `$(We) ${results}, then remove, ` : `$(We) remove `;
        msg += `${direct_object.articlename}. `;
        direct_object.incrementDoVerbCount("remove", 1);
        direct_object.is.worn = false;
      }

      // remove thing from its current container
      results = parent.onRemoveThatFromThis(direct_object);
      if ("undefined" !== typeof results) return results;

      // set thing's new location to indirect_object
      results = indirect_object.onMoveThatToThis(direct_object, "in");
      if ("undefined" !== typeof results) return results;

      // compose output
      msg += `$(We) give ${direct_object.articlename} to ${indirect_object.articlename}. `;

      if (
        (1 < input.parsedNoun1.matches.qualified.length ||
          this.game.parser.isParsingMultiple()) &&
        -1 === input.output_class.indexOf("concatenate_output")
      ) {
        input.output_class += " concatenate_output ";
      }

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