123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- const npcFieldBoundaries = {
- rel:{min:0,max:100}
- }
- let pronounDictLowerCase = {
- 'he': ['he','she'],
- 'him': ['him','her'],
- 'his': ['his','her'],
- }
- let pronounDictUpperFirst = {};
- let pronounDictUpperCase = {};
- for (const [key, value] of Object.entries(pronounDictLowerCase)) {
- pronounDictUpperFirst[key.toUpperFirst()] = [value[0].toUpperFirst(),value[1].toUpperFirst()];
- pronounDictUpperCase[key.toUpperCase()] = [value[0].toUpperCase(),value[1].toUpperCase()];
- }
- const pronounDict = Object.assign({},pronounDictLowerCase,pronounDictUpperFirst,pronounDictUpperCase);
- /*
- NPC-Fields:
- date:
- date.count:
- n: Number of dates one had with the NPC.
- During a date, it indicates that it is the nth date
- date.kissed:
- n: Number of times one has kissed the NPC.
- date.nextDateExpected:
- -1: does not expect a date and won't call asking for one
- most commonly set to -1 because a date is already scheduled
- n: will start calling at day n and ask for a date
- rel: Relationship Value / how much the NPC likes you
- familiarity:
- 0 - stranger
- 1000 - knows everything about each other
- */
- class NPCsDict{
- _dynamicData = {}
- _generatedIds = [];
- _generatedIdsCounter = 0;
- _boyfriends = [];
- dec(npcId,field,v){
- this.inc(npcId,field,v * -1);
- }
- generate(){
- this._generatedIdsCounter += 1;
- let id = 'C'+this._generatedIdsCounter;
- this._generatedIds.push(id);
- this.set(id,'keepAlive',true);
- return id;
- }
- get(npcId,field,def=''){
- let data = this.npcData(npcId);
- if(field in data)
- return data[field];
- if("passage" in data){
- let resultFromPassage = func(data.passage,'vars',field);
- if(resultFromPassage)
- return resultFromPassage;
- }
- switch(field){
- case 'age': return this.get_age(npcId);
- case 'birthday':
- let dayOfBirth = this.get(npcId,'dob');
- let day = dayOfBirth % 100;
- let month = Math.floor((dayOfBirth % 10000)/100);
- let year = Math.floor(dayOfBirth / 10000);
- return new Date(Date.UTC(year,month-1,day))
- case 'dick_girth':
- let thdick = this.get(npcId,'thdick');
- switch(thdick){
- case 'skinny':
- return 0;
- case 'slim':
- return 5;
- case 'well proportioned':
- return 10;
- case 'thicker than average':
- return 15;
- case 'thick':
- return 20;
- case 'massive':
- return 25;
- case 'monstrous':
- return 30;
- default :
- return 0;
- }
- case 'fullname': return this.get(npcId,'firstname')+' '+this.get(npcId,'lastname');
- case 'title':
- switch (this.get(npcId,'gender')) {
- case 0:
- return 'Mr.'
- case 1:
- return 'Mrs.'
- default:
- return 'Mrx.'
- }
- case 'usedname':
- switch (this.get(npcId,'nameScheme','firstname')) {
- case 'title_lastname':
- return `${this.get(npcId,'title')} ${this.get(npcId,'lastname')}`;
- case 'firstname':
- default:
- return this.get(npcId,'firstname');
- }
-
- }
- if(field.endsWith('_possessive')){
- field = field.replace('_possessive','');
- let value = this.get(npcId,field,def);
- if(value.endsWith('s'))
- return value+"'"; //https://dictionary.cambridge.org/grammar/british-grammar/possession-john-s-car-a-friend-of-mine
- return value + "'s";
- }
- return def;
- }
- get_age(npcId){
- let dayOfBirth = this.get(npcId,'dob');
- let day = dayOfBirth % 100;
- let month = Math.floor((dayOfBirth % 10000)/100);
- let year = Math.floor(dayOfBirth / 10000);
- return State.variables.time.ageOfDate(day,month,year);
- }
- getDynamicData(npcId){
- if(npcId in this._dynamicData)
- return this._dynamicData[npcId];
- return {};
- }
- getStaticData(npcId){
- if(npcId in setup.npcs)
- return setup.npcs[npcId];
- return {};
- }
- inc(npcId,field,v){
- let d = '';
- if(typeof v === "number")
- d = 0;
- let current = this.get(npcId,field,d);
- let newValue = current + v;
- this.set(npcId,field,newValue);
- }
- incBulk(npcIds,field,v){
- for(let npcId of npcIds){
- this.inc(npcId,field,v);
- }
- }
- incBulkByFilter(filter={},field,v){
- let npcsIds = this.ids(filter);
- this.incBulk(npcsIds,field,v);
- }
- ids(filter={}){
- let keysStatic = Object.keys(setup.npcs);
- let keysDynamic= Object.keys(this._dynamicData);
- let keys = keysStatic.concatUnique(keysDynamic);
- if(jQuery.isEmptyObject(filter))
- return keys;
- let keysMatchingFilter = [];
- for(let key of keys){
- if(this.npcFitsFilter(key,filter))
- keysMatchingFilter.push(key);
- }
- return keysMatchingFilter;
- }
- makeBoyfriend(npcId){
- if(this._boyfriends.includes(npcId)){
- console.warn('Already boyfriend:',npcId);
- return;
- }
- this.set(npcId,'keepAlive',true);
- let defaults = this.get(npcId,'defaults',[]);
- defaults.push('boyfriend');
- this.set(npcId,'defaults',defaults);
- this._boyfriends.push(npcId);
- }
- memoryUpdate(npcId){
- let rememberedFields = this.get(npcId,'memoryVars',[]);
- if(!rememberedFields.length)
- return;
- let now = State.variables.time.now;
- let memory = {time:now,vars:{}};
- for(let rememberedField of rememberedFields){
- memory.vars[rememberedField] = getvar(rememberedField);
- }
- this.set(npcId,'memory',memory);
- console.log('NPC memory updated',npcId,memory);
- }
- memoryUpdateAll(){
- const npcIds = this.ids();
- for(const npcId of npcIds){
- this.memoryUpdate(npcId);
- }
- }
- npcData(npcId){
- let staticData = this.getStaticData(npcId);
- let dynamicData = this.getDynamicData(npcId);
- let data = Object.assign({},staticData,dynamicData);
- if("gender" in data)
- data = Object.assign({},setup.npcDefaults.gender[data.gender],data); //Apply the gender-defaults first.
- if("defaults" in data){
- for(let defaultId of data.defaults){
- let def = setup.npcDefaults[defaultId];
- data = Object.assign({},def,data); //Apply the defaults first. They could be overwritten by static or dynamic data
- }
- }
- return data;
- }
- npcFitsFilter(npcId,filters={}){
- for (const [filterKey, filterValue] of Object.entries(filters)) {
- let npcValue = undefined;
- if(typeof filterValue === "number")
- npcValue = this.get(npcId,filterKey,0);
- else
- npcValue = this.get(npcId,filterKey,'');
- if(npcValue != filterValue){
- return false;
- }
- }
- return true;
- }
- pronoun(npcId,pronoun){
- let gender = this.get(npcId,'gender',0);
- if(pronoun in pronounDict)
- return pronounDict[pronoun][gender];
- return 'GENDER KEY MISSING: '+pronoun+'['+gender+']';
- }
- set(npcId,field,v){
- if(!(npcId in this._dynamicData))
- this._dynamicData[npcId] = {};
- if(field in npcFieldBoundaries){
- let fieldRules = npcFieldBoundaries[field];
- if("min" in fieldRules)
- v = Math.max(v, fieldRules.min);
- if("max" in fieldRules)
- v = Math.min(v, fieldRules.max);
- }
- switch(field){
- case 'age':
- let birthday = State.variables.time.dateOfAge(v);
- this.set(npcId,'birthday',birthday);
- break;
- case 'birthday':
- let dob = v.getUTCFullYear()*10000 + (v.getUTCMonth()+1)*100 + v.getUTCDate();
- this.set(npcId,'dob',dob);
- break;
- default:
- this._dynamicData[npcId][field] = v;
- }
- console.log("NPC value set",npcId,field,v);
- }
- //npcIds: Array of ids
- setBulk(npcIds,field,v){
- for(let npcId of npcIds){
- this.set(npcId,field,v);
- }
- }
- setBulkByFilter(filter={},field,v){
- let npcsIds = this.ids(filter);
- this.setBulk(npcsIds,field,v);
- }
- // ----- Dating -----
- _datingNPCs = [];
- get datingNPCs(){return this._datingNPCs;}
- // Only sets the NPC to dating. Does not make him call you at any point. Use <<datingStart>> for this.
- datingStart(npcID){
- console.log("Start Dating:",npcID);
- this._datingNPCs.push(npcID);
- this.set(npcID,'dating',true);
- }
- // ----- Location -----
- location(npcId){
- let locationInformation = this.get(npcId,'location',{});
- if(locationInformation.location && State.variables.time.now <= locationInformation.end)
- return locationInformation;
- return this._updateLocation(npcId);
- }
- locationGroup(npcIds){
- let locations = {};
- for(let npcId of npcIds){
- let location = this.location(npcId);
- if(!(location.location in locations)){
- locations[location.location] = {};
- }
- locations[location.location][npcId] = location;
- }
- return locations;
- }
- _updateLocation(npcId){
- let npcPassage = this.get(npcId,'passage');
- let locationFromPassage = {};
- let overwriteLocationInformation = this.get(npcId,'location',{});
- if(Story.has(npcPassage))
- locationFromPassage = func(npcPassage,'location');
- if(locationFromPassage){
- overwriteLocationInformation = Object.assign({},overwriteLocationInformation,locationFromPassage);
- }
- if(!('end' in overwriteLocationInformation))
- overwriteLocationInformation.end = State.variables.time.time(State.variables.time.hour+1,0);
- this.set(npcId,'location',overwriteLocationInformation);
- return overwriteLocationInformation;
- }
- constructor(){}
- _init(nPCsDict){
- Object.keys(nPCsDict).forEach(function (pn) {
- this[pn] = clone(nPCsDict[pn]);
- }, this);
- return this;
- }
- clone = function () {
- return (new NPCsDict())._init(this);
- };
- toJSON = function () {
- var ownData = {};
- Object.keys(this).forEach(function (pn) {
- if(typeof this[pn] !== "function")
- ownData[pn] = clone(this[pn]);
- }, this);
- return JSON.reviveWrapper('(new NPCsDict())._init($ReviveData$)', ownData);
- };
- }
- window.NPCsDict = NPCsDict;
|