crunch
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.
 
 
 

523 lines
15 KiB

  1. let currentRoom = null;
  2. let currentDialog = null;
  3. let currentFoe = null;
  4. let dirButtons = [];
  5. let actionButtons = [];
  6. let mode = "explore";
  7. let actions = [];
  8. let time = 9*60*60;
  9. let newline = " ";
  10. let player = new Player();
  11. let playerAttacks = [];
  12. let respawnRoom;
  13. function join(things) {
  14. if (things.length == 1) {
  15. return things[0].description("a");
  16. } else if (things.length == 2) {
  17. return things[0].description("a") + " and " + things[1].description("a");
  18. } else {
  19. let line = "";
  20. line = things.slice(0,-1).reduce((line, prey) => line + prey.description("a") + ", ", line);
  21. line += " and " + things[things.length-1].description("a");
  22. return line;
  23. }
  24. }
  25. function pickRandom(list) {
  26. return list[Math.floor(Math.random() * list.length)];
  27. }
  28. function pick(list, attacker, defender) {
  29. if (list.length == 0)
  30. return null;
  31. else {
  32. let sum = list.reduce((sum, choice) => choice.weight == undefined ? sum + 1 : sum + choice.weight(attacker, defender) + sum, 0);
  33. let target = Math.random() * sum;
  34. for (let i = 0; i < list.length; i++) {
  35. sum -= list[i].weight == undefined ? 1 : list[i].weight(attacker, defender);
  36. if (sum <= target) {
  37. return list[i];
  38. }
  39. }
  40. return list[list.length-1];
  41. }
  42. }
  43. function filterValid(options, attacker, defender) {
  44. let filtered = options.filter(option => option.conditions == undefined || option.conditions.reduce((result, test) => result && test(attacker, defender), true));
  45. return filtered.filter(option => option.requirements == undefined || option.requirements.reduce((result, test) => result && test(attacker, defender), true));
  46. }
  47. function filterPriority(options) {
  48. let max = options.reduce((max, option) => option.priority > max ? option.priority : max, -1000);
  49. return options.filter(option => option.priority == max);
  50. }
  51. function round(number, digits) {
  52. return Math.round(number * Math.pow(10,digits)) / Math.pow(10,digits);
  53. }
  54. function updateExploreCompass() {
  55. for (let i = 0; i < dirButtons.length; i++) {
  56. let button = dirButtons[i];
  57. button.classList.remove("active-button");
  58. button.classList.remove("inactive-button");
  59. button.classList.remove("disabled-button");
  60. if (currentRoom.exits[i] == null) {
  61. button.disabled = true;
  62. button.classList.add("inactive-button");
  63. button.innerHTML = "";
  64. } else {
  65. if (currentRoom.exits[i].conditions.reduce((result, test) => result && test(player.prefs), true)) {
  66. button.disabled = false;
  67. button.classList.add("active-button");
  68. button.innerHTML = currentRoom.exits[i].name;
  69. } else {
  70. button.disabled = true;
  71. button.classList.add("disabled-button");
  72. button.innerHTML = currentRoom.exits[i].name;
  73. }
  74. }
  75. }
  76. }
  77. function updateExploreActions() {
  78. for (let i = 0; i < actionButtons.length; i++) {
  79. if (i < actions.length) {
  80. actionButtons[i].disabled = false;
  81. actionButtons[i].innerHTML = actions[i].name;
  82. actionButtons[i].classList.remove("inactive-button");
  83. actionButtons[i].classList.add("active-button");
  84. }
  85. else {
  86. actionButtons[i].disabled = true;
  87. actionButtons[i].innerHTML = "";
  88. actionButtons[i].classList.remove("active-button");
  89. actionButtons[i].classList.add("inactive-button");
  90. }
  91. }
  92. }
  93. function updateExplore() {
  94. updateExploreCompass();
  95. updateExploreActions();
  96. }
  97. function updateEaten() {
  98. let list = document.getElementById("eaten");
  99. while(list.firstChild) {
  100. list.removeChild(list.firstChild);
  101. }
  102. for (let i = 0; i < currentFoe.struggles.length; i++) {
  103. let li = document.createElement("li");
  104. let button = document.createElement("button");
  105. button.classList.add("eaten-button");
  106. button.innerHTML = currentFoe.struggles[i].name;
  107. button.addEventListener("click", function() { struggleClicked(i); } );
  108. button.addEventListener("mouseover", function() { struggleHovered(i); } );
  109. button.addEventListener("mouseout", function() { document.getElementById("eaten-desc").innerHTML = ""; } );
  110. li.appendChild(button);
  111. list.appendChild(li);
  112. }
  113. }
  114. function updateCombat() {
  115. let list = document.getElementById("combat");
  116. while(list.firstChild) {
  117. list.removeChild(list.firstChild);
  118. }
  119. playerAttacks = filterValid(player.attacks, player, currentFoe);
  120. if (playerAttacks.length == 0)
  121. playerAttacks = [player.backupAttack];
  122. for (let i = 0; i < playerAttacks.length; i++) {
  123. let li = document.createElement("li");
  124. let button = document.createElement("button");
  125. button.classList.add("combat-button");
  126. button.innerHTML = playerAttacks[i].name;
  127. button.addEventListener("click", function() { attackClicked(i); } );
  128. button.addEventListener("mouseover", function() { attackHovered(i); } );
  129. button.addEventListener("mouseout", function() { document.getElementById("combat-desc").innerHTML = ""; } );
  130. li.appendChild(button);
  131. list.appendChild(li);
  132. }
  133. }
  134. function updateDialog() {
  135. let list = document.getElementById("dialog");
  136. while(list.firstChild) {
  137. list.removeChild(list.firstChild);
  138. }
  139. for (let i = 0; i < currentDialog.choices.length; i++) {
  140. let activated = currentDialog.choices[i].node.requirements == undefined || currentDialog.choices[i].node.requirements.reduce((result, test) => result && test(player, currentFoe), true);
  141. let li = document.createElement("li");
  142. let button = document.createElement("button");
  143. button.classList.add("dialog-button");
  144. button.innerHTML = currentDialog.choices[i].text;
  145. button.addEventListener("click", function() { dialogClicked(i); });
  146. if (!activated) {
  147. button.classList.add("disabled-button");
  148. button.disabled = true;
  149. }
  150. li.appendChild(button);
  151. list.appendChild(li);
  152. }
  153. }
  154. function updateDisplay() {
  155. document.querySelectorAll(".selector").forEach(function (x) {
  156. x.style.display = "none";
  157. });
  158. switch(mode) {
  159. case "explore":
  160. document.getElementById("selector-explore").style.display = "flex";
  161. updateExplore();
  162. break;
  163. case "combat":
  164. document.getElementById("selector-combat").style.display = "flex";
  165. updateCombat();
  166. break;
  167. case "dialog":
  168. document.getElementById("selector-dialog").style.display = "flex";
  169. updateDialog();
  170. break;
  171. case "eaten":
  172. document.getElementById("selector-eaten").style.display = "flex";
  173. updateEaten();
  174. break;
  175. }
  176. document.getElementById("time").innerHTML = "Time: " + renderTime(time);
  177. document.getElementById("stat-name").innerHTML = "Name: " + player.name;
  178. document.getElementById("stat-health").innerHTML = "Health: " + round(player.health,0) + "/" + round(player.maxHealth,0);
  179. document.getElementById("stat-cash").innerHTML = "Cash: $" + round(player.cash,0);
  180. document.getElementById("stat-stamina").innerHTML = "Stamina: " + round(player.stamina,0) + "/" + round(player.maxStamina,0);
  181. document.getElementById("stat-fullness").innerHTML = "Fullness: " + round(player.fullness(),0);
  182. if (player.prefs.scat) {
  183. document.getElementById("stat-bowels").innerHTML = "Bowels: " + round(player.bowels.fullness,0);
  184. } else {
  185. document.getElementById("stat-bowels").innerHTML = "";
  186. }
  187. }
  188. function advanceTime(amount) {
  189. time = (time + amount) % 86400;
  190. player.restoreHealth(amount);
  191. player.restoreStamina(amount);
  192. update(player.stomach.digest(amount));
  193. update(player.butt.digest(amount));
  194. }
  195. function renderTime(time) {
  196. let suffix = (time < 43200) ? "AM" : "PM";
  197. let hour = Math.floor((time % 43200) / 3600);
  198. if (hour == 0)
  199. hour = 12;
  200. let minute = Math.floor(time / 60) % 60;
  201. if (minute < 9)
  202. minute = "0" + minute;
  203. return hour + ":" + minute + " " + suffix;
  204. }
  205. function move(direction) {
  206. let target = currentRoom.exits[direction];
  207. if (target == null) {
  208. alert("Tried to move to an empty room!");
  209. return;
  210. }
  211. moveTo(target,currentRoom.exitDescs[direction]);
  212. }
  213. function moveTo(room,desc="You go places lol") {
  214. actions = [];
  215. currentRoom = room;
  216. advanceTime(30);
  217. currentRoom.objects.forEach(function (object) {
  218. object.actions.forEach(function (action) {
  219. if (action.conditions == undefined || action.conditions.reduce((result, cond) => result && cond(player.prefs), true))
  220. actions.push(action);
  221. });
  222. });
  223. update([desc,newline]);
  224. currentRoom.visit();
  225. }
  226. window.addEventListener('load', function(event) {
  227. document.getElementById("start-button").addEventListener("click", start, false);
  228. });
  229. function start() {
  230. applySettings(generateSettings());
  231. document.getElementById("create").style.display = "none";
  232. document.getElementById("game").style.display = "block";
  233. loadActions();
  234. loadCompass();
  235. loadDialog();
  236. currentRoom = createWorld();
  237. respawnRoom = currentRoom;
  238. moveTo(currentRoom);
  239. updateDisplay();
  240. }
  241. // copied from Stroll LUL
  242. function generateSettings() {
  243. let form = document.forms.namedItem("character-form");
  244. let settings = {};
  245. for (let i=0; i<form.length; i++) {
  246. let value = form[i].value == "" ? form[i].placeholder : form[i].value;
  247. if (form[i].type == "text")
  248. if (form[i].value == "")
  249. settings[form[i].name] = form[i].placeholder;
  250. else
  251. settings[form[i].name] = value;
  252. else if (form[i].type == "number")
  253. settings[form[i].name] = parseFloat(value);
  254. else if (form[i].type == "checkbox") {
  255. settings[form[i].name] = form[i].checked;
  256. } else if (form[i].type == "radio") {
  257. let name = form[i].name;
  258. if (form[i].checked)
  259. settings[name] = form[i].value;
  260. } else if (form[i].type == "select-one") {
  261. settings[form[i].name] = form[i][form[i].selectedIndex].value;
  262. }
  263. }
  264. return settings;
  265. }
  266. function applySettings(settings) {
  267. player.name = settings.name;
  268. player.species = settings.species;
  269. for (let key in settings) {
  270. if (settings.hasOwnProperty(key)) {
  271. if (key.match(/prefs/)) {
  272. let tokens = key.split("-");
  273. let pref = player.prefs;
  274. pref = tokens.slice(1,-1).reduce((pref, key) => pref[key], pref);
  275. pref[tokens.slice(-1)[0]] = settings[key];
  276. }
  277. }
  278. }
  279. }
  280. function saveSettings() {
  281. window.localStorage.setItem("settings", JSON.stringify(generateSettings()));
  282. }
  283. function retrieveSettings() {
  284. return JSON.parse(window.localStorage.getItem("settings"));
  285. }
  286. function update(lines=[]) {
  287. let log = document.getElementById("log");
  288. for (let i=0; i<lines.length; i++) {
  289. let div = document.createElement("div");
  290. div.innerHTML = lines[i];
  291. log.appendChild(div);
  292. }
  293. log.scrollTop = log.scrollHeight;
  294. updateDisplay();
  295. }
  296. function changeMode(newMode) {
  297. mode = newMode;
  298. let body = document.querySelector("body");
  299. body.className = "";
  300. switch(mode) {
  301. case "explore":
  302. case "dialog":
  303. body.classList.add("explore");
  304. break;
  305. case "combat":
  306. body.classList.add("combat");
  307. break;
  308. case "eaten":
  309. body.classList.add("eaten");
  310. break;
  311. }
  312. updateDisplay();
  313. }
  314. function respawn(respawnRoom) {
  315. moveTo(respawnRoom,"You drift through space and time...");
  316. player.clear();
  317. player.stomach.contents = [];
  318. player.butt.contents = [];
  319. advanceTime(86400/2);
  320. changeMode("explore");
  321. player.health = 100;
  322. update(["You wake back up in your bed."]);
  323. }
  324. function startCombat(opponent) {
  325. currentFoe = opponent;
  326. changeMode("combat");
  327. update(["Oh shit it's " + opponent.description("a")]);
  328. }
  329. function attackClicked(index) {
  330. update(playerAttacks[index].attack(currentFoe));
  331. if (currentFoe.health <= 0) {
  332. update([currentFoe.description("The") + " falls to the ground!"]);
  333. startDialog(new FallenFoe(currentFoe));
  334. } else if (mode == "combat") {
  335. let attack = pick(filterPriority(filterValid(currentFoe.attacks, currentFoe, player)), currentFoe, player);
  336. if (attack == null) {
  337. attack = currentFoe.backupAttack;
  338. }
  339. update(attack.attackPlayer(player));
  340. if (player.health <= -100) {
  341. update(["You die..."]);
  342. respawn(respawnRoom);
  343. } else if (player.health <= 0) {
  344. update(["You fall to the ground..."]);
  345. if (player.prefs.prey) {
  346. changeMode("eaten");
  347. } else {
  348. respawn(respawnRoom);
  349. }
  350. }
  351. }
  352. }
  353. function attackHovered(index) {
  354. document.getElementById("combat-desc").innerHTML = playerAttacks[index].desc;
  355. }
  356. function struggleClicked(index) {
  357. let struggle = currentFoe.struggles[index];
  358. let result = struggle.struggle(player);
  359. update([result.lines]);
  360. if (result.escape) {
  361. changeMode("explore");
  362. } else {
  363. let digest = pick(filterValid(currentFoe.digests, currentFoe, player), currentFoe, player);
  364. if (digest == null) {
  365. digest = currentFoe.backupDigest;
  366. }
  367. update([digest.digest(player)]);
  368. if (player.health <= -100) {
  369. update(["You digest in the depths of " + currentFoe.description("the")]);
  370. respawn(respawnRoom);
  371. }
  372. }
  373. }
  374. function struggleHovered(index) {
  375. document.getElementById("eaten-desc").innerHTML = currentFoe.struggles[index].desc;
  376. }
  377. function startDialog(dialog) {
  378. currentDialog = dialog;
  379. changeMode("dialog");
  380. update(currentDialog.text);
  381. currentDialog.visit();
  382. updateDisplay();
  383. }
  384. function dialogClicked(index) {
  385. currentDialog = currentDialog.choices[index].node;
  386. update(currentDialog.text);
  387. currentDialog.visit();
  388. if (currentDialog.choices.length == 0 && mode == "dialog") {
  389. changeMode("explore");
  390. updateDisplay();
  391. }
  392. }
  393. function loadDialog() {
  394. dialogButtons = Array.from( document.querySelectorAll(".dialog-button"));
  395. for (let i = 0; i < dialogButtons.length; i++) {
  396. dialogButtons[i].addEventListener("click", function() { dialogClicked(i); });
  397. }
  398. }
  399. function actionClicked(index) {
  400. actions[index].action();
  401. }
  402. function loadActions() {
  403. actionButtons = Array.from( document.querySelectorAll(".action-button"));
  404. for (let i = 0; i < actionButtons.length; i++) {
  405. actionButtons[i].addEventListener("click", function() { actionClicked(i); });
  406. }
  407. }
  408. function loadCompass() {
  409. dirButtons[NORTH_WEST] = document.getElementById("compass-north-west");
  410. dirButtons[NORTH_WEST].addEventListener("click", function() {
  411. move(NORTH_WEST);
  412. });
  413. dirButtons[NORTH] = document.getElementById("compass-north");
  414. dirButtons[NORTH].addEventListener("click", function() {
  415. move(NORTH);
  416. });
  417. dirButtons[NORTH_EAST] = document.getElementById("compass-north-east");
  418. dirButtons[NORTH_EAST].addEventListener("click", function() {
  419. move(NORTH_EAST);
  420. });
  421. dirButtons[WEST] = document.getElementById("compass-west");
  422. dirButtons[WEST].addEventListener("click", function() {
  423. move(WEST);
  424. });
  425. dirButtons[EAST] = document.getElementById("compass-east");
  426. dirButtons[EAST].addEventListener("click", function() {
  427. move(EAST);
  428. });
  429. dirButtons[SOUTH_WEST] = document.getElementById("compass-south-west");
  430. dirButtons[SOUTH_WEST].addEventListener("click", function() {
  431. move(SOUTH_WEST);
  432. });
  433. dirButtons[SOUTH] = document.getElementById("compass-south");
  434. dirButtons[SOUTH].addEventListener("click", function() {
  435. move(SOUTH);
  436. });
  437. dirButtons[SOUTH_EAST] = document.getElementById("compass-south-east");
  438. dirButtons[SOUTH_EAST].addEventListener("click", function() {
  439. move(SOUTH_EAST);
  440. });
  441. document.getElementById("compass-look").addEventListener("click", look, false);
  442. }
  443. function look() {
  444. update([currentRoom.description]);
  445. }