-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbmai.cpp
316 lines (286 loc) · 14.1 KB
/
bmai.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
///////////////////////////////////////////////////////////////////////////////////////////
// bmai.cpp
//
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright © 2001 Denis Papp <[email protected]>
// SPDX-FileComment: https://github.com/pappde/bmai
//
// DESC: BMAI main code and main game rules
//
// REVISION HISTORY:
// drp040101 - handle TRIP
// drp040101 - handle OPTION (basic decision only)
// drp040301 - added BERSERK, fixed SPEED, fixed parsing MOOD/TURBO SWING dice with a defined value
// drp040701 - handle TIME_AND_SPACE
// ability to set target wins and to set number of simulations,
// store who won initiative
// 'playfair' code
// BMC_Logger
// drp040801 - added MIGHTY and WEAK
// drp040901 - fixed so correctly allows changing # wins
// fixed PlayFair stats
// fixed memory leak in BMC_MoveList::Clear
// added mode 3 to PlayFair (2-ply mode 1)
// drp041001 - correctly fixed memory leak (~BMC_MoveList)
// don't consider TRIP when doing initiative
// play games with a copy of g_game (otherwise things like berserk maintain state change)
// sizeof die to 20, sizeof game to 432
// use g_ai_mode1 as the QAI for mode3
// changed movelist to start at 16 entries in the vector
// changed movelist to use vector of moves instead of move pointers
// drp041201 - added 'ply #' command
// drp041501 - fixed incorrect calculation of m_sides_max on some parsed option dice
// drp051001 - implemented GetSetSwingAction() - uses simulations
// drp051201 - implemented support for RESERVE (AI and simulation)
// drp051401 - implemented support for ORNERY
// - recognize that m_swing_set for opponent if only has (defined) option dice
// drp060201 - implemented support for TURBO OPTION (not TURBO SWING, and only one die)
// drp060301 - implemented support for TURBO SWING (slow!)
// drp060501 - implemented 'maxbranch' param for BMAI, to cap sims in "moves * sims"
// - implemented g_turbo_accuracy
// drp070701 - implemented support for CHANCE
// fixed initiative rules, which gave advantage to player with fewer dice (incorrect)
// drp072101 - added "debug" command,
// - added "seed" command
// - changed mode 3 for "playfair" to be standard QAI
// - in PlayGame() don't call GetReserveAction() unless the loser has reserve dice
// - more ROUND debugging
// drp080501 - implemented support for MORPHING dice, adds set The Metamorphers
// drp081201 - fixed NULL (count as 0 in addition to capture effect - i.e. they are 0 when captured or held)
// drp102801 - release 1.0
// drp111001 - support for FOCUS
// drp111401 - support for WARRIOR (Sailor Moon)
// drp032502 - support for SLOW (Brom - Giant)
// drp033002 - support for UNIQUE (2000 Rare Promo)
// drp033102 - moved to BMAI2 (with culling, ply 3)
// - TRIP illegal if can't capture (i.e. one die tripping two)
// drp040102 - website now live (www.buttonmen.com)
// - more BMAI2 improvements
// drp062702 - BMAI2: improvements with TRIP and bad situations
// drp062902 - better RNG
// - fixed dealing with TWIN that are MIGHTY/WEAK
// - BMAI2: surrenders if no move seems to result in a win
// - BMAI2: fixed movelist leak in CullMoves()
// - BMAI2: culling for preround (swing/option)
// drp063002 - Added performance stats
// - BMAI: fixed major bug where ply3+ BMAI/BMAI2 was not working
// - BMAI3: replaces BMAI2, major change (for attack actions) to use next ply for more accurate move scoring.
// Instead of playing out the sim and assigning a score of -1/0/+1 it uses the estimated P(win) of the next ply action.
// drp070202 - m_min_sims/m_max_sims. Previously the min was 20 now the default is 5.
// - apply decay to min/max sims [currently hard-coded]
// - BMAI: apply max_branch to UseChance() and Reserve()
// drp070502 - GenerateValidChance()
// - BMAI3: apply move culling to GetUseChance(), GetUseFocus()
// - BMAI3: use accurate move scoring on preround moves
// drp070602 - Stats: BMAI3 average moves/sims per ply
// drp072102 - support for UNSKILLED (The Flying Squirrel)
// drp082802 - support for STINGER (set Diceland)
// drp092302 - ignore setswing action if have no swing dice (useless ply)
// - AI::GetSetSwingAction() did not work with more than one swing type (but only used with ply 1 AIs)
// drp022203 - MoveList: reserve space for 32 instead of 16 moves. Also use store Move objects instead of pointers
// so that we cut out the extra allocs (if using pool, we would want to use pointers)
// - BMC_Move: cut from 32b to 24b by replacing m_option_die with a BitArray<>
// - BMAI3 SetSwing: catch case where the number of moves is too huge [Gordo] by randomly cutting out moves
// drp071005 - changed unskilled from "k" to "~" (for Konstant)
// - added basic support for Konstant
// drp071305 - konstant: don't reroll (not done yet)
// - fixed memory trasher with turbo swing
// drp071505 - fixed UNIQUE check (mattered), fixed OPTION check (didn't matter)
// drp091905 - BMAI3 Focus/Chance: "pass" moves were being mis-evaluated (scoring from POV of opponent) so
// the AI would almost always pick "pass". Now pass "pov_player" to EvaluateMove() functions.
// This was a big problem and explains the 42.4% with "Legend of the Five Rings" or
// 46.7% with Yoyodyne,
// dbl051823 - added P-Swing, Q-Swing support
// dbl052623 - main method shim allows us to split a library from an executable
// so we can link to the testing libraries
// dbl053123 - moved TestRNG and SetupTestGame to a `./test/` dir and testing framework
// dbl053123 - added ability to disallow surrenders
// dbl100824 - migrating most logic out into their own classes allows this to be the main method shim
// and the other classes can be linked for testing
// - created many BMC_ .cpp/.h files
// - adjusted many globals which helped narrow down the #include dependency tree
// dbl102424 - Implemented Insult skill
// dbl110424 - Parser can now parse passed in String
// - Implemented Value skill
///////////////////////////////////////////////////////////////////////////////////////////
// TODO:
// - reenable have_turbo check
// - 092103 BUG: the game had a small chance of ending in a tie but
// BMAI reported 100% win
// TOP TODO
// - allow 1 die skill attacks (important for FIRE)
// - decay param
// - Performance: cap ply on preround moves, est time after 1 ply 1 sim and readjust ply, tweak plies/sims/decay
// - Performance: VTune
// - Hope vs Hope
// - BMAI3: move culling and use scored moves on reserve
// - move ordering
// TODO:
// - test ^hHc
// - Washu: swing must be submitted with reserve
// - QAI fuzziness should be based on range of scores found
// - comprehensive search
// - optimize game state
// - pre-allocate moves, use move pool
// - in N->1 and 1->N, can optimize by cutting out based on !CanDoAttack, and !CanBeAttacked
// so it doesn't pursue lines that include that die
// - stats (i.e. nodes evaluated, etc)
// - ScoreAttack() based on capture, trip, mood/bers/mighty/weak
// - optimize: if player maintains which properties are present in dice, GetValidAttacks() can optimize check for TURBO
// - TURBO support for multiple dice
// - TURBO at end of game think of value for next game (Alice)
// - does not properly play consecutive games (e.g. not carrying over RESERVE action or clearing SwingDiceSet())
// - TRIP: looks like rerolling effects are only being applied for attacking dice and not tripped dice
// AI TODO:
// * apply same culling logic to other moves. Modularize code?
// * transposition table/stratification - seeing a lot of dup cases in later plies
// * stratify
// - stratify: attack rerolls, chance rolls, initiative roll, GetSetSwingAction(), TURBO SWING
// - don't recursively do BMAI (ply>1) if top-level action is reserve, or standalone swing
// - optimize QAI by not completely applying attack in eval?
// - optimize simulations by breaking up ApplyAttack into ApplyAttackPre, SimAttackRoll, ApplyAttackPost
// - QAI: bonus for rerolling a vulnerable die
// - sim: recognize endgame conditions
// - sim: recognize moves that we have already seen, slowly build up chance of winning as previously seen that move (?)
// - FOCUS: if only takes the minimal focus step, only goes 2 ply so 3rd "focus action" will be a PASS
//--------------------------------------------------------------------------------------------------------
// DICE HANDLED: (,Xpzsdqt?n/B^Hhrocmf`wug)
// - twin, swing, poison, speed, shadow, stealth, queer, trip, mood, null, option, berserk,
// time and space, mighty, weak, reserve, ornery, chance, morphing, focus,
// warrior, slow, unique, stinger
//
// PROPRIETARY DICE HANDLED: (~)
// - unskilled
// DICE REMAINING: (most worthwhile +FvGk) (+DC%GF@vk)
// k KONSTANT: (partial support)
// done: cannot power attack, cannot perform 1 die skill attack, does not reroll
// todo: can add or subtract in a skill attack
// + AUX: if both players have one and agree, they use them. If one player doesn't and both
// players agree, then you get the same die as the opponent.
// D DOPPLEGANGER: if doing a power attack, att_die becomes exact copy of tgt_die (still rerolls)
// C WILDCARD:
// % RADIOACTIVE: if attacks, decays into two dice (not radioactive, half sides - ROUND?)
// if attacked, all that attack it decay (lose RADIOACTIVE, TURBO, MOOD)
// G RAGE: when captured, owner of the RAGE adds equal die minus RAGE ability. Not counted for initiative
// F FIRE: cannot power, can increase value of other dice in skill or
// power attack by decreasing value of powre die.
// @ CHAOTIC: ? (fanatics)
// v VALUE: scored as if sides was number it was showing. Store this value when captured
// SPECIAL: cannot SKILL against Japanese Beetle
//--------------------------------------------------------------------------------------------------------
// SETS CAN PLAY:
// - 1999 Rare-Promo, 2000 Rare-Promo, *
// - 2002 Rare-Promo, 2003 Rare-Promo, *
// - Brawl, Brom, Bruno!, *
// - Chicago Crew, Club Foglio, Diceland, Dork Victory, Fairies, *
// - Fantasy, Four Horsemen, Freaks, *
// - Geekz, *
// - Howling Wolf, Iron Chef, *
// - Legend of the Five Rings, Lunch Money, Majesty, *
// - Presidential Button Men, Renaissance, Sailor Moon, Sailor Moon 2, Samurai, Sanctum, Save the Ogres,
// Sluggy Freelance, Soldiers, *
// - Tenchi Muyo, The Metamorphers, Vampyres, *
// - Wonderland, Yoyodyne
// CHECK:
// SETS CANT PLAY: (* = TL, ? = haven't verified dice)
// - * 2001 Rare-Promo (G)
// - 7 Deadly Sins (D)
// - Amber (D)
// - Avatars (...)
// - * Button Brains (k)
// - * Button Lords (+)
// - Fanatics (...)
// - Free Radicals (D)
// - Hodge Podge (v@DGF, 0-sided)
// - It Came From the 80's (G+%)
// - * Japanese Beetle (special rules - Beetle)
// - Las Vegas (C)
// - Moody Mutants (%J)
// - * Polycon (F)
// - Radioactive (%)
// - Space Girlz (plasma)
// - Victorian Horror (vk)
// RIP SETS WAS ABLE TO PLAY:
// - BladeMasters, Dr. Oche
// Game Stages and Valid Actions: [outdated]
// BME_PHASE_PREROUND: p0/p1 must both do (depending on state)
// BME_ACTION_SET_SWING_AND_OPTION,
// BME_PHASE_INITIATIVE: first CHANCE then FOCUS
// BME_PHASE_INITIATIVE_CHANCE: alternately give the non-initiative player the opporutnity to reroll one CHANCE die
// BME_ACTION_USE_CHANCE
// BME_ACTION_PASS
// BME_PHASE_INITIATIVE_FOCUS: alternates give the non-initiative player the opportunity to use FOCUS dice
// BME_ACTION_USE_FOCUS,
// BME_ACTION_PASS
// BME_PHASE_FIGHT: alternates p0/p1 until game over
// BME_ACTION_ATTACK,
// BME_ACTION_PASS
// BME_ACTION_SURRENDER
//--------------------------------------------------------------------------------------------------------
// LOW PRIORITY
// QUESTIONS (can test?)
// - reset SWING/OPTION in a TIE? RESERVE in a TIE? [normally no - site allows changing swing after 3 ties]
// - allowing skill only with 2 dice, but speed/berserk with 1. [skill is allowed with 1]
// - for TIME AND SPACE, works if *either* dice in a twin is odd?
// - rules for MOOD SWING on 'U'? or is MOOD SWING on X/V also uniform dist?
// - INITIATIVE: handle ties? [currently gives to player 0]
// - ORNERY causes MIGHTY(y)/WEAK(y)/MOOD(y)/TURBO(y)?
// - CHANCE causes ?Hh!? (assuming no)
// - MORPHING SPEED/BERSERK or TURBO works? (not allowing)
// - MORPHING MIGHTY/WEAK order? (applying morphing after)
// - WARRIOR: worth 0 when has skill, still worth 0 once loses? (assuming no)
// - KONSTANT: reroll on trip?
// LOW PRIORITY TODO:
// - clean up logging system
// - don't know "last_action". Needs to be communicated from bmai.pl to know if a pass ends game
// - BMAI3: try to increase sims after culling. I.e., right now it determines sims immediately
// for 'x' moves based on 'branch'. But we are culling moves so can reevaluate sims to
// target original 'branch'.
// SPECIAL RULES (official)
// - INITIATIVE: if run out of dice, initiative goes to player with extra dice
//--------------------------------------------------------------------------------------------------------
// includes
#include "bmai_lib.h"
#include <cstdio>
#include "BMC_Logger.h"
#include "BMC_Parser.h"
#include "BMC_Stats.h"
///////////////////////////////////////////////////////////////////////////////////////////
// main
///////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
g_stats.OnAppStarted();
// set up logging
// - disable in release build (for ZOM)
// drp051401 - reenabled in RELEASE since using for BMAI
#ifndef _DEBUG
//g_logger.SetLogging(BME_DEBUG_SIMULATION, false);
g_logger.SetLogging(BME_DEBUG_BMAI, false);
#endif
g_logger.SetLogging(BME_DEBUG_ROUND, false);
g_logger.SetLogging(BME_DEBUG_QAI, false);
//g_ai_mode1b.SetP(0.5);
//g_ai.SetQAI(&g_ai_mode0);
// banner
printf("BMAI: the Button Men AI\nCopyright © 2001-2024, Denis Papp.\nFor information, contact Denis Papp, [email protected]\nVersion: %s\n", GIT_DESCRIBE);
BMC_Parser parser;
if (argc>1)
{
FILE *fp = fopen(argv[1],"r");
if (!fp)
{
printf("could not open %s", argv[1]);
return 1;
}
printf("Reading from %s\n", argv[1]);
parser.ParseFile(fp);
fclose(fp);
}
else
{
parser.ParseStdIn();
}
//g_stats.DisplayStats();
return 0;
}