ActionTalk.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../../../Elements/Modules/HyperlinkHandler.ts" />
  3. /// <reference path="../../../Elements/Classes/Say/SayHeSheIt.ts" />
  4. class ActionTalk extends Action {
  5. public static PRIORITY_SITUATION_DIALOGUE = 9;
  6. public static PRIORITY_POSSESSION_DIALOGUE = 7;
  7. public static PRIORITY_LOCATION_DIALOGUE = 5;
  8. public static PRIORITY_COMMON_DIALOGUE = 3;
  9. public static PRIORITY_GLOBAL_DIALOGUE = 1;
  10. public static check: Rulebook<ActionTalk> = new Rulebook("Check Talking");
  11. public static carry: Rulebook<ActionTalk> = new Rulebook("Carry out Talking");
  12. public static defaultCarryTalkingRule = ActionTalk.carry.createAndAddRule({
  13. name : "Talking - Check with the AI",
  14. firstPriority : Rule.PRIORITY_HIGHEST,
  15. priority : Rule.PRIORITY_HIGHEST,
  16. code : async (rulebook : RulebookRunner<ActionTalk>) => {
  17. let action = <ActionGo> rulebook.noun;
  18. //let actor = action.actor;
  19. let thing = (<Thing>action.getNoun(0));
  20. if (thing instanceof Person && action.actor instanceof Person) {
  21. let runAndStop : Array<DialogueTree> = [];
  22. let runAndContinue : Array<DialogueTree> = [];
  23. let circumstantialOptions : Array<DialogueHook> = [];
  24. await thing.AI.answerTo({greeter : action.actor, answerer : thing, options : circumstantialOptions, runAndStop : runAndStop, runFirst : runAndContinue});
  25. if (runAndStop.length > 0) {
  26. await runAndStop[0].execute();
  27. return true; // must stop executing now
  28. } else if (runAndContinue.length > 0) {
  29. for (let i = 0; i < runAndContinue.length; i++) {
  30. await runAndContinue[i].execute();
  31. }
  32. }
  33. let investigativeOptions : Array<DialogueHook> = [];
  34. await thing.AI.interrogateTo({greeter : action.actor, answerer : thing, options : investigativeOptions, runAndStop : runAndStop, runFirst : runAndContinue});
  35. if (investigativeOptions.length == 0 && circumstantialOptions.length == 0) {
  36. return;
  37. }
  38. let choices : Array<Say> = [];
  39. let results = [];
  40. if (investigativeOptions.length > 0) {
  41. // TODO: Add more textx
  42. choices.push(new Say(new OneOf(OneOf.PURELY_AT_RANDOM, "Ask about...")));
  43. results.push(null);
  44. }
  45. for (let i = 0; i < circumstantialOptions.length; i++) {
  46. choices.push(circumstantialOptions[i].text);
  47. results.push(circumstantialOptions[i].tree);
  48. }
  49. // TODO: Add more texts
  50. choices.push(new Say(new OneOf(OneOf.PURELY_AT_RANDOM, "Goodbye")));
  51. results.push(undefined);
  52. let choice = await Controls.giveChoices(true, ...choices);
  53. if (results[choice[1]] === null) {
  54. choices = [];
  55. results = [];
  56. for (let i = 0; i < investigativeOptions.length; i++) {
  57. choices.push(investigativeOptions[i].text);
  58. results.push(investigativeOptions[i].tree);
  59. }
  60. choice = await Controls.giveChoices(true, ...choices);
  61. if (results[choice[1]] instanceof DialogueTree) {
  62. await (results[choice[1]]).execute();
  63. }
  64. } else if (results[choice[1]] instanceof DialogueTree) {
  65. await (results[choice[1]]).execute();
  66. }
  67. return true;
  68. }
  69. }
  70. });
  71. public static lastCarryTalkingRule = ActionTalk.carry.createAndAddRule({
  72. name : "Talking - Doesn't want to talk",
  73. firstPriority : -1,
  74. priority : -1,
  75. code : (rulebook : RulebookRunner<ActionTalk>) => {
  76. let action = <ActionGo> rulebook.noun;
  77. //let actor = action.actor;
  78. let thing = (<Thing>action.getNoun(0));
  79. if (thing instanceof Person) {
  80. action.say = new Say(...Say.Mention(action.actor), " greet", action.actor == WorldState.player ? " " : "s ", ...Say.Mention(thing), ". There is no response.");
  81. } else if (action.actor == WorldState.player) {
  82. action.say = new Say("How are you going to talk to that?")
  83. }
  84. }
  85. });
  86. public getCommandText () {
  87. return "talk to " + (this.getNoun(0) != undefined ? this.getNoun(0).getPrintedName() : "");
  88. }
  89. }
  90. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  91. {
  92. name : "Hyperlink - Talk",
  93. firstPriority : Rule.PRIORITY_HIGHEST,
  94. priority : Rule.PRIORITY_HIGHEST,
  95. code : (rulebook : RulebookRunner<Thing>) => {
  96. let thing = <Thing> rulebook.noun;
  97. if (thing instanceof Person && thing.getRoom() == WorldState.player.getRoom()) {
  98. Elements.HyperlinkHandler.addAvailableAction("Talk", new ActionTalk(WorldState.player, thing));
  99. }
  100. }
  101. }
  102. ));