// unplug.js
(function () {
/*global adventurejs A*/
/**
* @augments {adventurejs.Verb}
* @class unplug
* @ajsnode game.dictionary.verbs.unplug
* @ajsconstruct MyGame.createVerb({ "name": "unplug", [...] });
* @ajsconstructedby adventurejs.Dictionary#createVerb
* @hideconstructor
* @ajsinstanceof Verb
* @ajsnavheading ManipulationVerbs
* @summary Verb meaning unplug, as in "unplug computer".
* @tutorial Scripting_VerbSubscriptions
* @tutorial Verbs_VerbAnatomy
* @tutorial Verbs_VerbProcess
* @tutorial Verbs_ModifyVerbs
* @tutorial Verbs_WriteVerbs
* @classdesc
* <pre class="display border outline">
* <span class="input">> unplug computer</span>
* You unplug the computer! The screen goes dead!
* But did you stop the upload in time???
* </pre>
*
* <p>
* <strong>Unplug</strong> is understood to have four distinct uses.
* As an unverb to <a href="/doc/plug.html" class="property">plug</a>,
* it can unplug a sink in the sense of opening it, or unplug a
* sink in the sense of removing a stopper from it.
* As an unverb to <a href="/doc/plugIn.html" class="property">plugIn</a>,
* it can unplug a computer from an abstract non-specific connection,
* or unplug a computer from a specific connection like an outlet.
* Unplug determines based on context which situation is implied.
* Authors can determine which one to apply on a per-object basis
* through a
* {@link adventurejs.Tangible|Tangible}
* {@link adventurejs.Asset|Asset's}
* verb subscriptions.
* </p>
*
* <p>
* <code>sink.dov.plug.with_nothing</code><br>
* If this meaning is set, unplug will interpret "unplug sink"
* to mean simply changing the plugged state of the direct object.
* </p>
*
* <p>
* <code>sink.dov.plug.with_assets = ['stopper']</code><br>
* If this meaning is set, unplug will interpret "unplug sink"
* to mean remove a plug object from the direct object.
* </p>
*
* <p>
* <code>computer.dov.plugIn.with_nothing</code><br>
* If this meaning is set, unplug will interpret "unplug computer"
* to unplug a direct object from an abstract / non-existent
* indirect object, as in unplugging a computer without specifying
* an outlet, where the outlet is simply implied.
* </p>
*
* <p>
* <code>computer.dov.plugIn.with_assets = ['outlet']</code><br>
* If this meaning is set, unplug will interpret "unplug computer"
* to unplug a direct object from an indirect object, as in unplugging
* a computer from a specific tangible electrical outlet.
* </p>
*
* tryUnplugThis
* tryUnplugThis[Preposition]That
* tryUnplugThat[Preposition]This
* doUnplugThis
* doUnplugThis[Preposition]That
* doUnplugThat[Preposition]This
* @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
* @ajsdemo PlugGame, PlugSomethingIntoSomething, PlugSomethingIntoNothing, PlugSomethingWithSomething, PlugSomethingWithNothing
*/
A.Preverbs.unplug = {
name: "unplug",
prettyname: "unplug",
past_tense: "unplugged",
synonyms: ["unplug"],
//state_string: "unplugged",
gerund: "unplugging",
/**
* @ajsverbstructures
* @memberof unplug
*/
accepts_structures: ["verb noun", "verb noun preposition noun"],
/**
* @memberof unplug
* @ajsverbphrase
* phrase1:
* {
* accepts_noun: true,
* requires_noun: 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,
},
},
/**
* @memberof unplug
* @ajsverbphrase
* phrase2:
* {
* accepts_noun: true,
* noun_must_be:
* {
* known: true,
* tangible: true,
* present: true,
* visible: true,
* reachable: true,
* },
* accepts_preposition: true,
* requires_preposition: true,
* accepts_these_prepositions: ["from", "with"],
* },
*/
phrase2: {
accepts_noun: true,
noun_must_be: {
known: true,
tangible: true,
present: true,
visible: true,
reachable: true,
},
accepts_preposition: true,
requires_preposition: true,
accepts_these_prepositions: ["from", "with"],
},
/**
* @memberof unplug
* @ajsverbparams
* with_params: {},
*/
with_params: { on_unplug_take_plug: true },
doTry: function () {
var input = this.game.getInput();
var verb_phrase = input.verb_phrase;
var player = this.game.getPlayer();
var direct_object = input.getAsset(1);
var indirect_object = input.getAsset(2);
var indirect_preposition = input.getPreposition(2);
var indirect_inferred;
var results;
var msg = "";
var drain;
var can_plug, can_plugIn;
var unverb, unstate;
var plug_asset;
var is_plug_asset_takeable;
// 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.setInferred(1);
}
}
// which type of plug are we unplugging?
can_plug = direct_object.isDOV("plug");
can_plugIn = direct_object.isDOV("plugIn");
if (can_plug && can_plugIn) {
// this should not happen, but try to disambiguate
if (direct_object.is.plugged) unverb = "plug";
else if (direct_object.is.pluggedIn) unverb = "plugIn";
}
if (!unverb) {
unverb = can_plug ? "plug" : can_plugIn ? "plugIn" : null;
}
if (unverb) unstate = this.game.dictionary.verbs[unverb].state;
input.verb_params.unverb = unverb;
input.verb_params.unstate = unstate;
// sentence structure: verb
if (input.hasStructure("verb")) {
}
// 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(
`D1470 | ${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 (unstate && !direct_object.is[unstate]) {
this.game.debug(
`D1766 | ${this.name}.js | ${direct_object.id}.is.${unstate} is false `
);
msg += `${direct_object.Articlename_is} not ${
unstate === "pluggedIn" ? "plugged in" : unstate
}. `;
this.handleFailure(msg);
return false;
}
// single use direct object?
if (
direct_object.allowVerbOnce(this.name, "dov") &&
direct_object.didVerb(this.name, "dov")
) {
this.game.debug(
`D1832 | ${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;
}
// does direct object contain a plug?
if (unverb === "plug") {
// current logic only supports one thing plugging a drain
plug_asset = direct_object.is.connected_by.plug?.to_iov[0];
if (plug_asset) plug_asset = this.game.getAsset(plug_asset);
if (plug_asset) input.verb_params.plug_asset = plug_asset;
} // unverb===plug
// sentence structure: verb noun
// ie "unplug sink" or "unplug computer"
if (input.hasStructure("verb noun")) {
// indirect object required?
if (direct_object.allowVerbWithNothing(this.name, "dov")) {
return true;
}
}
// sentence structure: verb noun // ie "unplug sink"
if (input.hasStructure("verb noun") && unverb === "plugIn") {
switch (direct_object.getVerbConnectionCount("plugIn", "to_iov")) {
case 0:
// this case should've already been handled by unstate check
this.game.debug(
`D1717 | ${this.name}.js | ${direct_object.id}.is.connected_by.plugIn is unset `
);
msg += `${direct_object.Articlename} doesn't appear to be plugged in. `;
this.handleFailure(msg);
return null;
case 1:
// if it is plugged in to one thing,
// update the sentence structure to "unplug x from y"
input.setStructure("verb noun preposition noun");
indirect_object = this.game.getAsset(
direct_object.getVerbConnections("plugIn", "to_iov")[0]
);
input.setAsset(2, indirect_object);
input.setPreposition(2, "from");
indirect_preposition = "from";
break;
default:
// plugged in to more than one thing,
// prompt for which thing to unplug from
input.setPreposition(2, "from"); // set up next turn for "unplug x with y"
input.setSoftPrompt({ index: 2, type: "noun", noun2: true }); // ask for noun
this.game.debug(
`D1469 | ${this.name}.js | ${direct_object.id}.is.connected_by.plugIn.to_iov.length > 1, soft prompt for indirect object`
);
msg += `What would $(we) like to unplug ${direct_object.articlename} from? `;
this.handleFailure(msg);
return null;
}
} // verb noun && plugIn
// repeat sentence structure: verb noun
if (input.hasStructure("verb noun") && unverb === "plug") {
// indirect objects available?
if (!direct_object.hasIndirectObjects(this.name)) {
this.game.debug(
`D1713 | ${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(`D1714 | ${this.name}.js | soft prompt for noun2 `);
msg += `What would $(we) like to ${this.name} ${direct_object.articlename} with? `;
this.handleFailure(msg);
return null;
} 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
// ie: unplug computer from outlet
// ie: unplug toilet with plunger
if (input.hasStructure("verb noun preposition noun")) {
// with
if ("with" === indirect_preposition) {
// unplug sink with plunger
// is indirect object in player inventory?
if (
indirect_object.isDOV("take") &&
!indirect_object.isWithin(player)
) {
this.game.debug(
`D1734 | ${this.name}.js | ${indirect_object.id} is not in player inventory `
);
msg += `$(We're) not holding ${indirect_object.articlename}. `;
this.handleFailure(msg);
return null;
}
// 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(
`D1731 | ${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(
`D1727 | ${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(
`D1895 | ${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(
`D1818 | ${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;
}
} // with
// from
if ("from" === indirect_preposition) {
// unplug computer from outlet
// @TODO this logic is off since switching connected from verb to asset
// is direct object plugged into indirect object?
if (
!direct_object.isConnectedToAsset(
"plugIn",
indirect_object,
"to_iov"
)
) {
// if dobj isn't plugged into iobj,
// let's see if iobj is plugged into dobj
// 'cause we don't need to be dicks about it
if (
indirect_object.isConnectedToAsset(
"plugIn",
direct_object,
"to_iov"
)
) {
input.swapPhrases(1, 2);
input.setPreposition(1, "");
input.setPreposition(2, "from");
} else {
this.game.debug(
`D1735 | ${this.name}.js | ${direct_object.id}.is.connected_by.plugIn.to_iov does not include ${indirect_object.id} `
);
msg += `${direct_object.Articlename} doesn't appear to be plugged into ${indirect_object.articlename}. `;
this.handleFailure(msg);
return null;
}
}
} // from
} // verb noun preposition noun
return true;
},
doSuccess: function () {
var input = this.game.getInput();
var verb_phrase = input.verb_phrase;
var player = this.game.getPlayer();
var msg = "";
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 direct_place = direct_object.getPlaceAsset();
var results;
var plug_asset = input.verb_params.plug_asset;
// sentence structure: verb
if (input.hasStructure("verb")) {
}
// sentence structure: verb noun
if (input.hasStructure("verb noun")) {
} // verb noun
// sentence structure: verb noun
// ie "unplug computer"
if (
input.hasStructure("verb noun") &&
input.verb_params.unverb === "plugIn"
) {
// unplug from nothing
this.game.dictionary.verbs.plugIn.unsetVerbConnection(
direct_object,
null
);
direct_object.is.pluggedIn = false;
} // verb noun
// sentence structure: verb noun
// ie "unplug sink"
if (
input.hasStructure("verb noun") &&
direct_object.allowVerbWithNothing("plug", "dov")
) {
direct_object.is.plugged = false;
}
// was there a plug to remove?
// applies to verb noun and verb noun preposition noun
if (input.verb_params.unverb === "plug" && plug_asset) {
console.warn("try to remove plug from sink");
// remove plug from direct object - also handles state change
results = plug_asset.moveFrom(direct_object);
if ("undefined" !== typeof results) return results;
// disconnect plug from direct object (as indirect object)
// direct_object.unsetVerbConnection('plug',null);
// is plug takeable?
if (plug_asset.isDOV("take")) {
// move plug to player
results = plug_asset.moveTo("in", player);
} else {
// move plug to parent of direct_object
results = plug_asset.moveTo(
direct_place.default_aspect,
direct_place
);
}
if ("undefined" !== typeof results) return results;
} // plug_asset
// sentence structure: verb noun preposition noun
if (input.hasStructure("verb noun preposition noun")) {
// from
if (
input.verb_params.unverb === "plugIn" &&
"from" === indirect_preposition
) {
// disconnect them
this.game.dictionary.verbs.plugIn.unsetVerbConnection(
direct_object,
indirect_object
);
// things may be plugged into multiple items - how now?
if (direct_object.getVerbConnectionCount("plugIn", "to_iov") === 0) {
direct_object.is.pluggedIn = false;
}
} // from
} // verb noun preposition noun
// apply state changes
// already done
// compose output
msg +=
`$(We) ${this.name}` +
`${direct_preposition ? " " + direct_preposition : ""}` +
`${direct_object ? " " + direct_object.articlename : ""}` +
`${indirect_preposition ? " " + indirect_preposition : ""}` +
`${indirect_object ? " " + indirect_object.articlename : ""}`;
msg +=
plug_asset && plug_asset.isWithin(player)
? ` and take ${plug_asset.articlename}`
: ``;
msg +=
plug_asset && !plug_asset.isWithin(player)
? ` and leave
${plug_asset.articlename}
${plug_asset.getPlacePreposition()}
${plug_asset.getPlaceAsset().articlename}`
: ``;
msg += `. `;
// print output
return this.handleSuccess(msg, direct_object);
},
};
})(); // unplug