You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

163 lines
3.6 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. cpn[stateConfig.method].apply(cpn, stateConfig.args);
  80. return;
  81. }
  82. var result = {
  83. id: this.obj.id,
  84. msg: null,
  85. from: this.obj.name,
  86. options: []
  87. };
  88. if (stateConfig.msg instanceof Array) {
  89. var msgs = [];
  90. stateConfig.msg.forEach(function(m, i) {
  91. var rolls = (m.chance * 100) || 100;
  92. for (var j = 0; j < rolls; j++) {
  93. msgs.push({
  94. msg: m,
  95. index: i
  96. });
  97. }
  98. });
  99. var pick = msgs[~~(Math.random() * msgs.length)];
  100. result.msg = pick.msg.msg;
  101. result.options = stateConfig.msg[pick.index].options;
  102. }
  103. else {
  104. result.msg = stateConfig.msg;
  105. result.options = stateConfig.options;
  106. }
  107. if (!(result.options instanceof Array)) {
  108. if (result.options[0] == '$')
  109. result.options = this.states[result.options.replace('$', '')].options;
  110. result.options = Object.keys(result.options);
  111. }
  112. result.options = result.options.map(function(o) {
  113. var gotoState = this.states[(o + '').split('.')[0]];
  114. if (!gotoState.options[o])
  115. return null;
  116. return {
  117. id: o,
  118. msg: gotoState.options[o].msg
  119. };
  120. }, this);
  121. result.options.push({
  122. msg: 'Goodbye',
  123. id: 999
  124. });
  125. return result;
  126. },
  127. simplify: function(self) {
  128. return {
  129. type: 'dialogue'
  130. };
  131. }
  132. };
  133. });