-
Notifications
You must be signed in to change notification settings - Fork 1
/
Phases.h
256 lines (236 loc) · 7.71 KB
/
Phases.h
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
#pragma once
#include "KB.h"
extern int g_GameState;
extern int g_turns;
extern MapTile g_gameMap[g_mapWidth][g_mapHeight];
void UpdateCharPos( CharInfo& charInfo, const int x, const int y )
{
g_gameMap[charInfo._pos[0]][charInfo._pos[1]]._occupant = CharInfo();
g_gameMap[x][y]._occupant = charInfo;
charInfo._pos = { x,y };
}
namespace Phases
{
void evalGameState( CharInfoMap& charInfos )
{
using namespace std;
using namespace castor;
//check: exceeded max amount of turns
if ( exceededMaxTurns( g_turns )( ) )
{
g_GameState = -1;
return;
}
//check: if one team all dead
{
lref<string> deadTeamName;
relation sTAD = someTeamAllDead( charInfos, deadTeamName );
if ( sTAD() )
{
g_GameState = deadTeamName.get() == "A" ? 2 : 1;
return;
}
}
}
int tileIsDangerous (CharInfoMap& charInfos, CharInfo& curChar, castor::lref<std::string> oppTeamName)
{
using namespace std;
using namespace castor;
lref<string> enemyName;
relation enemyInfo = onTeam( enemyName, oppTeamName, { }, { }, { } );
int danger = 0;
while ( enemyInfo() )
{
if ( charInfos.find( enemyName.get() ) != charInfos.end() )
{
auto& curEnemy = charInfos[enemyName.get()];
relation canAttack = playerCanAttack( curChar._pos[0], curChar._pos[1], curEnemy._pos[0], curEnemy._pos[1] );
if ( canAttack() )
{
danger += curEnemy._attack;
}
}
}
if (danger >= curChar._hp)
{
return 1;
}
else
{
return 0;
}
}
void move_moveOnMap( CharInfoMap& charInfos, CharInfo& curChar )
{
using namespace std;
using namespace castor;
lref<int> adjPosX, adjPosY;
//get total possible moveable points and choose random
relation canMoveTo = playerCanMoveTo( charInfos, curChar._pos[0], curChar._pos[1], adjPosX, adjPosY );
auto i = 0, random_var = 0;
{
while ( canMoveTo() )
{
if ( !cellIsOccupied( charInfos, adjPosX.get(), adjPosY.get() ) )
++i;
}
if ( i == 0 )
{
cout << curChar._name << " cannot move anywhere\n";
return;
}
adjPosX.reset();
adjPosY.reset();
std::srand( std::time( 0 ) ); // use current time as seed for random generator
random_var = std::rand() % i;
}
//actually make the movement
i = 0;
lref<TerrainType> terrainType;
relation canMoveTo2 = playerCanMoveTo( charInfos, curChar._pos[0], curChar._pos[1], adjPosX, adjPosY, terrainType );
while ( canMoveTo2() )
{
if ( !cellIsOccupied( charInfos, adjPosX.get(), adjPosY.get() ) )
{
if ( i == random_var )
{
cout << string_format( "%s(team:%s)[%i,%i] has moved to:[%i,%i]",
curChar._name.c_str(), curChar._teamName.c_str(),
curChar._pos[0], curChar._pos[1], adjPosX.get(), adjPosY.get()
) << endl;
//update character position
UpdateCharPos( curChar, adjPosX.get(), adjPosY.get() );
break;
}
++i;
}
}
}
bool move_atkIfPossible( CharInfoMap& charInfos, CharInfo& curChar, std::string oppTeamName, std::string suffix="" )
{
using namespace std;
using namespace castor;
lref<string> enemyName;
relation enemyInfo = onTeam( enemyName, oppTeamName, { }, { }, { } );
while ( enemyInfo() )
{
if ( charInfos.find( enemyName.get() ) != charInfos.end() )
{
//note: 'while' is used so that we keep looping until we find an enemy we can attack and hp > 0
auto& curEnemy = charInfos[enemyName.get()];
relation canAttack = playerCanAttack( curChar._pos[0], curChar._pos[1], curEnemy._pos[0], curEnemy._pos[1] );
if ( canAttack() && curEnemy._hp > 0 )
{
//calculate damage
float attack = static_cast<float>( curChar._attack );
float attackMultiplier = 1.0f;
{
lref<float> multiplier;
lref<string> atkWeapon, defWeapon;
relation charWeapon = characterHeldWeapon( curChar._name, atkWeapon );
relation enemyWeapon = characterHeldWeapon( enemyName, defWeapon );
if ( charWeapon() && enemyWeapon() )
{
relation weaponMultiplier = weaponEffectOnWeapon( atkWeapon, defWeapon, multiplier );
if ( weaponMultiplier() )
attackMultiplier = multiplier.get();
}
}
curEnemy._hp -= (attack * attackMultiplier);
cout << string_format( "%s(team:%s[%i,%i]) atkd %s(team:%s, rem-hp:%.1f)[%i,%i] %s \n",
curChar._name.c_str(), curChar._teamName.c_str(),
curChar._pos[0], curChar._pos[1],
enemyName.get().c_str(), oppTeamName.c_str(), curEnemy._hp,
curEnemy._pos[0], curEnemy._pos[1],
suffix.c_str());
lref<string> msg;
relation atkEffectMsg = weaponEffectMsg( attackMultiplier, msg );
if ( atkEffectMsg() )
cout << msg.get();
//print appropriate dialogue
{
lref<string> dialogue;
relation charAtkDialogue = characterAtkDialogue( curChar._name, dialogue );
if ( charAtkDialogue() )
cout << dialogue.get();
lref<string> dialogue2;
relation charAtkdDialogue = characterAtkdDialogue( curEnemy._name, dialogue2 );
if ( charAtkdDialogue() )
cout << dialogue2.get();
}
if ( curEnemy._hp <= 0 )
{
//if enemy is dead, remove it from the map
cout << string_format( "-- %s died! -- \n", curEnemy._name.c_str() );
g_gameMap[curEnemy._pos[0]][curEnemy._pos[1]]._occupant = CharInfo();
lref<string> dialogue3;
relation charDeathDialogue = characterDeathDialogue( curEnemy._name, dialogue3 );
if ( charDeathDialogue() )
cout << dialogue3.get();
}
return true;
}
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
void fightPhase( CharInfoMap& charInfos )
{
using namespace std;
using namespace castor;
lref<string> charName, teamName, enemyName;
lref<int> hp, attack, movement;
//go through each team
lref<string> curTeamName, oppTeamName;
relation teams = teamNames( curTeamName, oppTeamName );
while ( teams() )
{
//go through all players in each team
charName.reset();
relation characterInfo = onTeam( charName, curTeamName, { }, { }, { } );
while ( characterInfo() )
{
if ( charInfos.find( charName.get() ) != charInfos.end() )
{
auto& curChar = charInfos[charName.get()];
if ( curChar._hp > 0 )
{
lref<StrategyType> strategyType;
relation charStratType = characterStrategyType( charName, strategyType );
if ( charStratType() )
{
//attack an enemy team player
//strategy
//relation canAttackImmediately = existAdjacentEnemies(curChar._pos[0], curChar._pos[1], oppTeamName);
//if ( !canAttackImmediately())
{
bool attackFinished = false;
if ( (strategyType.get() == StrategyType::calculated) && !tileIsDangerous( charInfos, curChar, oppTeamName))
{
//If current spot is safe, look for an enemy to attack, then move if none is found. If unsafe just move.
if ( move_atkIfPossible( charInfos, curChar, oppTeamName.get(), "[CALCULATED ATTACK]" ))
attackFinished = true;
}
else if ( strategyType.get() == StrategyType::aggressive )
{
if ( move_atkIfPossible( charInfos, curChar, oppTeamName.get(), "[AGGRESSIVE ATTACK]" ) )
attackFinished = true;
}
//[MOVE 1. Move to an adjacent position on the map if there is not already an enemy in attack range (adjacent).
if ( !attackFinished )
move_moveOnMap( charInfos, curChar );
//[MOVE 2. look for an enemy that is adjacent => attack]
if ( !attackFinished && strategyType.get() != StrategyType::run )
move_atkIfPossible( charInfos, curChar, oppTeamName.get() );
evalGameState( charInfos );
if ( g_GameState != 0 )
return;
}
}
}
}
}
}
}
};