NPCsDict.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. const npcFieldBoundaries = {
  2. rel:{min:0,max:100}
  3. }
  4. let pronounDictLowerCase = {
  5. 'he': ['he','she'],
  6. 'him': ['him','her'],
  7. 'his': ['his','her'],
  8. }
  9. let pronounDictUpperFirst = {};
  10. let pronounDictUpperCase = {};
  11. for (const [key, value] of Object.entries(pronounDictLowerCase)) {
  12. pronounDictUpperFirst[key.toUpperFirst()] = [value[0].toUpperFirst(),value[1].toUpperFirst()];
  13. pronounDictUpperCase[key.toUpperCase()] = [value[0].toUpperCase(),value[1].toUpperCase()];
  14. }
  15. const pronounDict = Object.assign({},pronounDictLowerCase,pronounDictUpperFirst,pronounDictUpperCase);
  16. /*
  17. NPC-Fields:
  18. date:
  19. date.count:
  20. n: Number of dates one had with the NPC.
  21. During a date, it indicates that it is the nth date
  22. date.kissed:
  23. n: Number of times one has kissed the NPC.
  24. date.nextDateExpected:
  25. -1: does not expect a date and won't call asking for one
  26. most commonly set to -1 because a date is already scheduled
  27. n: will start calling at day n and ask for a date
  28. */
  29. class NPCsDict{
  30. _dynamicData = {}
  31. _generatedIds = [];
  32. _generatedIdsCounter = 0;
  33. _boyfriends = [];
  34. dec(npcId,field,v){
  35. this.inc(npcId,field,v * -1);
  36. }
  37. generate(){
  38. this._generatedIdsCounter += 1;
  39. let id = 'C'+this._generatedIdsCounter;
  40. this._generatedIds.push(id);
  41. this.set(id,'keepAlive',true);
  42. return id;
  43. }
  44. get(npcId,field,def=''){
  45. let data = this.npcData(npcId);
  46. if(field in data)
  47. return data[field];
  48. if("passage" in data){
  49. let resultFromPassage = func(data.passage,'vars',field);
  50. if(resultFromPassage)
  51. return resultFromPassage;
  52. }
  53. switch(field){
  54. case 'age': return this.get_age(npcId);
  55. case 'birthday':
  56. let dayOfBirth = this.get(npcId,'dob');
  57. let day = dayOfBirth % 100;
  58. let month = Math.floor((dayOfBirth % 10000)/100);
  59. let year = Math.floor(dayOfBirth / 10000);
  60. return new Date(Date.UTC(year,month-1,day))
  61. case 'dick_girth':
  62. let thdick = this.get(npcId,'thdick');
  63. switch(thdick){
  64. case 'skinny':
  65. return 0;
  66. case 'slim':
  67. return 5;
  68. case 'well proportioned':
  69. return 10;
  70. case 'thicker than average':
  71. return 15;
  72. case 'thick':
  73. return 20;
  74. case 'massive':
  75. return 25;
  76. case 'monstrous':
  77. return 30;
  78. default :
  79. return 0;
  80. }
  81. case 'fullname': return this.get(npcId,'firstname')+' '+this.get(npcId,'lastname');
  82. case 'usedname': return this.get(npcId,'firstname');
  83. }
  84. if(field.endsWith('_possessive')){
  85. field = field.replace('_possessive','');
  86. let value = this.get(npcId,field,def);
  87. if(value.endsWith('s'))
  88. return value+"'"; //https://dictionary.cambridge.org/grammar/british-grammar/possession-john-s-car-a-friend-of-mine
  89. return value + "'s";
  90. }
  91. return def;
  92. }
  93. get_age(npcId){
  94. let dayOfBirth = this.get(npcId,'dob');
  95. let day = dayOfBirth % 100;
  96. let month = Math.floor((dayOfBirth % 10000)/100);
  97. let year = Math.floor(dayOfBirth / 10000);
  98. return State.variables.time.ageOfDate(day,month,year);
  99. }
  100. getDynamicData(npcId){
  101. if(npcId in this._dynamicData)
  102. return this._dynamicData[npcId];
  103. return {};
  104. }
  105. getStaticData(npcId){
  106. if(npcId in setup.npcs)
  107. return setup.npcs[npcId];
  108. return {};
  109. }
  110. inc(npcId,field,v){
  111. let d = '';
  112. if(typeof v === "number")
  113. d = 0;
  114. let current = this.get(npcId,field,d);
  115. let newValue = current + v;
  116. this.set(npcId,field,newValue);
  117. }
  118. incBulk(npcIds,field,v){
  119. for(let npcId of npcIds){
  120. this.inc(npcId,field,v);
  121. }
  122. }
  123. incBulkByFilter(filter={},field,v){
  124. let npcsIds = this.ids(filter);
  125. this.incBulk(npcsIds,field,v);
  126. }
  127. ids(filter={}){
  128. let keysStatic = Object.keys(setup.npcs);
  129. let keysDynamic= Object.keys(this._dynamicData);
  130. let keys = keysStatic.concatUnique(keysDynamic);
  131. if(jQuery.isEmptyObject(filter))
  132. return keys;
  133. let keysMatchingFilter = [];
  134. for(let key of keys){
  135. if(this.npcFitsFilter(key,filter))
  136. keysMatchingFilter.push(key);
  137. }
  138. return keysMatchingFilter;
  139. }
  140. makeBoyfriend(npcId){
  141. if(this._boyfriends.includes(npcId)){
  142. console.warn('Already boyfriend:',npcId);
  143. return;
  144. }
  145. this.set(npcId,'keepAlive',true);
  146. let defaults = this.get(npcId,'defaults',[]);
  147. defaults.push('boyfriend');
  148. this.set(npcId,'defaults',defaults);
  149. this._boyfriends.push(npcId);
  150. }
  151. memoryUpdate(npcId){
  152. let rememberedFields = this.get(npcId,'memoryVars',[]);
  153. if(!rememberedFields.length)
  154. return;
  155. let now = State.variables.time.now;
  156. let memory = {time:now,vars:{}};
  157. for(let rememberedField of rememberedFields){
  158. memory.vars[rememberedField] = getvar(rememberedField);
  159. }
  160. this.set(npcId,'memory',memory);
  161. console.log('NPC memory updated',npcId,memory);
  162. }
  163. memoryUpdateAll(){
  164. const npcIds = this.ids();
  165. for(const npcId of npcIds){
  166. this.memoryUpdate(npcId);
  167. }
  168. }
  169. npcData(npcId){
  170. let staticData = this.getStaticData(npcId);
  171. let dynamicData = this.getDynamicData(npcId);
  172. let data = Object.assign({},staticData,dynamicData);
  173. if("gender" in data)
  174. data = Object.assign({},setup.npcDefaults.gender[data.gender],data); //Apply the gender-defaults first.
  175. if("defaults" in data){
  176. for(let defaultId of data.defaults){
  177. let def = setup.npcDefaults[defaultId];
  178. data = Object.assign({},def,data); //Apply the defaults first. They could be overwritten by static or dynamic data
  179. }
  180. }
  181. return data;
  182. }
  183. npcFitsFilter(npcId,filters={}){
  184. for (const [filterKey, filterValue] of Object.entries(filters)) {
  185. let npcValue = undefined;
  186. if(typeof filterValue === "number")
  187. npcValue = this.get(npcId,filterKey,0);
  188. else
  189. npcValue = this.get(npcId,filterKey,'');
  190. if(npcValue != filterValue){
  191. return false;
  192. }
  193. }
  194. return true;
  195. }
  196. pronoun(npcId,pronoun){
  197. let gender = this.get(npcId,'gender',0);
  198. if(pronoun in pronounDict)
  199. return pronounDict[pronoun][gender];
  200. return 'GENDER KEY MISSING: '+pronoun+'['+gender+']';
  201. }
  202. set(npcId,field,v){
  203. if(!(npcId in this._dynamicData))
  204. this._dynamicData[npcId] = {};
  205. if(field in npcFieldBoundaries){
  206. let fieldRules = npcFieldBoundaries[field];
  207. if("min" in fieldRules)
  208. v = Math.max(v, fieldRules.min);
  209. if("max" in fieldRules)
  210. v = Math.min(v, fieldRules.max);
  211. }
  212. switch(field){
  213. case 'age':
  214. let birthday = State.variables.time.dateOfAge(v);
  215. this.set(npcId,'birthday',birthday);
  216. break;
  217. case 'birthday':
  218. let dob = v.getUTCFullYear()*10000 + (v.getUTCMonth()+1)*100 + v.getUTCDate();
  219. this.set(npcId,'dob',dob);
  220. break;
  221. default:
  222. this._dynamicData[npcId][field] = v;
  223. }
  224. console.log("NPC value set",npcId,field,v);
  225. }
  226. //npcIds: Array of ids
  227. setBulk(npcIds,field,v){
  228. for(let npcId of npcIds){
  229. this.set(npcId,field,v);
  230. }
  231. }
  232. setBulkByFilter(filter={},field,v){
  233. let npcsIds = this.ids(filter);
  234. this.setBulk(npcsIds,field,v);
  235. }
  236. // ----- Dating -----
  237. _datingNPCs = [];
  238. get datingNPCs(){return this._datingNPCs;}
  239. // Only sets the NPC to dating. Does not make him call you at any point. Use <<datingStart>> for this.
  240. datingStart(npcID){
  241. console.log("Start Dating:",npcID);
  242. this._datingNPCs.push(npcID);
  243. this.set(npcID,'dating',true);
  244. }
  245. // ----- Location -----
  246. location(npcId){
  247. let locationInformation = this.get(npcId,'location',{});
  248. if(locationInformation.location && State.variables.time.now <= locationInformation.end)
  249. return locationInformation;
  250. return this._updateLocation(npcId);
  251. }
  252. locationGroup(npcIds){
  253. let locations = {};
  254. for(let npcId of npcIds){
  255. let location = this.location(npcId);
  256. if(!(location.location in locations)){
  257. locations[location.location] = {};
  258. }
  259. locations[location.location][npcId] = location;
  260. }
  261. return locations;
  262. }
  263. _updateLocation(npcId){
  264. let npcPassage = this.get(npcId,'passage');
  265. let locationFromPassage = {};
  266. let overwriteLocationInformation = this.get(npcId,'location',{});
  267. if(Story.has(npcPassage))
  268. locationFromPassage = func(npcPassage,'location');
  269. if(locationFromPassage){
  270. overwriteLocationInformation = Object.assign({},overwriteLocationInformation,locationFromPassage);
  271. }
  272. if(!('end' in overwriteLocationInformation))
  273. overwriteLocationInformation = State.variables.time.time(State.variables.time.hour+1,0);
  274. this.set(npcId,'location',overwriteLocationInformation);
  275. return overwriteLocationInformation;
  276. }
  277. constructor(){}
  278. _init(nPCsDict){
  279. Object.keys(nPCsDict).forEach(function (pn) {
  280. this[pn] = clone(nPCsDict[pn]);
  281. }, this);
  282. return this;
  283. }
  284. clone = function () {
  285. return (new NPCsDict())._init(this);
  286. };
  287. toJSON = function () {
  288. var ownData = {};
  289. Object.keys(this).forEach(function (pn) {
  290. if(typeof this[pn] !== "function")
  291. ownData[pn] = clone(this[pn]);
  292. }, this);
  293. return JSON.reviveWrapper('(new NPCsDict())._init($ReviveData$)', ownData);
  294. };
  295. }
  296. window.NPCsDict = NPCsDict;