// detach.js
// OK 09 2023
(function () {
/*global adventurejs A*/
"use strict";
/**
* @augments {adventurejs.Verb}
* @class detach
* @ajsnode game.dictionary.verbs.detach
* @ajsconstruct MyGame.createVerb({ "name": "detach", [...] });
* @ajsconstructedby adventurejs.Dictionary#createVerb
* @hideconstructor
* @ajsinstanceof Verb
* @ajsnavheading ManipulationVerbs
* @summary Verb meaning detach one asset from another.
* @tutorial Scripting_VerbSubscriptions
* @tutorial Verbs_VerbAnatomy
* @tutorial Verbs_VerbProcess
* @tutorial Verbs_ModifyVerbs
* @tutorial Verbs_WriteVerbs
* @classdesc
* <pre class="display border outline">
* <span class="input">> detach anglerfish from head</span>
* You detach the anglerfish from your head. It wriggles angrily
* in your hand.
* </pre>
* <p>
* <strong>Detach</strong> one
* {@link adventurejs.Tangible|Tangible}
* {@link adventurejs.Asset|Asset} from another.
* Requires that the Asset to be removed from has an <code>attach</code>
* {@link adventurejs.Aspect|Aspect}.
* To learn about Aspects, see
* <a href="/doc/Tangibles_Aspects.html">How to Use Aspects</a>.
* </p>
* @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
*/
A.Preverbs.detach = {
name: "detach",
prettyname: "detach",
past_tense: "detached",
synonyms: ["detach", "disconnect"],
unstate: "attached", // state should be determined by place
/**
* @ajsverbstructures
* @memberof detach
*/
accepts_structures: [
"verb noun",
"verb noun preposition noun",
"verb noun preposition noun preposition noun",
],
/**
* @memberof detach
* @ajsverbphrase
* phrase1:
* {
* accepts_noun:true,
* noun_must_be:
* {
* known: true,
* tangible: true,
* present: true,
* reachable: true,
* //visible: true,
* },
* },
*/
phrase1: {
accepts_noun: true,
noun_must_be: {
known: true,
tangible: true,
present: true,
reachable: true,
//visible: true,
},
},
/**
* @memberof detach
* @ajsverbphrase
* phrase2:
* {
* accepts_noun:true,
* noun_must_be:
* {
* known: true,
* tangible: true,
* present: true,
* reachable: true,
* //visible: 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,
reachable: true,
//visible: true,
},
accepts_preposition: true,
requires_preposition: true,
accepts_these_prepositions: ["from", "with"],
},
/**
* @memberof detach
* @ajsverbphrase
* phrase3:
* {
* accepts_noun:true,
* noun_must_be:
* {
* in_inventory_if_takeable: true,
* known: true,
* tangible: true,
* present: true,
* reachable: true,
* },
* accepts_preposition:true,
* requires_preposition: true,
* accepts_these_prepositions: ["from","with"],
* },
*/
phrase3: {
accepts_noun: true,
noun_must_be: {
in_inventory_if_takeable: true,
known: true,
tangible: true,
present: true,
reachable: true,
},
accepts_preposition: true,
requires_preposition: true,
accepts_these_prepositions: ["from", "with"],
},
/**
* @memberof detach
* @ajsverbparams
* with_params: {},
*/
with_params: {},
doTry: function () {
var input = this.game.getInput();
var player = this.game.getPlayer();
var object1 = input.getAsset(1);
var object2, object2_preposition;
var tool, tool_preposition;
var results, results2;
var msg = "";
// is direct object worn?
if (object1.is.worn) {
this.game.debug(
`F1522 | ${this.name}.js | ${object1.id}.is.worn, infer verb remove `,
);
this.game.dictionary.doVerb("remove");
return null;
}
// is direct object tied?
if (object1.dov.tie?.with_params.connections.length) {
this.game.debug(
`F1249 | ${this.name}.js | ${object1.id}.dov.tie, infer verb untie `,
);
this.game.dictionary.doVerb(
input.getAssumed(2) ? "untie" : "untie_from",
);
return null;
}
// parsed sentence structure: verb
if (input.hasStructure("verb")) {
}
// parsed sentence structure: verb noun
if (input.hasStructure("verb noun")) {
// is object1 attached?
if (object1.isPlacedAtAspect("attached")) {
// infer indirect object
object2 = object1.getPlaceAsset();
input.setAsset(2, object2);
input.setPreposition(2, "from");
input.setStructure("verb noun preposition noun");
} else {
// prompt for indirect object
input.setPreposition(2, "from");
input.setSoftPrompt({
noun2: true,
structure: "verb noun preposition noun",
});
this.game.debug(`F1520 | ${this.name}.js | soft prompt for noun2 `);
msg += `What would $(we) like to ${this.name} ${object1.articlename} from? `;
this.handleFailure(msg);
return null;
}
} // verb noun
// did player input with/with or from/from ?
if (input.getPreposition(2) === input.getPreposition(3)) {
// we don't understand that
this.game.debug(
`F1251 | ${this.name}.js | expected A from B with C, received two identical prepositions `,
);
msg += this.game.parser.getUnparsedMessage(this.game.getInput().input);
this.handleFailure(msg);
return null;
}
// did player input 'detach A with B from C'?
if (
"with" === input.getPreposition(2) &&
"from" === input.getPreposition(3)
) {
// swap B with C
input.swapPhrases(2, 3);
}
// 'detach A with C'? try to infer 'from B'
// this is where detach differs from attach
if ("with" === input.getPreposition(2) && !input.hasPhrase(3)) {
input.swapPhrases(2, 3);
input.setPreposition(2, "from");
tool = input.getAsset(3);
tool_preposition = "from";
// is object1 attached?
if (object1.isPlacedAtAspect("attached")) {
// infer indirect object
input.setAsset(2, object1.getPlaceAsset());
input.setStructure("verb noun preposition noun preposition noun");
} else {
// prompt for indirect object
input.setSoftPrompt({
noun2: true,
structure: "verb noun preposition noun preposition noun",
});
this.game.debug(`F1520 | ${this.name}.js | soft prompt for noun3 `);
msg += `What would $(we) like to ${this.name} ${object1.articlename} from with ${tool.articlename}? `;
this.handleFailure(msg);
return null;
}
}
// at this point we should have 'detach A from B' or 'detach A from B with C'
object2 = input.getAsset(2);
object2_preposition = input.getPreposition(2);
// these things apply to 'verb noun preposition noun' and 'verb noun preposition noun preposition noun'
// verb enabled for one or the other?
if (!object1.isDOV(this.name) && !object2.isDOV(this.name)) {
this.game.debug(
`F1250 | ${this.name}.js | neither ${object1.id} nor ${object2.id}.dov.${this.name}.enabled `,
);
msg += `${object1.Articlename} can't be ${this.past_tense} from ${object2.articlename}. `;
this.handleFailure(msg);
return null;
}
// are neither of these assets be attached to the other?
if (
!object1.isPlacedAtAspect("attached", object2) &&
!object2.isPlacedAtAspect("attached", object1)
) {
this.game.debug(
`F1353 | ${this.name}.js | neither asset is in the other's .aspects.attached.contents `,
);
msg += `${object1.Articlename} and ${object2.articlename} aren't ${this.unstate}. `;
this.handleFailure(msg);
return null;
}
// is object2 actually in object1?
if (
!object1.isPlacedAtAspect("attached", object2) &&
object2.isPlacedAtAspect("attached", object1)
) {
input.swapNouns(1, 2);
object1 = input.getAsset(1);
object2 = input.getAsset(2);
}
// sentence structure: verb noun preposition noun
// we put this way down here after all the object swapping is done
if (input.hasStructure("verb noun preposition noun")) {
}
// tool, maybe?
tool = input.getAsset(3);
tool_preposition = input.getPreposition(3);
// no tool?
if (!tool) {
// is a tool needed?
if (
!object1.DOVallowWithNothing(this.name) &&
!object2.DOVallowWithNothing(this.name)
) {
// tool needed
results = this.tryToInferIndirectObject(object1);
results2 = this.tryToInferIndirectObject(object2);
if (results.success || results2.success) {
// found a tool
var either_one = results.success
? results.success
: results2.success;
tool = either_one.indirect_object;
tool_preposition = "with";
input.setNewPhrase({
asset: tool,
preposition: tool_preposition,
});
input.setStructure("verb noun preposition noun preposition noun");
} else if (results.prompt || results2.prompt) {
// prompt for tool
// restructure sentence for next turn
input.setPreposition(3, "with");
input.setSoftPrompt({
noun3: true,
structure: "verb noun preposition noun preposition noun",
});
this.game.debug(`F1720 | ${this.name}.js | soft prompt for noun3 `);
msg += `What would $(we) like to ${this.name} ${object1.articlename} from ${object2.articlename} with? `;
this.handleFailure(msg);
return false;
}
} // tool needed?
} // no tool
// by now we should have an input tool or an inferred tool or no tool needed
// sentence structure: verb noun preposition noun preposition noun
// ie: 'attach male pipe to female pipe with wrench'
if (input.hasStructure("verb noun preposition noun preposition noun")) {
// works with any indirect object?
if (object1.DOVallowWithAnything(this.name)) {
return true;
}
// indirect object not required?
if (
object1.DOVallowWithNothing(this.name) &&
object2.DOVallowWithNothing(this.name)
) {
this.game.debug(
`F1518 | ${this.name}.js | ${object1.id} and ${object2.id}.dov.${this.name}.with_nothing `,
);
msg += `${object1.Articlename} and ${object2.articlename} don't need another object to ${this.name} them. `;
this.handleFailure(msg);
return null;
}
// indirect object usable with direct object?
if (
!object1.DOVallowWithAsset(this.name, tool) &&
!object2.DOVallowWithAsset(this.name, tool)
) {
this.game.debug(
`F1521 | ${this.name}.js | neither ${object1.id} nor ${object2.id}.dov.${this.name}.with_assets/with_classes includes ${tool.id} `,
);
msg += `${tool.Articlename} can't be used to ${this.name} ${object1.articlename} from ${object2.articlename}. `;
this.handleFailure(msg);
return null;
}
} // verb noun preposition noun preposition noun
return true;
},
doSuccess: function () {
var input = this.game.getInput();
var player = this.game.getPlayer();
var object1 = input.getAsset(1);
var object1_parent = object1.getPlaceAsset();
var object2 = input.getAsset(2);
var object2_preposition = input.getPreposition(2);
var tool = input.getAsset(3);
var tool_preposition = input.getPreposition(3);
var results;
var msg = "";
this.game.debug(`F1253 | ${this.name}.js | print doSuccess `);
// set direct object place
if (object2.id !== player.id) {
// remove direct object from current parent
results = object1.moveFrom(object2);
if ("undefined" !== typeof results) return results;
// add direct object to new parent
var target = object1.isDOV("take") ? player : object2.getPlaceAsset();
var targetprep = object1.isDOV("take")
? "in"
: object2.getPlacePreposition();
results = object1.moveTo(targetprep, target);
if ("undefined" !== typeof results) return results;
// @TODO object1 may need to be placed in the room
// and what about water rooms?
}
// apply state changes
//object1.is[this.getState()] = false;
// compose output
msg += `$(We) ${this.name} ${object1.articlename}`;
msg += object2 ? ` from ${object2.articlename}` : ``;
msg += tool ? ` with ${tool.articlename}` : ``;
msg +=
target.id === player.id
? ` and take it`
: ` and set it ${targetprep} ${target.articlename}`;
msg += `. `;
// print output
this.handleSuccess(msg, object1);
return true;
},
};
})(); // detach