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

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

  /**
   * @augments {adventurejs.Verb}
   * @class smell
   * @ajsnode game.dictionary.verbs.smell
   * @ajsconstruct MyGame.createVerb({ "name": "smell", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading SensationVerbs
   * @summary Verb meaning smell asset, as in "smell roses".
   * @todo should return description.odor.
   * @todo smell without dObj should return room/zone/global smell.
   * @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; smell</span>
   * You smell like a daisy! Or is it a racehorse?
   * </pre>
   * <p>
   * <strong>Smell</strong> tries to found a smell description.
   * If no direct object is provided, it looks first at the current
   * room.descriptions.smell and then at the global_smell.description.
   * If a direct object is provided, it looks for
   * asset.descriptions.smell.
   * </p>
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.smell = {
    name: "smell",
    synonyms: ["smell"],
    past_tense: "smelled",

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

    /**
     * @memberof smell
     * @ajsverbphrase
     * phrase1:
     * {
     *   present: true, // @todo smells are non tangible
     * },
     */
    phrase1: {
      accepts_noun: true,
      noun_must_be: {
        present: true,
        matter: true,
      },
    },

    /**
     * @memberof close
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     in_inventory: true,
     *     known: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   accepts_these_prepositions: ["with"],
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: {
        in_inventory: true,
        known: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      accepts_these_prepositions: ["with"],
    },

    /**
     * @memberof smell
     * @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 currentRoom = this.game.getCurrentRoom();
      var player = this.game.getPlayer();
      var results;
      var msg = "";

      // sentence structure: verb
      if (input.hasStructure("verb")) {
        // does current room have smell?
        if (currentRoom.hasDescription("smell")) {
          input.verb_params.source = currentRoom.descriptions.smell;
        } else {
          // try to get global_smell
          input.verb_params.source = this.game.getGlobalAssetDescription(
            "global_smell",
            "smell",
          );
        }
        if (input.verb_params.source) {
          return true;
        } else {
          this.game.debug(`F1776 | ${this.name}.js | no smell found `);
          msg += `$(We) don't detect any particular odor. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb

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

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

      if (!direct_object.hasDescription("smell")) {
        this.game.debug(
          `F1413 | ${this.name}.js | ${direct_object.id}.descriptions.smell is unset `,
        );
        msg += `$(We) can't detect any particular odor from ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      } else {
        input.verb_params.source = direct_object.getDescription("smell");
      }

      // sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // indirect object required?
        if (direct_object.DOVallowWithNothing(this.name)) {
          return true;
        }

        // indirect objects available?
        if (!direct_object.DOVhasIndirectObjects(this.name)) {
          this.game.debug(
            `F1780 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing is false `,
          );
          msg += `$(We) don't have any way to ${this.name} ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // infer indirect object?
        results = this.tryToInferIndirectObject(direct_object, true);
        if (results.prompt) {
          this.game.debug(`F1781 | ${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) {
          indirect_object = results.indirect_object;
          indirect_preposition = "with";
        }
      } // verb noun

      // sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        // works with any indirect object?
        if (direct_object.DOVallowWithAnything(this.name)) {
          return true;
        }

        // indirect object not required?
        if (direct_object.DOVallowWithNothing(this.name)) {
          this.game.debug(
            `F1778 | ${this.name}.js | ${direct_object.id} can't be ${this.state} by ${indirect_object.id} `,
          );
          msg += `$(We) can't ${this.name} ${direct_object.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

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

        // can indirect object be used?
        if (!indirect_object.isIOV(this.name)) {
          this.game.debug(
            `F1911 | ${this.name}.js | ${indirect_object.id}.iov.${this.name}.enabled is false `,
          );
          msg += `$(We) can't ${this.name} anything ${indirect_preposition} ${indirect_object.articlename}. `;
          this.handleFailure(msg);
          return false;
        }

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

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      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 player = this.game.getPlayer();
      var results;
      var msg = "";

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

      // compose output
      msg += direct_object ? `$(We) sniff at ${direct_object.articlename}` : ``;
      msg += indirect_object
        ? `${indirect_preposition} ${indirect_object.articlename}}`
        : ``;
      msg += direct_object ? `. ` : ``;
      msg += input.verb_params.source
        ? A.getSAF.call(this.game, input.verb_params.source)
        : `$(We) don't smell anything in particular. `;

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