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.
 
 
 

516 lines
15 KiB

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