forked from dreampuf/sgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic_ai.js
231 lines (213 loc) · 8.37 KB
/
logic_ai.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var sgs = sgs || {};
var _ = sgs.func.format,
filter = sgs.func.filter,
exclude = sgs.func.exclude,
shuffle = sgs.func.shuffle,
range = sgs.func.range,
choice = sgs.func.choice,
each = sgs.func.each,
zip = sgs.func.zip,
max = sgs.func.max,
map = sgs.func.map;
(function(sgs){
/* 通知 */
sgs.uuid = "";
sgs.eventNotifyEquip = function(playerID, card, equipType, addOrRemove) {
}
sgs.eventNotify = function(msg, callback) {
};
sgs.Ai = function(bout, player, lv) {
/* AI 解析对象
* player: 扮演玩家对象
* bout: 当前局
* lv: AI难度 0, 1, 2; 简单,普通,困难
* */
this.player = player;
this.bout = bout;
this.lv = lv || bout.ailv;
this.hassha = false; /* 当前玩家是否已出杀 */
};
sgs.Ai.interpreter = function(bout, opt) {
};
sgs.Ai.magic_weigh = { /* 锦囊牌权重 */
"顺手牵羊": 5,
"无中生有": 5,
"南蛮入侵": 5,
"万箭齐发": 5,
"过河拆桥": 4,
"决斗": 4,
"借刀杀人": 3,
"五谷丰登": 2,
"桃园结义": 2,
"乐不思蜀": 2,
"无懈可击": 1,
"闪电": 1,
};
sgs.Ai.identity_rela = { /* 身份之间敌对关系 (1 ~ 3) */
/*主公*/
0 : { 0 : 0,
1 : 1,
2 : 2,
3 : 3 },
/*忠臣*/
1 : { 0 : 1,
1 : 0,
2 : 3,
3 : 3 },
/*内奸*/
2 : { 0 : 2,
1 : 3,
2 : 0,
3 : 3 },
/*反贼*/
3 : { 0 : 3,
1 : 3,
2 : 3,
3 : 0 },
};
sgs.Ai.interpreter.attack_deviation = (function(rela_map){ return function(bout, plsrc) {
/* 目前仅仅依据身份评判进攻对象 */
var plsrc_iden = plsrc.identity,
pls_rel = map(bout.player, function(i){ return rela_map[plsrc_iden][i.identity]; });
return pls_rel;
} })(sgs.Ai.identity_rela);
sgs.Ai.interpreter.magic_deviation = (function(magic_weigh,
CARD_MAGIC_RANGE_MAPPING){ return function(bout, plsrc, pltar) {
/* 使用锦囊决策 */
var magic_cards = filter(plsrc.card, function(i) { return CARD_MAGIC_RANGE_MAPPING[i.name] &&
i.name != "无懈可击"; }),
be_use_card, be_use_card_weigh = -1, card_weigh, card_select_info;
each(magic_cards, function(n, i){
card_weigh = magic_weigh[i.name];
if(be_use_card_weigh < card_weigh){
card_select_info = bout.select_card(new sgs.Operate(i.name, plsrc, pltar, i));
if(card_select_info[0].indexOf(pltar) != -1) {
be_use_card = i;
be_use_card_weigh = card_weigh;
}
}
});
return be_use_card;
} })(sgs.Ai.magic_weigh,
sgs.CARD_MAGIC_RANGE_MAPPING);
sgs.Ai.prototype.ask_card = (function(){ return function(opt) {
var pl = this.player,
bout = this.bout,
cardname = opt.data,
opt_top = this.bout.playOpts[bout.playOpts.length - 1];
if(opt.id == "技能") {
switch(cardname) {
case "洛神":
return bout.response_card(new sgs.Operate("技能", pl, pl, "洛神"));
case "鬼才":
return bout.response_card(new sgs.Operate("技能", pl, pl, false));
}
} else {
switch(cardname) {
case "无懈可击":
if(opt.source == pl && opt_top.target != pl) { /* 不无懈自己出的牌 */
return bout.response_card(new sgs.Operate(cardname, pl, pl, pl.findcard(cardname)));
}
break;
case "桃":
if(opt.source == pl) { /* 自己 */
return bout.response_card(new sgs.Operate(cardname, pl, pl, pl.findcard(cardname)));
}
break;
case "杀":
case "闪":
return bout.response_card(new sgs.Operate(cardname, pl, opt.source, pl.findcard(cardname)));
}
}
return bout.response_card(new sgs.Operate(cardname, pl, opt.source));
} })();
sgs.Ai.prototype.choose_card = (function(){ return function(opt) {
var pl = this.player,
bout = this.bout;
var card = bout.choiceList.pop();
pl.addcard(card);
bout.notify("get_card", pl, [card]);
} })();
sgs.Ai.prototype.play_card = (function(attack_deviation,
magic_deviation,
EQUIP_TYPE_MAPPING,
CARD_MAGIC_RANGE_MAPPING){ return function() {
var pl = this.player,
plstatus = pl.status,
bout = this.bout,
use = false,
cards = pl.card,
be_use_card,
card_select_info;
/* 有装备就装备 */
var equips = filter(cards, function(i) { return EQUIP_TYPE_MAPPING[i.name] != undefined; });
if(equips.length) {
var the_equip = equips[0],
equip_pos = EQUIP_TYPE_MAPPING[the_equip.name];
bout.play_card(new sgs.Operate("装备", pl, pl, the_equip));
return ;
}
/* 缺血有桃就桃 */
var peachs = filter(cards, function(i) { return i.name == "桃"; });
if(peachs.length && pl.blood < pl.maxblood) {
return bout.play_card(new sgs.Operate("桃", pl, pl, peachs[0]));
}
/* 非伤害性锦囊 */
each(cards, function(n, i) {
if(["无中生有", "桃园结义", "五谷丰登", "闪电"].indexOf(i.name) != -1) {
be_use_card = i;
return false;
}
});
if(be_use_card) {
return bout.play_card(new sgs.Operate(be_use_card.name, pl, pl, be_use_card));
}
/* 使用锦囊 */
var pls_rela = attack_deviation(bout, pl),
pls_max = max(pls_rela),
pltar = bout.player[pls_rela.indexOf(pls_max)];
be_use_card = magic_deviation(bout, pl, pltar);
if(be_use_card) {
return bout.play_card(new sgs.Operate(be_use_card.name, pl, pltar, be_use_card));
}
/* 使用杀 */
be_use_card = filter(cards, function(i) { return i.name == "杀"; });
use = be_use_card.length > 0 && !plstatus["hassha"];
if(use) {
card_select_info = bout.select_card(new sgs.Operate("杀", pl, pltar, be_use_card[0]));
if(card_select_info[0].indexOf(pltar) == -1) { /* 如果最佳对象不在可选区域,则改变次级对象 */
pltar = undefined;
each(card_select_info[0], function(n, i) {
if(pls_rela[pl.position] >= 2) {
pltar = i;
return false;
}
});
}
if(pltar) {
plstatus["hassha"] = pl.equip[0] && pl.equip[0].name == "诸葛连弩" || pl.skill("咆哮")
? false
: true; /* 诸葛连弩连杀 */
be_use_card = be_use_card[0];
return bout.play_card(new sgs.Operate(be_use_card.name, pl, pltar, be_use_card));
}
}
this.drop_card();
} })(sgs.Ai.interpreter.attack_deviation,
sgs.Ai.interpreter.magic_deviation,
sgs.EQUIP_TYPE_MAPPING,
sgs.CARD_MAGIC_RANGE_MAPPING);
sgs.Ai.prototype.drop_card = function() {
this.player.status["hassha"] = false;
var bout = this.bout;
/* 简单AI 啥也不做 */
opt = bout.drop_card(new sgs.Operate("弃牌", this.player));
while(opt) {
console.log("需要弃牌", opt.data, "张");
opt = bout.drop_card(new sgs.Operate("弃牌",
this.player,
undefined,
choice(this.player.card, opt.data)));
}
};
})(window.sgs);