使用缓存机制,优化了存有大量手牌时,chooseToDiscard的AI的计算速度。增加一个公用的缓存方法

This commit is contained in:
kuangthree 2023-10-05 12:39:23 +08:00
parent 7d2ffba9b4
commit 592d748099
1 changed files with 104 additions and 12 deletions

View File

@ -14816,7 +14816,8 @@
} }
event.trigger('phaseDiscard'); event.trigger('phaseDiscard');
"step 1" "step 1"
player.chooseToDiscard(num,true); player.chooseToDiscard(num,true)
.set('useCache',true);
"step 2" "step 2"
event.cards=result.cards; event.cards=result.cards;
}, },
@ -22594,6 +22595,20 @@
} }
return skills; return skills;
}, },
getModableSkills:function(useCache){
var func = function(player){
var skills=player.getSkills().concat(lib.skill.global);
game.expandSkills(skills);
skills = skills.filter(function(skill){
var info = get.info(skill);
return info && info.mod;
});
skills.sort((a,b)=>get.priority(a)-get.priority(b));
return skills;
};
if(!useCache)return func(this);
return game.callFuncUseStepCache("player.getModableSkills",func,[this]);
},
getSkills:function(arg2,arg3,arg4){ getSkills:function(arg2,arg3,arg4){
var skills=this.skills.slice(0); var skills=this.skills.slice(0);
var es=[]; var es=[];
@ -26212,12 +26227,14 @@
}, },
addInvisibleSkill:function(skill){ addInvisibleSkill:function(skill){
if(Array.isArray(skill)){ if(Array.isArray(skill)){
_status.event.clearStepCache();
for(var i=0;i<skill.length;i++){ for(var i=0;i<skill.length;i++){
this.addInvisibleSkill(skill[i]); this.addInvisibleSkill(skill[i]);
} }
} }
else{ else{
if(this.invisibleSkills.contains(skill)) return; if(this.invisibleSkills.contains(skill)) return;
_status.event.clearStepCache();
var info=lib.skill[skill]; var info=lib.skill[skill];
if(!info) return; if(!info) return;
this.invisibleSkills.add(skill); this.invisibleSkills.add(skill);
@ -26247,12 +26264,14 @@
}, },
addSkill:function(skill,checkConflict,nobroadcast,addToSkills){ addSkill:function(skill,checkConflict,nobroadcast,addToSkills){
if(Array.isArray(skill)){ if(Array.isArray(skill)){
_status.event.clearStepCache();
for(var i=0;i<skill.length;i++){ for(var i=0;i<skill.length;i++){
this.addSkill(skill[i]); this.addSkill(skill[i]);
} }
} }
else{ else{
if(this.skills.contains(skill)) return; if(this.skills.contains(skill)) return;
_status.event.clearStepCache();
var info=lib.skill[skill]; var info=lib.skill[skill];
if(!info) return; if(!info) return;
if(!addToSkills){ if(!addToSkills){
@ -26337,6 +26356,7 @@
this.additionalSkills[skill].push(skills[i]); this.additionalSkills[skill].push(skills[i]);
} }
this.checkConflict(); this.checkConflict();
_status.event.clearStepCache();
return this; return this;
}, },
removeAdditionalSkill:function(skill,target){ removeAdditionalSkill:function(skill,target){
@ -26360,6 +26380,7 @@
} }
} }
} }
_status.event.clearStepCache();
return this; return this;
}, },
awakenSkill:function(skill,nounmark){ awakenSkill:function(skill,nounmark){
@ -26367,6 +26388,7 @@
this.disableSkill(skill+'_awake',skill); this.disableSkill(skill+'_awake',skill);
this.awakenedSkills.add(skill); this.awakenedSkills.add(skill);
if(this.storage[skill]===false) this.storage[skill]=true; if(this.storage[skill]===false) this.storage[skill]=true;
_status.event.clearStepCache();
return this; return this;
}, },
restoreSkill:function(skill,nomark){ restoreSkill:function(skill,nomark){
@ -26374,6 +26396,7 @@
this.awakenedSkills.remove(skill); this.awakenedSkills.remove(skill);
this.enableSkill(skill+'_awake',skill); this.enableSkill(skill+'_awake',skill);
if(!nomark) this.markSkill(skill); if(!nomark) this.markSkill(skill);
_status.event.clearStepCache();
return this; return this;
}, },
disableSkill:function(skill,skills){ disableSkill:function(skill,skills){
@ -26426,6 +26449,7 @@
this.disableSkill(skill,skills[i]); this.disableSkill(skill,skills[i]);
} }
} }
_status.event.clearStepCache();
return this; return this;
}, },
enableSkill:function(skill){ enableSkill:function(skill){
@ -26435,6 +26459,7 @@
delete this.disabledSkills[i]; delete this.disabledSkills[i];
} }
} }
_status.event.clearStepCache();
return this; return this;
}, },
checkMarks:function(){ checkMarks:function(){
@ -29702,6 +29727,33 @@
finish:function(){ finish:function(){
this.finished=true; this.finished=true;
}, },
putStepCache:function(key,value){
if(!this._stepCache){
this._stepCache = {};
}
this._stepCache[key] = value;
},
getStepCache:function(key){
if(!this._stepCache)return undefined;
return this._stepCache[key];
},
clearStepCache:function(key){
if(key !== undefined && key !== null){
delete this._stepCache[key];
}
delete this._stepCache;
},
callFuncUseStepCache:function(prefix,func,params){
if(typeof func != 'function')return;
if(_status.closeStepCache)return func.apply(null,params);
var cacheKey = "["+prefix+"]"+get.paramToCacheKey.apply(null,params);
var ret = this.getStepCache(cacheKey);
if(ret === undefined || ret === null){
ret = func.apply(null,params);
this.putStepCache(cacheKey,ret);
}
return ret;
},
putTempCache:function(key1,key2,value){ putTempCache:function(key1,key2,value){
if(!this._tempCache){ if(!this._tempCache){
this._tempCache = {}; this._tempCache = {};
@ -34220,6 +34272,17 @@
} }
return next; return next;
}, },
callFuncUseStepCache:function(prefix,func,params){
if(typeof func != 'function')return;
if(_status.closeStepCache || !_status.event)return func.apply(null,params);
var cacheKey = "["+prefix+"]"+get.paramToCacheKey.apply(null,params);
var ret = _status.event.getStepCache(cacheKey);
if(ret === undefined || ret === null){
ret = func.apply(null,params);
_status.event.putStepCache(cacheKey,ret);
}
return ret;
},
getRarity:function(name){ getRarity:function(name){
var rank=lib.rank.rarity; var rank=lib.rank.rarity;
if(rank.legend.contains(name)) return 'legend'; if(rank.legend.contains(name)) return 'legend';
@ -38750,6 +38813,7 @@
_status,lib,game,ui,get,ai); _status,lib,game,ui,get,ai);
} }
} }
event.clearStepCache();
event.step++; event.step++;
} }
} }
@ -40471,17 +40535,12 @@
checkMod:function(){ checkMod:function(){
const argumentArray=Array.from(arguments),name=argumentArray[argumentArray.length-2]; const argumentArray=Array.from(arguments),name=argumentArray[argumentArray.length-2];
let skills=argumentArray[argumentArray.length-1]; let skills=argumentArray[argumentArray.length-1];
if(skills.getSkills) skills=skills.getSkills(); if(skills.getSkills) skills=skills.getModableSkills(_status.event.useCache === true);
skills=skills.concat(lib.skill.global);
game.expandSkills(skills);
skills=skills.filter(skill=>{
const info=get.info(skill);
return (info&&info.mod&&info.mod[name]);
})
skills.sort((a,b)=>get.priority(a)-get.priority(b));
const arg=argumentArray.slice(0,-2); const arg=argumentArray.slice(0,-2);
skills.forEach(value=>{ skills.forEach(value=>{
const result=get.info(value).mod[name].apply(this,arg); var mod = get.info(value).mod[name];
if(!mod)return;
const result=mod.apply(this,arg);
if(typeof arg[arg.length-1]!='object'&&result!=undefined) arg[arg.length-1]=result; if(typeof arg[arg.length-1]!='object'&&result!=undefined) arg[arg.length-1]=result;
}); });
return arg[arg.length-1]; return arg[arg.length-1];
@ -56030,6 +56089,27 @@
} }
return str; return str;
}, },
//用于将参数转换为字符串作为缓存的key。
paramToCacheKey:function(){
var str = "";
for(var arg of arguments){
if(arg === null || arg === undefined){
str += (arg + "-");
continue;
}
if(arg.playerid){
str += "p:"+arg.playerid;
}else if(arg.cardid){
str += "c:"+arg.cardid;
}else if(arg.name){
str += "n:"+arg.name;
}else{
str += "s:"+arg;
}
str+="-";
}
return str;
},
yunjiao:function(str){ yunjiao:function(str){
const util=window.pinyinUtilx; const util=window.pinyinUtilx;
if(util) str=util.removeTone(str) if(util) str=util.removeTone(str)
@ -59949,7 +60029,7 @@
sgnAttitude:function(){ sgnAttitude:function(){
return get.sgn(get.attitude.apply(this,arguments)); return get.sgn(get.attitude.apply(this,arguments));
}, },
useful:function(card,player){ useful_raw:function(card,player){
if(get.position(card)=='j') return -1; if(get.position(card)=='j') return -1;
if(get.position(card)=='e') return get.equipValue(card); if(get.position(card)=='e') return get.equipValue(card);
if(card._modUseful){ if(card._modUseful){
@ -59958,7 +60038,13 @@
var i=0; var i=0;
if(!player) player=_status.event.player; if(!player) player=_status.event.player;
if(player){ if(player){
i=player.getCards('h',card.name).indexOf(card); if(_status.event.useCache){
i = game.callFuncUseStepCache("player.getCardsInUseful",function(player,position,cardname){
return player.getCards(position,cardname);
},[player,'h',card.name]).indexOf(card);
}else{
i=player.getCards('h',card.name).indexOf(card);
}
if(i<0) i=0; if(i<0) i=0;
} }
var aii=get.info(card).ai; var aii=get.info(card).ai;
@ -59978,6 +60064,12 @@
result=game.checkMod(player,card,result,'aiUseful',player); result=game.checkMod(player,card,result,'aiUseful',player);
return result; return result;
}, },
useful:function(card,player){
if(_status.event.useCache){
return game.callFuncUseStepCache("get.useful_raw",get.useful_raw,[card,player]);
}
return this.useful_raw(card,player);
},
unuseful:function(card){ unuseful:function(card){
return -get.useful(card); return -get.useful(card);
}, },