big steppy
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

358 строки
9.0 KiB

  1. // describes everything in the container
  2. function describe_all(contents) {
  3. things = [];
  4. for (var key in contents) {
  5. if (contents.hasOwnProperty(key)) {
  6. things.push(contents[key].describe());
  7. }
  8. }
  9. return merge_things(things);
  10. }
  11. function random_desc(list, odds=1) {
  12. if (Math.random() < odds)
  13. return list[Math.floor(Math.random() * list.length)];
  14. else
  15. return "";
  16. }
  17. // combine strings into a list with proper grammar
  18. function merge_things(list,semicolons=false) {
  19. if (list.length == 0) {
  20. return "";
  21. } else if (list.length == 1) {
  22. return list[0];
  23. } else if (list.length == 2) {
  24. return list[0] + " and " + list[1];
  25. } else {
  26. result = "";
  27. list.slice(0,list.length-1).forEach(function(term) {
  28. result += term + ", ";
  29. })
  30. result += "and " + list[list.length-1]
  31. return result;
  32. }
  33. }
  34. // combine the adjectives for something into a single string
  35. function merge_desc(list) {
  36. result = ""
  37. list.forEach(function(term) {
  38. if (term != "")
  39. result += term + " ";
  40. });
  41. // knock off the last space
  42. if (result.length > 0) {
  43. result = result.substring(0, result.length - 1);
  44. }
  45. return result;
  46. }
  47. // maybe make this something that approximates a
  48. // normal distribution; doing this 15,000,000 times is bad...
  49. function distribution(min, max, samples) {
  50. var result = 0;
  51. for (var i = 0; i < samples; i++) {
  52. result += Math.floor(Math.random() * (max - min + 1) + min);
  53. }
  54. return result;
  55. }
  56. /* default actions */
  57. function defaultStomp(thing) {
  58. return function () { return "You crush " + thing.describe() + " underfoot."};
  59. }
  60. function defaultKick(thing) {
  61. return function() { return "You punt " + thing.describe() + ", destroying " + (thing.count > 1 ? "them" : "it") + "."; }
  62. }
  63. function defaultEat(thing) {
  64. return function() { return "You scoop up " + thing.describe() + " and swallow " + (thing.count > 1 ? "them" : "it") + " whole."; }
  65. }
  66. function defaultMassOne(thing) {
  67. return 80;
  68. }
  69. function defaultMass(thing) {
  70. return function() { return thing.mass_one * thing.count; }
  71. }
  72. function defaultSum(thing) {
  73. return function() {
  74. var counts = {}
  75. counts[thing.name] = thing.count;
  76. for (var key in thing.contents) {
  77. if (thing.contents.hasOwnProperty(key)) {
  78. subcount = thing.contents[key].sum();
  79. for (var subkey in subcount) {
  80. if (!counts.hasOwnProperty(subkey)) {
  81. counts[subkey] = 0;
  82. }
  83. counts[subkey] += subcount[subkey];
  84. }
  85. }
  86. }
  87. return counts;
  88. }
  89. }
  90. function DefaultEntity() {
  91. this.stomp = defaultStomp;
  92. this.eat = defaultEat;
  93. this.kick = defaultKick;
  94. this.sum = defaultSum;
  95. this.mass_one = defaultMassOne;
  96. this.mass = defaultMass;
  97. return this;
  98. }
  99. // god I love reinventing the wheel
  100. function copy_defaults(self,proto) {
  101. for (var key in proto) {
  102. if (proto.hasOwnProperty(key)) {
  103. self[key] = proto[key](self)
  104. }
  105. }
  106. }
  107. function Person(count = 1) {
  108. copy_defaults(this,new DefaultEntity());
  109. this.name = "Person";
  110. this.count = count;
  111. this.contents = {};
  112. this.describeOne = function (verbose=true) {
  113. sex = random_desc(["male", "female"], (verbose ? 1 : 0));
  114. body = random_desc(["skinny","fat","tall","short","stocky","spindly"], (verbose ? 0.6 : 0));
  115. species = random_desc(["wolf","cat","dog","squirrel","horse","hyena","fox","jackal","crux","sergal"]);
  116. return "a " + merge_desc([sex,body,species]);
  117. }
  118. this.describe = function() {
  119. if (count <= 3) {
  120. list = [];
  121. for (var i = 0; i < count; i++) {
  122. list.push(this.describeOne(this.count <= 2));
  123. }
  124. return merge_things(list);
  125. } else {
  126. return this.count + " people"
  127. }
  128. }
  129. return this;
  130. }
  131. function EmptyCar(count = 1) {
  132. copy_defaults(this,new DefaultEntity());
  133. this.name = "Car";
  134. this.count = count;
  135. this.contents = {};
  136. this.mass_one = 1000;
  137. this.describeOne = function() {
  138. color = random_desc(["black","black","gray","gray","blue","red","tan","white","white"]);
  139. adjective = random_desc(["rusty","brand-new"],0.3);
  140. type = random_desc(["SUV","coupe","sedan","truck","van","convertible"]);
  141. return "a parked " + merge_desc([adjective,color,type]);
  142. }
  143. this.describe = function() {
  144. if (this.count <= 3) {
  145. list = [];
  146. for (var i = 0; i < this.count; i++) {
  147. list.push(this.describeOne());
  148. }
  149. return merge_things(list);
  150. } else {
  151. return this.count + " parked cars";
  152. }
  153. }
  154. }
  155. function Car(count = 1) {
  156. copy_defaults(this,new DefaultEntity());
  157. this.name = "Car";
  158. this.count = count;
  159. this.contents = {};
  160. this.mass_one = 1000;
  161. var amount = distribution(2,5,count);
  162. this.contents.person = new Person(amount);
  163. this.describeOne = function(verbose=true) {
  164. color = random_desc(["black","black","gray","gray","blue","red","tan","white","white"], (verbose ? 1 : 0));
  165. adjective = random_desc(["rusty","brand-new"], (verbose ? 0.3 : 0));
  166. type = random_desc(["SUV","coupe","sedan","truck","van","convertible"]);
  167. return "a " + merge_desc([adjective,color,type]);
  168. }
  169. this.describe = function() {
  170. if (this.count <= 3) {
  171. list = [];
  172. for (var i = 0; i < this.count; i++) {
  173. list.push(this.describeOne(this.count < 2));
  174. }
  175. return merge_things(list) + " with " + this.contents.person.describe() + " inside";
  176. } else {
  177. return this.count + " cars with " + this.contents.person.describe() + " inside";
  178. }
  179. }
  180. }
  181. function Bus(count = 1) {
  182. copy_defaults(this,new DefaultEntity());
  183. this.name = "Bus";
  184. this.count = count;
  185. this.contents = {};
  186. this.mass_one = 3000;
  187. var amount = distribution(10,35,count);
  188. this.contents.person = new Person(amount);
  189. this.describeOne = function(verbose=true) {
  190. color = random_desc(["black","tan","gray"], (verbose ? 1 : 0));
  191. type = random_desc(["bus","double-decker bus","articulating bus"]);
  192. return "a " + merge_desc([adjective,color,type]);
  193. }
  194. this.describe = function() {
  195. if (this.count <= 3) {
  196. list = [];
  197. for (var i = 0; i < this.count; i++) {
  198. list.push(this.describeOne(this.count < 2));
  199. }
  200. return merge_things(list) + " with " + this.contents.person.describe() + " inside";
  201. } else {
  202. return this.count + " buses with " + this.contents.person.describe() + " inside";
  203. }
  204. }
  205. }
  206. function Motorcycle(count = 1) {
  207. copy_defaults(this,new DefaultEntity());
  208. this.name = "Motorcycle";
  209. this.count = count;
  210. this.contents = {};
  211. this.mass_one = 200;
  212. var amount = distribution(1,2,count);
  213. this.contents.person = new Person(amount);
  214. }
  215. function Train(count = 1) {
  216. copy_defaults(this,new DefaultEntity());
  217. this.name = "Train";
  218. this.count = count;
  219. this.contents = {};
  220. this.mass_one = 10000;
  221. var amount = distribution(20,60,count);
  222. this.contents.person = new Person(amount);
  223. }
  224. function House(count = 1) {
  225. copy_defaults(this,new DefaultEntity());
  226. this.name = "House";
  227. this.count = count;
  228. this.contents = {};
  229. this.mass_one = 20000;
  230. var amount = distribution(0,8,count);
  231. this.contents.person = new Person(amount);
  232. amount = distribution(0,2,count);
  233. this.contents.emptycar = new EmptyCar(amount);
  234. this.describeOne = function(verbose=true) {
  235. size = random_desc(["little","two-story","large"], (verbose ? 0.5 : 0));
  236. color = random_desc(["blue","white","gray","tan","green"], (verbose ? 0.5 : 0));
  237. name = random_desc(["house","house","house","house","house","trailer"], 1);
  238. return "a " + merge_desc([size,color,name]);
  239. }
  240. this.describe = function() {
  241. if (this.count <= 3) {
  242. list = [];
  243. for (var i = 0; i < this.count; i++) {
  244. list.push(this.describeOne(this.count < 2));
  245. }
  246. return merge_things(list) + " with " + this.contents.person.describe() + " inside";
  247. } else {
  248. return this.count + " homes with " + this.contents.person.describe() + " inside";
  249. }
  250. }
  251. }
  252. function ParkingGarage(count = 1) {
  253. copy_defaults(this,new DefaultEntity());
  254. this.name = "Parking Garage";
  255. this.count = count;
  256. this.contents = {};
  257. this.mass_one = 2000000;
  258. var amount = distribution(10,200,count);
  259. this.contents.person = new Person(amount);
  260. amount = distribution(30,100,count);
  261. this.contents.emptycar = new EmptyCar(amount);
  262. amount = distribution(5,20,count);
  263. this.contents.car = new Car(amount);
  264. this.describeOne = function(verbose=true) {
  265. return "a parking garage";
  266. }
  267. this.describe = function() {
  268. if (this.count <= 3) {
  269. list = [];
  270. for (var i = 0; i < this.count; i++) {
  271. list.push(this.describeOne(this.count < 2));
  272. }
  273. return merge_things(list) + " with " + describe_all(this.contents) + " inside";
  274. } else {
  275. return this.count + " parking garages with " + describe_all(this.contents) + " inside";
  276. }
  277. }
  278. }
  279. function Overpass(count = 1) {
  280. copy_defaults(this,new DefaultEntity());
  281. this.name = "Overpass";
  282. this.count = count;
  283. this.contents = {};
  284. this.mass_one = 4000000;
  285. var amount = distribution(0,20,count);
  286. this.contents.person = new Person(amount);
  287. amount = distribution(25,100,count);
  288. this.contents.car = new Car(amount);
  289. }