Skip to content

Commit

Permalink
+ CMake: reduce required from 3.14 to 3.8 to support VS2017
Browse files Browse the repository at this point in the history
+ Source:
- moved necessary STD headers used by bmai_ap.cpp to principal header bmai.h
- fixed some cast warnings. These are all acceptable loss of precision.
+ Mechanics
- Twin dice: the "Dice()" method was incorrectly casting the return value of 1/2 to a "bool".  That will not work in certain compilers. Fixed to return integer.
- Morphing: fixed so rule doesn't run if not doing an attack. This was triggering an assert
+ AI
- GetAttackAction(): fixed several uninitialized variables, pariticularly "score". In a release build this could have resulted in effectively arbitrary scores, therefore resulting in incorrect actions. In a debug build, this would have had very odd looking scores but would have worked.
+ Tests:
- bug11_in.txt from github issues
  • Loading branch information
aq-denis committed Feb 25, 2021
1 parent 15aba3c commit d8913b0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
cmake_minimum_required(VERSION 3.14)

project(bmai)
# drp022521 - reduced from 3.14 to 3.8 for VS2017 support
cmake_minimum_required (VERSION 3.8)

set(CMAKE_CXX_STANDARD 14)

Expand Down
12 changes: 5 additions & 7 deletions src/bmai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,6 @@
#include <cmath> // for fabs()
#include <cstdarg> // for va_start() va_end()

// to help make a cross-platform case insensitive compare
// we need transform and string
#include <algorithm>
#include <string>

// some compilers want to be very explicit
#include <cstring> // string functions on std namespace

Expand Down Expand Up @@ -474,7 +469,7 @@ BMC_RNG::BMC_RNG() :
void BMC_RNG::SRand(UINT _seed)
{
if (_seed==0)
_seed = time(NULL);
_seed = (UINT)time(NULL);

// bad arbitrary way of tweaking bad seed values
if (!_seed) m_seed=-1;
Expand Down Expand Up @@ -867,7 +862,8 @@ void BMC_Die::OnApplyAttackPlayer(BMC_Move &_move, BMC_Player *_owner, bool _act
OnBeforeRollInGame(_owner);

// MORPHING - assume same size as target
if (HasProperty(BME_PROPERTY_MORPHING))
// drp022521 - this rule only applies if actually going to capture a die
if (HasProperty(BME_PROPERTY_MORPHING) && _move.m_attack != BME_ATTACK_INVALID)
{
BM_ASSERT(g_attack_type[_move.m_attack]==BME_ATTACK_TYPE_1_1 || g_attack_type[_move.m_attack]==BME_ATTACK_TYPE_N_1);
BMC_Player *target = _move.m_game->GetPlayer(_move.m_target_player);
Expand Down Expand Up @@ -4134,7 +4130,9 @@ int main(int argc, char *argv[])
fclose(fp);
}
else
{
g_parser.Parse();
}

//g_stats.DisplayStats();

Expand Down
11 changes: 9 additions & 2 deletions src/bmai.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
// some compilers want to be very explicit
#include <cstdio> // FILE

// to help make a cross-platform case insensitive compare
// we need transform and string
// drp022521 - moved to main header
#include <algorithm>
#include <string>

// forward declarations
class BMC_Player;
class BMC_Game;
Expand Down Expand Up @@ -342,7 +348,7 @@ class BMC_MoveList
~BMC_MoveList() { Clear(); }
void Clear();
void Add(BMC_Move & _move );
INT Size() { return list.size(); }
INT Size() { return (INT)list.size(); }
BMC_Move * Get(INT _i) { return &list[_i]; }
bool Empty() { return Size()<1; }
void Remove(int _index);
Expand Down Expand Up @@ -399,7 +405,8 @@ class BMC_DieData
bool HasProperty(INT _p) { return m_properties & _p; }
BME_SWING GetSwingType(INT _d) { return (BME_SWING)m_swing_type[_d]; }
bool Valid() { return (m_properties & BME_PROPERTY_VALID); }
bool Dice() { return (m_properties & BME_PROPERTY_TWIN) ? 2 : 1; }
// drp022521 - fixed to return INT instead of bool
INT Dice() { return (m_properties & BME_PROPERTY_TWIN) ? 2 : 1; }
INT GetSides(INT _t) { return m_sides[_t]; }

protected:
Expand Down
9 changes: 5 additions & 4 deletions src/bmai_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ void BMC_QAI::GetAttackAction(BMC_Game *_game, BMC_Move &_move)
INT i,j;
BMC_Game sim(true);
BMC_Move * best_move = NULL;
float best_score, score, delta;
// drp022521 - fix uninitialized variables
float best_score = 0, score = 0, delta = 0;
BMC_Player *attacker = _game->GetPhasePlayer();
BMC_Die * die;

Expand All @@ -103,7 +104,7 @@ void BMC_QAI::GetAttackAction(BMC_Game *_game, BMC_Move &_move)
case BME_ATTACK_TYPE_1_1:
case BME_ATTACK_TYPE_1_N:
die = attacker->GetDie(attack->m_attacker);
delta = (die->GetSidesMax() + 1) * 0.5 - die->GetValueTotal();
delta = (die->GetSidesMax() + 1) * 0.5f - (float)die->GetValueTotal();
if ( die->HasProperty(BME_PROPERTY_SHADOW))
score += 0;
else
Expand All @@ -118,7 +119,7 @@ void BMC_QAI::GetAttackAction(BMC_Game *_game, BMC_Move &_move)
if (!attack->m_attackers.IsSet(j))
continue;
die = attacker->GetDie(j);
delta = (die->GetSidesMax() + 1) * 0.5 - die->GetValueTotal();
delta = (die->GetSidesMax() + 1) * 0.5f - (float)die->GetValueTotal();
if ( die->HasProperty(BME_PROPERTY_SHADOW))
score += 0;
else
Expand Down Expand Up @@ -467,7 +468,7 @@ void BMC_BMAI::OnPostSimulation(BMC_Game *_game, INT _enter_level)
INT BMC_BMAI::ComputeNumberSims(INT _moves)
{
float decay_factor = (float)pow(s_ply_decay, sm_level-1);
INT sims = m_max_branch * decay_factor / _moves;
INT sims = (INT)(m_max_branch * decay_factor / (float)_moves);

int adjusted_min_sims = (int)(m_min_sims * decay_factor + 0.99f);
if (adjusted_min_sims<1)
Expand Down
17 changes: 17 additions & 0 deletions test/bug11_in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
game
preround
player 0 3 0
W
om34
T
player 1 5 0
^10
s11
d12
z13
X
ply 3
max_sims 100
min_sims 5
maxbranch 400
getaction
4 changes: 2 additions & 2 deletions web/bmai_run.pl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# HACK
# Task Scheduler: doesn't have Y: drive mapped and starts in "C:\Windows"
system "net use y: \\\\fnord\\denis";
system "net use y: \\\\NAS\\SHARE";
chdir("y:\\dev\\bmai");

&log("run.pl started\n");
Expand All @@ -30,7 +30,7 @@ sub log

open(F,">>test.log");
open(F2,">>y:/dev/bmai/test2.log");
open(F3,">>\\\\fnord\\denis\\dev\\bmai\\test3.log");
open(F3,">>\\\\NAS\\SHARE\\dev\\bmai\\test3.log");
print F "$msg";
print F2 "$msg";
print F3 "$msg";
Expand Down

0 comments on commit d8913b0

Please sign in to comment.