PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Trunk/scripts/npc/9040006.js

https://github.com/system32/NinjaMS
JavaScript | 280 lines | 179 code | 27 blank | 74 comment | 64 complexity | 4d5e8b450f2f51c402b79333e254ecad MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*
  2. * This file is part of the OdinMS Maple Story Server
  3. Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
  4. Matthias Butz <matze@odinms.de>
  5. Jan Christian Meyer <vimes@odinms.de>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License version 3
  8. as published by the Free Software Foundation. You may not use, modify
  9. or distribute this program under any other version of the
  10. GNU Affero General Public License.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* @Author Lerk
  19. *
  20. * Guardian Statue - Sharenian: Fountain of the Wiseman (990000500)
  21. *
  22. * Guild Quest Stage 3
  23. */
  24. importPackage(net.sf.odinms.server.maps);
  25. function start() {
  26. //everything can be done in one status, so let's do it here.
  27. var eim = cm.getPlayer().getEventInstance();
  28. if (eim == null) {
  29. cm.warp(990001100);
  30. } else {
  31. if (eim.getProperty("leader").equals(cm.getPlayer().getName())) {
  32. if (cm.getPlayer().getMap().getReactorByName("watergate").getState() > 0){
  33. cm.sendOk("You may proceed.");
  34. } else {
  35. var currentCombo = eim.getProperty("stage3combo");
  36. if (currentCombo == null || currentCombo.equals("reset")) {
  37. var newCombo = makeCombo();
  38. eim.setProperty("stage3combo",newCombo);
  39. //cm.playerMessage("Debug: " + newCombo);
  40. eim.setProperty("stage3attempt","1");
  41. cm.sendOk("This fountain guards the secret passage to the throne room. Offer items in the area to the vassals to proceed. The vassals shall tell you whether your offerings are accepted, and if not, which vassals are displeased. You have seven attempts. Good luck.")
  42. } else {
  43. var attempt = parseInt(eim.getProperty("stage3attempt"));
  44. var combo = parseInt(currentCombo);
  45. var guess = getGroundItems();
  46. if (guess != null) {
  47. if (combo == guess) {
  48. cm.getPlayer().getMap().getReactorByName("watergate").hitReactor(cm.getC());
  49. cm.sendOk("You may proceed.");
  50. cm.showEffect("quest/party/clear");
  51. cm.playSound("Party1/Clear");
  52. var prev = eim.setProperty("stage3clear","true",true);
  53. if (prev == null) {
  54. cm.getGuild().gainGP(25);
  55. }
  56. } else {
  57. var black = net.sf.odinms.server.life.MapleLifeFactory.getMonster(9300036);
  58. var myst = net.sf.odinms.server.life.MapleLifeFactory.getMonster(9300037);
  59. if (attempt < 7) {
  60. //cm.playerMessage("Combo : " + combo);
  61. //cm.playerMessage("Guess : " + guess);
  62. var parsedCombo = parsePattern(combo);
  63. var parsedGuess = parsePattern(guess);
  64. var results = compare(parsedCombo, parsedGuess);
  65. var string = "";
  66. //cm.playerMessage("Results - Correct: " + results[0] + " | Incorrect: " + results[1] + " | Unknown: " + results[2]);
  67. if (results[0] != 0) {
  68. if (results[0] == 1) {
  69. string += "1 vassal is pleased with their offering.\r\n";
  70. } else {
  71. string += results[0] + " vassals are pleased with their offerings.\r\n";
  72. }
  73. }
  74. if (results[1] != 0) {
  75. if (results[1] == 1) {
  76. string += "1 vassal has recieved an incorrect offering.\r\n";
  77. } else {
  78. string += results[1] + " vassals have recieved incorrect offerings.\r\n";
  79. }
  80. }
  81. if (results[2] != 0) {
  82. if (results[2] == 1) {
  83. string += "1 vassal has recieved an unknown offering.\r\n";
  84. } else {
  85. string += results[2] + " vassals have recieved unknown offerings.\r\n";
  86. }
  87. }
  88. string += "This is your ";
  89. switch (attempt) {
  90. case 1:
  91. string += "1st";
  92. break;
  93. case 2:
  94. string += "2nd";
  95. break;
  96. case 3:
  97. string += "3rd";
  98. break;
  99. default:
  100. string += attempt + "th";
  101. break;
  102. }
  103. string += " attempt.";
  104. //spawn one black and one myst knight
  105. cm.getPlayer().getMap().spawnMonsterOnGroundBelow(black, new java.awt.Point(-350, 150));
  106. cm.getPlayer().getMap().spawnMonsterOnGroundBelow(myst, new java.awt.Point(400, 150));
  107. cm.sendOk(string);
  108. eim.setProperty("stage3attempt",attempt + 1);
  109. } else {
  110. //reset the combo and mass spawn monsters
  111. eim.setProperty("stage3combo","reset");
  112. cm.sendOk("You have failed the test. Please compose yourselves and try again later.");
  113. for (var i = 0; i < 5; i++) {
  114. //keep getting new monsters, lest we spawn the same monster five times o.o!
  115. black = net.sf.odinms.server.life.MapleLifeFactory.getMonster(9300036);
  116. myst = net.sf.odinms.server.life.MapleLifeFactory.getMonster(9300037);
  117. cm.getPlayer().getMap().spawnMonsterOnGroundBelow(black, new java.awt.Point(randX(), 150));
  118. cm.getPlayer().getMap().spawnMonsterOnGroundBelow(myst, new java.awt.Point(randX(), 150));
  119. }
  120. }
  121. }
  122. } else {
  123. cm.sendOk("Please make sure your attempt is properly set in front of the vassals and talk to me again.");
  124. }
  125. }
  126. }
  127. } else {
  128. cm.sendOk("Please have your leader speak to me.");
  129. }
  130. }
  131. cm.dispose();
  132. }
  133. function action(mode, type, selection) {
  134. }
  135. function makeCombo() {
  136. var combo = 0;
  137. for (var i = 0; i < 4; i++) {
  138. combo += Math.floor(Math.random() * 4) * Math.pow(10, i);
  139. }
  140. return combo;
  141. }
  142. //check the items on ground and convert into an applicable string; null if items aren't proper
  143. function getGroundItems() {
  144. var items = cm.getPlayer().getMap().getItemsInRange(cm.getPlayer().getPosition(), java.lang.Double.POSITIVE_INFINITY);
  145. var itemInArea = new Array(-1, -1, -1, -1);
  146. if (items.size() != 4) {
  147. //cm.playerMessage("Debug: Attempt failed due to lack of items: " + items.size());
  148. return null;
  149. }
  150. var iter = items.iterator();
  151. while (iter.hasNext()) {
  152. var item = iter.next();
  153. var id = item.getItem().getItemId();
  154. if (id < 4001027 || id > 4001030) {
  155. //cm.playerMessage("Debug: Attempt failed due to improper ID: " + id);
  156. return null;
  157. } else {
  158. //check item location
  159. for (var i = 0; i < 4; i++) {
  160. if (cm.getPlayer().getMap().getArea(i).contains(item.getPosition())) {
  161. itemInArea[i] = id - 4001027;
  162. //cm.playerMessage("Item in area "+i+": " + id);
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. //guaranteed four items that are part of the stage 3 item set by this point, check to see if each area has an item
  169. if (itemInArea[0] == -1 || itemInArea[1] == -1 || itemInArea[2] == -1 || itemInArea[3] == -1) {
  170. /*cm.playerMessage("Debug: Attempt failed due to improper location.");
  171. for (var i = 0; i < 4; i++) {
  172. cm.playerMessage("Item in area "+i+": " + itemInArea[i]);
  173. }*/
  174. return null;
  175. }
  176. return (itemInArea[0] * 1000 + itemInArea[1] * 100 + itemInArea[2] * 10 + itemInArea[3]);
  177. }
  178. //convert an integer for answer or guess into int array for comparison
  179. function parsePattern(pattern) {
  180. var tempPattern = pattern;
  181. var items = new Array(-1, -1, -1, -1);
  182. for (var i = 0; i < 4; i++) {
  183. items[i] = Math.floor(tempPattern / Math.pow(10, 3-i));
  184. tempPattern = tempPattern % Math.pow(10, 3-i);
  185. }
  186. return items;
  187. }
  188. // compare two int arrays for the puzzle
  189. function compare(answer, guess) {
  190. var correct = 0;
  191. var incorrect = 0;
  192. /*var debugAnswer = "Combo : ";
  193. var debugGuess = "Guess : ";
  194. for (var d = 0; d < answer.length; d++) {
  195. debugAnswer += answer[d] + " ";
  196. debugGuess += guess[d] + " ";
  197. }
  198. cm.playerMessage(debugAnswer);
  199. cm.playerMessage(debugGuess);*/
  200. for (var i = 0; i < answer.length; i) {
  201. if (answer[i] == guess[i]) {
  202. correct++;
  203. //cm.playerMessage("Item match : " + answer[i]);
  204. //pop the answer/guess at i
  205. if (i != answer.length - 1) {
  206. answer[i] = answer[answer.length - 1];
  207. guess[i] = guess[guess.length - 1];
  208. }
  209. answer.pop();
  210. guess.pop();
  211. /*/debugAnswer = "Combo : ";
  212. debugGuess = "Guess : ";
  213. for (var d = 0; d < answer.length; d++) {
  214. debugAnswer += answer[d] + " ";
  215. debugGuess += guess[d] + " ";
  216. }
  217. cm.playerMessage(debugAnswer);
  218. cm.playerMessage(debugGuess);*/
  219. }
  220. else {
  221. i++;
  222. }
  223. }
  224. //check remaining answers for "incorrect": correct item in incorrect position
  225. var answerItems = new Array(0, 0, 0, 0);
  226. var guessItems = new Array(0, 0, 0, 0);
  227. for (var j = 0; j < answer.length; j++) {
  228. var aItem = answer[j];
  229. var gItem = guess[j]
  230. answerItems[aItem]++;
  231. guessItems[gItem]++;
  232. }
  233. /*for (var d = 0; d < answer.length; d++) {
  234. cm.playerMessage("Item " + d + " in combo: " + answerItems[d] + " | in guess: " + guessItems[d]);
  235. }*/
  236. for (var k = 0; k < answerItems.length; k++) {
  237. var inc = Math.min(answerItems[k], guessItems[k]);
  238. //cm.playerMessage("Incorrect for item " + k + ": " + inc);
  239. incorrect += inc;
  240. }
  241. return new Array(correct, incorrect, (4 - correct - incorrect));
  242. }
  243. //for mass spawn
  244. function randX() {
  245. return -350 + Math.floor(Math.random() * 750);
  246. }