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

500 строки
11 KiB

  1. var things =
  2. {
  3. "Container": Container,
  4. "Person": Person,
  5. "Empty Car": EmptyCar,
  6. "Car": Car,
  7. "Bus": Bus,
  8. "Motorcycle": Motorcycle,
  9. "House": House,
  10. "Train": Train,
  11. "Parking Garage": ParkingGarage,
  12. "Overpass": Overpass
  13. };
  14. var areas =
  15. {
  16. "Container": 0,
  17. "Person": 1,
  18. "Car": 4,
  19. "Bus": 12,
  20. "Motorcycle": 2,
  21. "House": 1000,
  22. "Train": 10000,
  23. "Parking Garage": 20000,
  24. "Overpass": 10000
  25. };
  26. function fill_area(area, weights = {"Person": 0.1})
  27. {
  28. var testRadius = Math.sqrt(area / Math.PI);
  29. result = []
  30. for (var key in weights) {
  31. if (weights.hasOwnProperty(key)) {
  32. var objArea = areas[key];
  33. var objRadius = Math.sqrt(objArea / Math.PI);
  34. var newRadius = testRadius - objRadius;
  35. if (newRadius > 0) {
  36. var newArea = newRadius * newRadius * Math.PI;
  37. var numObjs = weights[key] * newArea;
  38. if (numObjs < 1) {
  39. numObjs = Math.random() > numObjs ? 0 : 1;
  40. }
  41. else {
  42. numObjs = Math.round(numObjs);
  43. }
  44. if (numObjs > 0)
  45. result.push(new things[key](numObjs));
  46. else {
  47. // try again with a better chance for just one
  48. }
  49. }
  50. }
  51. }
  52. if (result.length > 1)
  53. return new Container(result);
  54. else if (result.length == 1)
  55. return result[0];
  56. else
  57. return new Person();
  58. }
  59. var masses =
  60. {
  61. "Container": 0,
  62. "Person": 80,
  63. "Car": 1000,
  64. "Bus": 5000,
  65. "Motorcycle": 200,
  66. "House": 10000,
  67. "Train": 50000,
  68. "Parking Garage": 100000,
  69. "Overpass": 100000
  70. };
  71. // describes everything in the container
  72. function describe_all(contents) {
  73. var things = [];
  74. for (var key in contents) {
  75. if (contents.hasOwnProperty(key)) {
  76. things.push(contents[key].describe());
  77. }
  78. }
  79. return merge_things(things);
  80. }
  81. function random_desc(list, odds=1) {
  82. if (Math.random() < odds)
  83. return list[Math.floor(Math.random() * list.length)];
  84. else
  85. return "";
  86. }
  87. // combine strings into a list with proper grammar
  88. function merge_things(list,semicolons=false) {
  89. if (list.length == 0) {
  90. return "";
  91. } else if (list.length == 1) {
  92. return list[0];
  93. } else if (list.length == 2) {
  94. return list[0] + " and " + list[1];
  95. } else {
  96. result = "";
  97. list.slice(0,list.length-1).forEach(function(term) {
  98. result += term + ", ";
  99. })
  100. result += "and " + list[list.length-1]
  101. return result;
  102. }
  103. }
  104. // combine the adjectives for something into a single string
  105. function merge_desc(list) {
  106. result = ""
  107. list.forEach(function(term) {
  108. if (term != "")
  109. result += term + " ";
  110. });
  111. // knock off the last space
  112. if (result.length > 0) {
  113. result = result.substring(0, result.length - 1);
  114. }
  115. return result;
  116. }
  117. // maybe make this something that approximates a
  118. // normal distribution; doing this 15,000,000 times is bad...
  119. function distribution(min, max, samples) {
  120. var result = 0;
  121. for (var i = 0; i < samples; i++) {
  122. result += Math.floor(Math.random() * (max - min + 1) + min);
  123. }
  124. return result;
  125. }
  126. /* default actions */
  127. function defaultStomp(thing) {
  128. return function () { return "You crush " + thing.describe() + " underfoot."};
  129. }
  130. function defaultKick(thing) {
  131. return function() { return "You punt " + thing.describe() + ", destroying " + (thing.count > 1 ? "them" : "it") + "."; }
  132. }
  133. function defaultEat(thing) {
  134. return function() { return "You scoop up " + thing.describe() + " and swallow " + (thing.count > 1 ? "them" : "it") + " whole."; }
  135. }
  136. function defaultAnalVore(thing) {
  137. return function() { return "Your ass slams down on " + thing.describe() + ". " + (thing.count > 1 ? "They slide" : "It slides") + " inside with ease."; }
  138. }
  139. function defaultArea(thing) {
  140. return areas[thing.name];
  141. }
  142. function defaultMass(thing) {
  143. return masses[thing.name];
  144. }
  145. function defaultSum(thing) {
  146. return function() {
  147. var counts = {}
  148. if (thing.name != "Container")
  149. counts[thing.name] = thing.count;
  150. for (var key in thing.contents) {
  151. if (thing.contents.hasOwnProperty(key)) {
  152. subcount = thing.contents[key].sum();
  153. for (var subkey in subcount) {
  154. if (!counts.hasOwnProperty(subkey)) {
  155. counts[subkey] = 0;
  156. }
  157. counts[subkey] += subcount[subkey];
  158. }
  159. }
  160. }
  161. return counts;
  162. }
  163. }
  164. function defaultSumProperty(thing) {
  165. return function(prop) {
  166. var total = 0;
  167. total += thing[prop] * thing.count;
  168. for (var key in thing.contents) {
  169. if (thing.contents.hasOwnProperty(key)) {
  170. total += thing.contents[key].sum_property(prop);
  171. }
  172. }
  173. return total;
  174. }
  175. }
  176. function DefaultEntity() {
  177. this.stomp = defaultStomp;
  178. this.eat = defaultEat;
  179. this.kick = defaultKick;
  180. this.anal_vore = defaultAnalVore;
  181. this.sum = defaultSum;
  182. this.area = defaultArea;
  183. this.mass = defaultMass;
  184. this.sum_property = defaultSumProperty;
  185. return this;
  186. }
  187. // god I love reinventing the wheel
  188. function copy_defaults(self,proto) {
  189. for (var key in proto) {
  190. if (proto.hasOwnProperty(key)) {
  191. self[key] = proto[key](self)
  192. }
  193. }
  194. }
  195. function Container(contents = []) {
  196. this.name = "Container";
  197. copy_defaults(this,new DefaultEntity());
  198. this.count = 0;
  199. this.contents = {}
  200. for (var i=0; i < contents.length; i++) {
  201. this.contents[contents[i].name] = contents[i];
  202. }
  203. for (var key in this.contents) {
  204. if (this.contents.hasOwnProperty(key)) {
  205. this.count += this.contents[key].count;
  206. }
  207. }
  208. this.describe = function() {
  209. return describe_all(this.contents)
  210. }
  211. return this;
  212. }
  213. function Person(count = 1) {
  214. this.name = "Person";
  215. copy_defaults(this,new DefaultEntity());
  216. this.count = count;
  217. this.contents = {};
  218. this.describeOne = function (verbose=true) {
  219. sex = random_desc(["male", "female"], (verbose ? 1 : 0));
  220. body = random_desc(["skinny","fat","tall","short","stocky","spindly"], (verbose ? 0.6 : 0));
  221. species = random_desc(["wolf","cat","dog","squirrel","horse","hyena","fox","jackal","crux","sergal"]);
  222. return "a " + merge_desc([sex,body,species]);
  223. }
  224. this.describe = function() {
  225. if (count <= 3) {
  226. list = [];
  227. for (var i = 0; i < count; i++) {
  228. list.push(this.describeOne(this.count <= 2));
  229. }
  230. return merge_things(list);
  231. } else {
  232. return this.count + " people"
  233. }
  234. }
  235. return this;
  236. }
  237. function EmptyCar(count = 1) {
  238. this.name = "Car";
  239. copy_defaults(this,new DefaultEntity());
  240. this.count = count;
  241. this.contents = {};
  242. this.describeOne = function() {
  243. color = random_desc(["black","black","gray","gray","blue","red","tan","white","white"]);
  244. adjective = random_desc(["rusty","brand-new"],0.3);
  245. type = random_desc(["SUV","coupe","sedan","truck","van","convertible"]);
  246. return "a parked " + merge_desc([adjective,color,type]);
  247. }
  248. this.describe = function() {
  249. if (this.count <= 3) {
  250. list = [];
  251. for (var i = 0; i < this.count; i++) {
  252. list.push(this.describeOne());
  253. }
  254. return merge_things(list);
  255. } else {
  256. return this.count + " parked cars";
  257. }
  258. }
  259. }
  260. function Car(count = 1) {
  261. this.name = "Car";
  262. copy_defaults(this,new DefaultEntity());
  263. this.count = count;
  264. this.contents = {};
  265. var amount = distribution(2,5,count);
  266. this.contents.person = new Person(amount);
  267. this.describeOne = function(verbose=true) {
  268. color = random_desc(["black","black","gray","gray","blue","red","tan","white","white"], (verbose ? 1 : 0));
  269. adjective = random_desc(["rusty","brand-new"], (verbose ? 0.3 : 0));
  270. type = random_desc(["SUV","coupe","sedan","truck","van","convertible"]);
  271. return "a " + merge_desc([adjective,color,type]);
  272. }
  273. this.describe = function() {
  274. if (this.count <= 3) {
  275. list = [];
  276. for (var i = 0; i < this.count; i++) {
  277. list.push(this.describeOne(this.count < 2));
  278. }
  279. return merge_things(list) + " with " + this.contents.person.describe() + " inside";
  280. } else {
  281. return this.count + " cars with " + this.contents.person.describe() + " inside";
  282. }
  283. }
  284. }
  285. function Bus(count = 1) {
  286. this.name = "Bus";
  287. copy_defaults(this,new DefaultEntity());
  288. this.count = count;
  289. this.contents = {};
  290. var amount = distribution(10,35,count);
  291. this.contents.person = new Person(amount);
  292. this.describeOne = function(verbose=true) {
  293. color = random_desc(["black","tan","gray"], (verbose ? 1 : 0));
  294. type = random_desc(["bus","double-decker bus","articulating bus"]);
  295. return "a " + merge_desc([adjective,color,type]);
  296. }
  297. this.describe = function() {
  298. if (this.count <= 3) {
  299. list = [];
  300. for (var i = 0; i < this.count; i++) {
  301. list.push(this.describeOne(this.count < 2));
  302. }
  303. return merge_things(list) + " with " + this.contents.person.describe() + " inside";
  304. } else {
  305. return this.count + " buses with " + this.contents.person.describe() + " inside";
  306. }
  307. }
  308. }
  309. function Motorcycle(count = 1) {
  310. this.name = "Motorcycle";
  311. copy_defaults(this,new DefaultEntity());
  312. this.count = count;
  313. this.contents = {};
  314. var amount = distribution(1,2,count);
  315. this.contents.person = new Person(amount);
  316. }
  317. function Train(count = 1) {
  318. this.name = "Train";
  319. copy_defaults(this,new DefaultEntity());
  320. this.count = count;
  321. this.contents = {};
  322. var amount = distribution(20,60,count);
  323. this.contents.person = new Person(amount);
  324. }
  325. function House(count = 1) {
  326. this.name = "House";
  327. copy_defaults(this,new DefaultEntity());
  328. this.count = count;
  329. this.contents = {};
  330. var amount = distribution(0,8,count);
  331. this.contents.person = new Person(amount);
  332. amount = distribution(0,2,count);
  333. this.contents.emptycar = new EmptyCar(amount);
  334. this.describeOne = function(verbose=true) {
  335. size = random_desc(["little","two-story","large"], (verbose ? 0.5 : 0));
  336. color = random_desc(["blue","white","gray","tan","green"], (verbose ? 0.5 : 0));
  337. name = random_desc(["house","house","house","house","house","trailer"], 1);
  338. return "a " + merge_desc([size,color,name]);
  339. }
  340. this.describe = function() {
  341. if (this.count <= 3) {
  342. list = [];
  343. for (var i = 0; i < this.count; i++) {
  344. list.push(this.describeOne(this.count < 2));
  345. }
  346. return merge_things(list) + " with " + this.contents.person.describe() + " inside";
  347. } else {
  348. return this.count + " homes with " + this.contents.person.describe() + " inside";
  349. }
  350. }
  351. }
  352. function ParkingGarage(count = 1) {
  353. this.name = "Parking Garage";
  354. copy_defaults(this,new DefaultEntity());
  355. this.count = count;
  356. this.contents = {};
  357. var amount = distribution(10,200,count);
  358. this.contents.person = new Person(amount);
  359. amount = distribution(30,100,count);
  360. this.contents.emptycar = new EmptyCar(amount);
  361. amount = distribution(5,20,count);
  362. this.contents.car = new Car(amount);
  363. this.describeOne = function(verbose=true) {
  364. return "a parking garage";
  365. }
  366. this.describe = function() {
  367. if (this.count <= 3) {
  368. list = [];
  369. for (var i = 0; i < this.count; i++) {
  370. list.push(this.describeOne(this.count < 2));
  371. }
  372. return merge_things(list) + " with " + describe_all(this.contents) + " inside";
  373. } else {
  374. return this.count + " parking garages with " + describe_all(this.contents) + " inside";
  375. }
  376. }
  377. }
  378. function Overpass(count = 1) {
  379. this.name = "Overpass";
  380. copy_defaults(this,new DefaultEntity());
  381. this.count = count;
  382. this.contents = {};
  383. var amount = distribution(0,20,count);
  384. this.contents.person = new Person(amount);
  385. amount = distribution(25,100,count);
  386. this.contents.car = new Car(amount);
  387. }