#define VCMI_DLL #include #include #include #include "CGameState.h" #include #include "hch/CDefObjInfoHandler.h" #include "hch/CArtHandler.h" #include "hch/CTownHandler.h" #include "hch/CHeroHandler.h" #include "hch/CObjectHandler.h" #include "hch/CCreatureHandler.h" #include "lib/VCMI_Lib.h" #include "map.h" #include "StartInfo.h" #include "lib/NetPacks.h" #include #include #include boost::rand48 ran; CGObjectInstance * createObject(int id, int subid, int3 pos, int owner) { CGObjectInstance * nobj; switch(id) { case 34: //hero { CGHeroInstance * nobj; nobj = new CGHeroInstance(); nobj->pos = pos; nobj->tempOwner = owner; nobj->defInfo = new CGDefInfo(); nobj->defInfo->id = 34; nobj->defInfo->subid = subid; nobj->defInfo->printPriority = 0; nobj->type = VLC->heroh->heroes[subid]; for(int i=0;i<6;i++) { nobj->defInfo->blockMap[i]=255; nobj->defInfo->visitMap[i]=0; } nobj->ID = id; nobj->subID = subid; nobj->defInfo->handler=NULL; nobj->defInfo->blockMap[5] = 253; nobj->defInfo->visitMap[5] = 2; nobj->artifacts.resize(20); nobj->artifWorn[16] = 3; nobj->portrait = subid; nobj->primSkills.resize(4); nobj->primSkills[0] = nobj->type->heroClass->initialAttack; nobj->primSkills[1] = nobj->type->heroClass->initialDefence; nobj->primSkills[2] = nobj->type->heroClass->initialPower; nobj->primSkills[3] = nobj->type->heroClass->initialKnowledge; nobj->mana = 10 * nobj->primSkills[3]; return nobj; } case 98: //town nobj = new CGTownInstance; break; default: //rest of objects nobj = new CGObjectInstance; nobj->defInfo = VLC->dobjinfo->gobjs[id][subid]; break; } nobj->ID = id; nobj->subID = subid; if(!nobj->defInfo) std::cout <<"No def declaration for " <pos = pos; //nobj->state = NULL;//new CLuaObjectScript(); nobj->tempOwner = owner; nobj->info = NULL; nobj->defInfo->id = id; nobj->defInfo->subid = subid; //assigning defhandler if(nobj->ID==34 || nobj->ID==98) return nobj; nobj->defInfo = VLC->dobjinfo->gobjs[id][subid]; //if(!nobj->defInfo->handler) //{ // nobj->defInfo->handler = CDefHandler::giveDef(nobj->defInfo->name); // nobj->defInfo->width = nobj->defInfo->handler->ourImages[0].bitmap->w/32; // nobj->defInfo->height = nobj->defInfo->handler->ourImages[0].bitmap->h/32; //} return nobj; } CStack * BattleInfo::getStack(int stackID) { for(int g=0; gID == stackID) return stacks[g]; } return NULL; } CStack * BattleInfo::getStackT(int tileID) { for(int g=0; gposition == tileID || (stacks[g]->creature->isDoubleWide() && stacks[g]->attackerOwned && stacks[g]->position-1 == tileID) || (stacks[g]->creature->isDoubleWide() && !stacks[g]->attackerOwned && stacks[g]->position+1 == tileID)) { if(stacks[g]->alive) { return stacks[g]; } } } return NULL; } void BattleInfo::getAccessibilityMap(bool *accessibility, int stackToOmmit) { memset(accessibility,1,187); //initialize array with trues for(int g=0; galive || stacks[g]->ID==stackToOmmit) //we don't want to lock position of this stack continue; accessibility[stacks[g]->position] = false; if(stacks[g]->creature->isDoubleWide()) //if it's a double hex creature { if(stacks[g]->attackerOwned) accessibility[stacks[g]->position-1] = false; else accessibility[stacks[g]->position+1] = false; } } //TODO: obstacles } void BattleInfo::getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit) //send pointer to at least 187 allocated bytes { bool mac[187]; getAccessibilityMap(mac,stackToOmmit); memcpy(accessibility,mac,187); for(int b=0; b<187; ++b) { if( mac[b] && !(atackerSide ? mac[b-1] : mac[b+1])) { accessibility[b] = false; } } //removing accessibility for side hexes for(int v=0; v<187; ++v) if(atackerSide ? (v%17)==1 : (v%17)==15) accessibility[v] = false; } void BattleInfo::makeBFS(int start, bool*accessibility, int *predecessor, int *dists) //both pointers must point to the at least 187-elements int arrays { //inits for(int b=0; b<187; ++b) predecessor[b] = -1; for(int g=0; g<187; ++g) dists[g] = 100000000; std::queue hexq; //bfs queue hexq.push(start); dists[hexq.front()] = 0; int curNext = -1; //for bfs loop only (helper var) while(!hexq.empty()) //bfs loop { int curHex = hexq.front(); std::vector neighbours = neighbouringTiles(curHex); hexq.pop(); for(int nr=0; nr=dists[curNext]) continue; hexq.push(curNext); dists[curNext] = dists[curHex] + 1; predecessor[curNext] = curHex; } } }; std::vector BattleInfo::getAccessibility(int stackID) { std::vector ret; bool ac[187]; CStack *s = getStack(stackID); if(s->creature->isDoubleWide()) getAccessibilityMapForTwoHex(ac,s->attackerOwned,stackID); else getAccessibilityMap(ac,stackID); int pr[187], dist[187]; makeBFS(s->position,ac,pr,dist); for(int i=0;i<187;i++) if(dist[i] <= s->creature->speed) ret.push_back(i); return ret; } signed char BattleInfo::mutualPosition(int hex1, int hex2) { if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left return 0; if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right return 1; if(hex2 == hex1 - 1 && hex1%17 != 0) //left return 5; if(hex2 == hex1 + 1 && hex1%17 != 16) //right return 2; if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left return 4; if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right return 3; return -1; } std::vector BattleInfo::neighbouringTiles(int hex) { #define CHECK_AND_PUSH(tile) {int hlp = (tile); if(hlp>=0 && hlp<187 && (hlp%17!=16) && hlp%17) ret.push_back(hlp);} std::vector ret; CHECK_AND_PUSH(hex - ( (hex/17)%2 ? 18 : 17 )); CHECK_AND_PUSH(hex - ( (hex/17)%2 ? 17 : 16 )); CHECK_AND_PUSH(hex - 1); CHECK_AND_PUSH(hex + 1); CHECK_AND_PUSH(hex + ( (hex/17)%2 ? 16 : 17 )); CHECK_AND_PUSH(hex + ( (hex/17)%2 ? 17 : 18 )); #undef CHECK_AND_PUSH return ret; } std::vector BattleInfo::getPath(int start, int dest, bool*accessibility) { int predecessor[187]; //for getting the Path int dist[187]; //calculated distances makeBFS(start,accessibility,predecessor,dist); //making the Path std::vector path; int curElem = dest; while(curElem != start) { path.push_back(curElem); curElem = predecessor[curElem]; } return path; } CStack::CStack(CCreature * C, int A, int O, int I, bool AO) :creature(C),amount(A),owner(O), alive(true), position(-1), ID(I), attackerOwned(AO), firstHPleft(C->hitPoints) { } void CGameState::apply(IPack * pack) { mx->lock(); switch(pack->getType()) { case 101://NewTurn { NewTurn * n = static_cast(pack); day = n->day; BOOST_FOREACH(NewTurn::Hero h, n->heroes) //give mana/movement point { static_cast(map->objects[h.id])->movement = h.move; static_cast(map->objects[h.id])->mana = h.mana; } BOOST_FOREACH(NewTurn::Resources h, n->res) //give resources { for(int i=0;iresetBuilded) //reset amount of structures set in this turn in towns BOOST_FOREACH(CGTownInstance* t, map->towns) t->builded = 0; break; } case 102: //set resource amount { SetResource *sr = static_cast(pack); players[sr->player].resources[sr->resid] = sr->val; break; } case 104: { SetResources *sr = static_cast(pack); for(int i=0;ires.size();i++) players[sr->player].resources[i] = sr->res[i]; break; } case 105: { SetPrimSkill *sr = static_cast(pack); CGHeroInstance *hero = getHero(sr->id); if(sr->which <4) { if(sr->abs) hero->primSkills[sr->which] = sr->val; else hero->primSkills[sr->which] += sr->val; } else if(sr->which == 4) //XP { if(sr->abs) hero->exp = sr->val; else hero->exp += sr->val; } break; } case 500: { RemoveHero *rh = static_cast(pack); CGHeroInstance *h = static_cast(map->objects[rh->id]); std::vector::iterator nitr = std::find(map->heroes.begin(), map->heroes.end(),h); map->heroes.erase(nitr); int player = h->tempOwner; nitr = std::find(players[player].heroes.begin(), players[player].heroes.end(), h); players[player].heroes.erase(nitr); map->objects[h->id] = NULL; break; } case 501://hero try-move { TryMoveHero * n = static_cast(pack); CGHeroInstance *h = static_cast(map->objects[n->id]); h->movement = n->movePoints; if(n->result) h->pos = n->end; else h->pos = n->start; BOOST_FOREACH(int3 t, n->fowRevealed) players[h->getOwner()].fogOfWarMap[t.x][t.y][t.z] = 1; break; } case 502: { SetGarrisons * n = static_cast(pack); for(std::map::iterator i = n->garrs.begin(); i!=n->garrs.end(); i++) static_cast(map->objects[i->first])->army = i->second; break; } case 503: { SetStrInfo *ssi = static_cast(pack); static_cast(map->objects[ssi->tid])->strInfo.creatures = ssi->cres; break; } case 504: { NewStructures *ns = static_cast(pack); CGTownInstance*t = static_cast(map->objects[ns->tid]); BOOST_FOREACH(si32 bid,ns->bid) t->builtBuildings.insert(bid); t->builded = ns->builded; break; } case 1001://set object property { SetObjectProperty *p = static_cast(pack); int CGObjectInstance::*point; switch(p->what) { case 1: point = &CGObjectInstance::tempOwner; break; case 2: point = &CGObjectInstance::blockVisit; break; } map->objects[p->id]->*point = p->val; break; } case 3000: { BattleStart * bs = static_cast(pack); curB = bs->info; break; } case 3001: { BattleNextRound *ns = static_cast(pack); curB->round = ns->round; break; } case 3002: { BattleSetActiveStack *ns = static_cast(pack); curB->activeStack = ns->stack; break; } case 3003: { BattleResult *br = static_cast(pack); //TODO: give exp, artifacts to winner, decrease armies (casualties) for(unsigned i=0;istacks.size();i++) delete curB->stacks[i]; delete curB; curB = NULL; break; } case 3004: { BattleStackMoved *br = static_cast(pack); curB->getStack(br->stack)->position = br->tile; break; } //case 1002://set hover name // { // SetHoverName * shn = static_cast(pack); // map->objects[shn->id]->hoverName = toString(shn->name); // break; // } } mx->unlock(); } int CGameState::pickHero(int owner) { int h=-1; if(map->getHero(h = scenarioOps->getIthPlayersSettings(owner).hero,0) && h>=0) //we haven't used selected hero return h; int f = scenarioOps->getIthPlayersSettings(owner).castle; int i=0; do //try to find free hero of our faction { i++; h = scenarioOps->getIthPlayersSettings(owner).castle*HEROES_PER_TYPE*2+(ran()%(HEROES_PER_TYPE*2));//->scenarioOps->playerInfos[pru].hero = VLC-> } while( map->getHero(h) && i<175); if(i>174) //probably no free heroes - there's no point in further search, we'll take first free { std::cout << "Warning: cannot find free hero - trying to get first available..."<getHero(j)) h=j; } return h; } CGHeroInstance *CGameState::getHero(int objid) { if(objid<0 || objid>=map->objects.size()) return NULL; return static_cast(map->objects[objid]); } std::pair CGameState::pickObject(CGObjectInstance *obj) { switch(obj->ID) { case 65: //random artifact return std::pair(5,(ran()%136)+7); //tylko sensowny zakres - na poczatku sa katapulty itp, na koncu specjalne i blanki case 66: //random treasure artifact return std::pair(5,VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id); case 67: //random minor artifact return std::pair(5,VLC->arth->minors[ran()%VLC->arth->minors.size()]->id); case 68: //random major artifact return std::pair(5,VLC->arth->majors[ran()%VLC->arth->majors.size()]->id); case 69: //random relic artifact return std::pair(5,VLC->arth->relics[ran()%VLC->arth->relics.size()]->id); case 70: //random hero { return std::pair(34,pickHero(obj->tempOwner)); } case 71: //random monster return std::pair(54,ran()%(VLC->creh->creatures.size())); case 72: //random monster lvl1 return std::pair(54,VLC->creh->levelCreatures[1][ran()%VLC->creh->levelCreatures[1].size()]->idNumber); case 73: //random monster lvl2 return std::pair(54,VLC->creh->levelCreatures[2][ran()%VLC->creh->levelCreatures[2].size()]->idNumber); case 74: //random monster lvl3 return std::pair(54,VLC->creh->levelCreatures[3][ran()%VLC->creh->levelCreatures[3].size()]->idNumber); case 75: //random monster lvl4 return std::pair(54,VLC->creh->levelCreatures[4][ran()%VLC->creh->levelCreatures[4].size()]->idNumber); case 76: //random resource return std::pair(79,ran()%7); //now it's OH3 style, use %8 for mithril case 77: //random town { int align = ((CGTownInstance*)obj)->alignment, f; if(align>PLAYER_LIMIT-1)//same as owner / random { if(obj->tempOwner > PLAYER_LIMIT-1) f = -1; //random else f = scenarioOps->getIthPlayersSettings(obj->tempOwner).castle; } else { f = scenarioOps->getIthPlayersSettings(align).castle; } if(f<0) f = ran()%VLC->townh->towns.size(); return std::pair(98,f); } case 162: //random monster lvl5 return std::pair(54,VLC->creh->levelCreatures[5][ran()%VLC->creh->levelCreatures[5].size()]->idNumber); case 163: //random monster lvl6 return std::pair(54,VLC->creh->levelCreatures[6][ran()%VLC->creh->levelCreatures[6].size()]->idNumber); case 164: //random monster lvl7 return std::pair(54,VLC->creh->levelCreatures[7][ran()%VLC->creh->levelCreatures[7].size()]->idNumber); case 216: //random dwelling { int faction = ran()%F_NUMBER; CCreGen2ObjInfo* info =(CCreGen2ObjInfo*)obj->info; if (info->asCastle) { for(int i=0;iobjects.size();i++) { if(map->objects[i]->ID==77 && dynamic_cast(map->objects[i])->identifier == info->identifier) { randomizeObject(map->objects[i]); //we have to randomize the castle first faction = map->objects[i]->subID; break; } else if(map->objects[i]->ID==98 && dynamic_cast(map->objects[i])->identifier == info->identifier) { faction = map->objects[i]->subID; break; } } } else { while((!(info->castles[0]&(1<7) && (info->castles[1]&(1<<(faction-8)))) break; faction = ran()%F_NUMBER; } } int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel)); int cid = VLC->townh->towns[faction].basicCreatures[level]; for(int i=0;iobjh->cregens.size();i++) if(VLC->objh->cregens[i]==cid) return std::pair(17,i); std::cout << "Cannot find a dwelling for creature "<(17,0); } case 217: { int faction = ran()%F_NUMBER; CCreGenObjInfo* info =(CCreGenObjInfo*)obj->info; if (info->asCastle) { for(int i=0;iobjects.size();i++) { if(map->objects[i]->ID==77 && dynamic_cast(map->objects[i])->identifier == info->identifier) { randomizeObject(map->objects[i]); //we have to randomize the castle first faction = map->objects[i]->subID; break; } else if(map->objects[i]->ID==98 && dynamic_cast(map->objects[i])->identifier == info->identifier) { faction = map->objects[i]->subID; break; } } } else { while((!(info->castles[0]&(1<7) && (info->castles[1]&(1<<(faction-8)))) break; faction = ran()%F_NUMBER; } } int cid = VLC->townh->towns[faction].basicCreatures[obj->subID]; for(int i=0;iobjh->cregens.size();i++) if(VLC->objh->cregens[i]==cid) return std::pair(17,i); std::cout << "Cannot find a dwelling for creature "<(17,0); } case 218: { CCreGen3ObjInfo* info =(CCreGen3ObjInfo*)obj->info; int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel)); int cid = VLC->townh->towns[obj->subID].basicCreatures[level]; for(int i=0;iobjh->cregens.size();i++) if(VLC->objh->cregens[i]==cid) return std::pair(17,i); std::cout << "Cannot find a dwelling for creature "<(17,0); } } return std::pair(-1,-1); } void CGameState::randomizeObject(CGObjectInstance *cur) { std::pair ran = pickObject(cur); if(ran.first<0 || ran.second<0) //this is not a random object, or we couldn't find anything { if(cur->ID==98) //town - set def { CGTownInstance *t = dynamic_cast(cur); if(t->hasCapitol()) t->defInfo = capitols[t->subID]; else if(t->hasFort()) t->defInfo = forts[t->subID]; else t->defInfo = villages[t->subID]; } return; } else if(ran.first==34)//special code for hero { CGHeroInstance *h = dynamic_cast(cur); if(!h) {std::cout<<"Wrong random hero at "<pos<ID = ran.first; h->portrait = cur->subID = ran.second; h->type = VLC->heroh->heroes[ran.second]; map->heroes.push_back(h); return; //TODO: maybe we should do something with definfo? } else if(ran.first==98)//special code for town { CGTownInstance *t = dynamic_cast(cur); if(!t) {std::cout<<"Wrong random town at "<pos<ID = ran.first; cur->subID = ran.second; t->town = &VLC->townh->towns[ran.second]; if(t->hasCapitol()) t->defInfo = capitols[t->subID]; else if(t->hasFort()) t->defInfo = forts[t->subID]; else t->defInfo = villages[t->subID]; map->towns.push_back(t); return; } //we have to replace normal random object cur->ID = ran.first; cur->subID = ran.second; map->defs.insert(cur->defInfo = VLC->dobjinfo->gobjs[ran.first][ran.second]); if(!cur->defInfo) { std::cout<<"Missing def declaration for "<ID<<" "<subID<map = map; for(int i=0;idobjinfo->castles[i]); forts[i] = VLC->dobjinfo->castles[i]; capitols[i] = new CGDefInfo(*VLC->dobjinfo->castles[i]); } //picking random factions for players for(int i=0;iplayerInfos.size();i++) { if(scenarioOps->playerInfos[i].castle==-1) { int f; do { f = ran()%F_NUMBER; }while(!(map->players[scenarioOps->playerInfos[i].color].allowedFactions & 1<playerInfos[i].castle = f; } } //randomizing objects for(int no=0; noobjects.size(); ++no) { randomizeObject(map->objects[no]); if(map->objects[no]->ID==26) map->objects[no]->defInfo->handler=NULL; map->objects[no]->hoverName = VLC->objh->names[map->objects[no]->ID]; } //std::cout<<"\tRandomizing objects: "<players[i].generateHeroAtMainTown && map->players[i].hasMainTown) || (map->players[i].hasMainTown && map->version==RoE)) { int3 hpos = map->players[i].posOfMainTown; hpos.x+=1;// hpos.y+=1; int j; for(j=0; jplayerInfos.size(); j++) if(scenarioOps->playerInfos[j].color == i) break; if(j == scenarioOps->playerInfos.size()) continue; int h=pickHero(i); CGHeroInstance * nnn = static_cast(createObject(34,h,hpos,i)); nnn->id = map->objects.size(); //nnn->defInfo->handler = graphics->flags1[0]; map->heroes.push_back(nnn); map->objects.push_back(nnn); } } //std::cout<<"\tGiving starting heroes: "<playerInfos.size();i++) { std::pair ins(scenarioOps->playerInfos[i].color,PlayerState()); ins.second.color=ins.first; ins.second.serial=i; players.insert(ins); } /******************RESOURCES****************************************************/ //TODO: zeby komputer dostawal inaczej niz gracz std::vector startres; std::ifstream tis("config/startres.txt"); int k; for (int j=0;jdifficulty;j++) { tis >> k; for (int z=0;z>k; } tis >> k; for (int i=0;i> k; startres.push_back(k); } tis.close(); for (std::map::iterator i = players.begin(); i!=players.end(); i++) { (*i).second.resources.resize(RESOURCE_QUANTITY); for (int x=0;xheroes.size();i++) //heroes instances { if (map->heroes[i]->getOwner()<0) continue; CGHeroInstance * vhi = (map->heroes[i]); if(!vhi->type) vhi->type = VLC->heroh->heroes[vhi->subID]; //vhi->subID = vhi->type->ID; if (vhi->level<1) { vhi->exp=40+ran()%50; vhi->level = 1; } if (vhi->level>1) ;//TODO dodac um dr, ale potrzebne los if ((!vhi->primSkills.size()) || (vhi->primSkills[0]<0)) { if (vhi->primSkills.size()primSkills.resize(PRIMARY_SKILLS); vhi->primSkills[0] = vhi->type->heroClass->initialAttack; vhi->primSkills[1] = vhi->type->heroClass->initialDefence; vhi->primSkills[2] = vhi->type->heroClass->initialPower; vhi->primSkills[3] = vhi->type->heroClass->initialKnowledge; } vhi->mana = vhi->primSkills[3]*10; if (!vhi->name.length()) { vhi->name = vhi->type->name; } if (!vhi->biography.length()) { vhi->biography = vhi->type->biography; } if (vhi->portrait < 0) vhi->portrait = vhi->type->ID; //initial army if (!vhi->army.slots.size()) //standard army { int pom, pom2=0; for(int x=0;x<3;x++) { pom = (VLC->creh->nameToID[vhi->type->refTypeStack[x]]); if(pom>=145 && pom<=149) //war machine { pom2++; switch (pom) { case 145: //catapult vhi->artifWorn[16] = 3; break; default: pom-=145; vhi->artifWorn[13+pom] = 4+pom; break; } continue; } vhi->army.slots[x-pom2].first = pom; if((pom = (vhi->type->highStack[x]-vhi->type->lowStack[x])) > 0) vhi->army.slots[x-pom2].second = (ran()%pom)+vhi->type->lowStack[x]; else vhi->army.slots[x-pom2].second = +vhi->type->lowStack[x]; } } players[vhi->getOwner()].heroes.push_back(vhi); } /*************************FOG**OF**WAR******************************************/ for(std::map::iterator k=players.begin(); k!=players.end(); ++k) { k->second.fogOfWarMap.resize(map->width); for(int g=0; gwidth; ++g) k->second.fogOfWarMap[g].resize(map->height); for(int g=-0; gwidth; ++g) for(int h=0; hheight; ++h) k->second.fogOfWarMap[g][h].resize(map->twoLevel+1, 0); for(int g=0; gwidth; ++g) for(int h=0; hheight; ++h) for(int v=0; vtwoLevel+1; ++v) k->second.fogOfWarMap[g][h][v] = 0; for(int xd=0; xdwidth; ++xd) //revealing part of map around heroes { for(int yd=0; ydheight; ++yd) { for(int ch=0; chsecond.heroes.size(); ++ch) { int deltaX = (k->second.heroes[ch]->getPosition(false).x-xd)*(k->second.heroes[ch]->getPosition(false).x-xd); int deltaY = (k->second.heroes[ch]->getPosition(false).y-yd)*(k->second.heroes[ch]->getPosition(false).y-yd); if(deltaX+deltaYsecond.heroes[ch]->getSightDistance()*k->second.heroes[ch]->getSightDistance()) k->second.fogOfWarMap[xd][yd][k->second.heroes[ch]->getPosition(false).z] = 1; } } } } /****************************TOWNS************************************************/ for (int i=0;itowns.size();i++) { CGTownInstance * vti =(map->towns[i]); if(!vti->town) vti->town = &VLC->townh->towns[vti->subID]; if (vti->name.length()==0) // if town hasn't name we draw it vti->name=vti->town->names[ran()%vti->town->names.size()]; if(vti->builtBuildings.find(-50)!=vti->builtBuildings.end()) //give standard set of buildings { vti->builtBuildings.erase(-50); vti->builtBuildings.insert(10); vti->builtBuildings.insert(5); vti->builtBuildings.insert(30); if(ran()%2) vti->builtBuildings.insert(31); } players[vti->getOwner()].towns.push_back(vti); } for(std::map::iterator k=players.begin(); k!=players.end(); ++k) { if(k->first==-1 || k->first==255) continue; for(int xd=0; xdwidth; ++xd) //revealing part of map around towns { for(int yd=0; ydheight; ++yd) { for(int ch=0; chsecond.towns.size(); ++ch) { int deltaX = (k->second.towns[ch]->pos.x-xd)*(k->second.towns[ch]->pos.x-xd); int deltaY = (k->second.towns[ch]->pos.y-yd)*(k->second.towns[ch]->pos.y-yd); if(deltaX+deltaYsecond.towns[ch]->getSightDistance()*k->second.towns[ch]->getSightDistance()) k->second.fogOfWarMap[xd][yd][k->second.towns[ch]->pos.z] = 1; } } } //init visiting heroes for(int l=0; lsecond.heroes.size();l++) { for(int m=0; msecond.towns.size();m++) { int3 vistile = k->second.towns[m]->pos; vistile.x--; //tile next to the entrance if(vistile == k->second.heroes[l]->pos) { k->second.towns[m]->visitingHero = k->second.heroes[l]; break; } } } } } bool CGameState::battleShootCreatureStack(int ID, int dest) {/* CStack * curStack = NULL; for(int y=0; ystacks.size(); ++y) { if(curB->stacks[y]->ID == ID) { curStack = curB->stacks[y]; break; } } if(!curStack) return false; int IDOfStackAtEnd = battleGetStack(dest); int numberOfStackAtEnd = -1; for(int v=0; vstacks.size(); ++v) { if(curB->stacks[v]->ID == IDOfStackAtEnd) { numberOfStackAtEnd = v; break; } } if(IDOfStackAtEnd == -1 || curB->stacks[numberOfStackAtEnd]->owner == curStack->owner || !curB->stacks[numberOfStackAtEnd]->alive) return false; LOCPLINT->battleStackIsShooting(ID, dest); //counting dealt damage int finalDmg = calculateDmg(curStack, curB->stacks[numberOfStackAtEnd]); //applying damages int cresKilled = finalDmg / curB->stacks[ID]->creature->hitPoints; int damageFirst = finalDmg % curB->stacks[ID]->creature->hitPoints; if( curB->stacks[numberOfStackAtEnd]->firstHPleft <= damageFirst ) { curB->stacks[numberOfStackAtEnd]->amount -= 1; curB->stacks[numberOfStackAtEnd]->firstHPleft += curB->stacks[numberOfStackAtEnd]->creature->hitPoints - damageFirst; } else { curB->stacks[numberOfStackAtEnd]->firstHPleft -= damageFirst; } int cresInstackBefore = curB->stacks[numberOfStackAtEnd]->amount; curB->stacks[numberOfStackAtEnd]->amount -= cresKilled; if(curB->stacks[numberOfStackAtEnd]->amount<=0) //stack killed { curB->stacks[numberOfStackAtEnd]->amount = 0; LOCPLINT->battleStackKilled(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore), ID, true); curB->stacks[numberOfStackAtEnd]->alive = false; } else { LOCPLINT->battleStackIsAttacked(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore), ID, true); } //damage applied*/ return true; } int CGameState::battleGetStack(int pos) { for(int g=0; gstacks.size(); ++g) { if(curB->stacks[g]->position == pos || ( curB->stacks[g]->creature->isDoubleWide() && ( (curB->stacks[g]->attackerOwned && curB->stacks[g]->position-1 == pos) || (!curB->stacks[g]->attackerOwned && curB->stacks[g]->position+1 == pos) ) ) ) return curB->stacks[g]->ID; } return -1; } int CGameState::calculateDmg(const CStack* attacker, const CStack* defender) { int attackDefenseBonus = attacker->creature->attack - defender->creature->defence; int damageBase = 0; if(attacker->creature->damageMax == attacker->creature->damageMin) //constant damage { damageBase = attacker->creature->damageMin; } else { damageBase = rand()%(attacker->creature->damageMax - attacker->creature->damageMin) + attacker->creature->damageMin + 1; } float dmgBonusMultiplier = 1.0; if(attackDefenseBonus < 0) //decreasing dmg { if(0.02f * (-attackDefenseBonus) > 0.3f) { dmgBonusMultiplier += -0.3f; } else { dmgBonusMultiplier += 0.02f * attackDefenseBonus; } } else //increasing dmg { if(0.05f * attackDefenseBonus > 4.0f) { dmgBonusMultiplier += 4.0f; } else { dmgBonusMultiplier += 0.05f * attackDefenseBonus; } } return (float)damageBase * (float)attacker->amount * dmgBonusMultiplier; } std::vector CGameState::battleGetRange(int ID) {/* std::vector additionals; //adding enemies' positions for(int c=0; cstacks.size(); ++c) { if(curB->stacks[c]->alive && curB->stacks[c]->owner != owner) { for(int g=0; gstacks[c]->position) != -1) { additionals.push_back(curB->stacks[c]->position); } if(curB->stacks[c]->creature->isDoubleWide() && curB->stacks[c]->attackerOwned && CBattleHex::mutualPosition(ret[g], curB->stacks[c]->position-1) != -1) { additionals.push_back(curB->stacks[c]->position-1); } if(curB->stacks[c]->creature->isDoubleWide() && !curB->stacks[c]->attackerOwned && CBattleHex::mutualPosition(ret[g], curB->stacks[c]->position+1) != -1) { additionals.push_back(curB->stacks[c]->position+1); } } } } for(int g=0; g::iterator nend = std::unique(ret.begin(), ret.end()); std::vector ret2; for(std::vector::iterator it = ret.begin(); it != nend; ++it) { ret2.push_back(*it); } return ret2;*/ return std::vector(); }