Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

216 rindas
4.8 KiB

  1. define([
  2. ], function (
  3. ) {
  4. return {
  5. type: 'dialogue',
  6. states: {},
  7. sourceStates: {},
  8. trigger: null,
  9. init: function (blueprint) {
  10. this.states = blueprint.config;
  11. },
  12. destroy: function () {
  13. if (this.trigger)
  14. this.trigger.destroyed = true;
  15. },
  16. talk: function (msg) {
  17. if (!msg)
  18. return false;
  19. var target = msg.target;
  20. if ((target == null) && (!msg.targetName))
  21. return false;
  22. if ((target != null) && (target.id == null)) {
  23. target = this.obj.instance.objects.objects.find(o => o.id == target);
  24. if (!target)
  25. return false;
  26. } else if (msg.targetName != null) {
  27. target = this.obj.instance.objects.objects.find(o => ((o.name) && (o.name.toLowerCase() == msg.targetName.toLowerCase())));
  28. if (!target)
  29. return false;
  30. }
  31. if (!target.dialogue)
  32. return false;
  33. //Auto-discover faction
  34. if ((target.trade) && (target.trade.faction))
  35. this.obj.reputation.discoverFaction(target.trade.faction.id);
  36. var state = target.dialogue.getState(this.obj, msg.state)
  37. if (!state) {
  38. this.obj.syncer.set(true, 'dialogue', 'state', null);
  39. return false;
  40. }
  41. this.obj.syncer.set(true, 'dialogue', 'state', state);
  42. },
  43. stopTalk: function () {
  44. this.obj.syncer.set(true, 'dialogue', 'state', null);
  45. },
  46. getState: function (sourceObj, state) {
  47. state = state || 1;
  48. //Goto?
  49. if ((state + '').indexOf('.') > -1) {
  50. var config = this.states[(state + '').split('.')[0]];
  51. if (!config)
  52. return false;
  53. var goto = (config.options[state] || {}).goto;
  54. if (goto instanceof Array) {
  55. var gotos = [];
  56. goto.forEach(function (g) {
  57. var rolls = (g.chance * 100) || 100;
  58. for (var i = 0; i < rolls; i++) {
  59. gotos.push(g.number);
  60. }
  61. });
  62. state = gotos[~~(Math.random() * gotos.length)];
  63. } else
  64. state = goto;
  65. }
  66. this.sourceStates[sourceObj.id] = state;
  67. if (!this.states) {
  68. console.log(sourceObj.name, this.obj.name, state);
  69. return null;
  70. }
  71. var stateConfig = this.states[state];
  72. if (!stateConfig)
  73. return null;
  74. var useMsg = stateConfig.msg;
  75. if (stateConfig.cpn) {
  76. var cpn = sourceObj[stateConfig.cpn];
  77. var newArgs = extend(true, [], stateConfig.args);
  78. newArgs.push(this.obj);
  79. var result = cpn[stateConfig.method].apply(cpn, newArgs);
  80. if (stateConfig.goto) {
  81. if (result)
  82. return this.getState(sourceObj, stateConfig.goto.success);
  83. else
  84. return this.getState(sourceObj, stateConfig.goto.failure);
  85. } else {
  86. if (result) {
  87. useMsg = extend(true, [], useMsg);
  88. useMsg[0].msg = result;
  89. } else
  90. return null;
  91. }
  92. } else if (stateConfig.method) {
  93. var methodResult = stateConfig.method.call(this.obj, sourceObj);
  94. if (methodResult) {
  95. useMsg = extend(true, [], useMsg);
  96. useMsg[0].msg = methodResult;
  97. }
  98. if (!useMsg)
  99. return;
  100. }
  101. var result = {
  102. id: this.obj.id,
  103. msg: null,
  104. from: this.obj.name,
  105. options: []
  106. };
  107. if (useMsg instanceof Array) {
  108. var msgs = [];
  109. useMsg.forEach(function (m, i) {
  110. var rolls = (m.chance * 100) || 100;
  111. for (var j = 0; j < rolls; j++) {
  112. msgs.push({
  113. msg: m,
  114. index: i
  115. });
  116. }
  117. });
  118. var pick = msgs[~~(Math.random() * msgs.length)];
  119. result.msg = pick.msg.msg;
  120. result.options = useMsg[pick.index].options;
  121. } else {
  122. result.msg = useMsg;
  123. result.options = stateConfig.options;
  124. }
  125. if (!(result.options instanceof Array)) {
  126. if (result.options[0] == '$')
  127. result.options = this.states[result.options.replace('$', '')].options;
  128. result.options = Object.keys(result.options);
  129. }
  130. result.options = result.options
  131. .map(function (o) {
  132. var gotoState = this.states[(o + '').split('.')[0]];
  133. var picked = gotoState.options[o];
  134. if (!picked)
  135. return null;
  136. else if (picked.prereq) {
  137. var doesConform = picked.prereq(sourceObj);
  138. if (!doesConform)
  139. return null;
  140. }
  141. return {
  142. id: o,
  143. msg: picked.msg
  144. };
  145. }, this)
  146. .filter(o => !!o);
  147. result.options.push({
  148. msg: 'Goodbye',
  149. id: 999
  150. });
  151. return result;
  152. },
  153. simplify: function (self) {
  154. return {
  155. type: 'dialogue'
  156. };
  157. },
  158. //These don't belong here, but I can't figure out where to put them right now
  159. //They are actions that can be performed while chatting with someone
  160. teleport: function (msg) {
  161. this.obj.syncer.set(true, 'dialogue', 'state', null);
  162. var portal = extend(true, {}, require('./components/portal'), msg);
  163. portal.collisionEnter(this.obj);
  164. },
  165. getItem: function (msg, source) {
  166. var inventory = this.obj.inventory;
  167. var exists = inventory.items.find(i => (i.name == msg.item.name));
  168. if (!exists) {
  169. inventory.getItem(msg.item);
  170. return msg.successMsg || false;
  171. } else
  172. return msg.existsMsg || false;
  173. }
  174. };
  175. });