NPCsDict.js 11 KB

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