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
|
#include "MrboomHelper.hpp"
#include "Bot.hpp"
#include "BotTree.hpp"
#ifdef IOS
void std::__throw_out_of_range(char const *)
{
}
#endif
class ConditionNode : public bt::Node
{
public:
ConditionNode(Bot *bot) : Node(), bot(bot)
{
}
void Initialize()
{
}
virtual bool Condition() = 0;
bt::Status Update()
{
if (Condition())
{
return(bt::Success);
}
return(bt::Failure);
}
protected:
Bot *bot;
};
class MoveToNode : public bt::Node
{
public:
MoveToNode(Bot *bot) : Node(), bot(bot)
{
}
void Initialize()
{
}
virtual int Cell() = 0;
bt::Status Update()
{
int cell = Cell();
if (cell == -1)
{
if (isInMiddleOfCell(bot->_playerIndex))
{
bot->stopWalking();
}
return(bt::Failure);
}
if (((!(isInMiddleOfCell(bot->_playerIndex) && bot->getCurrentCell() == cell))) || (bot->getCurrentCell() != cell))
{
if (bot->walkToCell(cell))
{
return(bt::Running);
}
if (tracesDecisions(bot->_playerIndex))
{
log_debug("BOTTREEDECISIONS: %d/%d:Failed to go to %d (%d/%d)\n", frameNumber(), bot->_playerIndex, cell, CELLX(cell), CELLY(cell));
}
return(bt::Failure);
}
bot->stopWalking();
if (tracesDecisions(bot->_playerIndex))
{
log_debug("BOTTREEDECISIONS: %d/%d:stopWalking arrived in %d (%d/%d)\n", frameNumber(), bot->_playerIndex, cell, CELLX(cell), CELLY(cell));
}
return(bt::Success);
}
protected:
Bot *bot;
};
class MoveToBonus : public MoveToNode
{
public:
MoveToBonus(Bot *bot) : MoveToNode(bot)
{
}
int Cell()
{
int bestCell = bot->bestBonusCell();
#ifdef DEBUG
botStates[bot->_playerIndex] = goingBonus;
#endif
if (tracesDecisions(bot->_playerIndex))
{
log_debug("BOTTREEDECISIONS: %d/%d:gotoBonus:%d (%d/%d) current=%d (%d/%d)\n", frameNumber(), bot->_playerIndex, bestCell, CELLX(bestCell), CELLY(bestCell), bot->getCurrentCell(), CELLX(bot->getCurrentCell()), CELLY(bot->getCurrentCell()));
}
return(bestCell);
}
};
class MoveToBombBestBombCell : public MoveToNode
{
public:
MoveToBombBestBombCell(Bot *bot) : MoveToNode(bot)
{
}
int Cell()
{
int bestCell = bot->bestCellToDropABomb();
#ifdef DEBUG
botStates[bot->_playerIndex] = goingBomb;
#endif
if (tracesDecisions(bot->_playerIndex))
{
log_debug("BOTTREEDECISIONS: %d/%d:gotoBestBombCell:%d (%d/%d) current=%d (%d/%d)\n", frameNumber(), bot->_playerIndex, bestCell, CELLX(bestCell), CELLY(bestCell), bot->getCurrentCell(), CELLX(bot->getCurrentCell()), CELLY(bot->getCurrentCell()));
}
return(bestCell);
}
};
class MoveToSafeCell : public MoveToNode
{
public:
MoveToSafeCell(Bot *bot) : MoveToNode(bot)
{
}
int Cell()
{
int bestCell = bot->bestSafeCell();
#ifdef DEBUG
botStates[bot->_playerIndex] = goingSafe;
#endif
if (tracesDecisions(bot->_playerIndex))
{
log_debug("BOTTREEDECISIONS: %d/%d:gotoBestSafeCell:%d (%d/%d) current=%d (%d/%d)\n", frameNumber(), bot->_playerIndex, bestCell, CELLX(bestCell), CELLY(bestCell), bot->getCurrentCell(), CELLX(bot->getCurrentCell()), CELLY(bot->getCurrentCell()));
}
return(bestCell);
}
};
class ConditionBombsLeft : public ConditionNode
{
public:
ConditionBombsLeft(Bot *bot) : ConditionNode(bot)
{
}
bool Condition()
{
// condition "i have more bombs"
int howManyBombs = bot->howManyBombsLeft();
if (tracesDecisions(bot->_playerIndex))
{
log_debug("BOTTREEDECISIONS: %d/%d:bombLeft:%d\n", frameNumber(), bot->_playerIndex, howManyBombs);
}
return(howManyBombs);
}
};
class ConditionDropBomb : public ConditionNode
{
public:
ConditionDropBomb(Bot *bot) : ConditionNode(bot)
{
}
bool Condition()
{
bot->startPushingBombDropButton(); //TOFIX ? return false or running ?
if (tracesDecisions(bot->_playerIndex))
{
log_debug("BOTTREEDECISIONS: %d/%d:dropBomb\n", frameNumber(), bot->_playerIndex);
}
return(true);
}
};
BotTree::BotTree(int playerIndex) : Bot(playerIndex)
{
tree = new bt::BehaviorTree();
MoveToBonus * gotoBonus = new MoveToBonus(this);
bt::Sequence *bombSeq = new bt::Sequence();
ConditionBombsLeft *bombLeft = new ConditionBombsLeft(this);
bombSeq->AddChild(bombLeft);
MoveToBombBestBombCell *gotoBestBombCell = new MoveToBombBestBombCell(this);
bombSeq->AddChild(gotoBestBombCell);
ConditionDropBomb *dropBomb = new ConditionDropBomb(this);
bombSeq->AddChild(dropBomb);
MoveToSafeCell *gotoSafePlace = new MoveToSafeCell(this);
bt::Selector * rootNode = new bt::Selector();
rootNode->AddChild(gotoBonus);
rootNode->AddChild(bombSeq);
rootNode->AddChild(gotoSafePlace);
tree->SetRoot(rootNode);
}
void BotTree::updateGrids()
{
updateFlameAndDangerGridsWithBombs(_playerIndex, flameGrid, dangerGrid);
updateDangerGridWithMonstersSickPlayersAndCulDeSacs(_playerIndex, dangerGrid);
updateDangerGridWithMonster4CellsTerritories(dangerGrid);
updateMonsterIsComingGrid(monsterIsComingGrid);
updateTravelGrid(_playerIndex, false, travelGrid, flameGrid, noDangerGrid);
updateTravelGrid(_playerIndex, false, travelSafeGrid, flameGrid, dangerGrid);
#ifdef DEBUG
printGrid();
#endif
if (!((frameNumber() + _playerIndex) % nb_dyna))
{
calculatedBestCellToPickUpBonus = calculateBestCellToPickUpBonus();
}
updateBestExplosionGrid(_playerIndex, bestExplosionsGrid, travelGrid, flameGrid, dangerGrid);
}
void BotTree::tick()
{
stopPushingRemoteButton();
stopPushingBombDropButton();
stopPushingJumpButton();
tree->Update();
if (monsterIsComingGrid[cellPlayer(_playerIndex)])
{
startPushingBombDropButton();
}
if (isSomewhatInTheMiddleOfCell() && frameNumber() % 2 && pushingDropBombButton == false)
{
bool cantPlaceMoreBombsAndSafe = (((bestCellToDropABomb() == -1) || (howManyBombsLeft() == 0)) && amISafe());
if (cantPlaceMoreBombsAndSafe || shouldActivateRemote(_playerIndex))
{
startPushingRemoteButton();
}
}
}
// filled by serialize...
static size_t serializeSize = 0;
size_t BotTree::serialize_size(void)
{
#ifndef ONLY_LOCAL
if (serializeSize == 0)
{
uint8_t tmpBuffer[MEM_STREAM_BUFFER_SIZE];
serialize(tmpBuffer);
log_error("HARDCODED_RETRO_SERIALIZE_SIZE=SIZE_SER+%d*8\n", serializeSize);
}
assert(serializeSize != 0);
#endif
return(serializeSize);
}
bool BotTree::serialize(void *data_)
{
#ifndef ONLY_LOCAL
memstream_set_buffer(buffer, MEM_STREAM_BUFFER_SIZE);
static memstream_t *stream = memstream_open(1);
assert(stream != NULL);
memstream_rewind(stream);
assert(tree != NULL);
tree->serialize(stream); // write to the stream
memstream_write(stream, &calculatedBestCellToPickUpBonus, sizeof(calculatedBestCellToPickUpBonus)); // write to the stream
memstream_write(stream, &_direction1FrameAgo, sizeof(_direction1FrameAgo)); // write to the stream
memstream_write(stream, &_direction2FramesAgo, sizeof(_direction2FramesAgo)); // write to the stream
memstream_write(stream, &_shiveringCounter, sizeof(_shiveringCounter)); // write to the stream
serializeSize = memstream_pos(stream);
memstream_rewind(stream);
memstream_read(stream, data_, serializeSize); // read from the stream
#endif
return(true);
}
bool BotTree::unserialize(const void *data_)
{
#ifndef ONLY_LOCAL
memstream_set_buffer(buffer, MEM_STREAM_BUFFER_SIZE);
static memstream_t *stream = memstream_open(1);
assert(stream != NULL);
memstream_rewind(stream);
memstream_write(stream, data_, serialize_size()); // write to the stream
memstream_rewind(stream);
assert(tree != NULL);
tree->unserialize(stream);
memstream_read(stream, &calculatedBestCellToPickUpBonus, sizeof(calculatedBestCellToPickUpBonus)); // read from the stream
memstream_read(stream, &_direction1FrameAgo, sizeof(_direction1FrameAgo)); // write to the stream
memstream_read(stream, &_direction2FramesAgo, sizeof(_direction2FramesAgo)); // write to the stream
memstream_read(stream, &_shiveringCounter, sizeof(_shiveringCounter)); // write to the stream
#endif
return(true);
}
|