-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
73 lines (54 loc) · 1.92 KB
/
main.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
#include "common/Thread.hpp"
#include "common/ErrnoCheck.h"
#include "fheroes2/battle/battle_arena.h"
#include "fheroes2/army/army.h"
#include "fheroes2/world/world.h"
#include "fheroes2/system/settings.h"
#include "engine/rand.h"
int timesArmy1WonTotal = 0;
int timesArmy2WonTotal = 0;
Thread::ResultType simulateBattles(Thread* unused) {
int timesArmy1Won = 0;
int timesArmy2Won = 0;
World::Get().NewMaps( 10, 10 );
Settings & conf = Settings::Get();
conf.GetPlayers().Init( Color::ALL );
conf.GetPlayers().Get(Color::BLUE)->SetControl(CONTROL_AI);
conf.GetPlayers().Get(Color::GREEN)->SetControl(CONTROL_AI);
World::Get().GetKingdom(Color::BLUE).SetAiType(AI::AI_TYPE::AI_DENIS);
World::Get().GetKingdom(Color::GREEN).SetAiType(AI::AI_TYPE::NORMAL);
for (int i = 0; i < 100 * 1000; ++i) {
// Army1 is weaker but makes the first move.
Army army1;
army1.GetTroop(0)->Set(Monster::PEASANT, 821);
army1.GetTroop(1)->Set(Monster::ARCHER, 50);
army1.SetColor(Color::BLUE);
Army army2;
army2.GetTroop(0)->Set(Monster::PEASANT, 1001);
army2.GetTroop(1)->Set(Monster::ARCHER, 50);
army2.SetColor(Color::GREEN);
Battle::Result result = Battle::Loader(army1, army2, /* mapsindex= */ 0);
if (result.AttackerWins()) {
++timesArmy1Won;
} else {
++timesArmy2Won;
}
}
__atomic_add_fetch(×Army1WonTotal, timesArmy1Won, __ATOMIC_RELAXED);
__atomic_add_fetch(×Army2WonTotal, timesArmy2Won, __ATOMIC_RELAXED);
return Thread::successCode();
}
int main() {
clearErrno();
// populates monster data to avoid race condition doing so later.
fheroes2::getMonsterData(0);
Thread threads[16];
for (int i = 0; i < 16; ++i) {
threads[i].start(&threads[i], &simulateBattles);
}
for (int i = 0; i < 16; ++i) {
threads[i].join();
}
printf("timesArmy1Won: %d, timesArmy2Won: %d\n", timesArmy1WonTotal, timesArmy2WonTotal);
return 0;
}