// 目标1:武将每隔2-5年有概率生娃
var handle = {
OnStoryStart:OnStoryStart,
OnSubgroupTurn:OnSubgroupTurn,
};
RegGameHandler(handle); //注册回调函数
var test_mode = false;
function OnStoryStart(){
// 如果是开发者模式,显示菜单,选择模式。
if (Story.dev){
var idx = UI.Menu(["测试模式","正常模式"]);
if (idx == 0){
test_mode = true;
}
}
}
function OnSubgroupTurn(groupid, subgroupid){
if (test_mode){
// 如果是开发者选择了调试模式,每一回合都执行
// 刘备势力以外的不进行测试
if (groupid != 3){
return;
}
}else{
// 只有每年1月上旬执行
if (Story.month != 1 || Story.tendays != 0){
return;
}
}
// 查询本势力所有武将
var myheros = Story.GetHeroByGroup(groupid);
console.log("heros=" + myheros.length);
for (var i=0; i<myheros.length; i++){
var hero = myheros[i];
if (hero.male){
RandomMakeABaby(hero);
}
}
}
function RandomMakeABaby(father){
if (father.nextbabyyear == null){
if (test_mode){
father.nextbabyyear = Story.year;
}else{
father.nextbabyyear = Story.year + Misc.RandomI(2,5);
}
}
// 检查“预定生孩子”是否到期
if (father.nextbabyyear > Story.year){
return;
}
// 根据年龄计算概率
// 孩子登场岁数统一为18岁
var baby_age = 18;
var father_age_min = 38;
var father_age_max = 78;
// 男性武将从20岁到60岁为生0岁孩子区间,加上18后,男性武将38-78岁区间为得到18岁孩子的区间。
var father_age = father.Age();
if (father_age < father_age_min || father_age > father_age_max){
// 不符合年龄要求,生不出孩子
return;
}
// 符合要求,计算概率
// 概率分布类似正弦函数0-180度之间的图像
var r = (father_age - father_age_min)* 180/(father_age_max - father_age_min);
var x = Math.sin(Misc.DEG2RAD(r)) * 0.3;
var act_x = Misc.RandomF(0,1);
console.log("概率为x:" + x + ",实际随机act_x" + act_x);
if (act_x < x){
var baby = MakeBaby(father);
// 这里要判断一下,可能生失败。比如武将满了。。。
if (baby != null){
console.log("生了一个孩子:" + JSON.stringify(baby));
UI.Talks([
{heroid:baby.id, msg:"爹!"},
{heroid:father.id, msg:"我儿!"},
]);
// 生娃成功后,设置武将下一次生娃的时间
father.nextbabyyear = Story.year + Misc.RandomI(2,5);
}
}
}
var lastnamelist = ["建国","保国","利给","司机"];
function GetRandomLastName(){
var idx = Misc.RandomI(0, lastnamelist.length);
return lastnamelist[idx];
}
function MakeBaby(father){
var param = {
name_f: father.name_f,
name_l: GetRandomLastName(),
faceid: "def:"+Misc.RandomI(1,500),
male: true,
ayear: Story.year,
byear: Story.year - 18,
dyear: Story.year +Misc.RandomI(10, 50),
fatherid: father.id,
motherid: father.mateid,
like: father.like,
skillid: father.skillid,
strategy: father.strategy,
tong_base: father.tong_base + Misc.RandomI(-10, 2),
wu_base: father.wu_base + Misc.RandomI(-10, 2),
zhi_base: father.zhi_base + Misc.RandomI(-10, 2),
zheng_base: father.zheng_base + Misc.RandomI(-10, 2),
mei_base: father.mei_base + Misc.RandomI(-10, 2),
buildingid: father.buildingid,
maxequipt: 1,
};
var babyid = Story.AddHero(param);
var baby = Story.GetHero(babyid);
return baby;
}