sideigc.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. ** Copyright (C) 1996, 1997 Microsoft Corporation. All Rights Reserved.
  3. **
  4. ** File: sideIGC.cpp
  5. **
  6. ** Author:
  7. **
  8. ** Description:
  9. ** Implementation of the CsideIGC class. This file was initially created by
  10. ** the ATL wizard for the core object.
  11. **
  12. ** History:
  13. */
  14. // sideIGC.cpp : Implementation of CsideIGC
  15. #include "pch.h"
  16. #include "sideIGC.h"
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CsideIGC
  19. HRESULT CsideIGC::Initialize(ImissionIGC* pMission,
  20. Time now,
  21. const void* data,
  22. int dataSize)
  23. {
  24. m_pMission = pMission;
  25. ZRetailAssert (data && (dataSize >= sizeof(DataSideIGC)));
  26. m_data = *((DataSideIGC*)data);
  27. m_pCivilization = pMission->GetCivilization(m_data.civilizationID);
  28. assert (m_pCivilization);
  29. m_pCivilization->AddRef();
  30. m_ttbmBuildingTechs.ClearAll(); //Will automatically adjust for starting buildings
  31. m_lastUpdate = now;
  32. pMission->AddSide(this);
  33. m_pMission->GetIgcSite()->CreateSideEvent(static_cast<IsideIGC*>(this));
  34. return S_OK;
  35. }
  36. void CsideIGC::Terminate(void)
  37. {
  38. m_pMission->GetIgcSite()->DestroySideEvent(static_cast<IsideIGC*>(this));
  39. AddRef();
  40. {
  41. //Nuke all of the side's buckets
  42. BucketLinkIGC* l;
  43. while (l = m_buckets.first()) //intentional assignment
  44. l->data()->Terminate();
  45. }
  46. {
  47. //Set all of the side's ships to the neutral side
  48. ShipLinkIGC* l;
  49. while (l = m_ships.first()) //intentional assignment
  50. l->data()->SetSide(NULL);
  51. }
  52. {
  53. //Nuke all of the stations
  54. StationLinkIGC* l;
  55. while (l = m_stations.first()) //intentional assignment
  56. l->data()->Terminate();
  57. }
  58. m_pMission->DeleteSide(this);
  59. m_pCivilization->Release();
  60. m_pCivilization = NULL;
  61. Release();
  62. }
  63. int CsideIGC::Export(void* data) const
  64. {
  65. int nBytes = sizeof(DataSideIGC);
  66. if (data)
  67. *((DataSideIGC*)data) = m_data;
  68. return sizeof(DataSideIGC);
  69. }
  70. void CsideIGC::DestroyBuckets(void)
  71. {
  72. m_data.ttbmDevelopmentTechs = GetCivilization()->GetBaseTechs();
  73. m_ttbmBuildingTechs.ClearAll();
  74. {
  75. //Nuke all of the side's buckets
  76. BucketLinkIGC* l;
  77. while (l = m_buckets.first()) //intentional assignment
  78. l->data()->Terminate();
  79. }
  80. }
  81. void CsideIGC::CreateBuckets(void)
  82. {
  83. assert(!m_buckets.first());
  84. if (m_pMission->GetMissionParams()->bAllowDevelopments)
  85. {
  86. TechTreeBitMask ttbmLocalUltimate;
  87. ttbmLocalUltimate.ClearAll();
  88. {
  89. //Resolve what the ultimate techs are for this civ ...
  90. m_ttbmUltimateTechs = m_data.ttbmDevelopmentTechs;
  91. {
  92. //Start with their initial techs and add anything they could capture.
  93. for (PartTypeLinkIGC* l = m_pMission->GetPartTypes()->first();
  94. (l != NULL);
  95. l = l->next())
  96. {
  97. m_ttbmUltimateTechs |= l->data()->GetEffectTechs();
  98. }
  99. }
  100. {
  101. //Do the building techs ... assume they capture every type of building
  102. for (StationTypeLinkIGC* l = m_pMission->GetStationTypes()->first();
  103. (l != NULL);
  104. l = l->next())
  105. {
  106. m_ttbmUltimateTechs |= l->data()->GetEffectTechs();
  107. ttbmLocalUltimate |= l->data()->GetLocalTechs();
  108. }
  109. }
  110. {
  111. //Now add any techs they could buy (assuming they could build or
  112. //buy them) until we have a pass where we didn't add any.
  113. bool continueF;
  114. do
  115. {
  116. continueF = false;
  117. {
  118. //Do the same for developments
  119. for (DevelopmentLinkIGC* l = m_pMission->GetDevelopments()->first();
  120. (l != NULL);
  121. l = l->next())
  122. {
  123. //Can we buy it?
  124. if (l->data()->GetRequiredTechs() <= m_ttbmUltimateTechs)
  125. {
  126. //yes ... does it make a difference?
  127. if (!(l->data()->GetEffectTechs() <= m_ttbmUltimateTechs))
  128. {
  129. //yes ... 'buy it'
  130. m_ttbmUltimateTechs |= l->data()->GetEffectTechs();
  131. continueF = true;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. while (continueF);
  138. }
  139. }
  140. {
  141. //Add all developments that the side could, potentially, produce
  142. for (DevelopmentLinkIGC* l = m_pMission->GetDevelopments()->first();
  143. (l != NULL);
  144. l = l->next())
  145. {
  146. IdevelopmentIGC* d = l->data();
  147. //Only add the bucket if I'll be able to buy it eventually and it is not
  148. //obsolete.
  149. if ((d->GetObjectID() != c_didTeamMoney) && (d->GetRequiredTechs() <= m_ttbmUltimateTechs) &&
  150. !d->IsObsolete(m_data.ttbmInitialTechs))
  151. {
  152. //It is something we might be able to build and it will actually
  153. //do something useful
  154. DataBucketIGC db = {d, this};
  155. IbucketIGC* b = (IbucketIGC*)(m_pMission->CreateObject(m_lastUpdate,
  156. OT_bucket,
  157. &db,
  158. sizeof(db)));
  159. assert (b);
  160. b->Release(); //Creating the bucket adds it to the side's list of buckets
  161. }
  162. }
  163. }
  164. {
  165. //Add All drone and station types that they might, theoretically, be able to produce
  166. ttbmLocalUltimate |= m_ttbmUltimateTechs;
  167. {
  168. //Add all developments that the side could, potentially, produce
  169. for (DroneTypeLinkIGC* l = m_pMission->GetDroneTypes()->first();
  170. (l != NULL);
  171. l = l->next())
  172. {
  173. IdroneTypeIGC* d = l->data();
  174. if (d->GetRequiredTechs() <= ttbmLocalUltimate)
  175. {
  176. //It is something we might be able to build and it will actually
  177. //do something useful
  178. DataBucketIGC db = {d, this};
  179. IbucketIGC* b = (IbucketIGC*)(m_pMission->CreateObject(m_lastUpdate,
  180. OT_bucket,
  181. &db,
  182. sizeof(db)));
  183. assert (b);
  184. b->Release(); //Creating the bucket adds it to the side's list of buckets
  185. }
  186. }
  187. }
  188. {
  189. //Add all developments that the side could, potentially, produce
  190. for (StationTypeLinkIGC* l = m_pMission->GetStationTypes()->first();
  191. (l != NULL);
  192. l = l->next())
  193. {
  194. IstationTypeIGC* s = l->data();
  195. if (s->GetRequiredTechs() <= ttbmLocalUltimate)
  196. {
  197. //It is something we might be able to build and it will actually
  198. //do something useful
  199. DataBucketIGC db = {s, this};
  200. IbucketIGC* b = (IbucketIGC*)(m_pMission->CreateObject(m_lastUpdate,
  201. OT_bucket,
  202. &db,
  203. sizeof(db)));
  204. assert (b);
  205. b->Release(); //Creating the bucket adds it to the side's list of buckets
  206. }
  207. }
  208. }
  209. //Add all hull and part types that cost money
  210. {
  211. /*
  212. for (HullTypeLinkIGC* l = m_pMission->GetHullTypes()->first();
  213. (l != NULL);
  214. l = l->next())
  215. {
  216. IhullTypeIGC* h = l->data();
  217. if ((h->GetPrice() != 0) && (h->GetTimeToBuild() != 0) &&
  218. (h->GetRequiredTechs() <= ttbmLocalUltimate))
  219. {
  220. //It is something we might be able to build and it will actually
  221. //do something useful
  222. DataBucketIGC db = {h, this};
  223. IbucketIGC* b = (IbucketIGC*)(m_pMission->CreateObject(m_lastUpdate,
  224. OT_bucket,
  225. &db,
  226. sizeof(db)));
  227. assert (b);
  228. b->Release(); //Creating the bucket adds it to the side's list of buckets
  229. }
  230. }
  231. */
  232. }
  233. {
  234. /*
  235. for (PartTypeLinkIGC* l = m_pMission->GetPartTypes()->first();
  236. (l != NULL);
  237. l = l->next())
  238. {
  239. IpartTypeIGC* p = l->data();
  240. if ((p->GetPrice() != 0) && (p->GetTimeToBuild() != 0) &&
  241. (p->GetRequiredTechs() <= ttbmLocalUltimate))
  242. {
  243. //It is something we might be able to build and it will actually
  244. //do something useful
  245. DataBucketIGC db = {p, this};
  246. IbucketIGC* b = (IbucketIGC*)(m_pMission->CreateObject(m_lastUpdate,
  247. OT_bucket,
  248. &db,
  249. sizeof(db)));
  250. assert (b);
  251. if (IlauncherTypeIGC::IsLauncherType(p->GetEquipmentType()))
  252. {
  253. b->SetPrice(b->GetPrice() * p->GetAmount(NULL));
  254. }
  255. b->Release(); //Creating the bucket adds it to the side's list of buckets
  256. }
  257. }
  258. */
  259. }
  260. }
  261. }
  262. }
  263. long CsideIGC::GetProsperityPercentBought(void) const
  264. {
  265. // Iterate through each bucket, looking for the prosperity development
  266. const BucketListIGC* pBuckets = GetBuckets();
  267. for (BucketLinkIGC* it = pBuckets->first(); it; it = it->next())
  268. {
  269. IbucketIGC* pBucket = it->data();
  270. assert(pBucket);
  271. if (OT_development == pBucket->GetBucketType())
  272. if (c_didTeamMoney == pBucket->GetBuyable()->GetObjectID())
  273. return pBucket->GetPercentBought();
  274. }
  275. // Return zero if no prosperity development was found
  276. return 0;
  277. }
  278. long CsideIGC::GetProsperityPercentComplete(void) const
  279. {
  280. // Iterate through each bucket, looking for the prosperity development
  281. const BucketListIGC* pBuckets = GetBuckets();
  282. for (BucketLinkIGC* it = pBuckets->first(); it; it = it->next())
  283. {
  284. IbucketIGC* pBucket = it->data();
  285. assert(pBucket);
  286. if (OT_development == pBucket->GetBucketType())
  287. if (c_didTeamMoney == pBucket->GetBuyable()->GetObjectID())
  288. return pBucket->GetPercentComplete();
  289. }
  290. // Return zero if no prosperity development was found
  291. return 0;
  292. }