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

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

  /**
   * @augments {adventurejs.Verb}
   * @class plug
   * @ajsnode game.dictionary.verbs.plug
   * @ajsconstruct MyGame.createVerb({ "name": "plug", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @ajsstate plugged
   * @summary Verb meaning plug, as in "plug sink".
   * @tutorial Scripting_VerbSubscriptions
   * @tutorial Verbs_VerbAnatomy
   * @tutorial Verbs_VerbProcess
   * @tutorial Verbs_ModifyVerbs
   * @tutorial Verbs_WriteVerbs
   * @classdesc
   * <pre class="display">
   * <span class="input">&gt; plug rusty drain</span>
   * You plug the rusty drain with the rubber stopper. It makes a
   * poor seal and probably won't hold water for very long.
   * Whatever you have in mind, you'd better do it quickly.
   * </pre>
   * <p>
   * <strong>Plug</strong> can be understood a couple of ways.
   * In the case of a sink that can be plugged without
   * the use of another asset, "plug sink" will be equivalent
   * to open/close. Here's how you might create this sink:
   * </p>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   class: "Sink",
   *   name: "sink",
   *   dov: { plug: { with_nothing: true, }, },
   * })</code></pre>
   * <p>
   * In the case of a sink that can be plugged by inserting another asset,
   * like a stopper, to plug it, "plug sink with stopper" will mean
   * putting the stopper in the sink (or sink drain, if it has one).
   * Here's how you might create this sink and stopper:
   * </p>
   * <pre class="display"><code class="language-javascript">MyGame.createAsset({
   *   class: "Sink",
   *   name: "sink",
   *   dov: { plug: { with_assets: ['stopper'], }, },
   * })
   * MyGame.createAsset({
   *   class: "Plug",
   *   name: "stopper",
   *   iov: { plug: { with_assets: ['sink'], }, },
   * })</code></pre>
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   * @ajsdemo PlugGame, PlugSomethingIntoSomething, PlugSomethingIntoNothing, PlugSomethingWithSomething, PlugSomethingWithNothing
   */
  A.Preverbs.plug = {
    name: "plug",
    prettyname: "plug",
    past_tense: "plugged",
    synonyms: ["plug"],
    state: "plugged",
    state_strings: { state: "plugged", unstate: "unplugged" },
    state_string: "plugged",
    unstate_string: "unplugged",

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

    /**
     * @memberof plug
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   accepts_preposition: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
      accepts_preposition: true,
    },

    /**
     * @memberof plug
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *     in_inventory_if_takeable: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   accepts_these_prepositions: [ "with" ],
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
        in_inventory_if_takeable: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      accepts_these_prepositions: ["with"],
    },

    /**
     * @memberof plug
     * @ajsverbparams
     * with_params: {
     *   connections: [],
     *   max_connections: 1,
     *   take_breaks_connections: true,
     *   take_takes_connections: false,
     * },
     */
    with_params: {
      connections: [],
      max_connections: 1,
      take_breaks_connections: true,
      take_takes_connections: false,
    },

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

      // has direct object got a registered drain?
      if (direct_object.registered_parts?.Drain) {
        drain = this.game.getAsset(direct_object.registered_parts.Drain);
        if (drain) {
          direct_object = drain;
          input.setAsset(1, direct_object);
          input.setAssumed(1);
        }
      }

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

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

      // can be direct object of verb?
      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `F1730 | ${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.isState(this.name)) {
        this.game.debug(
          `F1766 | ${this.name}.js | ${
            direct_object.id
          }.is.${this.getState()} is ${direct_object.isState(this.name)}`,
        );
        msg += `${direct_object.Articlename} is already ${this.getState()}. `;
        this.handleFailure(msg);
        return false;
      }

      // single use direct object?
      if (
        direct_object.DOVallowOnce(this.name) &&
        direct_object.DOVdidDo(this.name)
      ) {
        this.game.debug(
          `F1827 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.dov.${this.name}.did_do `,
        );
        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.DOVallowWithNothing(this.name)) {
          return true;
        }

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

        // infer indirect object?
        results = this.tryToInferIndirectObject(direct_object, true);
        if (results.prompt) {
          this.game.debug(`F1470 | ${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(
            `F1243 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing `,
          );
          msg += `${direct_object.Articlename} doesn't need anything to ${this.name} it with. `;
          this.handleFailure(msg);
          return null;
        }

        // indirect object usable with direct object?
        if (!direct_object.DOVallowWithAsset(this.name, indirect_object)) {
          this.game.debug(
            `F1715 | ${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(
            `F1906 | ${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(
            `F1812 | ${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 player = this.game.getPlayer();
      var direct_object = input.getAsset(1);
      var direct_preposition = input.getPreposition(1);
      var direct_place = direct_object.getPlaceAsset();
      var indirect_object = input.getAsset(2);
      var indirect_preposition = input.getPreposition(2);
      var msg = "";
      var results;

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

      // sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        // is plug in player inventory?
        if (indirect_object.isIn(player)) {
          // remove indirect_object from player
          results = indirect_object.moveFrom(player);
          if ("undefined" !== typeof results) return results;
        }

        // move indirect_object to direct_object
        results = indirect_object.moveTo("in", direct_object);
        if ("undefined" !== typeof results) return results;

        // connect direct object to indirect object
        this.setVerbSubscriptionConnection(direct_object, indirect_object);
        //direct_object.DOVsetConnection(this.name, indirect_object);
      }

      // apply state changes
      this.setState(direct_object, true);
      // partially redundant because if we have an indirect object
      // it'll have been handled by onMoveThatToThis but still required
      // if dov.plug.with_nothing = true

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

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