CGameState.cpp 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052
  1. #define VCMI_DLL
  2. #include "CCampaignHandler.h"
  3. #include <algorithm>
  4. #include <queue>
  5. #include <fstream>
  6. #include "CGameState.h"
  7. #include <boost/random/linear_congruential.hpp>
  8. #include "CDefObjInfoHandler.h"
  9. #include "CArtHandler.h"
  10. #include "CBuildingHandler.h"
  11. #include "CGeneralTextHandler.h"
  12. #include "CTownHandler.h"
  13. #include "CSpellHandler.h"
  14. #include "CHeroHandler.h"
  15. #include "CObjectHandler.h"
  16. #include "CCreatureHandler.h"
  17. //#include "../lib/IGameCallback.h"
  18. #include "VCMI_Lib.h"
  19. #include "Connection.h"
  20. #include "map.h"
  21. #include "../StartInfo.h"
  22. #include "NetPacks.h"
  23. #include <boost/foreach.hpp>
  24. #include <boost/lexical_cast.hpp>
  25. #include <boost/thread.hpp>
  26. #include <boost/thread/shared_mutex.hpp>
  27. #include <boost/assign/list_of.hpp>
  28. #include "RegisterTypes.cpp"
  29. #include <algorithm>
  30. #include <numeric>
  31. #include "CMapInfo.h"
  32. #include "BattleState.h"
  33. #include "../lib/JsonNode.h"
  34. boost::rand48 ran;
  35. class CGObjectInstance;
  36. #ifdef min
  37. #undef min
  38. #endif
  39. #ifdef max
  40. #undef max
  41. #endif
  42. /*
  43. * CGameState.cpp, part of VCMI engine
  44. *
  45. * Authors: listed in file AUTHORS in main folder
  46. *
  47. * License: GNU General Public License v2.0 or later
  48. * Full text of license available in license.txt file, in main folder
  49. *
  50. */
  51. void foofoofoo()
  52. {
  53. //never called function to force instantation of templates
  54. int *ccc = NULL;
  55. registerTypes((CISer<CConnection>&)*ccc);
  56. registerTypes((COSer<CConnection>&)*ccc);
  57. registerTypes((CSaveFile&)*ccc);
  58. registerTypes((CLoadFile&)*ccc);
  59. registerTypes((CTypeList&)*ccc);
  60. }
  61. template <typename T> class CApplyOnGS;
  62. class CBaseForGSApply
  63. {
  64. public:
  65. virtual void applyOnGS(CGameState *gs, void *pack) const =0;
  66. virtual ~CBaseForGSApply(){};
  67. template<typename U> static CBaseForGSApply *getApplier(const U * t=NULL)
  68. {
  69. return new CApplyOnGS<U>;
  70. }
  71. };
  72. template <typename T> class CApplyOnGS : public CBaseForGSApply
  73. {
  74. public:
  75. void applyOnGS(CGameState *gs, void *pack) const
  76. {
  77. T *ptr = static_cast<T*>(pack);
  78. gs->mx->lock();
  79. // while(!gs->mx->try_lock())
  80. // boost::this_thread::sleep(boost::posix_time::milliseconds(1)); //give other threads time to finish
  81. ptr->applyGs(gs);
  82. gs->mx->unlock();
  83. }
  84. };
  85. static CApplier<CBaseForGSApply> *applierGs = NULL;
  86. class IObjectCaller
  87. {
  88. public:
  89. virtual void preInit()=0;
  90. virtual void postInit()=0;
  91. };
  92. template <typename T>
  93. class CObjectCaller : public IObjectCaller
  94. {
  95. public:
  96. void preInit()
  97. {
  98. //T::preInit();
  99. }
  100. void postInit()
  101. {
  102. //T::postInit();
  103. }
  104. };
  105. class CObjectCallersHandler
  106. {
  107. public:
  108. std::vector<IObjectCaller*> apps;
  109. template<typename T> void registerType(const T * t=NULL)
  110. {
  111. apps.push_back(new CObjectCaller<T>);
  112. }
  113. CObjectCallersHandler()
  114. {
  115. registerTypes1(*this);
  116. }
  117. ~CObjectCallersHandler()
  118. {
  119. for (size_t i = 0; i < apps.size(); i++)
  120. delete apps[i];
  121. }
  122. void preInit()
  123. {
  124. // for (size_t i = 0; i < apps.size(); i++)
  125. // apps[i]->preInit();
  126. }
  127. void postInit()
  128. {
  129. //for (size_t i = 0; i < apps.size(); i++)
  130. //apps[i]->postInit();
  131. }
  132. } *objCaller = NULL;
  133. InfoAboutTown::InfoAboutTown()
  134. {
  135. tType = NULL;
  136. details = NULL;
  137. fortLevel = 0;
  138. owner = -1;
  139. }
  140. InfoAboutTown::~InfoAboutTown()
  141. {
  142. delete details;
  143. }
  144. void InfoAboutTown::initFromTown( const CGTownInstance *t, bool detailed )
  145. {
  146. obj = t;
  147. army = ArmyDescriptor(t->getUpperArmy(), detailed);
  148. built = t->builded;
  149. fortLevel = t->fortLevel();
  150. name = t->name;
  151. tType = t->town;
  152. owner = t->tempOwner;
  153. if(detailed)
  154. {
  155. //include details about hero
  156. details = new Details;
  157. details->goldIncome = t->dailyIncome();
  158. details->customRes = vstd::contains(t->builtBuildings, 15);
  159. details->hallLevel = t->hallLevel();
  160. details->garrisonedHero = t->garrisonHero;
  161. }
  162. //TODO: adjust undetailed info about army to our count of thieves guilds
  163. }
  164. void InfoAboutTown::initFromGarrison(const CGGarrison *garr, bool detailed)
  165. {
  166. obj = garr;
  167. fortLevel = 0;
  168. army = ArmyDescriptor(garr, detailed);
  169. name = VLC->generaltexth->names[33]; // "Garrison"
  170. owner = garr->tempOwner;
  171. built = false;
  172. tType = NULL;
  173. // Show detailed info only to owning player.
  174. if(detailed)
  175. {
  176. details = new InfoAboutTown::Details;
  177. details->customRes = false;
  178. details->garrisonedHero = false;
  179. details->goldIncome = -1;
  180. details->hallLevel = -1;
  181. }
  182. }
  183. void MetaString::getLocalString(const std::pair<ui8,ui32> &txt, std::string &dst) const
  184. {
  185. int type = txt.first, ser = txt.second;
  186. if(type == ART_NAMES)
  187. {
  188. dst = VLC->arth->artifacts[ser]->Name();
  189. }
  190. else if(type == CRE_PL_NAMES)
  191. {
  192. dst = VLC->creh->creatures[ser]->namePl;
  193. }
  194. else if(type == MINE_NAMES)
  195. {
  196. dst = VLC->generaltexth->mines[ser].first;
  197. }
  198. else if(type == MINE_EVNTS)
  199. {
  200. dst = VLC->generaltexth->mines[ser].second;
  201. }
  202. else if(type == SPELL_NAME)
  203. {
  204. dst = VLC->spellh->spells[ser]->name;
  205. }
  206. else if(type == CRE_SING_NAMES)
  207. {
  208. dst = VLC->creh->creatures[ser]->nameSing;
  209. }
  210. else if(type == ART_DESCR)
  211. {
  212. dst = VLC->arth->artifacts[ser]->Description();
  213. }
  214. else
  215. {
  216. std::vector<std::string> *vec;
  217. switch(type)
  218. {
  219. case GENERAL_TXT:
  220. vec = &VLC->generaltexth->allTexts;
  221. break;
  222. case XTRAINFO_TXT:
  223. vec = &VLC->generaltexth->xtrainfo;
  224. break;
  225. case OBJ_NAMES:
  226. vec = &VLC->generaltexth->names;
  227. break;
  228. case RES_NAMES:
  229. vec = &VLC->generaltexth->restypes;
  230. break;
  231. case ARRAY_TXT:
  232. vec = &VLC->generaltexth->arraytxt;
  233. break;
  234. case CREGENS:
  235. vec = &VLC->generaltexth->creGens;
  236. break;
  237. case CREGENS4:
  238. vec = &VLC->generaltexth->creGens4;
  239. break;
  240. case ADVOB_TXT:
  241. vec = &VLC->generaltexth->advobtxt;
  242. break;
  243. case ART_EVNTS:
  244. vec = &VLC->generaltexth->artifEvents;
  245. break;
  246. case SEC_SKILL_NAME:
  247. vec = &VLC->generaltexth->skillName;
  248. break;
  249. case COLOR:
  250. vec = &VLC->generaltexth->capColors;
  251. break;
  252. default:
  253. tlog1 << "Failed string substitution because type is " << type << std::endl;
  254. dst = "#@#";
  255. return;
  256. }
  257. if(vec->size() <= ser)
  258. {
  259. tlog1 << "Failed string substitution with type " << type << " because index " << ser << " is out of bounds!\n";
  260. dst = "#!#";
  261. }
  262. else
  263. dst = (*vec)[ser];
  264. }
  265. }
  266. DLL_EXPORT void MetaString::toString(std::string &dst) const
  267. {
  268. size_t exSt = 0, loSt = 0, nums = 0;
  269. dst.clear();
  270. for(size_t i=0;i<message.size();++i)
  271. {//TEXACT_STRING, TLOCAL_STRING, TNUMBER, TREPLACE_ESTRING, TREPLACE_LSTRING, TREPLACE_NUMBER
  272. switch(message[i])
  273. {
  274. case TEXACT_STRING:
  275. dst += exactStrings[exSt++];
  276. break;
  277. case TLOCAL_STRING:
  278. {
  279. std::string hlp;
  280. getLocalString(localStrings[loSt++], hlp);
  281. dst += hlp;
  282. }
  283. break;
  284. case TNUMBER:
  285. dst += boost::lexical_cast<std::string>(numbers[nums++]);
  286. break;
  287. case TREPLACE_ESTRING:
  288. dst.replace (dst.find("%s"), 2, exactStrings[exSt++]);
  289. break;
  290. case TREPLACE_LSTRING:
  291. {
  292. std::string hlp;
  293. getLocalString(localStrings[loSt++], hlp);
  294. dst.replace (dst.find("%s"), 2, hlp);
  295. }
  296. break;
  297. case TREPLACE_NUMBER:
  298. dst.replace (dst.find("%d"), 2, boost::lexical_cast<std::string>(numbers[nums++]));
  299. break;
  300. case TREPLACE_PLUSNUMBER:
  301. dst.replace (dst.find("%+d"), 3, '+' + boost::lexical_cast<std::string>(numbers[nums++]));
  302. break;
  303. default:
  304. tlog1 << "MetaString processing error!\n";
  305. break;
  306. }
  307. }
  308. }
  309. DLL_EXPORT std::string MetaString::toString() const
  310. {
  311. std::string ret;
  312. toString(ret);
  313. return ret;
  314. }
  315. DLL_EXPORT std::string MetaString::buildList () const
  316. ///used to handle loot from creature bank
  317. {
  318. size_t exSt = 0, loSt = 0, nums = 0;
  319. std::string lista;
  320. for (int i = 0; i < message.size(); ++i)
  321. {
  322. if (i > 0 && (message[i] == TEXACT_STRING || message[i] == TLOCAL_STRING))
  323. {
  324. if (exSt == exactStrings.size() - 1)
  325. lista += VLC->generaltexth->allTexts[141]; //" and "
  326. else
  327. lista += ", ";
  328. }
  329. switch (message[i])
  330. {
  331. case TEXACT_STRING:
  332. lista += exactStrings[exSt++];
  333. break;
  334. case TLOCAL_STRING:
  335. {
  336. std::string hlp;
  337. getLocalString (localStrings[loSt++], hlp);
  338. lista += hlp;
  339. }
  340. break;
  341. case TNUMBER:
  342. lista += boost::lexical_cast<std::string>(numbers[nums++]);
  343. break;
  344. case TREPLACE_ESTRING:
  345. lista.replace (lista.find("%s"), 2, exactStrings[exSt++]);
  346. break;
  347. case TREPLACE_LSTRING:
  348. {
  349. std::string hlp;
  350. getLocalString (localStrings[loSt++], hlp);
  351. lista.replace (lista.find("%s"), 2, hlp);
  352. }
  353. break;
  354. case TREPLACE_NUMBER:
  355. lista.replace (lista.find("%d"), 2, boost::lexical_cast<std::string>(numbers[nums++]));
  356. break;
  357. default:
  358. tlog1 << "MetaString processing error!\n";
  359. }
  360. }
  361. return lista;
  362. }
  363. void MetaString::addCreReplacement(TCreature id, TQuantity count) //adds sing or plural name;
  364. {
  365. assert(count);
  366. if (count == 1)
  367. addReplacement (CRE_SING_NAMES, id);
  368. else
  369. addReplacement (CRE_PL_NAMES, id);
  370. }
  371. void MetaString::addReplacement(const CStackBasicDescriptor &stack)
  372. {
  373. assert(stack.count); //valid count
  374. assert(stack.type); //valid type
  375. addCreReplacement(stack.type->idNumber, stack.count);
  376. }
  377. static CGObjectInstance * createObject(int id, int subid, int3 pos, int owner)
  378. {
  379. CGObjectInstance * nobj;
  380. switch(id)
  381. {
  382. case HEROI_TYPE: //hero
  383. {
  384. CGHeroInstance * nobj = new CGHeroInstance();
  385. nobj->pos = pos;
  386. nobj->tempOwner = owner;
  387. nobj->subID = subid;
  388. //nobj->initHero(ran);
  389. return nobj;
  390. }
  391. case TOWNI_TYPE: //town
  392. nobj = new CGTownInstance;
  393. break;
  394. default: //rest of objects
  395. nobj = new CGObjectInstance;
  396. nobj->defInfo = VLC->dobjinfo->gobjs[id][subid];
  397. break;
  398. }
  399. nobj->ID = id;
  400. nobj->subID = subid;
  401. if(!nobj->defInfo)
  402. tlog3 <<"No def declaration for " <<id <<" "<<subid<<std::endl;
  403. nobj->pos = pos;
  404. //nobj->state = NULL;//new CLuaObjectScript();
  405. nobj->tempOwner = owner;
  406. nobj->defInfo->id = id;
  407. nobj->defInfo->subid = subid;
  408. //assigning defhandler
  409. if(nobj->ID==HEROI_TYPE || nobj->ID==TOWNI_TYPE)
  410. return nobj;
  411. nobj->defInfo = VLC->dobjinfo->gobjs[id][subid];
  412. return nobj;
  413. }
  414. CGHeroInstance * CGameState::HeroesPool::pickHeroFor(bool native, int player, const CTown *town, bmap<ui32, ConstTransitivePtr<CGHeroInstance> > &available, const CHeroClass *bannedClass /*= NULL*/) const
  415. {
  416. CGHeroInstance *ret = NULL;
  417. if(player<0 || player>=PLAYER_LIMIT)
  418. {
  419. tlog1 << "Cannot pick hero for " << town->Name() << ". Wrong owner!\n";
  420. return NULL;
  421. }
  422. std::vector<CGHeroInstance *> pool;
  423. if(native)
  424. {
  425. for(bmap<ui32, ConstTransitivePtr<CGHeroInstance> >::iterator i=available.begin(); i!=available.end(); i++)
  426. {
  427. if(pavailable.find(i->first)->second & 1<<player
  428. && i->second->type->heroType/2 == town->typeID)
  429. {
  430. pool.push_back(i->second); //get all avaliable heroes
  431. }
  432. }
  433. if(!pool.size())
  434. {
  435. tlog1 << "Cannot pick native hero for " << player << ". Picking any...\n";
  436. return pickHeroFor(false, player, town, available);
  437. }
  438. else
  439. {
  440. ret = pool[rand()%pool.size()];
  441. }
  442. }
  443. else
  444. {
  445. int sum=0, r;
  446. for(bmap<ui32, ConstTransitivePtr<CGHeroInstance> >::iterator i=available.begin(); i!=available.end(); i++)
  447. {
  448. if ((!bannedClass && (pavailable.find(i->first)->second & (1<<player))) ||
  449. i->second->type->heroClass != bannedClass)
  450. {
  451. pool.push_back(i->second);
  452. sum += i->second->type->heroClass->selectionProbability[town->typeID]; //total weight
  453. }
  454. }
  455. if(!pool.size())
  456. {
  457. tlog1 << "There are no heroes available for player " << player<<"!\n";
  458. return NULL;
  459. }
  460. r = rand()%sum;
  461. for (unsigned int i=0; i<pool.size(); i++)
  462. {
  463. r -= pool[i]->type->heroClass->selectionProbability[town->typeID];
  464. if(r < 0)
  465. {
  466. ret = pool[i];
  467. break;
  468. }
  469. }
  470. if(!ret)
  471. ret = pool.back();
  472. }
  473. available.erase(ret->subID);
  474. return ret;
  475. }
  476. //void CGameState::apply(CPack * pack)
  477. //{
  478. // while(!mx->try_lock())
  479. // boost::this_thread::sleep(boost::posix_time::milliseconds(50)); //give other threads time to finish
  480. // //applyNL(pack);
  481. // mx->unlock();
  482. //}
  483. int CGameState::pickHero(int owner)
  484. {
  485. int h=-1;
  486. const PlayerSettings &ps = scenarioOps->getIthPlayersSettings(owner);
  487. if(!map->getHero(h = ps.hero,0) && h>=0) //we haven't used selected hero
  488. return h;
  489. int i=0;
  490. do //try to find free hero of our faction
  491. {
  492. i++;
  493. h = ps.castle*HEROES_PER_TYPE*2+(ran()%(HEROES_PER_TYPE*2));//->scenarioOps->playerInfos[pru].hero = VLC->
  494. } while( map->getHero(h) && i<175);
  495. if(i>174) //probably no free heroes - there's no point in further search, we'll take first free
  496. {
  497. tlog3 << "Warning: cannot find free hero - trying to get first available..."<<std::endl;
  498. for(int j=0; j<HEROES_PER_TYPE * 2 * F_NUMBER; j++)
  499. if(!map->getHero(j))
  500. h=j;
  501. }
  502. return h;
  503. }
  504. std::pair<int,int> CGameState::pickObject (CGObjectInstance *obj)
  505. {
  506. switch(obj->ID)
  507. {
  508. case 65:
  509. return std::pair<int,int>(5, VLC->arth->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR | CArtifact::ART_RELIC));
  510. case 66: //random treasure artifact
  511. return std::pair<int,int>(5, VLC->arth->getRandomArt (CArtifact::ART_TREASURE));
  512. case 67: //random minor artifact
  513. return std::pair<int,int>(5, VLC->arth->getRandomArt (CArtifact::ART_MINOR));
  514. case 68: //random major artifact
  515. return std::pair<int,int>(5, VLC->arth->getRandomArt (CArtifact::ART_MAJOR));
  516. case 69: //random relic artifact
  517. return std::pair<int,int>(5, VLC->arth->getRandomArt (CArtifact::ART_RELIC));
  518. case 70: //random hero
  519. return std::pair<int,int>(HEROI_TYPE,pickHero(obj->tempOwner));
  520. case 71: //random monster
  521. return std::pair<int,int>(54,VLC->creh->pickRandomMonster(boost::ref(ran)));
  522. case 72: //random monster lvl1
  523. return std::pair<int,int>(54,VLC->creh->pickRandomMonster(boost::ref(ran), 1));
  524. case 73: //random monster lvl2
  525. return std::pair<int,int>(54,VLC->creh->pickRandomMonster(boost::ref(ran), 2));
  526. case 74: //random monster lvl3
  527. return std::pair<int,int>(54,VLC->creh->pickRandomMonster(boost::ref(ran), 3));
  528. case 75: //random monster lvl4
  529. return std::pair<int,int>(54, VLC->creh->pickRandomMonster(boost::ref(ran), 4));
  530. case 76: //random resource
  531. return std::pair<int,int>(79,ran()%7); //now it's OH3 style, use %8 for mithril
  532. case 77: //random town
  533. {
  534. int align = (static_cast<CGTownInstance*>(obj))->alignment,
  535. f;
  536. if(align>PLAYER_LIMIT-1)//same as owner / random
  537. {
  538. if(obj->tempOwner > PLAYER_LIMIT-1)
  539. f = -1; //random
  540. else
  541. f = scenarioOps->getIthPlayersSettings(obj->tempOwner).castle;
  542. }
  543. else
  544. {
  545. f = scenarioOps->getIthPlayersSettings(align).castle;
  546. }
  547. if(f<0) f = ran()%VLC->townh->towns.size();
  548. return std::pair<int,int>(TOWNI_TYPE,f);
  549. }
  550. case 162: //random monster lvl5
  551. return std::pair<int,int>(54, VLC->creh->pickRandomMonster(boost::ref(ran), 5));
  552. case 163: //random monster lvl6
  553. return std::pair<int,int>(54, VLC->creh->pickRandomMonster(boost::ref(ran), 6));
  554. case 164: //random monster lvl7
  555. return std::pair<int,int>(54, VLC->creh->pickRandomMonster(boost::ref(ran), 7));
  556. case 216: //random dwelling
  557. {
  558. int faction = ran()%F_NUMBER;
  559. CGDwelling * dwl = static_cast<CGDwelling*>(obj);
  560. CCreGen2ObjInfo* info = static_cast<CCreGen2ObjInfo*>(dwl->info);
  561. if (info->asCastle)
  562. {
  563. for(unsigned int i=0;i<map->objects.size();i++)
  564. {
  565. if(map->objects[i]->ID==77 && dynamic_cast<CGTownInstance*>(map->objects[i].get())->identifier == info->identifier)
  566. {
  567. randomizeObject(map->objects[i]); //we have to randomize the castle first
  568. faction = map->objects[i]->subID;
  569. break;
  570. }
  571. else if(map->objects[i]->ID==TOWNI_TYPE && dynamic_cast<CGTownInstance*>(map->objects[i].get())->identifier == info->identifier)
  572. {
  573. faction = map->objects[i]->subID;
  574. break;
  575. }
  576. }
  577. }
  578. else
  579. {
  580. while((!(info->castles[0]&(1<<faction))))
  581. {
  582. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  583. break;
  584. faction = ran()%F_NUMBER;
  585. }
  586. }
  587. int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  588. int cid = VLC->townh->towns[faction].basicCreatures[level];
  589. for(unsigned int i=0;i<VLC->objh->cregens.size();i++)
  590. if(VLC->objh->cregens[i]==cid)
  591. return std::pair<int,int>(17,i);
  592. tlog3 << "Cannot find a dwelling for creature "<< cid << std::endl;
  593. return std::pair<int,int>(17,0);
  594. delete dwl->info;
  595. dwl->info = NULL;
  596. }
  597. case 217:
  598. {
  599. int faction = ran()%F_NUMBER;
  600. CGDwelling * dwl = static_cast<CGDwelling*>(obj);
  601. CCreGenObjInfo* info = static_cast<CCreGenObjInfo*>(dwl->info);
  602. if (info->asCastle)
  603. {
  604. for(unsigned int i=0;i<map->objects.size();i++)
  605. {
  606. if(map->objects[i]->ID==77 && dynamic_cast<CGTownInstance*>(map->objects[i].get())->identifier == info->identifier)
  607. {
  608. randomizeObject(map->objects[i]); //we have to randomize the castle first
  609. faction = map->objects[i]->subID;
  610. break;
  611. }
  612. else if(map->objects[i]->ID==TOWNI_TYPE && dynamic_cast<CGTownInstance*>(map->objects[i].get())->identifier == info->identifier)
  613. {
  614. faction = map->objects[i]->subID;
  615. break;
  616. }
  617. }
  618. }
  619. else
  620. {
  621. while((!(info->castles[0]&(1<<faction))))
  622. {
  623. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  624. break;
  625. faction = ran()%F_NUMBER;
  626. }
  627. }
  628. int cid = VLC->townh->towns[faction].basicCreatures[obj->subID];
  629. for(unsigned int i=0;i<VLC->objh->cregens.size();i++)
  630. if(VLC->objh->cregens[i]==cid)
  631. return std::pair<int,int>(17,i);
  632. tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl;
  633. return std::pair<int,int>(17,0);
  634. delete dwl->info;
  635. dwl->info = NULL;
  636. }
  637. case 218:
  638. {
  639. CGDwelling * dwl = static_cast<CGDwelling*>(obj);
  640. CCreGen3ObjInfo* info = static_cast<CCreGen3ObjInfo*>(dwl->info);
  641. int level = ((info->maxLevel-info->minLevel) ? (ran()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  642. int cid = VLC->townh->towns[obj->subID].basicCreatures[level];
  643. for(unsigned int i=0;i<VLC->objh->cregens.size();i++)
  644. if(VLC->objh->cregens[i]==cid)
  645. return std::pair<int,int>(17,i);
  646. tlog3 << "Cannot find a dwelling for creature "<<cid <<std::endl;
  647. return std::pair<int,int>(17,0);
  648. delete dwl->info;
  649. dwl->info = NULL;
  650. }
  651. }
  652. return std::pair<int,int>(-1,-1);
  653. }
  654. void CGameState::randomizeObject(CGObjectInstance *cur)
  655. {
  656. std::pair<int,int> ran = pickObject(cur);
  657. if(ran.first<0 || ran.second<0) //this is not a random object, or we couldn't find anything
  658. {
  659. if(cur->ID==TOWNI_TYPE) //town - set def
  660. {
  661. CGTownInstance *t = dynamic_cast<CGTownInstance*>(cur);
  662. t->town = &VLC->townh->towns[t->subID];
  663. if(t->hasCapitol())
  664. t->defInfo = capitols[t->subID];
  665. else if(t->hasFort())
  666. t->defInfo = forts[t->subID];
  667. else
  668. t->defInfo = villages[t->subID];
  669. }
  670. return;
  671. }
  672. else if(ran.first==HEROI_TYPE)//special code for hero
  673. {
  674. CGHeroInstance *h = dynamic_cast<CGHeroInstance *>(cur);
  675. if(!h) {tlog2<<"Wrong random hero at "<<cur->pos<<std::endl; return;}
  676. cur->ID = ran.first;
  677. h->portrait = cur->subID = ran.second;
  678. h->type = VLC->heroh->heroes[ran.second];
  679. h->randomizeArmy(h->type->heroType/2);
  680. map->heroes.push_back(h);
  681. return; //TODO: maybe we should do something with definfo?
  682. }
  683. else if(ran.first==TOWNI_TYPE)//special code for town
  684. {
  685. CGTownInstance *t = dynamic_cast<CGTownInstance*>(cur);
  686. if(!t) {tlog2<<"Wrong random town at "<<cur->pos<<std::endl; return;}
  687. cur->ID = ran.first;
  688. cur->subID = ran.second;
  689. t->town = &VLC->townh->towns[ran.second];
  690. if(t->hasCapitol())
  691. t->defInfo = capitols[t->subID];
  692. else if(t->hasFort())
  693. t->defInfo = forts[t->subID];
  694. else
  695. t->defInfo = villages[t->subID];
  696. t->randomizeArmy(t->subID);
  697. map->towns.push_back(t);
  698. return;
  699. }
  700. //we have to replace normal random object
  701. cur->ID = ran.first;
  702. cur->subID = ran.second;
  703. map->removeBlockVisTiles(cur); //recalculate blockvis tiles - picked object might have different than random placeholder
  704. map->defy.push_back(cur->defInfo = VLC->dobjinfo->gobjs[ran.first][ran.second]);
  705. if(!cur->defInfo)
  706. {
  707. tlog1<<"*BIG* WARNING: Missing def declaration for "<<cur->ID<<" "<<cur->subID<<std::endl;
  708. return;
  709. }
  710. map->addBlockVisTiles(cur);
  711. }
  712. int CGameState::getDate(int mode) const
  713. {
  714. int temp;
  715. switch (mode)
  716. {
  717. case 0: //day number
  718. return day;
  719. break;
  720. case 1: //day of week
  721. temp = (day)%7; // 1 - Monday, 7 - Sunday
  722. if (temp)
  723. return temp;
  724. else return 7;
  725. break;
  726. case 2: //current week
  727. temp = ((day-1)/7)+1;
  728. if (!(temp%4))
  729. return 4;
  730. else
  731. return (temp%4);
  732. break;
  733. case 3: //current month
  734. return ((day-1)/28)+1;
  735. break;
  736. case 4: //day of month
  737. temp = (day)%28;
  738. if (temp)
  739. return temp;
  740. else return 28;
  741. break;
  742. }
  743. return 0;
  744. }
  745. CGameState::CGameState()
  746. {
  747. gs = this;
  748. mx = new boost::shared_mutex();
  749. applierGs = new CApplier<CBaseForGSApply>;
  750. registerTypes2(*applierGs);
  751. objCaller = new CObjectCallersHandler;
  752. globalEffects.setDescription("Global effects");
  753. }
  754. CGameState::~CGameState()
  755. {
  756. delete mx;
  757. map.dellNull();
  758. curB.dellNull();
  759. //delete scenarioOps; //TODO: fix for loading ind delete
  760. //delete initialOpts;
  761. delete applierGs;
  762. delete objCaller;
  763. //TODO: delete properly that definfos
  764. villages.clear();
  765. capitols.clear();
  766. }
  767. BattleInfo * CGameState::setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town)
  768. {
  769. int terrain = map->getTile(tile).tertype;
  770. int terType = battleGetBattlefieldType(tile);
  771. return BattleInfo::setupBattle(tile, terrain, terType, armies, heroes, creatureBank, town);
  772. }
  773. void CGameState::init( StartInfo * si, ui32 checksum, int Seed )
  774. {
  775. struct HLP
  776. {
  777. //it's assumed that given hero should receive the bonus
  778. static void giveCampaignBonusToHero(CGHeroInstance * hero, const StartInfo * si, const CScenarioTravel & st )
  779. {
  780. const CScenarioTravel::STravelBonus & curBonus = st.bonusesToChoose[si->choosenCampaignBonus];
  781. if(curBonus.isBonusForHero())
  782. {
  783. //apply bonus
  784. switch (curBonus.type)
  785. {
  786. case 0: //spell
  787. hero->spells.insert(curBonus.info2);
  788. break;
  789. case 1: //monster
  790. {
  791. for(int i=0; i<ARMY_SIZE; i++)
  792. {
  793. if(hero->slotEmpty(i))
  794. {
  795. hero->addToSlot(i, curBonus.info2, curBonus.info3);
  796. break;
  797. }
  798. }
  799. }
  800. break;
  801. case 3: //artifact
  802. hero->giveArtifact(curBonus.info2);
  803. break;
  804. case 4: //spell scroll
  805. {
  806. CArtifactInstance * scroll = CArtifactInstance::createScroll(VLC->spellh->spells[curBonus.info2]);
  807. scroll->putAt(hero, scroll->firstAvailableSlot(hero));
  808. }
  809. break;
  810. case 5: //prim skill
  811. {
  812. const ui8* ptr = reinterpret_cast<const ui8*>(&curBonus.info2);
  813. for (int g=0; g<PRIMARY_SKILLS; ++g)
  814. {
  815. int val = ptr[g];
  816. if (val == 0)
  817. {
  818. continue;
  819. }
  820. Bonus *bb = new Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::CAMPAIGN_BONUS, val, si->whichMapInCampaign, g);
  821. hero->addNewBonus(bb);
  822. }
  823. }
  824. break;
  825. case 6: //sec skills
  826. hero->setSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(curBonus.info2), curBonus.info3, true);
  827. break;
  828. }
  829. }
  830. }
  831. static std::vector<const PlayerSettings *> getHumanPlayerInfo(const StartInfo * si)
  832. {
  833. std::vector<const PlayerSettings *> ret;
  834. for(std::map<int, PlayerSettings>::const_iterator it = si->playerInfos.begin();
  835. it != si->playerInfos.end(); ++it)
  836. {
  837. if(it->second.human)
  838. ret.push_back(&it->second);
  839. }
  840. return ret;
  841. }
  842. static void replaceHero( CGameState * gs, int objId, CGHeroInstance * ghi )
  843. {
  844. ghi->id = objId;
  845. gs->map->objects[objId] = ghi;
  846. gs->map->heroes.push_back(ghi);
  847. }
  848. //sort in descending order by power
  849. static bool heroPowerSorter(const CGHeroInstance * a, const CGHeroInstance * b)
  850. {
  851. return a->getHeroStrength() > b->getHeroStrength();
  852. }
  853. };
  854. seed = Seed;
  855. ran.seed((boost::int32_t)seed);
  856. scenarioOps = new StartInfo(*si);
  857. initialOpts = new StartInfo(*si);
  858. si = NULL;
  859. switch(scenarioOps->mode)
  860. {
  861. case StartInfo::NEW_GAME:
  862. map = new Mapa(scenarioOps->mapname);
  863. break;
  864. case StartInfo::CAMPAIGN:
  865. {
  866. campaign = new CCampaignState();
  867. campaign->initNewCampaign(*scenarioOps);
  868. assert(vstd::contains(campaign->camp->mapPieces, scenarioOps->whichMapInCampaign));
  869. std::string &mapContent = campaign->camp->mapPieces[scenarioOps->whichMapInCampaign];
  870. map = new Mapa();
  871. map->initFromBytes((const unsigned char*)mapContent.c_str());
  872. }
  873. break;
  874. case StartInfo::DUEL:
  875. {
  876. bool success = false;
  877. DuelParameters dp;
  878. try
  879. {
  880. CLoadFile lf(scenarioOps->mapname);
  881. lf >> dp;
  882. success = true;
  883. }
  884. catch(...)
  885. {}
  886. const CArmedInstance *armies[2] = {0};
  887. const CGHeroInstance *heroes[2] = {0};
  888. CGTownInstance *town = NULL;
  889. for(int i = 0; i < 2; i++)
  890. {
  891. CArmedInstance *obj = NULL;
  892. if(dp.sides[i].heroId >= 0)
  893. {
  894. CGHeroInstance *h = new CGHeroInstance();
  895. armies[i] = heroes[i] = h;
  896. obj = h;
  897. h->subID = dp.sides[i].heroId;
  898. h->initHero(h->subID);
  899. }
  900. else
  901. {
  902. CGCreature *c = new CGCreature();
  903. armies[i] = obj = c;
  904. c->subID = 34;
  905. }
  906. obj->initObj();
  907. obj->setOwner(i);
  908. for(int j = 0; j < ARRAY_COUNT(dp.sides[i].stacks); j++)
  909. {
  910. int cre = dp.sides[i].stacks[j].type, count = dp.sides[i].stacks[j].count;
  911. if(count || obj->hasStackAtSlot(j))
  912. obj->setCreature(j, cre, count);
  913. }
  914. }
  915. curB = BattleInfo::setupBattle(int3(), dp.bfieldType, dp.terType, armies, heroes, false, town);
  916. curB->localInit();
  917. return;
  918. }
  919. break;
  920. default:
  921. tlog1 << "Wrong mode: " << (int)scenarioOps->mode << std::endl;
  922. return;
  923. }
  924. VLC->arth->initAllowedArtifactsList(map->allowedArtifact);
  925. tlog0 << "Map loaded!" << std::endl;
  926. //tlog0 <<"Reading and detecting map file (together): "<<tmh.getDif()<<std::endl;
  927. if(checksum)
  928. {
  929. tlog0 << "\tServer checksum for " << scenarioOps->mapname <<": "<< checksum << std::endl;
  930. tlog0 << "\tOur checksum for the map: "<< map->checksum << std::endl;
  931. if(map->checksum != checksum)
  932. {
  933. tlog1 << "Wrong map checksum!!!" << std::endl;
  934. throw std::string("Wrong checksum");
  935. }
  936. }
  937. day = 0;
  938. loadTownDInfos();
  939. //pick grail location
  940. if(map->grailPos.x < 0 || map->grailRadious) //grail not set or set within a radius around some place
  941. {
  942. if(!map->grailRadious) //radius not given -> anywhere on map
  943. map->grailRadious = map->width * 2;
  944. std::vector<int3> allowedPos;
  945. // add all not blocked tiles in range
  946. for (int i = 0; i < map->width ; i++)
  947. {
  948. for (int j = 0; j < map->height ; j++)
  949. {
  950. for (int k = 0; k <= map->twoLevel ; k++)
  951. {
  952. const TerrainTile &t = map->terrain[i][j][k];
  953. if(!t.blocked
  954. && !t.visitable
  955. && t.tertype != TerrainTile::water
  956. && t.tertype != TerrainTile::rock
  957. && map->grailPos.dist2d(int3(i,j,k)) <= map->grailRadious)
  958. allowedPos.push_back(int3(i,j,k));
  959. }
  960. }
  961. }
  962. //remove tiles with holes
  963. for(unsigned int no=0; no<map->objects.size(); ++no)
  964. if(map->objects[no]->ID == 124)
  965. allowedPos -= map->objects[no]->pos;
  966. if(allowedPos.size())
  967. map->grailPos = allowedPos[ran() % allowedPos.size()];
  968. else
  969. tlog2 << "Warning: Grail cannot be placed, no appropriate tile found!\n";
  970. }
  971. //picking random factions for players
  972. for(std::map<int, PlayerSettings>::iterator it = scenarioOps->playerInfos.begin();
  973. it != scenarioOps->playerInfos.end(); ++it)
  974. {
  975. if(it->second.castle==-1)
  976. {
  977. int f;
  978. do
  979. {
  980. f = ran()%F_NUMBER;
  981. }while(!(map->players[it->first].allowedFactions & 1<<f));
  982. it->second.castle = f;
  983. }
  984. }
  985. //randomizing objects
  986. BOOST_FOREACH(CGObjectInstance *obj, map->objects)
  987. {
  988. randomizeObject(obj);
  989. obj->hoverName = VLC->generaltexth->names[obj->ID];
  990. //handle Favouring Winds - mark tiles under it
  991. if(obj->ID == 225)
  992. for (int i = 0; i < obj->getWidth() ; i++)
  993. for (int j = 0; j < obj->getHeight() ; j++)
  994. {
  995. int3 pos = obj->pos - int3(i,j,0);
  996. if(map->isInTheMap(pos))
  997. map->getTile(pos).siodmyTajemniczyBajt |= 128;
  998. }
  999. }
  1000. //std::cout<<"\tRandomizing objects: "<<th.getDif()<<std::endl;
  1001. /*********creating players entries in gs****************************************/
  1002. for(std::map<int, PlayerSettings>::iterator it = scenarioOps->playerInfos.begin();
  1003. it != scenarioOps->playerInfos.end(); ++it)
  1004. {
  1005. std::pair<int,PlayerState> ins(it->first,PlayerState());
  1006. ins.second.color=ins.first;
  1007. ins.second.human = it->second.human;
  1008. ins.second.team = map->players[ins.first].team;
  1009. teams[ins.second.team].id = ins.second.team;//init team
  1010. teams[ins.second.team].players.insert(ins.first);//add player to team
  1011. players.insert(ins);
  1012. }
  1013. /*********give starting hero****************************************/
  1014. for(int i=0;i<PLAYER_LIMIT;i++)
  1015. {
  1016. const PlayerInfo &p = map->players[i];
  1017. bool generateHero = (p.generateHeroAtMainTown && p.hasMainTown);
  1018. if(generateHero && vstd::contains(scenarioOps->playerInfos, i))
  1019. {
  1020. int3 hpos = p.posOfMainTown;
  1021. hpos.x+=1;
  1022. int h = pickHero(i);
  1023. if(scenarioOps->playerInfos[i].hero == -1)
  1024. scenarioOps->playerInfos[i].hero = h;
  1025. CGHeroInstance * nnn = static_cast<CGHeroInstance*>(createObject(HEROI_TYPE,h,hpos,i));
  1026. nnn->id = map->objects.size();
  1027. nnn->initHero();
  1028. map->heroes.push_back(nnn);
  1029. map->objects.push_back(nnn);
  1030. map->addBlockVisTiles(nnn);
  1031. }
  1032. }
  1033. /*************************replace hero placeholders*****************************/
  1034. if (campaign)
  1035. {
  1036. CScenarioTravel::STravelBonus bonus =
  1037. campaign->camp->scenarios[scenarioOps->whichMapInCampaign].travelOptions.bonusesToChoose[scenarioOps->choosenCampaignBonus];
  1038. std::vector<CGHeroInstance *> Xheroes;
  1039. if (bonus.type == 8) //hero crossover
  1040. {
  1041. Xheroes = campaign->camp->scenarios[bonus.info2].crossoverHeroes;
  1042. }
  1043. //selecting heroes by type
  1044. for(int g=0; g<map->objects.size(); ++g)
  1045. {
  1046. CGObjectInstance * obj = map->objects[g];
  1047. if (obj->ID != 214) //not a placeholder
  1048. {
  1049. continue;
  1050. }
  1051. CGHeroPlaceholder * hp = static_cast<CGHeroPlaceholder*>(obj);
  1052. if(hp->subID != 0xFF) //select by type
  1053. {
  1054. bool found = false;
  1055. BOOST_FOREACH(CGHeroInstance * ghi, Xheroes)
  1056. {
  1057. if (ghi->subID == hp->subID)
  1058. {
  1059. found = true;
  1060. HLP::replaceHero(this, g, ghi);
  1061. Xheroes -= ghi;
  1062. break;
  1063. }
  1064. }
  1065. if (!found)
  1066. {
  1067. //TODO: create new hero of this type
  1068. }
  1069. }
  1070. }
  1071. //selecting heroes by power
  1072. std::sort(Xheroes.begin(), Xheroes.end(), HLP::heroPowerSorter);
  1073. for(int g=0; g<map->objects.size(); ++g)
  1074. {
  1075. CGObjectInstance * obj = map->objects[g];
  1076. if (obj->ID != 214) //not a placeholder
  1077. {
  1078. continue;
  1079. }
  1080. CGHeroPlaceholder * hp = static_cast<CGHeroPlaceholder*>(obj);
  1081. if (hp->subID == 0xFF) //select by power
  1082. {
  1083. if(Xheroes.size() > hp->power - 1)
  1084. HLP::replaceHero(this, g, Xheroes[hp->power - 1]);
  1085. else
  1086. tlog2 << "Warning, to hero to replace!\n";
  1087. //we don't have to remove hero from Xheroes because it would destroy the order and duplicates shouldn't happen
  1088. }
  1089. }
  1090. }
  1091. /******************RESOURCES****************************************************/
  1092. TResources startresAI, startresHuman;
  1093. const JsonNode config(DATA_DIR "/config/startres.json");
  1094. const JsonVector &vector = config["difficulty"].Vector();
  1095. const JsonNode &level = vector[scenarioOps->difficulty];
  1096. const JsonNode &human = level["human"];
  1097. const JsonNode &ai = level["ai"];
  1098. startresHuman[0] = human["wood"].Float();
  1099. startresHuman[1] = human["mercury"].Float();
  1100. startresHuman[2] = human["ore"].Float();
  1101. startresHuman[3] = human["sulfur"].Float();
  1102. startresHuman[4] = human["crystal"].Float();
  1103. startresHuman[5] = human["gems"].Float();
  1104. startresHuman[6] = human["gold"].Float();
  1105. startresHuman[7] = human["mithril"].Float();
  1106. startresAI[0] = ai["wood"].Float();
  1107. startresAI[1] = ai["mercury"].Float();
  1108. startresAI[2] = ai["ore"].Float();
  1109. startresAI[3] = ai["sulfur"].Float();
  1110. startresAI[4] = ai["crystal"].Float();
  1111. startresAI[5] = ai["gems"].Float();
  1112. startresAI[6] = ai["gold"].Float();
  1113. startresAI[7] = ai["mithril"].Float();
  1114. for (std::map<ui8,PlayerState>::iterator i = players.begin(); i!=players.end(); i++)
  1115. {
  1116. PlayerState &p = i->second;
  1117. if (p.human)
  1118. p.resources = startresHuman;
  1119. else
  1120. p.resources = startresAI;
  1121. }
  1122. //give start resource bonus in case of campaign
  1123. if (scenarioOps->mode == StartInfo::CAMPAIGN)
  1124. {
  1125. CScenarioTravel::STravelBonus chosenBonus =
  1126. campaign->camp->scenarios[scenarioOps->whichMapInCampaign].travelOptions.bonusesToChoose[scenarioOps->choosenCampaignBonus];
  1127. if(chosenBonus.type == 7) //resource
  1128. {
  1129. std::vector<const PlayerSettings *> people = HLP::getHumanPlayerInfo(scenarioOps); //players we will give resource bonus
  1130. BOOST_FOREACH(const PlayerSettings *ps, people)
  1131. {
  1132. std::vector<int> res; //resources we will give
  1133. switch (chosenBonus.info1)
  1134. {
  1135. case 0: case 1: case 2: case 3: case 4: case 5: case 6:
  1136. res.push_back(chosenBonus.info1);
  1137. break;
  1138. case 0xFD: //wood+ore
  1139. res.push_back(Res::WOOD); res.push_back(Res::ORE);
  1140. break;
  1141. case 0xFE: //rare
  1142. res.push_back(Res::MERCURY); res.push_back(Res::SULFUR); res.push_back(Res::CRYSTAL); res.push_back(Res::GEMS);
  1143. break;
  1144. default:
  1145. assert(0);
  1146. break;
  1147. }
  1148. //increasing resource quantity
  1149. for (int n=0; n<res.size(); ++n)
  1150. {
  1151. players[ps->color].resources[res[n]] += chosenBonus.info2;
  1152. }
  1153. }
  1154. }
  1155. }
  1156. /*************************HEROES************************************************/
  1157. std::set<int> hids; //hero ids to create pool
  1158. for(unsigned int i=0; i<map->allowedHeroes.size(); i++) //add to hids all allowed heroes
  1159. if(map->allowedHeroes[i])
  1160. hids.insert(i);
  1161. for (unsigned int i=0; i<map->heroes.size();i++) //heroes instances initialization
  1162. {
  1163. if (map->heroes[i]->getOwner()<0)
  1164. {
  1165. tlog2 << "Warning - hero with uninitialized owner!\n";
  1166. continue;
  1167. }
  1168. CGHeroInstance * vhi = (map->heroes[i]);
  1169. vhi->initHero();
  1170. players.find(vhi->getOwner())->second.heroes.push_back(vhi);
  1171. hids.erase(vhi->subID);
  1172. }
  1173. for (unsigned int i=0; i<map->objects.size();i++) //prisons
  1174. {
  1175. if (map->objects[i]->ID == 62)
  1176. hids.erase(map->objects[i]->subID);
  1177. }
  1178. for(unsigned int i=0; i<map->predefinedHeroes.size(); i++)
  1179. {
  1180. if(!vstd::contains(hids,map->predefinedHeroes[i]->subID))
  1181. continue;
  1182. map->predefinedHeroes[i]->initHero();
  1183. hpool.heroesPool[map->predefinedHeroes[i]->subID] = map->predefinedHeroes[i];
  1184. hpool.pavailable[map->predefinedHeroes[i]->subID] = 0xff;
  1185. hids.erase(map->predefinedHeroes[i]->subID);
  1186. }
  1187. BOOST_FOREACH(int hid, hids) //all not used allowed heroes go with default state into the pool
  1188. {
  1189. CGHeroInstance * vhi = new CGHeroInstance();
  1190. vhi->initHero(hid);
  1191. hpool.heroesPool[hid] = vhi;
  1192. hpool.pavailable[hid] = 0xff;
  1193. }
  1194. for(unsigned int i=0; i<map->disposedHeroes.size(); i++)
  1195. {
  1196. hpool.pavailable[map->disposedHeroes[i].ID] = map->disposedHeroes[i].players;
  1197. }
  1198. if (scenarioOps->mode == StartInfo::CAMPAIGN) //give campaign bonuses for specific / best hero
  1199. {
  1200. CScenarioTravel::STravelBonus chosenBonus =
  1201. campaign->camp->scenarios[scenarioOps->whichMapInCampaign].travelOptions.bonusesToChoose[scenarioOps->choosenCampaignBonus];
  1202. if (chosenBonus.isBonusForHero() && chosenBonus.info1 != 0xFFFE) //exclude generated heroes
  1203. {
  1204. //find human player
  1205. int humanPlayer;
  1206. for (std::map<ui8, PlayerState>::iterator it=players.begin(); it != players.end(); ++it)
  1207. {
  1208. if(it->second.human)
  1209. {
  1210. humanPlayer = it->first;
  1211. break;
  1212. }
  1213. }
  1214. std::vector<ConstTransitivePtr<CGHeroInstance> > & heroes = players[humanPlayer].heroes;
  1215. if (chosenBonus.info1 == 0xFFFD) //most powerful
  1216. {
  1217. int maxB = -1;
  1218. for (int b=0; b<heroes.size(); ++b)
  1219. {
  1220. if (maxB == -1 || heroes[b]->getTotalStrength() > heroes[maxB]->getTotalStrength())
  1221. {
  1222. maxB = b;
  1223. }
  1224. }
  1225. if(maxB < 0)
  1226. tlog2 << "Warning - cannot give bonus to hero cause there are no heroes!\n";
  1227. else
  1228. HLP::giveCampaignBonusToHero(heroes[maxB], scenarioOps, campaign->camp->scenarios[scenarioOps->whichMapInCampaign].travelOptions);
  1229. }
  1230. else //specific hero
  1231. {
  1232. for (int b=0; b<heroes.size(); ++b)
  1233. {
  1234. if (heroes[b]->subID == chosenBonus.info1)
  1235. {
  1236. HLP::giveCampaignBonusToHero(heroes[b], scenarioOps, campaign->camp->scenarios[scenarioOps->whichMapInCampaign].travelOptions);
  1237. break;
  1238. }
  1239. }
  1240. }
  1241. }
  1242. }
  1243. /*************************FOG**OF**WAR******************************************/
  1244. for(std::map<ui8, TeamState>::iterator k=teams.begin(); k!=teams.end(); ++k)
  1245. {
  1246. k->second.fogOfWarMap.resize(map->width);
  1247. for(int g=0; g<map->width; ++g)
  1248. k->second.fogOfWarMap[g].resize(map->height);
  1249. for(int g=-0; g<map->width; ++g)
  1250. for(int h=0; h<map->height; ++h)
  1251. k->second.fogOfWarMap[g][h].resize(map->twoLevel+1, 0);
  1252. for(int g=0; g<map->width; ++g)
  1253. for(int h=0; h<map->height; ++h)
  1254. for(int v=0; v<map->twoLevel+1; ++v)
  1255. k->second.fogOfWarMap[g][h][v] = 0;
  1256. BOOST_FOREACH(CGObjectInstance *obj, map->objects)
  1257. {
  1258. if( !vstd::contains(k->second.players, obj->tempOwner)) continue; //not a flagged object
  1259. boost::unordered_set<int3, ShashInt3> tiles;
  1260. obj->getSightTiles(tiles);
  1261. BOOST_FOREACH(int3 tile, tiles)
  1262. {
  1263. k->second.fogOfWarMap[tile.x][tile.y][tile.z] = 1;
  1264. }
  1265. }
  1266. }
  1267. for(std::map<ui8, PlayerState>::iterator k=players.begin(); k!=players.end(); ++k)
  1268. {
  1269. //starting bonus
  1270. if(scenarioOps->playerInfos[k->first].bonus==PlayerSettings::brandom)
  1271. scenarioOps->playerInfos[k->first].bonus = ran()%3;
  1272. switch(scenarioOps->playerInfos[k->first].bonus)
  1273. {
  1274. case PlayerSettings::bgold:
  1275. k->second.resources[Res::GOLD] += 500 + (ran()%6)*100;
  1276. break;
  1277. case PlayerSettings::bresource:
  1278. {
  1279. int res = VLC->townh->towns[scenarioOps->playerInfos[k->first].castle].primaryRes;
  1280. if(res == 127)
  1281. {
  1282. k->second.resources[Res::WOOD] += 5 + ran()%6;
  1283. k->second.resources[Res::ORE] += 5 + ran()%6;
  1284. }
  1285. else
  1286. {
  1287. k->second.resources[res] += 3 + ran()%4;
  1288. }
  1289. break;
  1290. }
  1291. case PlayerSettings::bartifact:
  1292. {
  1293. if(!k->second.heroes.size())
  1294. {
  1295. tlog5 << "Cannot give starting artifact - no heroes!" << std::endl;
  1296. break;
  1297. }
  1298. CArtifact *toGive;
  1299. toGive = VLC->arth->artifacts[VLC->arth->getRandomArt (CArtifact::ART_TREASURE)];
  1300. CGHeroInstance *hero = k->second.heroes[0];
  1301. hero->giveArtifact(toGive->id);
  1302. }
  1303. }
  1304. }
  1305. /****************************TOWNS************************************************/
  1306. for ( int i=0; i<4; i++)
  1307. CGTownInstance::universitySkills.push_back(14+i);//skills for university
  1308. for (unsigned int i=0;i<map->towns.size();i++)
  1309. {
  1310. CGTownInstance * vti =(map->towns[i]);
  1311. if(!vti->town)
  1312. vti->town = &VLC->townh->towns[vti->subID];
  1313. if (vti->name.length()==0) // if town hasn't name we draw it
  1314. vti->name = vti->town->Names()[ran()%vti->town->Names().size()];
  1315. //init buildings
  1316. if(vti->builtBuildings.find(-50)!=vti->builtBuildings.end()) //give standard set of buildings
  1317. {
  1318. vti->builtBuildings.erase(-50);
  1319. vti->builtBuildings.insert(10);
  1320. vti->builtBuildings.insert(5);
  1321. vti->builtBuildings.insert(30);
  1322. if(ran()%2)
  1323. vti->builtBuildings.insert(31);
  1324. }
  1325. if (vstd::contains(vti->builtBuildings,(6)) && vti->state()==2)
  1326. vti->builtBuildings.erase(6);//if we have harbor without water - erase it (this is H3 behaviour)
  1327. //init hordes
  1328. for (int i = 0; i<CREATURES_PER_TOWN; i++)
  1329. if (vstd::contains(vti->builtBuildings,(-31-i))) //if we have horde for this level
  1330. {
  1331. vti->builtBuildings.erase(-31-i);//remove old ID
  1332. if (vti->town->hordeLvl[0] == i)//if town first horde is this one
  1333. {
  1334. vti->builtBuildings.insert(18);//add it
  1335. if (vstd::contains(vti->builtBuildings,(37+i)))//if we have upgraded dwelling as well
  1336. vti->builtBuildings.insert(19);//add it as well
  1337. }
  1338. if (vti->town->hordeLvl[1] == i)//if town second horde is this one
  1339. {
  1340. vti->builtBuildings.insert(24);
  1341. if (vstd::contains(vti->builtBuildings,(37+i)))
  1342. vti->builtBuildings.insert(25);
  1343. }
  1344. }
  1345. //town events
  1346. BOOST_FOREACH(CCastleEvent *ev, vti->events)
  1347. {
  1348. for (int i = 0; i<CREATURES_PER_TOWN; i++)
  1349. if (vstd::contains(ev->buildings,(-31-i))) //if we have horde for this level
  1350. {
  1351. ev->buildings.erase(-31-i);
  1352. if (vti->town->hordeLvl[0] == i)
  1353. ev->buildings.insert(18);
  1354. if (vti->town->hordeLvl[1] == i)
  1355. ev->buildings.insert(24);
  1356. }
  1357. }
  1358. //init spells
  1359. vti->spells.resize(SPELL_LEVELS);
  1360. CSpell *s;
  1361. for(unsigned int z=0; z<vti->obligatorySpells.size();z++)
  1362. {
  1363. s = VLC->spellh->spells[vti->obligatorySpells[z]];
  1364. vti->spells[s->level-1].push_back(s->id);
  1365. vti->possibleSpells -= s->id;
  1366. }
  1367. while(vti->possibleSpells.size())
  1368. {
  1369. ui32 total=0;
  1370. int sel = -1;
  1371. for(unsigned int ps=0;ps<vti->possibleSpells.size();ps++)
  1372. total += VLC->spellh->spells[vti->possibleSpells[ps]]->probabilities[vti->subID];
  1373. int r = (total)? ran()%total : -1;
  1374. for(unsigned int ps=0; ps<vti->possibleSpells.size();ps++)
  1375. {
  1376. r -= VLC->spellh->spells[vti->possibleSpells[ps]]->probabilities[vti->subID];
  1377. if(r<0)
  1378. {
  1379. sel = ps;
  1380. break;
  1381. }
  1382. }
  1383. if(sel<0)
  1384. sel=0;
  1385. CSpell *s = VLC->spellh->spells[vti->possibleSpells[sel]];
  1386. vti->spells[s->level-1].push_back(s->id);
  1387. vti->possibleSpells -= s->id;
  1388. }
  1389. if(vti->getOwner() != 255)
  1390. getPlayer(vti->getOwner())->towns.push_back(vti);
  1391. }
  1392. //campaign bonuses for towns
  1393. if (scenarioOps->mode == StartInfo::CAMPAIGN)
  1394. {
  1395. CScenarioTravel::STravelBonus chosenBonus =
  1396. campaign->camp->scenarios[scenarioOps->whichMapInCampaign].travelOptions.bonusesToChoose[scenarioOps->choosenCampaignBonus];
  1397. if (chosenBonus.type == 2)
  1398. {
  1399. for (int g=0; g<map->towns.size(); ++g)
  1400. {
  1401. PlayerState * owner = getPlayer(map->towns[g]->getOwner());
  1402. PlayerInfo & pi = map->players[owner->color];
  1403. if (owner->human && //human-owned
  1404. map->towns[g]->pos == pi.posOfMainTown + int3(2, 0, 0))
  1405. {
  1406. map->towns[g]->builtBuildings.insert(
  1407. CBuildingHandler::campToERMU(chosenBonus.info1, map->towns[g]->town->typeID, map->towns[g]->builtBuildings));
  1408. break;
  1409. }
  1410. }
  1411. }
  1412. }
  1413. objCaller->preInit();
  1414. BOOST_FOREACH(CGObjectInstance *obj, map->objects)
  1415. {
  1416. obj->initObj();
  1417. if(obj->ID == 62) //prison also needs to initialize hero
  1418. static_cast<CGHeroInstance*>(obj)->initHero();
  1419. }
  1420. CGTeleport::postInit(); //pairing subterranean gates
  1421. buildBonusSystemTree();
  1422. for(std::map<ui8, PlayerState>::iterator k=players.begin(); k!=players.end(); ++k)
  1423. {
  1424. if(k->first==-1 || k->first==255)
  1425. continue;
  1426. //init visiting and garrisoned heroes
  1427. BOOST_FOREACH(CGHeroInstance *h, k->second.heroes)
  1428. {
  1429. BOOST_FOREACH(CGTownInstance *t, k->second.towns)
  1430. {
  1431. int3 vistile = t->pos; vistile.x--; //tile next to the entrance
  1432. if(vistile == h->pos || h->pos==t->pos)
  1433. {
  1434. t->setVisitingHero(h);
  1435. if(h->pos == t->pos) //visiting hero placed in the editor has same pos as the town - we need to correct it
  1436. {
  1437. map->removeBlockVisTiles(h);
  1438. h->pos.x -= 1;
  1439. map->addBlockVisTiles(h);
  1440. }
  1441. break;
  1442. }
  1443. }
  1444. }
  1445. }
  1446. map->checkForObjectives(); //needs to be run when all objects are properly placed
  1447. }
  1448. int CGameState::battleGetBattlefieldType(int3 tile)
  1449. {
  1450. if(tile==int3() && curB)
  1451. tile = curB->tile;
  1452. else if(tile==int3() && !curB)
  1453. return -1;
  1454. const TerrainTile &t = map->getTile(tile);
  1455. //fight in mine -> subterranean
  1456. if(dynamic_cast<const CGMine *>(t.visitableObjects.front()))
  1457. return 12;
  1458. const std::vector <ConstTransitivePtr<CGObjectInstance> > & objs = map->objects;
  1459. for(int g=0; g<objs.size(); ++g)
  1460. {
  1461. if( !objs[g] || objs[g]->pos.x - tile.x < 0 || objs[g]->pos.x - tile.x >= 8
  1462. || tile.y - objs[g]->pos.y + 5 < 0 || tile.y - objs[g]->pos.y + 5 >=6
  1463. || !objs[g]->coveringAt(objs[g]->pos.x - tile.x, tile.y - objs[g]->pos.y + 5)
  1464. ) //look only for objects covering given tile
  1465. continue;
  1466. switch(objs[g]->ID)
  1467. {
  1468. case 222: //clover field
  1469. return 19;
  1470. case 21: case 223: //cursed ground
  1471. return 22;
  1472. case 224: //evil fog
  1473. return 20;
  1474. case 225: //favourable winds
  1475. return 21;
  1476. case 226: //fiery fields
  1477. return 14;
  1478. case 227: //holy ground
  1479. return 18;
  1480. case 228: //lucid pools
  1481. return 17;
  1482. case 229: //magic clouds
  1483. return 16;
  1484. case 46: case 230: //magic plains
  1485. return 9;
  1486. case 231: //rocklands
  1487. return 15;
  1488. }
  1489. }
  1490. switch(t.tertype)
  1491. {
  1492. case TerrainTile::dirt:
  1493. return rand()%3+3;
  1494. case TerrainTile::sand:
  1495. return 2; //TODO: coast support
  1496. case TerrainTile::grass:
  1497. return rand()%2+6;
  1498. case TerrainTile::snow:
  1499. return rand()%2+10;
  1500. case TerrainTile::swamp:
  1501. return 13;
  1502. case TerrainTile::rough:
  1503. return 23;
  1504. case TerrainTile::subterranean:
  1505. return 12;
  1506. case TerrainTile::lava:
  1507. return 8;
  1508. case TerrainTile::water:
  1509. return 25;
  1510. case TerrainTile::rock:
  1511. return 15;
  1512. default:
  1513. return -1;
  1514. }
  1515. }
  1516. std::set<std::pair<int, int> > costDiff(const std::vector<ui32> &a, const std::vector<ui32> &b, const int modifier = 100) //modifer %
  1517. {
  1518. std::set<std::pair<int, int> > ret;
  1519. for(int j=0;j<RESOURCE_QUANTITY;j++)
  1520. {
  1521. assert(a[j] >= b[j]);
  1522. if(int dif = modifier * (a[j] - b[j]) / 100)
  1523. ret.insert(std::make_pair(j,dif));
  1524. }
  1525. return ret;
  1526. }
  1527. UpgradeInfo CGameState::getUpgradeInfo(const CStackInstance &stack)
  1528. {
  1529. UpgradeInfo ret;
  1530. const CCreature *base = stack.type;
  1531. const CGHeroInstance *h = stack.armyObj->ID == HEROI_TYPE ? static_cast<const CGHeroInstance*>(stack.armyObj) : NULL;
  1532. const CGTownInstance *t = NULL;
  1533. if(stack.armyObj->ID == TOWNI_TYPE)
  1534. t = static_cast<const CGTownInstance *>(stack.armyObj);
  1535. else if(h)
  1536. { //hero speciality
  1537. boost::shared_ptr<BonusList> lista = h->speciality.getBonuses(Selector::typeSubtype(Bonus::SPECIAL_UPGRADE, base->idNumber));
  1538. BOOST_FOREACH(const Bonus *it, *lista)
  1539. {
  1540. ui16 nid = it->additionalInfo;
  1541. if (nid != base->idNumber) //in very specific case the upgrade is available by default (?)
  1542. {
  1543. ret.newID.push_back(nid);
  1544. ret.cost.push_back(VLC->creh->creatures[nid]->cost - base->cost);
  1545. }
  1546. }
  1547. t = h->visitedTown;
  1548. }
  1549. if(t)
  1550. {
  1551. BOOST_FOREACH(si32 bid, t->builtBuildings)
  1552. {
  1553. if( bid >= 37 && bid < 44 ) //upgraded creature dwelling
  1554. {
  1555. int nid = t->town->upgradedCreatures[bid-37]; //upgrade offered by that building
  1556. if(vstd::contains(base->upgrades, nid)) //possible upgrade
  1557. {
  1558. ret.newID.push_back(nid);
  1559. ret.cost.push_back(VLC->creh->creatures[nid]->cost - base->cost);
  1560. }
  1561. }
  1562. }
  1563. }
  1564. //hero is visiting Hill Fort
  1565. if(h && map->getTile(h->visitablePos()).visitableObjects.front()->ID == 35)
  1566. {
  1567. static const int costModifiers[] = {0, 25, 50, 75, 100}; //we get cheaper upgrades depending on level
  1568. const int costModifier = costModifiers[std::min<int>(std::max((int)base->level - 1, 0), ARRAY_COUNT(costModifiers) - 1)];
  1569. BOOST_FOREACH(si32 nid, base->upgrades)
  1570. {
  1571. ret.newID.push_back(nid);
  1572. ret.cost.push_back((VLC->creh->creatures[nid]->cost - base->cost) * costModifier / 100);
  1573. }
  1574. }
  1575. if(ret.newID.size())
  1576. ret.oldID = base->idNumber;
  1577. return ret;
  1578. }
  1579. int CGameState::getPlayerRelations( ui8 color1, ui8 color2 )
  1580. {
  1581. if ( color1 == color2 )
  1582. return 2;
  1583. const TeamState * ts = getPlayerTeam(color1);
  1584. if (ts && vstd::contains(ts->players, color2))
  1585. return 1;
  1586. return 0;
  1587. }
  1588. void CGameState::loadTownDInfos()
  1589. {
  1590. for(int i=0;i<F_NUMBER;i++)
  1591. {
  1592. villages[i] = new CGDefInfo(*VLC->dobjinfo->castles[i]);
  1593. forts[i] = VLC->dobjinfo->castles[i];
  1594. map->defy.push_back(forts[i]);
  1595. capitols[i] = new CGDefInfo(*VLC->dobjinfo->castles[i]);
  1596. }
  1597. std::ifstream ifs(DATA_DIR "/config/townsDefs.txt");
  1598. int ccc;
  1599. ifs>>ccc;
  1600. for(int i=0; i < ccc*2; i++)
  1601. {
  1602. CGDefInfo *n;
  1603. if(i<ccc)
  1604. n = villages[i];
  1605. else
  1606. n = capitols[i%ccc];
  1607. ifs >> n->name;
  1608. map->defy.push_back(n);
  1609. }
  1610. }
  1611. void CGameState::getNeighbours(const TerrainTile &srct, int3 tile, std::vector<int3> &vec, const boost::logic::tribool &onLand, bool limitCoastSailing)
  1612. {
  1613. static int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0),
  1614. int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
  1615. vec.clear();
  1616. for (size_t i = 0; i < ARRAY_COUNT(dirs); i++)
  1617. {
  1618. const int3 hlp = tile + dirs[i];
  1619. if(!map->isInTheMap(hlp))
  1620. continue;
  1621. const TerrainTile &hlpt = map->getTile(hlp);
  1622. //we cannot visit things from blocked tiles
  1623. if(srct.blocked && !srct.visitable && hlpt.visitable && srct.blockingObjects.front()->ID != HEROI_TYPE)
  1624. {
  1625. continue;
  1626. }
  1627. if(srct.tertype == TerrainTile::water && limitCoastSailing && hlpt.tertype == TerrainTile::water && dirs[i].x && dirs[i].y) //diagonal move through water
  1628. {
  1629. int3 hlp1 = tile,
  1630. hlp2 = tile;
  1631. hlp1.x += dirs[i].x;
  1632. hlp2.y += dirs[i].y;
  1633. if(map->getTile(hlp1).tertype != TerrainTile::water || map->getTile(hlp2).tertype != TerrainTile::water)
  1634. continue;
  1635. }
  1636. if((indeterminate(onLand) || onLand == (hlpt.tertype!=TerrainTile::water) )
  1637. && hlpt.tertype != TerrainTile::rock)
  1638. {
  1639. vec.push_back(hlp);
  1640. }
  1641. }
  1642. }
  1643. int CGameState::getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, int remainingMovePoints, bool checkLast)
  1644. {
  1645. if(src == dest) //same tile
  1646. return 0;
  1647. TerrainTile &s = map->terrain[src.x][src.y][src.z],
  1648. &d = map->terrain[dest.x][dest.y][dest.z];
  1649. //get basic cost
  1650. int ret = h->getTileCost(d,s);
  1651. if(d.blocked && h->hasBonusOfType(Bonus::FLYING_MOVEMENT))
  1652. {
  1653. bool freeFlying = h->getBonusesCount(Selector::typeSubtype(Bonus::FLYING_MOVEMENT, 1)) > 0;
  1654. if(!freeFlying)
  1655. {
  1656. ret *= 1.4f; //40% penalty for movement over blocked tile
  1657. }
  1658. }
  1659. else if (d.tertype == TerrainTile::water)
  1660. {
  1661. if(h->boat && s.siodmyTajemniczyBajt & 128 && d.siodmyTajemniczyBajt & 128) //Favourable Winds
  1662. ret *= 0.666f;
  1663. else if (!h->boat && h->getBonusesCount(Selector::typeSubtype(Bonus::WATER_WALKING, 1)) > 0)
  1664. ret *= 1.4f; //40% penalty for water walking
  1665. }
  1666. if(src.x != dest.x && src.y != dest.y) //it's diagonal move
  1667. {
  1668. int old = ret;
  1669. ret *= 1.414213;
  1670. //diagonal move costs too much but normal move is possible - allow diagonal move for remaining move points
  1671. if(ret > remainingMovePoints && remainingMovePoints >= old)
  1672. {
  1673. return remainingMovePoints;
  1674. }
  1675. }
  1676. int left = remainingMovePoints-ret;
  1677. if(checkLast && left > 0 && remainingMovePoints-ret < 250) //it might be the last tile - if no further move possible we take all move points
  1678. {
  1679. std::vector<int3> vec;
  1680. getNeighbours(d, dest, vec, s.tertype != TerrainTile::water, true);
  1681. for(size_t i=0; i < vec.size(); i++)
  1682. {
  1683. int fcost = getMovementCost(h,dest,vec[i],left,false);
  1684. if(fcost <= left)
  1685. {
  1686. return ret;
  1687. }
  1688. }
  1689. ret = remainingMovePoints;
  1690. }
  1691. return ret;
  1692. }
  1693. void CGameState::apply(CPack *pack)
  1694. {
  1695. ui16 typ = typeList.getTypeID(pack);
  1696. applierGs->apps[typ]->applyOnGS(this,pack);
  1697. }
  1698. bool CGameState::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  1699. {
  1700. if(!map->isInTheMap(src) || !map->isInTheMap(dest)) //check input
  1701. return false;
  1702. int3 hpos = hero->getPosition(false);
  1703. bool flying = false; //hero is under flying effect TODO
  1704. bool waterWalking = false; //hero is on land and can walk on water TODO
  1705. bool onLand = map->getTile(hpos).tertype != TerrainTile::water;
  1706. // tribool blockLandSea; //true - blocks sea, false - blocks land, indeterminate - allows all
  1707. //
  1708. // if (!hero->canWalkOnSea())
  1709. // blockLandSea = (map->getTile(hpos).tertype != TerrainTile::water); //block land if hero is on water and vice versa
  1710. // else
  1711. // blockLandSea = boost::logic::indeterminate;
  1712. const std::vector<std::vector<std::vector<ui8> > > &FoW = getPlayerTeam(hero->tempOwner)->fogOfWarMap;
  1713. //graph initialization
  1714. std::vector< std::vector<CPathNode> > graph;
  1715. graph.resize(map->width);
  1716. for(size_t i=0; i<graph.size(); ++i)
  1717. {
  1718. graph[i].resize(map->height);
  1719. for(size_t j=0; j<graph[i].size(); ++j)
  1720. {
  1721. const TerrainTile *tinfo = &map->terrain[i][j][src.z];
  1722. CPathNode &node = graph[i][j];
  1723. node.accessible = !tinfo->blocked;
  1724. node.dist = -1;
  1725. node.theNodeBefore = NULL;
  1726. node.visited = false;
  1727. node.coord.x = i;
  1728. node.coord.y = j;
  1729. node.coord.z = dest.z;
  1730. if(!tinfo->entrableTerrain(onLand, flying || waterWalking)
  1731. || !FoW[i][j][src.z] //tile is covered by the FoW
  1732. )
  1733. {
  1734. node.accessible = false;
  1735. }
  1736. }
  1737. }
  1738. //Special rules for the destination tile
  1739. {
  1740. const TerrainTile *t = &map->terrain[dest.x][dest.y][dest.z];
  1741. CPathNode &d = graph[dest.x][dest.y];
  1742. //tile may be blocked by blockvis / normal vis obj but it still must be accessible
  1743. if(t->visitable)
  1744. {
  1745. d.accessible = true; //for allowing visiting objects
  1746. }
  1747. if(onLand && t->tertype == TerrainTile::water) //hero can walk only on land and dst lays on the water
  1748. {
  1749. size_t i = 0;
  1750. for(; i < t->visitableObjects.size(); i++)
  1751. if(t->visitableObjects[i]->ID == 8 || t->visitableObjects[i]->ID == HEROI_TYPE) //it's a Boat
  1752. break;
  1753. d.accessible = (i < t->visitableObjects.size()); //dest is accessible only if there is boat/hero
  1754. }
  1755. else if(!onLand && t->tertype != TerrainTile::water) //hero is moving by water
  1756. {
  1757. d.accessible = (t->siodmyTajemniczyBajt & 64) && !t->blocked; //tile is accessible if it's coastal and not blocked
  1758. }
  1759. }
  1760. //graph initialized
  1761. //initial tile - set cost on 0 and add to the queue
  1762. graph[src.x][src.y].dist = 0;
  1763. std::queue<CPathNode> mq;
  1764. mq.push(graph[src.x][src.y]);
  1765. ui32 curDist = 0xffffffff; //total cost of path - init with max possible val
  1766. std::vector<int3> neighbours;
  1767. neighbours.reserve(8);
  1768. while(!mq.empty())
  1769. {
  1770. CPathNode &cp = graph[mq.front().coord.x][mq.front().coord.y];
  1771. mq.pop();
  1772. if (cp.coord == dest) //it's destination tile
  1773. {
  1774. if (cp.dist < curDist) //that path is better than previous one
  1775. curDist = cp.dist;
  1776. continue;
  1777. }
  1778. else
  1779. {
  1780. if (cp.dist > curDist) //it's not dest and current length is greater than cost of already found path
  1781. continue;
  1782. }
  1783. //add accessible neighbouring nodes to the queue
  1784. getNeighbours(map->getTile(cp.coord), cp.coord, neighbours, boost::logic::indeterminate, true);
  1785. for(unsigned int i=0; i < neighbours.size(); i++)
  1786. {
  1787. CPathNode & dp = graph[neighbours[i].x][neighbours[i].y];
  1788. if(dp.accessible)
  1789. {
  1790. int cost = getMovementCost(hero,cp.coord,dp.coord,hero->movement - cp.dist);
  1791. if((dp.dist==-1 || (dp.dist > cp.dist + cost)) && dp.accessible && checkForVisitableDir(cp.coord, dp.coord) && checkForVisitableDir(dp.coord, cp.coord))
  1792. {
  1793. dp.dist = cp.dist + cost;
  1794. dp.theNodeBefore = &cp;
  1795. mq.push(dp);
  1796. }
  1797. }
  1798. }
  1799. }
  1800. CPathNode *curNode = &graph[dest.x][dest.y];
  1801. if(!curNode->theNodeBefore) //destination is not accessible
  1802. return false;
  1803. //fill ret with found path
  1804. ret.nodes.clear();
  1805. while(curNode->coord != graph[src.x][src.y].coord)
  1806. {
  1807. ret.nodes.push_back(*curNode);
  1808. curNode = curNode->theNodeBefore;
  1809. }
  1810. ret.nodes.push_back(graph[src.x][src.y]);
  1811. return true;
  1812. }
  1813. void CGameState::calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src, int movement)
  1814. {
  1815. assert(hero);
  1816. if(src.x < 0)
  1817. src = hero->getPosition(false);
  1818. if(movement < 0)
  1819. movement = hero->movement;
  1820. out.hero = hero;
  1821. out.hpos = src;
  1822. if(!map->isInTheMap(src)/* || !map->isInTheMap(dest)*/) //check input
  1823. {
  1824. tlog1 << "CGameState::calculatePaths: Hero outside the map? How dare you...\n";
  1825. return;
  1826. }
  1827. tribool onLand; //true - blocks sea, false - blocks land, indeterminate - allows all
  1828. if (!hero->canWalkOnSea())
  1829. onLand = (map->getTile(src).tertype != TerrainTile::water); //block land if hero is on water and vice versa
  1830. else
  1831. onLand = boost::logic::indeterminate;
  1832. const std::vector<std::vector<std::vector<ui8> > > &FoW = getPlayerTeam(hero->tempOwner)->fogOfWarMap;
  1833. bool flying = hero->hasBonusOfType(Bonus::FLYING_MOVEMENT);
  1834. bool waterWalk = hero->hasBonusOfType(Bonus::WATER_WALKING);
  1835. //graph initialization
  1836. CGPathNode ***graph = out.nodes;
  1837. for(size_t i=0; i < out.sizes.x; ++i)
  1838. {
  1839. for(size_t j=0; j < out.sizes.y; ++j)
  1840. {
  1841. for(size_t k=0; k < out.sizes.z; ++k)
  1842. {
  1843. const TerrainTile *tinfo = &map->terrain[i][j][k];
  1844. CGPathNode &node = graph[i][j][k];
  1845. node.accessible = (tinfo->blocked ? CGPathNode::FLYABLE : CGPathNode::ACCESSIBLE);
  1846. if(!flying && node.accessible == CGPathNode::FLYABLE)
  1847. {
  1848. node.accessible = CGPathNode::BLOCKED;
  1849. }
  1850. node.turns = 0xff;
  1851. node.moveRemains = 0;
  1852. node.coord.x = i;
  1853. node.coord.y = j;
  1854. node.coord.z = k;
  1855. node.land = tinfo->tertype != TerrainTile::water;
  1856. node.theNodeBefore = NULL;
  1857. bool leaveAsBlocked = false;
  1858. if((onLand || indeterminate(onLand)) && !node.land)//it's sea and we cannot walk on sea
  1859. {
  1860. if (waterWalk || flying)
  1861. {
  1862. node.accessible = CGPathNode::FLYABLE;
  1863. }
  1864. else
  1865. {
  1866. node.accessible = CGPathNode::BLOCKED;
  1867. leaveAsBlocked = true;
  1868. }
  1869. }
  1870. if ( tinfo->tertype == TerrainTile::rock//it's rock
  1871. || (!onLand && node.land) //it's land and we cannot walk on land (complementary condition is handled above)
  1872. || !FoW[i][j][k] //tile is covered by the FoW
  1873. || leaveAsBlocked
  1874. )
  1875. {
  1876. node.accessible = CGPathNode::BLOCKED;
  1877. }
  1878. else if(tinfo->visitable)
  1879. {
  1880. //hero is protected in Sanctuary
  1881. if(tinfo->visitableObjects.front()->ID == 80 && tinfo->visitableObjects.back()->ID == HEROI_TYPE && tinfo->visitableObjects.back()->tempOwner != hero->tempOwner)
  1882. node.accessible = CGPathNode::BLOCKED;
  1883. else
  1884. {
  1885. for(size_t ii = 0; ii < tinfo->visitableObjects.size(); ii++)
  1886. {
  1887. const CGObjectInstance * const obj = tinfo->visitableObjects[ii];
  1888. if(obj->getPassableness() & 1<<hero->tempOwner) //special object instance specific passableness flag - overwrites other accessibility flags
  1889. {
  1890. node.accessible = CGPathNode::ACCESSIBLE;
  1891. }
  1892. else if(obj->blockVisit)
  1893. {
  1894. node.accessible = CGPathNode::BLOCKVIS;
  1895. break;
  1896. }
  1897. else if(obj->ID != EVENTI_TYPE) //pathfinder should ignore placed events
  1898. {
  1899. node.accessible = CGPathNode::VISITABLE;
  1900. }
  1901. }
  1902. }
  1903. }
  1904. else if (map->isInTheMap(guardingCreaturePosition(int3(i, j, k)))
  1905. && tinfo->blockingObjects.size() == 0)
  1906. {
  1907. // Monster close by; blocked visit for battle.
  1908. node.accessible = CGPathNode::BLOCKVIS;
  1909. }
  1910. if(onLand && !node.land) //hero can walk only on land and tile lays on the water
  1911. {
  1912. size_t i = 0;
  1913. for(; i < tinfo->visitableObjects.size(); i++)
  1914. if(tinfo->visitableObjects[i]->ID == 8 || tinfo->visitableObjects[i]->ID == HEROI_TYPE) //it's a Boat
  1915. break;
  1916. if(i < tinfo->visitableObjects.size())
  1917. node.accessible = CGPathNode::BLOCKVIS; //dest is accessible only if there is boat/hero
  1918. }
  1919. else if(!onLand && tinfo->tertype != TerrainTile::water) //hero is moving by water
  1920. {
  1921. if((tinfo->siodmyTajemniczyBajt & 64) && !tinfo->blocked)
  1922. node.accessible = CGPathNode::VISITABLE; //tile is accessible if it's coastal and not blocked
  1923. }
  1924. }
  1925. }
  1926. }
  1927. //graph initialized
  1928. //initial tile - set cost on 0 and add to the queue
  1929. graph[src.x][src.y][src.z].turns = 0;
  1930. graph[src.x][src.y][src.z].moveRemains = movement;
  1931. std::queue<CGPathNode*> mq;
  1932. mq.push(&graph[src.x][src.y][src.z]);
  1933. //ui32 curDist = 0xffffffff; //total cost of path - init with max possible val
  1934. std::vector<int3> neighbours;
  1935. neighbours.reserve(8);
  1936. while(!mq.empty())
  1937. {
  1938. CGPathNode *cp = mq.front();
  1939. mq.pop();
  1940. const int3 guardPosition = guardingCreaturePosition(cp->coord);
  1941. const bool guardedPosition = (guardPosition != int3(-1, -1, -1) && cp->coord != src);
  1942. const TerrainTile &ct = map->getTile(cp->coord);
  1943. int movement = cp->moveRemains, turn = cp->turns;
  1944. if(!movement)
  1945. {
  1946. movement = hero->maxMovePoints(ct.tertype != TerrainTile::water);
  1947. turn++;
  1948. }
  1949. //add accessible neighbouring nodes to the queue
  1950. getNeighbours(ct, cp->coord, neighbours, boost::logic::indeterminate, !onLand);
  1951. for(unsigned int i=0; i < neighbours.size(); i++)
  1952. {
  1953. int moveAtNextTile = movement;
  1954. int turnAtNextTile = turn;
  1955. const int3 &n = neighbours[i]; //current neighbor
  1956. CGPathNode & dp = graph[n.x][n.y][n.z];
  1957. if( !checkForVisitableDir(cp->coord, dp.coord)
  1958. || !checkForVisitableDir(dp.coord, cp->coord)
  1959. || dp.accessible == CGPathNode::BLOCKED )
  1960. {
  1961. continue;
  1962. }
  1963. int cost = getMovementCost(hero, cp->coord, dp.coord, movement);
  1964. int remains = movement - cost;
  1965. if(remains < 0)
  1966. {
  1967. //occurs rarely, when hero with low movepoints tries to go leave the road
  1968. turnAtNextTile++;
  1969. moveAtNextTile = hero->maxMovePoints(ct.tertype != TerrainTile::water);
  1970. cost = getMovementCost(hero, cp->coord, dp.coord, moveAtNextTile); //cost must be updated, movement points changed :(
  1971. remains = moveAtNextTile - cost;
  1972. }
  1973. const bool neighborIsGuard = guardingCreaturePosition(cp->coord) == dp.coord;
  1974. if((dp.turns==0xff //we haven't been here before
  1975. || dp.turns > turnAtNextTile
  1976. || (dp.turns >= turnAtNextTile && dp.moveRemains < remains)) //this route is faster
  1977. && (!guardedPosition || neighborIsGuard)) // Can step into tile of guard
  1978. {
  1979. assert(&dp != cp->theNodeBefore); //two tiles can't point to each other
  1980. dp.moveRemains = remains;
  1981. dp.turns = turnAtNextTile;
  1982. dp.theNodeBefore = cp;
  1983. const bool guardedNeighbor = guardingCreaturePosition(dp.coord) != int3(-1, -1, -1);
  1984. //const bool positionIsGuard = guardingCreaturePosition(cp->coord) == cp->coord; //can this be true? hero never passes from monster tile...
  1985. if (dp.accessible == CGPathNode::ACCESSIBLE || dp.accessible == CGPathNode::FLYABLE
  1986. || (guardedNeighbor && !guardedPosition)) // Can step into a hostile tile once.
  1987. {
  1988. mq.push(&dp);
  1989. }
  1990. }
  1991. } //neighbours loop
  1992. } //queue loop
  1993. }
  1994. /**
  1995. * Tells if the tile is guarded by a monster as well as the position
  1996. * of the monster that will attack on it.
  1997. *
  1998. * @return int3(-1, -1, -1) if the tile is unguarded, or the position of
  1999. * the monster guarding the tile.
  2000. */
  2001. int3 CGameState::guardingCreaturePosition (int3 pos) const
  2002. {
  2003. const int3 originalPos = pos;
  2004. // Give monster at position priority.
  2005. if (!map->isInTheMap(pos))
  2006. return int3(-1, -1, -1);
  2007. const TerrainTile &posTile = map->terrain[pos.x][pos.y][pos.z];
  2008. if (posTile.visitable)
  2009. {
  2010. BOOST_FOREACH (CGObjectInstance* obj, posTile.visitableObjects)
  2011. {
  2012. if(obj->blockVisit)
  2013. {
  2014. if (obj->ID == 54) // Monster
  2015. return pos;
  2016. else
  2017. return int3(-1, -1, -1); //blockvis objects are not guarded by neighbouring creatures
  2018. }
  2019. }
  2020. }
  2021. // See if there are any monsters adjacent.
  2022. pos -= int3(1, 1, 0); // Start with top left.
  2023. for (int dx = 0; dx < 3; dx++)
  2024. {
  2025. for (int dy = 0; dy < 3; dy++)
  2026. {
  2027. if (map->isInTheMap(pos))
  2028. {
  2029. TerrainTile &tile = map->terrain[pos.x][pos.y][pos.z];
  2030. if (tile.visitable && (tile.tertype == TerrainTile::water) == (posTile.tertype == TerrainTile::water))
  2031. {
  2032. BOOST_FOREACH (CGObjectInstance* obj, tile.visitableObjects)
  2033. {
  2034. if (obj->ID == 54 && checkForVisitableDir(pos, &map->getTile(originalPos), originalPos)) // Monster being able to attack investigated tile
  2035. {
  2036. return pos;
  2037. }
  2038. }
  2039. }
  2040. }
  2041. pos.y++;
  2042. }
  2043. pos.y -= 3;
  2044. pos.x++;
  2045. }
  2046. return int3(-1, -1, -1);
  2047. }
  2048. bool CGameState::isVisible(int3 pos, int player)
  2049. {
  2050. if(player == 255) //neutral player
  2051. return false;
  2052. return getPlayerTeam(player)->fogOfWarMap[pos.x][pos.y][pos.z];
  2053. }
  2054. bool CGameState::isVisible( const CGObjectInstance *obj, int player )
  2055. {
  2056. if(player == -1)
  2057. return true;
  2058. if(player == 255) //neutral player -> TODO ??? needed?
  2059. return false;
  2060. //object is visible when at least one blocked tile is visible
  2061. for(int fx=0; fx<8; ++fx)
  2062. {
  2063. for(int fy=0; fy<6; ++fy)
  2064. {
  2065. int3 pos = obj->pos + int3(fx-7,fy-5,0);
  2066. if(map->isInTheMap(pos)
  2067. && !((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1)
  2068. && isVisible(pos, player) )
  2069. return true;
  2070. }
  2071. }
  2072. return false;
  2073. }
  2074. bool CGameState::checkForVisitableDir(const int3 & src, const int3 & dst) const
  2075. {
  2076. const TerrainTile * pom = &map->getTile(dst);
  2077. return checkForVisitableDir(src, pom, dst);
  2078. }
  2079. bool CGameState::checkForVisitableDir( const int3 & src, const TerrainTile *pom, const int3 & dst ) const
  2080. {
  2081. for(unsigned int b=0; b<pom->visitableObjects.size(); ++b) //checking destination tile
  2082. {
  2083. if(!vstd::contains(pom->blockingObjects, pom->visitableObjects[b])) //this visitable object is not blocking, ignore
  2084. continue;
  2085. CGDefInfo * di = pom->visitableObjects[b]->defInfo;
  2086. if( (dst.x == src.x-1 && dst.y == src.y-1) && !(di->visitDir & (1<<4)) )
  2087. {
  2088. return false;
  2089. }
  2090. if( (dst.x == src.x && dst.y == src.y-1) && !(di->visitDir & (1<<5)) )
  2091. {
  2092. return false;
  2093. }
  2094. if( (dst.x == src.x+1 && dst.y == src.y-1) && !(di->visitDir & (1<<6)) )
  2095. {
  2096. return false;
  2097. }
  2098. if( (dst.x == src.x+1 && dst.y == src.y) && !(di->visitDir & (1<<7)) )
  2099. {
  2100. return false;
  2101. }
  2102. if( (dst.x == src.x+1 && dst.y == src.y+1) && !(di->visitDir & (1<<0)) )
  2103. {
  2104. return false;
  2105. }
  2106. if( (dst.x == src.x && dst.y == src.y+1) && !(di->visitDir & (1<<1)) )
  2107. {
  2108. return false;
  2109. }
  2110. if( (dst.x == src.x-1 && dst.y == src.y+1) && !(di->visitDir & (1<<2)) )
  2111. {
  2112. return false;
  2113. }
  2114. if( (dst.x == src.x-1 && dst.y == src.y) && !(di->visitDir & (1<<3)) )
  2115. {
  2116. return false;
  2117. }
  2118. }
  2119. return true;
  2120. }
  2121. int CGameState::victoryCheck( ui8 player ) const
  2122. {
  2123. const PlayerState *p = CGameInfoCallback::getPlayer(player);
  2124. if(map->victoryCondition.condition == winStandard || map->victoryCondition.allowNormalVictory)
  2125. if(player == checkForStandardWin())
  2126. return -1;
  2127. if (p->enteredWinningCheatCode)
  2128. { //cheater or tester, but has entered the code...
  2129. return 1;
  2130. }
  2131. if(p->human || map->victoryCondition.appliesToAI)
  2132. {
  2133. switch(map->victoryCondition.condition)
  2134. {
  2135. case artifact:
  2136. //check if any hero has winning artifact
  2137. for(size_t i = 0; i < p->heroes.size(); i++)
  2138. if(p->heroes[i]->hasArt(map->victoryCondition.ID))
  2139. return 1;
  2140. break;
  2141. case gatherTroop:
  2142. {
  2143. //check if in players armies there is enough creatures
  2144. int total = 0; //creature counter
  2145. for(size_t i = 0; i < map->objects.size(); i++)
  2146. {
  2147. const CArmedInstance *ai = NULL;
  2148. if(map->objects[i]
  2149. && map->objects[i]->tempOwner == player //object controlled by player
  2150. && (ai = dynamic_cast<const CArmedInstance*>(map->objects[i].get()))) //contains army
  2151. {
  2152. for(TSlots::const_iterator i=ai->Slots().begin(); i!=ai->Slots().end(); ++i) //iterate through army
  2153. if(i->second->type->idNumber == map->victoryCondition.ID) //it's searched creature
  2154. total += i->second->count;
  2155. }
  2156. }
  2157. if(total >= map->victoryCondition.count)
  2158. return 1;
  2159. }
  2160. break;
  2161. case gatherResource:
  2162. if(p->resources[map->victoryCondition.ID] >= map->victoryCondition.count)
  2163. return 1;
  2164. break;
  2165. case buildCity:
  2166. {
  2167. const CGTownInstance *t = static_cast<const CGTownInstance *>(map->victoryCondition.obj);
  2168. if(t->tempOwner == player && t->fortLevel()-1 >= map->victoryCondition.ID && t->hallLevel()-1 >= map->victoryCondition.count)
  2169. return 1;
  2170. }
  2171. break;
  2172. case buildGrail:
  2173. BOOST_FOREACH(const CGTownInstance *t, map->towns)
  2174. if((t == map->victoryCondition.obj || !map->victoryCondition.obj)
  2175. && t->tempOwner == player
  2176. && vstd::contains(t->builtBuildings, 26))
  2177. return 1;
  2178. break;
  2179. case beatHero:
  2180. if(map->victoryCondition.obj->tempOwner >= PLAYER_LIMIT) //target hero not present on map
  2181. return 1;
  2182. break;
  2183. case captureCity:
  2184. {
  2185. if(map->victoryCondition.obj->tempOwner == player)
  2186. return 1;
  2187. }
  2188. break;
  2189. case beatMonster:
  2190. if(!map->objects[map->victoryCondition.obj->id]) //target monster not present on map
  2191. return 1;
  2192. break;
  2193. case takeDwellings:
  2194. for(size_t i = 0; i < map->objects.size(); i++)
  2195. {
  2196. if(map->objects[i] && map->objects[i]->tempOwner != player) //check not flagged objs
  2197. {
  2198. switch(map->objects[i]->ID)
  2199. {
  2200. case 17: case 18: case 19: case 20: //dwellings
  2201. case 216: case 217: case 218:
  2202. return 0; //found not flagged dwelling - player not won
  2203. }
  2204. }
  2205. }
  2206. return 1;
  2207. break;
  2208. case takeMines:
  2209. for(size_t i = 0; i < map->objects.size(); i++)
  2210. {
  2211. if(map->objects[i] && map->objects[i]->tempOwner != player) //check not flagged objs
  2212. {
  2213. switch(map->objects[i]->ID)
  2214. {
  2215. case 53: case 220:
  2216. return 0; //found not flagged mine - player not won
  2217. }
  2218. }
  2219. }
  2220. return 1;
  2221. break;
  2222. case transportItem:
  2223. {
  2224. const CGTownInstance *t = static_cast<const CGTownInstance *>(map->victoryCondition.obj);
  2225. if((t->visitingHero && t->visitingHero->hasArt(map->victoryCondition.ID))
  2226. || (t->garrisonHero && t->garrisonHero->hasArt(map->victoryCondition.ID)))
  2227. {
  2228. return 1;
  2229. }
  2230. }
  2231. break;
  2232. }
  2233. }
  2234. return 0;
  2235. }
  2236. ui8 CGameState::checkForStandardWin() const
  2237. {
  2238. //std victory condition is:
  2239. //all enemies lost
  2240. ui8 supposedWinner = 255, winnerTeam = 255;
  2241. for(std::map<ui8,PlayerState>::const_iterator i = players.begin(); i != players.end(); i++)
  2242. {
  2243. if(i->second.status == PlayerState::INGAME && i->first < PLAYER_LIMIT)
  2244. {
  2245. if(supposedWinner == 255)
  2246. {
  2247. //first player remaining ingame - candidate for victory
  2248. supposedWinner = i->second.color;
  2249. winnerTeam = i->second.team;
  2250. }
  2251. else if(winnerTeam != i->second.team)
  2252. {
  2253. //current candidate has enemy remaining in game -> no vicotry
  2254. return 255;
  2255. }
  2256. }
  2257. }
  2258. return supposedWinner;
  2259. }
  2260. bool CGameState::checkForStandardLoss( ui8 player ) const
  2261. {
  2262. //std loss condition is: player lost all towns and heroes
  2263. const PlayerState &p = *CGameInfoCallback::getPlayer(player);
  2264. return !p.heroes.size() && !p.towns.size();
  2265. }
  2266. struct statsHLP
  2267. {
  2268. typedef std::pair< ui8, si64 > TStat;
  2269. //converts [<player's color, value>] to vec[place] -> platers
  2270. static std::vector< std::list< ui8 > > getRank( std::vector<TStat> stats )
  2271. {
  2272. std::sort(stats.begin(), stats.end(), statsHLP());
  2273. //put first element
  2274. std::vector< std::list<ui8> > ret;
  2275. std::list<ui8> tmp;
  2276. tmp.push_back( stats[0].first );
  2277. ret.push_back( tmp );
  2278. //the rest of elements
  2279. for(int g=1; g<stats.size(); ++g)
  2280. {
  2281. if(stats[g].second == stats[g-1].second)
  2282. {
  2283. (ret.end()-1)->push_back( stats[g].first );
  2284. }
  2285. else
  2286. {
  2287. //create next occupied rank
  2288. std::list<ui8> tmp;
  2289. tmp.push_back(stats[g].first);
  2290. ret.push_back(tmp);
  2291. }
  2292. }
  2293. return ret;
  2294. }
  2295. bool operator()(const TStat & a, const TStat & b) const
  2296. {
  2297. return a.second > b.second;
  2298. }
  2299. static const CGHeroInstance * findBestHero(CGameState * gs, int color)
  2300. {
  2301. std::vector<ConstTransitivePtr<CGHeroInstance> > &h = gs->players[color].heroes;
  2302. if(!h.size())
  2303. return NULL;
  2304. //best hero will be that with highest exp
  2305. int best = 0;
  2306. for(int b=1; b<h.size(); ++b)
  2307. {
  2308. if(h[b]->exp > h[best]->exp)
  2309. {
  2310. best = b;
  2311. }
  2312. }
  2313. return h[best];
  2314. }
  2315. //calculates total number of artifacts that belong to given player
  2316. static int getNumberOfArts(const PlayerState * ps)
  2317. {
  2318. int ret = 0;
  2319. for(int g=0; g<ps->heroes.size(); ++g)
  2320. {
  2321. ret += ps->heroes[g]->artifactsInBackpack.size() + ps->heroes[g]->artifactsWorn.size();
  2322. }
  2323. return ret;
  2324. }
  2325. };
  2326. void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level)
  2327. {
  2328. #define FILL_FIELD(FIELD, VAL_GETTER) \
  2329. { \
  2330. std::vector< std::pair< ui8, si64 > > stats; \
  2331. for(std::map<ui8, PlayerState>::const_iterator g = players.begin(); g != players.end(); ++g) \
  2332. { \
  2333. if(g->second.color == 255) \
  2334. continue; \
  2335. std::pair< ui8, si64 > stat; \
  2336. stat.first = g->second.color; \
  2337. stat.second = VAL_GETTER; \
  2338. stats.push_back(stat); \
  2339. } \
  2340. tgi.FIELD = statsHLP::getRank(stats); \
  2341. }
  2342. for(std::map<ui8, PlayerState>::const_iterator g = players.begin(); g != players.end(); ++g)
  2343. {
  2344. if(g->second.color != 255)
  2345. tgi.playerColors.push_back(g->second.color);
  2346. }
  2347. if(level >= 1) //num of towns & num of heroes
  2348. {
  2349. //num of towns
  2350. FILL_FIELD(numOfTowns, g->second.towns.size())
  2351. //num of heroes
  2352. FILL_FIELD(numOfHeroes, g->second.heroes.size())
  2353. //best hero's portrait
  2354. for(std::map<ui8, PlayerState>::const_iterator g = players.begin(); g != players.end(); ++g)
  2355. {
  2356. if(g->second.color == 255)
  2357. continue;
  2358. const CGHeroInstance * best = statsHLP::findBestHero(this, g->second.color);
  2359. InfoAboutHero iah;
  2360. iah.initFromHero(best, level >= 8);
  2361. iah.army.clear();
  2362. tgi.colorToBestHero[g->second.color] = iah;
  2363. }
  2364. }
  2365. if(level >= 2) //gold
  2366. {
  2367. FILL_FIELD(gold, g->second.resources[Res::GOLD])
  2368. }
  2369. if(level >= 2) //wood & ore
  2370. {
  2371. FILL_FIELD(woodOre, g->second.resources[Res::WOOD] + g->second.resources[Res::ORE])
  2372. }
  2373. if(level >= 3) //mercury, sulfur, crystal, gems
  2374. {
  2375. FILL_FIELD(mercSulfCrystGems, g->second.resources[Res::MERCURY] + g->second.resources[Res::SULFUR] + g->second.resources[Res::CRYSTAL] + g->second.resources[Res::GEMS])
  2376. }
  2377. if(level >= 4) //obelisks found
  2378. {
  2379. //TODO
  2380. }
  2381. if(level >= 5) //artifacts
  2382. {
  2383. FILL_FIELD(artifacts, statsHLP::getNumberOfArts(&g->second))
  2384. }
  2385. if(level >= 6) //army strength
  2386. {
  2387. //TODO
  2388. }
  2389. if(level >= 7) //income
  2390. {
  2391. //TODO
  2392. }
  2393. if(level >= 8) //best hero's stats
  2394. {
  2395. //already set in lvl 1 handling
  2396. }
  2397. if(level >= 9) //personality
  2398. {
  2399. for(std::map<ui8, PlayerState>::const_iterator g = players.begin(); g != players.end(); ++g)
  2400. {
  2401. if(g->second.color == 255) //do nothing for neutral player
  2402. continue;
  2403. if(g->second.human)
  2404. {
  2405. tgi.personality[g->second.color] = -1;
  2406. }
  2407. else //AI
  2408. {
  2409. tgi.personality[g->second.color] = map->players[g->second.color].AITactic;
  2410. }
  2411. }
  2412. }
  2413. if(level >= 10) //best creature
  2414. {
  2415. //best creatures belonging to player (highest AI value)
  2416. for(std::map<ui8, PlayerState>::const_iterator g = players.begin(); g != players.end(); ++g)
  2417. {
  2418. if(g->second.color == 255) //do nothing for neutral player
  2419. continue;
  2420. int bestCre = -1; //best creature's ID
  2421. for(int b=0; b<g->second.heroes.size(); ++b)
  2422. {
  2423. for(TSlots::const_iterator it = g->second.heroes[b]->Slots().begin(); it != g->second.heroes[b]->Slots().end(); ++it)
  2424. {
  2425. int toCmp = it->second->type->idNumber; //ID of creature we should compare with the best one
  2426. if(bestCre == -1 || VLC->creh->creatures[bestCre]->AIValue < VLC->creh->creatures[toCmp]->AIValue)
  2427. {
  2428. bestCre = toCmp;
  2429. }
  2430. }
  2431. }
  2432. tgi.bestCreature[g->second.color] = bestCre;
  2433. }
  2434. }
  2435. #undef FILL_FIELD
  2436. }
  2437. int CGameState::lossCheck( ui8 player ) const
  2438. {
  2439. const PlayerState *p = CGameInfoCallback::getPlayer(player);
  2440. //if(map->lossCondition.typeOfLossCon == lossStandard)
  2441. if(checkForStandardLoss(player))
  2442. return -1;
  2443. if (p->enteredLosingCheatCode)
  2444. {
  2445. return 1;
  2446. }
  2447. if(p->human) //special loss condition applies only to human player
  2448. {
  2449. switch(map->lossCondition.typeOfLossCon)
  2450. {
  2451. case lossCastle:
  2452. {
  2453. const CGTownInstance *t = dynamic_cast<const CGTownInstance *>(map->lossCondition.obj);
  2454. assert(t);
  2455. if(t->tempOwner != player)
  2456. return 1;
  2457. }
  2458. break;
  2459. case lossHero:
  2460. {
  2461. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(map->lossCondition.obj);
  2462. assert(h);
  2463. if(h->tempOwner != player)
  2464. return 1;
  2465. }
  2466. break;
  2467. case timeExpires:
  2468. if(map->lossCondition.timeLimit < day)
  2469. return 1;
  2470. break;
  2471. }
  2472. }
  2473. if(!p->towns.size() && p->daysWithoutCastle >= 7)
  2474. return 2;
  2475. return false;
  2476. }
  2477. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > CGameState::unusedHeroesFromPool()
  2478. {
  2479. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > pool = hpool.heroesPool;
  2480. for ( std::map<ui8, PlayerState>::iterator i = players.begin() ; i != players.end();i++)
  2481. for(std::vector< ConstTransitivePtr<CGHeroInstance> >::iterator j = i->second.availableHeroes.begin(); j != i->second.availableHeroes.end(); j++)
  2482. if(*j)
  2483. pool.erase((**j).subID);
  2484. return pool;
  2485. }
  2486. void CGameState::buildBonusSystemTree()
  2487. {
  2488. buildGlobalTeamPlayerTree();
  2489. attachArmedObjects();
  2490. BOOST_FOREACH(CGTownInstance *t, map->towns)
  2491. {
  2492. t->deserializationFix();
  2493. }
  2494. // CStackInstance <-> CCreature, CStackInstance <-> CArmedInstance, CArtifactInstance <-> CArtifact
  2495. // are provided on initializing / deserializing
  2496. }
  2497. void CGameState::deserializationFix()
  2498. {
  2499. buildGlobalTeamPlayerTree();
  2500. attachArmedObjects();
  2501. }
  2502. void CGameState::buildGlobalTeamPlayerTree()
  2503. {
  2504. for(std::map<ui8, TeamState>::iterator k=teams.begin(); k!=teams.end(); ++k)
  2505. {
  2506. TeamState *t = &k->second;
  2507. t->attachTo(&globalEffects);
  2508. BOOST_FOREACH(ui8 teamMember, k->second.players)
  2509. {
  2510. PlayerState *p = getPlayer(teamMember);
  2511. assert(p);
  2512. p->attachTo(t);
  2513. }
  2514. }
  2515. }
  2516. void CGameState::attachArmedObjects()
  2517. {
  2518. BOOST_FOREACH(CGObjectInstance *obj, map->objects)
  2519. {
  2520. if(CArmedInstance *armed = dynamic_cast<CArmedInstance*>(obj))
  2521. armed->whatShouldBeAttached()->attachTo(armed->whereShouldBeAttached(this));
  2522. }
  2523. }
  2524. int3 CPath::startPos() const
  2525. {
  2526. return nodes[nodes.size()-1].coord;
  2527. }
  2528. void CPath::convert(ui8 mode) //mode=0 -> from 'manifest' to 'object'
  2529. {
  2530. if (mode==0)
  2531. {
  2532. for (unsigned int i=0;i<nodes.size();i++)
  2533. {
  2534. nodes[i].coord = CGHeroInstance::convertPosition(nodes[i].coord,true);
  2535. }
  2536. }
  2537. }
  2538. int3 CPath::endPos() const
  2539. {
  2540. return nodes[0].coord;
  2541. }
  2542. CGPathNode::CGPathNode()
  2543. :coord(-1,-1,-1)
  2544. {
  2545. accessible = 0;
  2546. land = 0;
  2547. moveRemains = 0;
  2548. turns = 255;
  2549. theNodeBefore = NULL;
  2550. }
  2551. bool CPathsInfo::getPath( const int3 &dst, CGPath &out )
  2552. {
  2553. out.nodes.clear();
  2554. const CGPathNode *curnode = &nodes[dst.x][dst.y][dst.z];
  2555. if(!curnode->theNodeBefore || curnode->accessible == CGPathNode::FLYABLE)
  2556. return false;
  2557. //we'll transform number of turns to conform the rule that hero cannot stop on blocked tile
  2558. bool transition01 = false;
  2559. while(curnode)
  2560. {
  2561. CGPathNode cpn = *curnode;
  2562. if(transition01)
  2563. {
  2564. if (curnode->accessible == CGPathNode::ACCESSIBLE)
  2565. {
  2566. transition01 = false;
  2567. }
  2568. else if (curnode->accessible == CGPathNode::FLYABLE)
  2569. {
  2570. cpn.turns = 1;
  2571. }
  2572. }
  2573. if(curnode->turns == 1 && curnode->theNodeBefore->turns == 0)
  2574. {
  2575. transition01 = true;
  2576. }
  2577. curnode = curnode->theNodeBefore;
  2578. out.nodes.push_back(cpn);
  2579. }
  2580. return true;
  2581. }
  2582. CPathsInfo::CPathsInfo( const int3 &Sizes )
  2583. :sizes(Sizes)
  2584. {
  2585. hero = NULL;
  2586. nodes = new CGPathNode**[sizes.x];
  2587. for(int i = 0; i < sizes.x; i++)
  2588. {
  2589. nodes[i] = new CGPathNode*[sizes.y];
  2590. for (int j = 0; j < sizes.y; j++)
  2591. {
  2592. nodes[i][j] = new CGPathNode[sizes.z];
  2593. }
  2594. }
  2595. }
  2596. CPathsInfo::~CPathsInfo()
  2597. {
  2598. for(int i = 0; i < sizes.x; i++)
  2599. {
  2600. for (int j = 0; j < sizes.y; j++)
  2601. {
  2602. delete [] nodes[i][j];
  2603. }
  2604. delete [] nodes[i];
  2605. }
  2606. delete [] nodes;
  2607. }
  2608. int3 CGPath::startPos() const
  2609. {
  2610. return nodes[nodes.size()-1].coord;
  2611. }
  2612. int3 CGPath::endPos() const
  2613. {
  2614. return nodes[0].coord;
  2615. }
  2616. void CGPath::convert( ui8 mode )
  2617. {
  2618. if(mode==0)
  2619. {
  2620. for(unsigned int i=0;i<nodes.size();i++)
  2621. {
  2622. nodes[i].coord = CGHeroInstance::convertPosition(nodes[i].coord,true);
  2623. }
  2624. }
  2625. }
  2626. PlayerState::PlayerState()
  2627. : color(-1), currentSelection(0xffffffff), enteredWinningCheatCode(0),
  2628. enteredLosingCheatCode(0), status(INGAME), daysWithoutCastle(0)
  2629. {
  2630. setNodeType(PLAYER);
  2631. }
  2632. std::string PlayerState::nodeName() const
  2633. {
  2634. return "Player " + (color < VLC->generaltexth->capColors.size() ? VLC->generaltexth->capColors[color] : boost::lexical_cast<std::string>(color));
  2635. }
  2636. // void PlayerState::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  2637. // {
  2638. // return; //no loops possible
  2639. // }
  2640. //
  2641. // void PlayerState::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  2642. // {
  2643. // for (std::vector<CGHeroInstance *>::const_iterator it = heroes.begin(); it != heroes.end(); it++)
  2644. // {
  2645. // if (*it != root)
  2646. // (*it)->getBonuses(out, selector, this);
  2647. // }
  2648. // }
  2649. InfoAboutHero::InfoAboutHero()
  2650. {
  2651. details = NULL;
  2652. hclass = NULL;
  2653. portrait = -1;
  2654. }
  2655. InfoAboutHero::InfoAboutHero( const InfoAboutHero & iah )
  2656. {
  2657. assign(iah);
  2658. }
  2659. InfoAboutHero::~InfoAboutHero()
  2660. {
  2661. delete details;
  2662. }
  2663. void InfoAboutHero::initFromHero( const CGHeroInstance *h, bool detailed )
  2664. {
  2665. if(!h) return;
  2666. owner = h->tempOwner;
  2667. hclass = h->type->heroClass;
  2668. name = h->name;
  2669. portrait = h->portrait;
  2670. army = ArmyDescriptor(h, detailed);
  2671. if(detailed)
  2672. {
  2673. //include details about hero
  2674. details = new Details;
  2675. details->luck = h->LuckVal();
  2676. details->morale = h->MoraleVal();
  2677. details->mana = h->mana;
  2678. details->primskills.resize(PRIMARY_SKILLS);
  2679. for (int i = 0; i < PRIMARY_SKILLS ; i++)
  2680. {
  2681. details->primskills[i] = h->getPrimSkillLevel(i);
  2682. }
  2683. }
  2684. }
  2685. void InfoAboutHero::assign( const InfoAboutHero & iah )
  2686. {
  2687. army = iah.army;
  2688. details = (iah.details ? new Details(*iah.details) : NULL);
  2689. hclass = iah.hclass;
  2690. name = iah.name;
  2691. owner = iah.owner;
  2692. portrait = iah.portrait;
  2693. }
  2694. InfoAboutHero & InfoAboutHero::operator=( const InfoAboutHero & iah )
  2695. {
  2696. assign(iah);
  2697. return *this;
  2698. }
  2699. ArmyDescriptor::ArmyDescriptor(const CArmedInstance *army, bool detailed) : isDetailed(detailed)
  2700. {
  2701. for(TSlots::const_iterator i = army->Slots().begin(); i != army->Slots().end(); i++)
  2702. {
  2703. if(detailed)
  2704. (*this)[i->first] = *i->second;
  2705. else
  2706. (*this)[i->first] = CStackBasicDescriptor(i->second->type, i->second->getQuantityID());
  2707. }
  2708. }
  2709. ArmyDescriptor::ArmyDescriptor() : isDetailed(true)
  2710. {
  2711. }
  2712. int ArmyDescriptor::getStrength() const
  2713. {
  2714. ui64 ret = 0;
  2715. if(isDetailed)
  2716. {
  2717. for(const_iterator i = begin(); i != end(); i++)
  2718. ret += i->second.type->AIValue * i->second.count;
  2719. }
  2720. else
  2721. {
  2722. for(const_iterator i = begin(); i != end(); i++)
  2723. ret += i->second.type->AIValue * CCreature::estimateCreatureCount(i->second.count);
  2724. }
  2725. return ret;
  2726. }
  2727. DuelParameters::SideSettings::StackSettings::StackSettings()
  2728. : type(-1), count(0)
  2729. {
  2730. }
  2731. DuelParameters::SideSettings::StackSettings::StackSettings(si32 Type, si32 Count)
  2732. : type(Type), count(Count)
  2733. {
  2734. }
  2735. DuelParameters::SideSettings::SideSettings()
  2736. {
  2737. heroId = -1;
  2738. }
  2739. DuelParameters::DuelParameters()
  2740. {
  2741. terType = TerrainTile::dirt;
  2742. bfieldType = 15;
  2743. }
  2744. TeamState::TeamState()
  2745. {
  2746. setNodeType(TEAM);
  2747. }