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.
 
 
 

728 line
20 KiB

  1. let world = null;
  2. let currentRoom = null;
  3. let currentDialog = null;
  4. let currentFoe = null;
  5. let dirButtons = [];
  6. let actionButtons = [];
  7. let mode = "explore";
  8. let actions = [];
  9. let time = 9*60*60;
  10. let date = 1;
  11. let newline = " ";
  12. let player = new Player();
  13. let playerAttacks = [];
  14. let struggles = [];
  15. let killingBlow = null;
  16. let deaths = [];
  17. let respawnRoom;
  18. function join(things) {
  19. if (things.length == 1) {
  20. return things[0].description("a");
  21. } else if (things.length == 2) {
  22. return things[0].description("a") + " and " + things[1].description("a");
  23. } else {
  24. let line = "";
  25. line = things.slice(0,-1).reduce((line, prey) => line + prey.description("a") + ", ", line);
  26. line += " and " + things[things.length-1].description("a");
  27. return line;
  28. }
  29. }
  30. function pickRandom(list) {
  31. return list[Math.floor(Math.random() * list.length)];
  32. }
  33. function pick(list, attacker, defender) {
  34. if (list.length == 0)
  35. return null;
  36. else {
  37. let sum = list.reduce((sum, choice) => choice.weight == undefined ? sum + 1 : sum + choice.weight(attacker, defender), 0);
  38. let target = Math.random() * sum;
  39. for (let i = 0; i < list.length; i++) {
  40. sum -= list[i].weight == undefined ? 1 : list[i].weight(attacker, defender);
  41. if (sum <= target) {
  42. return list[i];
  43. }
  44. }
  45. return list[list.length-1];
  46. }
  47. }
  48. function filterValid(options, attacker, defender) {
  49. let filtered = options.filter(option => option.conditions == undefined || option.conditions.reduce((result, test) => result && test(attacker, defender), true));
  50. return filtered.filter(option => option.requirements == undefined || option.requirements.reduce((result, test) => result && test(attacker, defender), true));
  51. }
  52. function filterPriority(options) {
  53. let max = options.reduce((max, option) => option.priority > max ? option.priority : max, -1000);
  54. return options.filter(option => option.priority == max);
  55. }
  56. function round(number, digits) {
  57. return Math.round(number * Math.pow(10,digits)) / Math.pow(10,digits);
  58. }
  59. function updateExploreCompass() {
  60. for (let i = 0; i < dirButtons.length; i++) {
  61. let button = dirButtons[i];
  62. button.classList.remove("active-button");
  63. button.classList.remove("inactive-button");
  64. button.classList.remove("disabled-button");
  65. if (currentRoom.exits[i] == null) {
  66. button.disabled = true;
  67. button.classList.add("inactive-button");
  68. button.innerHTML = "";
  69. } else {
  70. if (currentRoom.exits[i].conditions.reduce((result, test) => result && test(player.prefs), true)) {
  71. button.disabled = false;
  72. button.classList.add("active-button");
  73. button.innerHTML = currentRoom.exits[i].name;
  74. } else {
  75. button.disabled = true;
  76. button.classList.add("disabled-button");
  77. button.innerHTML = currentRoom.exits[i].name;
  78. }
  79. }
  80. }
  81. }
  82. function updateExploreActions() {
  83. for (let i = 0; i < actionButtons.length; i++) {
  84. if (i < actions.length) {
  85. actionButtons[i].disabled = false;
  86. actionButtons[i].innerHTML = actions[i].name;
  87. actionButtons[i].classList.remove("inactive-button");
  88. actionButtons[i].classList.add("active-button");
  89. }
  90. else {
  91. actionButtons[i].disabled = true;
  92. actionButtons[i].innerHTML = "";
  93. actionButtons[i].classList.remove("active-button");
  94. actionButtons[i].classList.add("inactive-button");
  95. }
  96. }
  97. }
  98. function updateExplore() {
  99. updateExploreCompass();
  100. updateExploreActions();
  101. }
  102. function updateEaten() {
  103. let list = document.getElementById("eaten");
  104. while(list.firstChild) {
  105. list.removeChild(list.firstChild);
  106. }
  107. if (player.health > 0)
  108. struggles = filterValid(currentFoe.struggles, currentFoe, player);
  109. else
  110. struggles = [submit(currentFoe)];
  111. for (let i = 0; i < struggles.length; i++) {
  112. let li = document.createElement("li");
  113. let button = document.createElement("button");
  114. button.classList.add("eaten-button");
  115. button.innerHTML = struggles[i].name;
  116. button.addEventListener("click", function() { struggleClicked(i); } );
  117. button.addEventListener("mouseover", function() { struggleHovered(i); } );
  118. button.addEventListener("mouseout", function() { document.getElementById("eaten-desc").innerHTML = ""; } );
  119. li.appendChild(button);
  120. list.appendChild(li);
  121. }
  122. }
  123. function updateCombat() {
  124. let list = document.getElementById("combat");
  125. while(list.firstChild) {
  126. list.removeChild(list.firstChild);
  127. }
  128. if (player.health > 0)
  129. playerAttacks = filterValid(player.attacks, player, currentFoe);
  130. else
  131. playerAttacks = [pass(player)];
  132. if (playerAttacks.length == 0)
  133. playerAttacks = [player.backupAttack];
  134. for (let i = 0; i < playerAttacks.length; i++) {
  135. let li = document.createElement("li");
  136. let button = document.createElement("button");
  137. button.classList.add("combat-button");
  138. button.innerHTML = playerAttacks[i].name;
  139. button.addEventListener("click", function() { attackClicked(i); } );
  140. button.addEventListener("mouseover", function() { attackHovered(i); } );
  141. button.addEventListener("mouseout", function() { document.getElementById("combat-desc").innerHTML = ""; } );
  142. li.appendChild(button);
  143. list.appendChild(li);
  144. }
  145. document.getElementById("stat-foe-name").innerHTML = "Name: " + currentFoe.name;
  146. document.getElementById("stat-foe-health").innerHTML = "Health: " + currentFoe.health + "/" + currentFoe.maxHealth;
  147. document.getElementById("stat-foe-stamina").innerHTML = "Stamina: " + currentFoe.stamina + "/" + currentFoe.maxStamina;
  148. document.getElementById("stat-foe-str").innerHTML = "Str: " + currentFoe.str;
  149. document.getElementById("stat-foe-dex").innerHTML = "Dex: " + currentFoe.dex;
  150. document.getElementById("stat-foe-con").innerHTML = "Con: " + currentFoe.con;
  151. }
  152. function updateDialog() {
  153. let list = document.getElementById("dialog");
  154. while(list.firstChild) {
  155. list.removeChild(list.firstChild);
  156. }
  157. for (let i = 0; i < currentDialog.choices.length; i++) {
  158. let activated = currentDialog.choices[i].node.requirements == undefined || currentDialog.choices[i].node.requirements.reduce((result, test) => result && test(player, currentFoe), true);
  159. let li = document.createElement("li");
  160. let button = document.createElement("button");
  161. button.classList.add("dialog-button");
  162. button.innerHTML = currentDialog.choices[i].text;
  163. button.addEventListener("click", function() { dialogClicked(i); });
  164. if (!activated) {
  165. button.classList.add("disabled-button");
  166. button.disabled = true;
  167. }
  168. li.appendChild(button);
  169. list.appendChild(li);
  170. }
  171. }
  172. function updateDisplay() {
  173. document.querySelectorAll(".selector").forEach(function (x) {
  174. x.style.display = "none";
  175. });
  176. switch(mode) {
  177. case "explore":
  178. document.getElementById("selector-explore").style.display = "flex";
  179. updateExplore();
  180. break;
  181. case "combat":
  182. document.getElementById("selector-combat").style.display = "flex";
  183. updateCombat();
  184. break;
  185. case "dialog":
  186. document.getElementById("selector-dialog").style.display = "flex";
  187. updateDialog();
  188. break;
  189. case "eaten":
  190. document.getElementById("selector-eaten").style.display = "flex";
  191. updateEaten();
  192. break;
  193. }
  194. document.getElementById("time").innerHTML = "Time: " + renderTime(time);
  195. document.getElementById("date").innerHTML = "Day " + date;
  196. document.getElementById("stat-name").innerHTML = "Name: " + player.name;
  197. document.getElementById("stat-health").innerHTML = "Health: " + round(player.health,0) + "/" + round(player.maxHealth,0);
  198. document.getElementById("stat-cash").innerHTML = "Cash: $" + round(player.cash,0);
  199. document.getElementById("stat-stamina").innerHTML = "Stamina: " + round(player.stamina,0) + "/" + round(player.maxStamina,0);
  200. document.getElementById("stat-fullness").innerHTML = "Fullness: " + round(player.fullness(),0);
  201. if (player.prefs.scat) {
  202. document.getElementById("stat-bowels").innerHTML = "Bowels: " + round(player.bowels.fullness,0);
  203. } else {
  204. document.getElementById("stat-bowels").innerHTML = "";
  205. }
  206. }
  207. function advanceTime(amount) {
  208. time = (time + amount);
  209. date += Math.floor(time / 86400);
  210. time = time % 86400;
  211. player.restoreHealth(amount);
  212. player.restoreStamina(amount);
  213. update(player.stomach.digest(amount));
  214. update(player.butt.digest(amount));
  215. }
  216. function renderTime(time) {
  217. let suffix = (time < 43200) ? "AM" : "PM";
  218. let hour = Math.floor((time % 43200) / 3600);
  219. if (hour == 0)
  220. hour = 12;
  221. let minute = Math.floor(time / 60) % 60;
  222. if (minute < 9)
  223. minute = "0" + minute;
  224. return hour + ":" + minute + " " + suffix;
  225. }
  226. function move(direction) {
  227. let target = currentRoom.exits[direction];
  228. if (target == null) {
  229. alert("Tried to move to an empty room!");
  230. return;
  231. }
  232. moveTo(target,currentRoom.exitDescs[direction]);
  233. }
  234. function moveToByName(roomName, desc="You go places lol", loading=false) {
  235. moveTo(world[roomName], desc, loading);
  236. }
  237. function moveTo(room,desc="You go places lol", loading=false) {
  238. actions = [];
  239. currentRoom = room;
  240. if (!loading)
  241. advanceTime(30);
  242. currentRoom.objects.forEach(function (object) {
  243. object.actions.forEach(function (action) {
  244. if (action.conditions == undefined || action.conditions.reduce((result, cond) => result && cond(player.prefs), true))
  245. actions.push(action);
  246. });
  247. });
  248. update([desc,newline]);
  249. currentRoom.visit();
  250. }
  251. window.addEventListener('load', function(event) {
  252. document.getElementById("start-button").addEventListener("click", start, false);
  253. });
  254. function start() {
  255. applySettings(generateSettings());
  256. document.getElementById("create").style.display = "none";
  257. document.getElementById("game").style.display = "block";
  258. document.getElementById("stat-button-status").addEventListener("click", status, false);
  259. loadActions();
  260. loadCompass();
  261. loadDialog();
  262. world = createWorld();
  263. currentRoom = world["Bedroom"];
  264. respawnRoom = currentRoom;
  265. moveTo(currentRoom,"");
  266. updateDisplay();
  267. }
  268. // copied from Stroll LUL
  269. function generateSettings() {
  270. let form = document.forms.namedItem("character-form");
  271. let settings = {};
  272. for (let i=0; i<form.length; i++) {
  273. let value = form[i].value == "" ? form[i].placeholder : form[i].value;
  274. if (form[i].type == "text")
  275. if (form[i].value == "")
  276. settings[form[i].name] = form[i].placeholder;
  277. else
  278. settings[form[i].name] = value;
  279. else if (form[i].type == "number")
  280. settings[form[i].name] = parseFloat(value);
  281. else if (form[i].type == "checkbox") {
  282. settings[form[i].name] = form[i].checked;
  283. } else if (form[i].type == "radio") {
  284. let name = form[i].name;
  285. if (form[i].checked)
  286. settings[name] = form[i].value;
  287. } else if (form[i].type == "select-one") {
  288. settings[form[i].name] = form[i][form[i].selectedIndex].value;
  289. }
  290. }
  291. return settings;
  292. }
  293. function applySettings(settings) {
  294. player.name = settings.name;
  295. player.species = settings.species;
  296. for (let key in settings) {
  297. if (settings.hasOwnProperty(key)) {
  298. if (key.match(/prefs/)) {
  299. let tokens = key.split("-");
  300. let pref = player.prefs;
  301. pref = tokens.slice(1,-1).reduce(function(pref, key) {
  302. if (pref[key] == undefined)
  303. pref[key] = {};
  304. return pref[key];
  305. }, pref);
  306. pref[tokens.slice(-1)[0]] = settings[key];
  307. }
  308. }
  309. }
  310. }
  311. function saveSettings() {
  312. window.localStorage.setItem("settings", JSON.stringify(generateSettings()));
  313. }
  314. function retrieveSettings() {
  315. return JSON.parse(window.localStorage.getItem("settings"));
  316. }
  317. function clearScreen() {
  318. let log = document.getElementById("log");
  319. let child = log.firstChild;
  320. while (child != null) {
  321. log.removeChild(child);
  322. child = log.firstChild;
  323. }
  324. }
  325. function update(lines=[]) {
  326. let log = document.getElementById("log");
  327. for (let i=0; i<lines.length; i++) {
  328. let div = document.createElement("div");
  329. div.innerHTML = lines[i];
  330. log.appendChild(div);
  331. }
  332. log.scrollTop = log.scrollHeight;
  333. updateDisplay();
  334. }
  335. function changeMode(newMode) {
  336. mode = newMode;
  337. let body = document.querySelector("body");
  338. document.getElementById("foe-stats").style.display = "none";
  339. body.className = "";
  340. switch(mode) {
  341. case "explore":
  342. case "dialog":
  343. body.classList.add("explore");
  344. break;
  345. case "combat":
  346. body.classList.add("combat");
  347. document.getElementById("foe-stats").style.display = "block";
  348. break;
  349. case "eaten":
  350. body.classList.add("eaten");
  351. document.getElementById("foe-stats").style.display = "block";
  352. break;
  353. }
  354. updateDisplay();
  355. }
  356. function respawn(respawnRoom) {
  357. if (killingBlow.gameover == undefined) {
  358. if (player.prefs.prey) {
  359. deaths.push("Digested by " + currentFoe.description("a") + " at " + renderTime(time) + " on day " + date);
  360. } else {
  361. deaths.push("Defeated by " + currentFoe.description("a") + " at " + renderTime(time) + " on day " + date);
  362. }
  363. } else {
  364. deaths.push(killingBlow.gameover() + " at " + renderTime(time) + " on day " + date);
  365. }
  366. moveTo(respawnRoom,"You drift through space and time...");
  367. player.clear();
  368. player.stomach.contents = [];
  369. player.butt.contents = [];
  370. player.bowels.contents = [];
  371. player.bowels.fullness = 0;
  372. advanceTime(Math.floor(86400 / 2 * (Math.random() * 0.5 - 0.25 + 1)));
  373. changeMode("explore");
  374. player.health = 100;
  375. update(["You wake back up in your bed."]);
  376. }
  377. function startCombat(opponent) {
  378. currentFoe = opponent;
  379. changeMode("combat");
  380. update(opponent.startCombat());
  381. }
  382. function attackClicked(index) {
  383. update(playerAttacks[index].attack(currentFoe).concat([newline]));
  384. if (currentFoe.health <= 0) {
  385. currentFoe.defeated();
  386. } else if (mode == "combat") {
  387. let attack = pick(filterPriority(filterValid(currentFoe.attacks, currentFoe, player)), currentFoe, player);
  388. if (attack == null) {
  389. attack = currentFoe.backupAttack;
  390. }
  391. update(attack.attackPlayer(player).concat([newline]));
  392. if (player.health <= -100) {
  393. killingBlow = attack;
  394. update(["You die..."]);
  395. respawn(respawnRoom);
  396. } else if (player.health <= 0) {
  397. update(["You're too weak to do anything..."]);
  398. if (player.prefs.prey) {
  399. // nada
  400. } else {
  401. killingBlow = attack;
  402. update(["You die..."]);
  403. respawn(respawnRoom);
  404. }
  405. }
  406. }
  407. }
  408. function attackHovered(index) {
  409. document.getElementById("combat-desc").innerHTML = playerAttacks[index].desc;
  410. }
  411. function struggleClicked(index) {
  412. let struggle = struggles[index];
  413. let result = struggle.struggle(player);
  414. update(result.lines.concat([newline]));
  415. if (result.escape == "stay") {
  416. changeMode("combat");
  417. } else if (result.escape == "escape") {
  418. changeMode("explore");
  419. } else {
  420. let digest = pick(filterValid(currentFoe.digests, currentFoe, player), currentFoe, player);
  421. if (digest == null) {
  422. digest = currentFoe.backupDigest;
  423. }
  424. update(digest.digest(player).concat([newline]));
  425. if (player.health <= -100) {
  426. killingBlow = digest;
  427. update(currentFoe.finishDigest().concat([newline]));
  428. respawn(respawnRoom);
  429. }
  430. }
  431. }
  432. function struggleHovered(index) {
  433. document.getElementById("eaten-desc").innerHTML = currentFoe.struggles[index].desc;
  434. }
  435. function startDialog(dialog) {
  436. currentDialog = dialog;
  437. changeMode("dialog");
  438. update(currentDialog.text);
  439. currentDialog.visit();
  440. updateDisplay();
  441. }
  442. function dialogClicked(index) {
  443. currentDialog = currentDialog.choices[index].node;
  444. update(currentDialog.text);
  445. currentDialog.visit();
  446. if (currentDialog.choices.length == 0 && mode == "dialog") {
  447. changeMode("explore");
  448. updateDisplay();
  449. }
  450. }
  451. function loadDialog() {
  452. dialogButtons = Array.from( document.querySelectorAll(".dialog-button"));
  453. for (let i = 0; i < dialogButtons.length; i++) {
  454. dialogButtons[i].addEventListener("click", function() { dialogClicked(i); });
  455. }
  456. }
  457. function actionClicked(index) {
  458. actions[index].action();
  459. }
  460. function loadActions() {
  461. actionButtons = Array.from( document.querySelectorAll(".action-button"));
  462. for (let i = 0; i < actionButtons.length; i++) {
  463. actionButtons[i].addEventListener("click", function() { actionClicked(i); });
  464. }
  465. }
  466. function loadCompass() {
  467. dirButtons[NORTH_WEST] = document.getElementById("compass-north-west");
  468. dirButtons[NORTH_WEST].addEventListener("click", function() {
  469. move(NORTH_WEST);
  470. });
  471. dirButtons[NORTH] = document.getElementById("compass-north");
  472. dirButtons[NORTH].addEventListener("click", function() {
  473. move(NORTH);
  474. });
  475. dirButtons[NORTH_EAST] = document.getElementById("compass-north-east");
  476. dirButtons[NORTH_EAST].addEventListener("click", function() {
  477. move(NORTH_EAST);
  478. });
  479. dirButtons[WEST] = document.getElementById("compass-west");
  480. dirButtons[WEST].addEventListener("click", function() {
  481. move(WEST);
  482. });
  483. dirButtons[EAST] = document.getElementById("compass-east");
  484. dirButtons[EAST].addEventListener("click", function() {
  485. move(EAST);
  486. });
  487. dirButtons[SOUTH_WEST] = document.getElementById("compass-south-west");
  488. dirButtons[SOUTH_WEST].addEventListener("click", function() {
  489. move(SOUTH_WEST);
  490. });
  491. dirButtons[SOUTH] = document.getElementById("compass-south");
  492. dirButtons[SOUTH].addEventListener("click", function() {
  493. move(SOUTH);
  494. });
  495. dirButtons[SOUTH_EAST] = document.getElementById("compass-south-east");
  496. dirButtons[SOUTH_EAST].addEventListener("click", function() {
  497. move(SOUTH_EAST);
  498. });
  499. document.getElementById("compass-look").addEventListener("click", look, false);
  500. }
  501. function look() {
  502. update([currentRoom.description]);
  503. }
  504. function status() {
  505. let lines = [];
  506. lines.push("You are a " + player.species);
  507. lines.push(newline);
  508. if (player.stomach.contents.length > 0) {
  509. lines.push("Your stomach bulges with prey.");
  510. player.stomach.contents.map(function(prey) {
  511. let state = "";
  512. let healthRatio = prey.health / prey.maxHealth;
  513. if (healthRatio > 0.75) {
  514. state = "is thrashing in your gut";
  515. } else if (healthRatio > 0.5) {
  516. state = "is squirming in your belly";
  517. } else if (healthRatio > 0.25) {
  518. state = "is pressing out at your stomach walls";
  519. } else if (healthRatio > 0) {
  520. state = "is weakly squirming";
  521. } else {
  522. state = "has stopped moving";
  523. }
  524. lines.push(prey.description("A") + " " + state);
  525. });
  526. lines.push(newline);
  527. }
  528. if (player.butt.contents.length > 0) {
  529. lines.push("Your bowels churn with prey.");
  530. player.butt.contents.map(function(prey) {
  531. let state = "";
  532. let healthRatio = prey.health / prey.maxHealth;
  533. if (healthRatio > 0.75) {
  534. state = "is writhing in your bowels";
  535. } else if (healthRatio > 0.5) {
  536. state = "is struggling against your intestines";
  537. } else if (healthRatio > 0.25) {
  538. state = "is bulging out of your lower belly";
  539. } else if (healthRatio > 0) {
  540. state = "is squirming weakly, slipping deeper and deeper";
  541. } else {
  542. state = "has succumbed to your bowels";
  543. }
  544. lines.push(prey.description("A") + " " + state);
  545. });
  546. lines.push(newline);
  547. }
  548. update(lines);
  549. }
  550. let toSave = ["str","dex","con","name","species"];
  551. function saveGame() {
  552. let save = {};
  553. save.player = JSON.stringify(player, function(key, value) {
  554. if (toSave.includes(key) || key == "") {
  555. return value;
  556. } else {
  557. return undefined;
  558. }
  559. });
  560. save.position = currentRoom.name;
  561. save.date = date;
  562. save.time = time;
  563. save.deaths = deaths;
  564. let stringified = JSON.stringify(save);
  565. window.localStorage.setItem("save", stringified);
  566. }
  567. function loadGame() {
  568. changeMode("explore");
  569. let save = JSON.parse(window.localStorage.getItem("save"));
  570. let playerSave = JSON.parse(save.player);
  571. for (let key in playerSave) {
  572. if (playerSave.hasOwnProperty(key)) {
  573. player[key] = playerSave[key];
  574. }
  575. }
  576. deaths = save.deaths;
  577. date = save.date;
  578. time = save.time;
  579. clearScreen();
  580. moveToByName(save.position, "");
  581. }
  582. // wow polyfills
  583. if (![].includes) {
  584. Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
  585. 'use strict';
  586. var O = Object(this);
  587. var len = parseInt(O.length) || 0;
  588. if (len === 0) {
  589. return false;
  590. }
  591. var n = parseInt(arguments[1]) || 0;
  592. var k;
  593. if (n >= 0) {
  594. k = n;
  595. } else {
  596. k = len + n;
  597. if (k < 0) {k = 0;}
  598. }
  599. var currentElement;
  600. while (k < len) {
  601. currentElement = O[k];
  602. if (searchElement === currentElement ||
  603. (searchElement !== searchElement && currentElement !== currentElement)) {
  604. return true;
  605. }
  606. k++;
  607. }
  608. return false;
  609. };
  610. }