Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

193 řádky
4.4 KiB

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