Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// open.js
// OK 09 2023
(function () {
  /*global adventurejs A*/

  /**
   * @augments {adventurejs.Verb}
   * @class open
   * @ajsnode game.dictionary.verbs.open
   * @ajsconstruct MyGame.createVerb({ "name": "open", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading OpenCloseVerbs
   * @ajscaninferindirect true
   * @summary Verb meaning open asset.
   * @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; open refrigerator</span>
   * You open the refrigerator. Contrary to the expectations created
   * by a lifetime of watching film, it does NOT contain the bloody
   * corpse of a wife or girlfriend, nor does it contain rotting meat
   * replete with buzzing flies, nor is it comically empty. In fact,
   * this refrigerator (or 'fridge, if you prefer) is full, nay, packed,
   * with healthy and nutritious looking fruits and vegetables.
   * </pre>
   * <p>
   * <strong>Open</strong> a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}.
   * Requires that the Asset to open has
   * <code>asset.dov.open.enabled</code> set to true and
   * <code>asset.is.closed</code> set to true.
   * For the sake of giving better user experience, "open" is
   * generously interpreted according to context, since players
   * might use it broadly in several contexts. For example,
   * "open" can be understood to mean unlock, unseal, unzip, or
   * unplug, if conditions suggest that's what the user means.
   * </p>
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   * @ajsdemo OpenGame, SittingRoom, Library, Playground, Objects
   */
  A.Preverbs.open = {
    name: "open",
    prettyname: "open",
    past_tense: "opened",
    synonyms: ["open"],
    unstate: "closed",
    enqueue_collections: true,
    gerund: "opening",

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

    /**
     * @ajsadverbs
     * @memberof open
     */
    accepts_adverbs: [
      "quietly",
      "quickly",
      "gently",
      "slowly",
      "carefully",
      "cautiously",
    ],

    /**
     * @memberof open
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *     singular: false,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
        singular: false,
      },
    },

    /**
     * @memberof open
     * @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 open
     * @ajsverbparams
     * with_params: {},
     */
    with_params: {},

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

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

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

      // has direct object got a registered drain asset?
      // if so use that as direct object
      if (direct_object.registered_parts?.Drain) {
        direct_object = this.game.getAsset(
          direct_object.registered_parts.Drain
        );
        if (!direct_object) return null;
        input.setAsset(1, direct_object);
        input.setInferred(1, true);
      }

      // did player input "open drain"
      // where open means unplug?
      if (direct_object.isDOV("unplug") && direct_object.is.plugged) {
        this.game.debug(
          `D1367 | ${this.name}.js | ${direct_object.id}.dov.plug, doVerb plug `
        );
        return this.game.dictionary.doVerb("unplug");
      }

      // we give some latitude in interpreting 'open'
      // see if any of these verbs can be applied
      var openverbs = ["unlock", "pick", "unseal", "unzip"];
      for (var i = 0; i < openverbs.length; i++) {
        if (
          direct_object.isDOV(openverbs[i]) &&
          !direct_object.isVerbState(openverbs[i])
        ) {
          // @TODO this complicates verb.with_results
          // when open-> unlock with auto-open
          // may need to rethink or add a setting for it
          if (direct_object.allowVerbWithNothing(openverbs[i], "dov")) {
            input.verb_params.auto_open = true;
            this.game.log(
              "L1087",
              "log",
              "high",
              `${this.name}.js > ${direct_object.id}.is.locked, doVerb unlock `,
              "verbs"
            );
            return this.game.dictionary.doVerb(openverbs[i]);
          }

          this.game.debug(
            `D1703 | ${this.name}.js | ${
              direct_object.id
            }.is.${this.game.dictionary.verbs[openverbs[i]].getState()}`
          );
          msg += `${direct_object.Articlename_is} ${this.game.dictionary.verbs[
            openverbs[i]
          ].getState()}. `;
          this.handleFailure(msg);
          return false;
        }
      }

      // verb enabled?
      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `D1144 | ${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 null;
      }

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

      // single use direct object?
      if (
        direct_object.allowVerbOnce(this.name, "dov") &&
        direct_object.didVerb(this.name, "dov")
      ) {
        this.game.debug(
          `D1825 | ${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;
      }

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

        // indirect objects available?
        if (!direct_object.hasIndirectObjects(this.name)) {
          this.game.debug(
            `D1754 | ${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 null;
        }

        // infer indirect object?
        results = this.tryToInferIndirectObject({
          direct_object: direct_object,
          context: player,
          handle_input: true,
        });
        if (results.prompt) {
          this.game.debug(`D1756 | ${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";
          indirect_inferred = true;
          input.setAsset(2, indirect_object);
          input.setPreposition(2, indirect_preposition);
          input.setStructure("verb noun preposition noun");
          this.game.printInferred(
            `${indirect_preposition} ${indirect_object.articlename}`
          );
        }
      } // verb noun

      // sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        // 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(
            `D1356 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing `
          );
          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.allowVerbWithAsset(this.name, indirect_object, "dov")
        ) {
          this.game.debug(
            `D1357 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_assets/with_classes 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(
            `D1904 | ${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.allowVerbOnce(this.name, "iov") &&
          indirect_object.iDidVerb(this.name, "iov")
        ) {
          this.game.debug(
            `D1810 | ${this.name}.js | ${indirect_object.id}.iov.${
              this.name
            }.once and ${indirect_object.id}.did.${this.name}.indirectly is ${
              indirect_object.did[this.name].indirectly
            } `
          );
          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 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 player = this.game.getPlayer();
      var msg = "";
      var results;

      // apply state changes
      this.setState(direct_object, false);

      // compose output
      msg +=
        `$(We) ${this.name}` +
        `${direct_preposition ? " " + direct_preposition : ""}` +
        `${direct_object ? " " + direct_object.articlename : ""}` +
        `${indirect_preposition ? " " + indirect_preposition : ""}` +
        `${indirect_object ? " " + indirect_object.articlename : ""}` +
        `. `;

      // printable contents?
      if (direct_object.hasContentsAtAspect("in")) {
        msg += direct_object.getPrintableListOfContentsAt("in");
      }

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