vector<Unit*> Group::unitsAdjacentTo(int x, int y) const { vector<Unit*> ret; if (x - 1 >= 0 && hasUnitAt(x - 1, y)) ret.push_back(getUnitAt(x - 1, y)); if (y - 1 >= 0 && hasUnitAt(x, y - 1)) ret.push_back(getUnitAt(x, y - 1)); if (x + 1 <= 2 && hasUnitAt(x + 1, y)) ret.push_back(getUnitAt(x + 1, y)); if (y + 1 <= 2 && hasUnitAt(x, y + 1)) ret.push_back(getUnitAt(x, y + 1)); return ret; }
void World::healUnitArea(int src,int x,int y,int range,int amount,int flags) { int myowner = units.at(src).Owner; for(int i = x-range;i<=x+range;i++) { for(int j = y-range;j<=y+range;j++) { int uid = getUnitAt(i,j); if(uid!=-1 && (uid!=src || !(flags&TARGET_NONSELF))) { if(isUnitAvatar(uid)==0 || !(flags&TARGET_NONAVATAR)) { int owner = units.at(uid).Owner; if(flags&TARGET_ALLY && !(flags&TARGET_ENEMY) && owner==myowner) //damage only allies healUnit(src,uid,amount); else if(!(flags&TARGET_ALLY) && flags&TARGET_ENEMY && owner!=myowner) //damage only enemies healUnit(src,uid,amount); else if(!(flags&TARGET_ALLY) && !(flags&TARGET_ENEMY)) //damage both healUnit(src,uid,amount); else if(flags&TARGET_ALLY && flags&TARGET_ENEMY) //damage both healUnit(src,uid,amount); } } } } }
IAsset* AssetCollection::getAssetAt(const unsigned short x, const unsigned short y) { IAsset* result = NULL; // buildings have higher return priority result = getBuildingAt(x, y); if(result) return result; // try for unit result = getUnitAt(x, y); return result; }
std::vector<int> GameState::getAdjEnemies(int id, int aIndex) const { const auto &unit = getUnit(id); if (!unit.isAlive()) return {}; std::vector<int> enemies; for (auto n : grid_.aryNeighbors(aIndex)) { const auto &adjUnit = getUnitAt(n); if (adjUnit.isAlive() && unit.isEnemy(adjUnit)) { enemies.push_back(adjUnit.entityId); } } return enemies; }
//returns true if captured, false otherwise bool Map::captureStructure(uint8 x, uint8 y){ Unit *unit = getUnitAt(x, y); Terrain *captureTerrain = getTerrainAt(x, y); if (!unit->isActive() || !captureTerrain->canBeCaptured()){ return false; } bool captured = unit->capture(); unit->deactivate(); //Set owner if captured if (captured){ m_terrain[x][y]->setOwner(unit->getOwner()); } return captured; }
bool GameState::isHexOpen(int aIndex) const { if (grid_.offGrid(aIndex)) return false; return !getUnitAt(aIndex).isAlive(); }
int World::handleMessage(Message& msg) { std::string type = msg.getString("msgtype"); if(type=="startturn") { int plyr = msg.getInt("player"); ManaCap[plyr] += 1; Mana[plyr] = ManaCap[plyr]; for(int i = 0;i<VerticalTileCount;i++) { for(int j = 0;j<HorizontalTileCount;j++) { int id = getUnitAt(j,i); if(id!=-1) { if(plyr==units.at(id).Owner) { for(int k = 0;k<units.at(id).abilityCount;k++) //lower cooldowns { if(units.at(id).abilities.at(k).currCooldown>0) { units.at(id).abilities.at(k).currCooldown -= 1; } } if(getUnitCanReady(id)) { Message msg("unitready"); msg.addValue("unit",std::to_string(id)); MsgMngr.sendMessage(msg); } } } } } } else if(type=="endturn") { int plyr = msg.getInt("player"); for(int i = 0;i<VerticalTileCount;i++) { for(int j = 0;j<HorizontalTileCount;j++) { int id = getUnitAt(j,i); if(id!=-1) { for(int i = 0;i<units.at(id).buffs.size();i++) { if(units.at(id).buffs.at(i).Duration!=-1) { units.at(id).buffs.at(i).Duration--; //reduce buff duration if(units.at(id).buffs.at(i).Duration==0) { units.at(id).buffs.erase(units.at(id).buffs.begin()+i); i--; } } } if(units.at(id).timedLife>0) { units.at(id).timedLife--; } } } } deadUnitCheck(); } else if(type=="unitcast") { castSpell(msg.getInt("caster"),msg.getInt("spell"),msg.getInt("targetx"),msg.getInt("targety")); } else if(type=="dodeadunitcheck") { deadUnitCheck(); } else if(type=="unitsummon") { summonUnit(msg.getString("unittype"),msg.getInt("x"),msg.getInt("y"),msg.getInt("owner")); } else if(type=="unitmove") { int uid = msg.getInt("unit"); int j = msg.getInt("targetx"); int i = msg.getInt("targety"); units.at(uid).MovePoints -= getUnitTileEnterCost(uid,units.at(uid).x,units.at(uid).y,j,i) + getUnitTileExitCost(uid,units.at(uid).x,units.at(uid).y,j,i); moveUnit(uid,j,i); } else if(type=="unitattack") { int uid = msg.getInt("unit"); int target = msg.getInt("target"); int attack = getUnitAttack(uid); if(attack>0) { Message msg("unitdamage"); msg.addValue("source",uid); msg.addValue("target",target); msg.addValue("amount",attack); msg.addValue("damagetype",DAMAGETYPE_ATTACK); MsgMngr.sendMessage(msg); } units.at(uid).hasAttacked = 1; units.at(uid).MovePoints -= getUnitAttackCost(uid); Animation a("Attack1","Attack",getSpritePos(units.at(target).x,units.at(target).y),1); ActiveAnimations.push_back(a); ActiveAnimations.at(ActiveAnimations.size()-1).play(); } else if(type=="unitdamage") { int src = msg.getInt("source"); int target = msg.getInt("target"); int amt = msg.getInt("amount"); damageUnit(src,target,amt); deadUnitCheck(); } else if(type=="unitdamagearea") { int src = msg.getInt("source"); int tx = msg.getInt("targetx"); int ty = msg.getInt("targety"); int range = msg.getInt("range"); int amt = msg.getInt("amount"); int dmgtype = msg.getInt("damagetype"); int flags = msg.getInt("targetflags"); damageUnitArea(src,tx,ty,range,amt,dmgtype,flags); deadUnitCheck(); } else if(type=="unitheal") { healUnit(msg.getInt("source"),msg.getInt("target"),msg.getInt("amount")); deadUnitCheck(); } else if(type=="unithealarea") { int src = msg.getInt("source"); int tx = msg.getInt("targetx"); int ty = msg.getInt("targety"); int range = msg.getInt("range"); int amt = msg.getInt("amount"); int flags = msg.getInt("targetflags"); healUnitArea(src,tx,ty,range,amt,flags); deadUnitCheck(); } else if(type=="unitdestroy") { units.at(msg.getInt("unit")).Life = 0; deadUnitCheck(); } else if(type=="unitteleport") { moveUnit(msg.getInt("unit"),msg.getInt("targetx"),msg.getInt("targety")); } else if(type=="unitdeath") { int uid = msg.getInt("unit"); tiles[units.at(uid).x][units.at(uid).y].isOccupied = false; //units.erase(uid); units.at(uid).UniqueId = -1; if(SelectionId == uid) { SelectionId = -1; } } else if(type=="unitready") { units.at(msg.getInt("unit")).hasAttacked = 0; //refresh attack status units.at(msg.getInt("unit")).MovePoints = 6; } else if(type=="unitsetlife") { units.at(msg.getInt("unit")).Life = msg.getInt("amount"); } else if(type=="unitsetmaxlife") { units.at(msg.getInt("unit")).maxLife = msg.getInt("amount"); } else if(type=="unitsetbaseattack") { units.at(msg.getInt("unit")).Attack = msg.getInt("amount"); } else if(type=="unitsetmovepoints") { units.at(msg.getInt("unit")).MovePoints = msg.getInt("amount"); } else if(type=="unitchangeowner") { units.at(msg.getInt("unit")).Owner = msg.getInt("owner"); } else if(type=="unitsettimedlife") { units.at(msg.getInt("unit")).timedLife = msg.getInt("amount"); } else if(type=="buffcreate") { Buff b(msg.getInt("bufftype"),msg.getInt("target"),msg.getInt("duration"),msg.getInt("owner"),msg.getInt("ispermanent"),0,0); units.at(msg.getInt("target")).buffs.push_back(b); } else if(type=="buffremove") { int uid = msg.getInt("target"); int bid = msg.getInt("bufftype"); for(int i = 0;i<world.units.at(uid).buffs.size();i++) { if(world.units.at(uid).buffs.at(i).BuffType==bid) { units.at(uid).buffs.erase(world.units.at(uid).buffs.begin()+i); i--; } } } else if(type=="buffdispel") { dispelUnit(msg.getInt("unit"),msg.getInt("targetflags")); } else if(type=="buffsetduration") { units.at(msg.getInt("unit")).buffs.at(msg.getInt("buff")).Duration = msg.getInt("duration"); } else if(type=="spellsetcurrentcooldown") { units.at(msg.getInt("unit")).abilities.at(msg.getInt("spell")).currCooldown = msg.getInt("cooldown"); } else if(type=="tiletypechange") { world.tiles[msg.getInt("x")][msg.getInt("y")].setType(msg.getInt("tiletype")); } else if(type=="playanimation") { Animation a(msg.getString("animation"),msg.getString("sound"),getSpritePos(msg.getInt("x"),msg.getInt("y")),atof(msg.getString("scalingfactor").c_str())); ActiveAnimations.push_back(a); ActiveAnimations.at(ActiveAnimations.size()-1).play(); } else if(type=="playsound") { ActiveAnimations.push_back(Animation("Attack1",msg.getString("sound"),0,0,1.0,1)); ActiveAnimations.at(ActiveAnimations.size()-1).play(); } return 0; }