hullTypeIGC.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. ** Copyright (C) 1996, 1997 Microsoft Corporation. All Rights Reserved.
  3. **
  4. ** File: hullTypeIGC.cpp
  5. **
  6. ** Author:
  7. **
  8. ** Description:
  9. ** Implementation of the ChullTypeIGC class. This file was initially created by
  10. ** the ATL wizard for the core object.
  11. **
  12. ** History:
  13. */
  14. // hullTypeIGC.cpp : Implementation of ChullTypeIGC
  15. #include "pch.h"
  16. #include "hullTypeIGC.h"
  17. /////////////////////////////////////////////////////////////////////////////
  18. // ChullTypeIGC
  19. HRESULT ChullTypeIGC::Initialize(ImissionIGC* pMission,
  20. Time now,
  21. const void* data,
  22. int dataSize)
  23. {
  24. assert (pMission);
  25. m_pMission = pMission;
  26. ZRetailAssert (data && (dataSize >= sizeof(DataHullTypeIGC)));
  27. //Note ... we need to allocate & copy the DataHullTypeIGC + the variable length fields
  28. m_data = (DataHullTypeIGC*)(new char [dataSize]);
  29. assert (m_data);
  30. memcpy(m_data, data, dataSize);
  31. {
  32. for (int i = 0;
  33. ((i < c_cMaxPreferredPartTypes) && (m_data->preferredPartsTypes[i] != NA));
  34. i++)
  35. {
  36. IpartTypeIGC* ppt = pMission->GetPartType(m_data->preferredPartsTypes[i]);
  37. assert (ppt);
  38. ppt->AddRef();
  39. m_preferredPartTypes.last(ppt);
  40. }
  41. }
  42. IIgcSite* pigc = pMission->GetIgcSite();
  43. pigc->Preload(m_data->modelName, iswalpha(m_data->textureName[0])
  44. ? m_data->textureName
  45. : NULL);
  46. m_poIcon = pigc->LoadRadarIcon(m_data->iconName);
  47. {
  48. static const Vector z(0.0f, 0.0f, 1.0f);
  49. MultiHullBase* pmhb = HitTest::Load(m_data->modelName);
  50. #ifdef DREAMCAST
  51. if (pmhb)
  52. #else
  53. assert (pmhb);
  54. #endif
  55. {
  56. {
  57. float r = pmhb->GetOriginalRadius();
  58. m_scale = (0.5f * m_data->length / r);
  59. #if 0
  60. {
  61. const Vector& ellipseEquation = pmhb->GetEllipseEquation();
  62. debugf("%s/%d %f %f %f\n", m_data->name, m_data->hullID, ellipseEquation.x * m_scale, ellipseEquation.y * m_scale, ellipseEquation.z * m_scale);
  63. double v = ellipseEquation.x * ellipseEquation.y * ellipseEquation.z;
  64. debugf("%s/%d\t(%s)\n\tOld length = %f\n\tNew Length = %f\n\tFrontal area = %f\n\tVolume = %f\n",
  65. m_data->name, m_data->hullID, m_data->modelName,
  66. float(m_data->length), float(m_data->length) * r * float(pow(v, -1.0/3.0)),
  67. pmhb->GetEllipseEquation().x * pmhb->GetEllipseEquation().y * m_scale * m_scale,
  68. pmhb->GetEllipseEquation().x * pmhb->GetEllipseEquation().y * pmhb->GetEllipseEquation().z * m_scale * m_scale * m_scale);
  69. debugf("\tDensity %f\n", m_data->mass / (0.2f * (pmhb->GetEllipseEquation().x * pmhb->GetEllipseEquation().y * pmhb->GetEllipseEquation().z * m_scale * m_scale * m_scale)));
  70. }
  71. #endif
  72. }
  73. {
  74. const FrameDataUTL* pfd = pmhb->GetFrame("cockpt");
  75. m_cockpit = pfd ? (pfd->position * m_scale) : z;
  76. }
  77. for (Mount i = m_data->maxWeapons - 1; (i >= 0); i--)
  78. {
  79. const HardpointData& hd = GetHardpointData(i);
  80. const FrameDataUTL* pfd = pmhb->GetFrame(hd.frameName);
  81. assert (pfd); //Note: still need to handle pfd == NULL semi-gracefully in retail
  82. if (hd.bFixed)
  83. {
  84. assert (i < m_data->maxFixedWeapons);
  85. m_positionWeapons[i] = pfd ? (pfd->position * m_scale) : Vector::GetZero();
  86. }
  87. else
  88. {
  89. assert (i >= m_data->maxFixedWeapons);
  90. if (pfd)
  91. {
  92. m_positionWeapons[i] = pfd->position * m_scale;
  93. static const Vector up(0.0f, 1.0f, 0.0f);
  94. if (fabs(pfd->forward * up) < 0.9f)
  95. m_orientationWeapons[i].Set(pfd->forward, up);
  96. else
  97. {
  98. m_orientationWeapons[i].Set(pfd->forward, z);
  99. }
  100. }
  101. else
  102. {
  103. m_positionWeapons[i] = Vector::GetZero();
  104. m_orientationWeapons[i].Set(z);
  105. }
  106. }
  107. }
  108. #if 0
  109. if (m_data->maxFixedWeapons > 1)
  110. {
  111. debugf("\n%s/%d\t%s\t%f\n", GetName(), GetObjectID(), m_data->modelName, m_data->length);
  112. for (Mount i = 0; (i < m_data->maxFixedWeapons); i++)
  113. {
  114. for (Mount j = i+1; (j < m_data->maxFixedWeapons); j++)
  115. {
  116. debugf("%d <-> %d\t%f %s %s\n", i, j, (m_positionWeapons[i] - m_positionWeapons[j]).Length(),
  117. GetHardpointData(i).frameName, GetHardpointData(j).frameName);
  118. }
  119. }
  120. }
  121. #endif
  122. m_nLandSlots = 0;
  123. m_nLaunchSlots = 0;
  124. {
  125. //Get the stations launch slots
  126. static char bfrLaunch[] = "launchX";
  127. while (true)
  128. {
  129. //Do we have a launch slot?
  130. bfrLaunch[6] = '1' + m_nLaunchSlots;
  131. const FrameDataUTL* pfd = pmhb->GetFrame(bfrLaunch);
  132. if (pfd)
  133. {
  134. assert (m_nLaunchSlots < c_maxShipLaunchSlots);
  135. m_positionLaunches[m_nLaunchSlots] = pfd->position * m_scale;
  136. m_directionLaunches[m_nLaunchSlots++] = pfd->forward;
  137. }
  138. else
  139. break;
  140. }
  141. }
  142. {
  143. //debugf("Station %s/%s\n", GetName(), m_data.modelName);
  144. //Refigure the stations launch slots
  145. static char bfrLand[] = "garageXA";
  146. while (true)
  147. {
  148. //Do we have a launch slot?
  149. bfrLand[6] = '1' + m_nLandSlots;
  150. bfrLand[7] = 'A';
  151. const FrameDataUTL* pfd = pmhb->GetFrame(bfrLand);
  152. if (pfd)
  153. {
  154. assert (m_nLandSlots < c_maxShipLandSlots);
  155. int i = 0;
  156. do
  157. {
  158. assert (i < c_maxLandPlanes);
  159. m_positionLandPlanes[m_nLandSlots][i] = pfd->position * m_scale;
  160. m_directionLandPlanes[m_nLandSlots][i] = pfd->forward;
  161. //debugf("\t%s %f %f %f\n", bfrLand, m_positionLandPlanes[m_nLandSlots][i].x, m_positionLandPlanes[m_nLandSlots][i].y, m_positionLandPlanes[m_nLandSlots][i].z);
  162. bfrLand[7] = 'A' + ++i;
  163. pfd = pmhb->GetFrame(bfrLand);
  164. }
  165. while (pfd);
  166. m_nLandPlanes[m_nLandSlots++] = i;
  167. }
  168. else
  169. break;
  170. }
  171. }
  172. }
  173. #ifdef DREAMCAST
  174. else
  175. {
  176. m_cockpit = Vector::GetZero();
  177. for (int i = m_data->maxWeapons - 1; (i >= 0); i--)
  178. m_positionWeapons[i] = Vector::GetZero();
  179. }
  180. #endif
  181. }
  182. if (m_data->successorHullID != NA)
  183. {
  184. m_phtSuccessor = pMission->GetHullType(m_data->successorHullID);
  185. assert (m_phtSuccessor);
  186. }
  187. pMission->AddHullType(this);
  188. /*
  189. if (this->GetRechargeRate() > 0.0f)
  190. {
  191. float eTotal = 0.0f;
  192. for (Mount i = 0; (i < this->GetMaxWeapons()); i++)
  193. {
  194. const HardpointData& hd = this->GetHardpointData(i);
  195. float eMax = 0.0f;
  196. for (PartTypeLinkIGC* ppl = pMission->GetPartTypes()->first(); (ppl != NULL); ppl = ppl->next())
  197. {
  198. IpartTypeIGC* pt = ppl->data();
  199. if ((pt->GetEquipmentType() == ET_Weapon) &&
  200. ((pt->GetPartMask() & ~hd.partMask) == 0))
  201. {
  202. DataWeaponTypeIGC* dwt = (DataWeaponTypeIGC*)(pt->GetData());
  203. float e = dwt->energyPerShot / dwt->dtimeBurst;
  204. if (e > eMax)
  205. eMax = e;
  206. }
  207. }
  208. eTotal += eMax;
  209. }
  210. if (this->GetPartMask(ET_Cloak, 0) != 0)
  211. {
  212. debugf("\n%s (%d)\n\t%8.3f%8.0f%8.0f\n\t%8.3f (120)\n\t%8.3f (180)\n\t%8.3f (200)\n\t%8.3f (%8.3f) %8.3f\n",
  213. GetName(), GetObjectID(),
  214. this->GetMaxEnergy() / this->GetRechargeRate(), this->GetMaxEnergy(), this->GetRechargeRate(),
  215. this->GetMaxEnergy() / (120.0f - this->GetRechargeRate()),
  216. this->GetMaxEnergy() / (180.0f - this->GetRechargeRate()),
  217. this->GetMaxEnergy() / (200.0f - this->GetRechargeRate()),
  218. (eTotal > this->GetRechargeRate())
  219. ? this->GetMaxEnergy() / (eTotal - this->GetRechargeRate())
  220. : -1.0f, eTotal, eTotal/this->GetRechargeRate());
  221. }
  222. else
  223. {
  224. debugf("\n%s (%d)\n\t%8.3f%8.0f%8.0f\n\t%8.3f (%8.3f) %8.3f\n",
  225. GetName(), GetObjectID(),
  226. this->GetMaxEnergy() / this->GetRechargeRate(), this->GetMaxEnergy(), this->GetRechargeRate(),
  227. (eTotal > this->GetRechargeRate())
  228. ? this->GetMaxEnergy() / (eTotal - this->GetRechargeRate())
  229. : -1.0f, eTotal, eTotal/this->GetRechargeRate());
  230. }
  231. }
  232. */
  233. return S_OK;
  234. }
  235. void ChullTypeIGC::Terminate(void)
  236. {
  237. {
  238. PartTypeLinkIGC* pptl;
  239. while (pptl = m_preferredPartTypes.first()) //intentional =
  240. {
  241. pptl->data()->Release();
  242. delete pptl;
  243. }
  244. }
  245. m_pMission->DeleteHullType(this);
  246. }
  247. int ChullTypeIGC::Export(void* data) const
  248. {
  249. int nBytes = sizeof(DataHullTypeIGC) + sizeof(HardpointData) * m_data->maxWeapons;
  250. if (data)
  251. memcpy(data, m_data, nBytes);
  252. return nBytes;
  253. }