teleportpane.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #include "pch.h"
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // TeleportPane
  5. //
  6. //////////////////////////////////////////////////////////////////////////////
  7. class TeleportPane :
  8. public IItemEvent::Sink,
  9. public EventTargetContainer<TeleportPane>,
  10. public TrekClientEventSink
  11. {
  12. protected:
  13. TRef<ButtonPane> m_pbuttonTeleport;
  14. TRef<ButtonPane> m_pbuttonBack;
  15. TRef<ButtonBarPane> m_pbuttonbarDestinations;
  16. TRef<ListPane> m_plistPaneDestinations;
  17. TRef<IItemEvent::Source> m_peventDestinations;
  18. TRef<IItemEvent::Sink> m_psinkDestinations;
  19. TVector<int> m_viColumns;
  20. TRef<TListListWrapper<ImodelIGC*> > m_plistDestinations;
  21. enum
  22. {
  23. sortName,
  24. sortType,
  25. sortSector,
  26. } m_sortDestination;
  27. public:
  28. class DestinationPainter : public ItemPainter
  29. {
  30. const TVector<int>& m_viColumns;
  31. public:
  32. DestinationPainter(const TVector<int>& viColumns)
  33. : m_viColumns(viColumns) {};
  34. int GetXSize()
  35. {
  36. return m_viColumns[3];
  37. }
  38. int GetYSize()
  39. {
  40. return 14;
  41. }
  42. void Paint(ItemID pitemArg, Surface* psurface, bool bSelected, bool bFocus)
  43. {
  44. ImodelIGC* pDestination = (ImodelIGC*)pitemArg;
  45. IshipIGC* pship = NULL;
  46. IstationIGC* pstation = NULL;
  47. if (pDestination == NULL || trekClient.MyMission() == NULL)
  48. return;
  49. if (pDestination->GetObjectType() == OT_ship)
  50. {
  51. CastTo(pship, pDestination);
  52. }
  53. else
  54. {
  55. assert(pDestination->GetObjectType() == OT_station);
  56. CastTo(pstation, pDestination);
  57. }
  58. // draw the selection bar
  59. if (bSelected) {
  60. psurface->FillRect(
  61. WinRect(0, 0, GetXSize(), GetYSize()),
  62. Color::Red()
  63. );
  64. }
  65. TRef<IEngineFont> pfont;
  66. // show the place we currently are in bold
  67. if (pship && pship == trekClient.GetShip()->GetParentShip()
  68. || pstation && pstation == trekClient.GetShip()->GetStation())
  69. {
  70. pfont = TrekResources::SmallBoldFont();
  71. }
  72. else
  73. {
  74. pfont = TrekResources::SmallFont();
  75. }
  76. Color color;
  77. if (CanKeepCurrentShip(pDestination))
  78. {
  79. color = Color::White();
  80. }
  81. else
  82. {
  83. color = 0.75f * Color::White();
  84. }
  85. // draw the name
  86. psurface->DrawString(
  87. pfont,
  88. color,
  89. WinPoint(0, 0),
  90. pship ? pship->GetName() : pstation->GetName()
  91. );
  92. // draw the ship type (if any)
  93. if (pship)
  94. {
  95. psurface->DrawString(
  96. pfont,
  97. color,
  98. WinPoint(m_viColumns[0], 0),
  99. HullName(pDestination)
  100. );
  101. }
  102. // draw the cluster
  103. psurface->DrawString(
  104. pfont,
  105. color,
  106. WinPoint(m_viColumns[1] + 2, 0),
  107. SectorName(pDestination)
  108. );
  109. // draw the total number of turrets (if appropriate)
  110. if (pship && NumTurrets(pship))
  111. {
  112. int nNumTurrets = NumTurrets(pship);
  113. if (nNumTurrets != 0)
  114. {
  115. psurface->DrawString(
  116. pfont,
  117. color,
  118. WinPoint(m_viColumns[2] + 2, 0),
  119. ZString((int)MannedTurrets(pship)) + ZString("/") + ZString(nNumTurrets)
  120. );
  121. }
  122. }
  123. }
  124. };
  125. public:
  126. TeleportPane(Modeler* pmodeler)
  127. : m_peventDestinations(NULL), m_sortDestination(sortName)
  128. {
  129. TRef<IObject> pobjColumns;
  130. // a team pane is meaningless without a team
  131. if (trekClient.MyMission() == NULL)
  132. return;
  133. // Load the members from MDL
  134. TRef<INameSpace> pns = pmodeler->GetNameSpace("teleportpane");
  135. CastTo(m_pbuttonTeleport, pns->FindMember("teleportTeleportButtonPane"));
  136. CastTo(m_pbuttonBack, pns->FindMember("teleportBackButtonPane"));
  137. CastTo(m_plistPaneDestinations, (Pane*)pns->FindMember("teleportPaneDestinationListPane"));
  138. CastTo(pobjColumns, pns->FindMember("teleportPaneDestinationColumns"));
  139. CastTo(m_pbuttonbarDestinations, pns->FindMember("teleportPaneDestinationListHeader"));
  140. //
  141. // Buttons
  142. //
  143. AddEventTarget(OnButtonTeleport, m_pbuttonTeleport->GetEventSource());
  144. AddEventTarget(OnButtonBack, m_pbuttonBack->GetEventSource());
  145. AddEventTarget(OnButtonBar, m_pbuttonbarDestinations->GetEventSource());
  146. //
  147. // The Destination list
  148. //
  149. ParseIntVector(pobjColumns, m_viColumns);
  150. m_plistPaneDestinations->SetItemPainter(new DestinationPainter(m_viColumns));
  151. m_peventDestinations = m_plistPaneDestinations->GetSelectionEventSource();
  152. if (m_psinkDestinations)
  153. m_peventDestinations->RemoveSink(m_psinkDestinations);
  154. m_peventDestinations->AddSink(m_psinkDestinations = new IItemEvent::Delegate(this));
  155. AddEventTarget(OnButtonTeleport, m_plistPaneDestinations->GetDoubleClickEventSource());
  156. RebuildList();
  157. }
  158. ~TeleportPane()
  159. {
  160. if (m_peventDestinations)
  161. {
  162. m_peventDestinations->RemoveSink(m_psinkDestinations);
  163. }
  164. }
  165. //////////////////////////////////////////////////////////////////////////////
  166. //
  167. // Event handlers
  168. //
  169. //////////////////////////////////////////////////////////////////////////////
  170. bool OnEvent(IItemEvent::Source *pevent, ItemID pitem)
  171. {
  172. if (pevent == m_peventDestinations) {
  173. OnSelectDestination(pitem);
  174. }
  175. return true;
  176. }
  177. void OnShipStatusChange(PlayerInfo* pPlayer)
  178. {
  179. // this might have been someone buying a ship with turrets.
  180. RebuildList();
  181. }
  182. void OnAddPlayer(MissionInfo* pMissionInfo, SideID sideID, PlayerInfo* pPlayerInfo)
  183. {
  184. // this might become someone in our list.
  185. RebuildList();
  186. }
  187. void OnDelPlayer(MissionInfo* pMissionInfo, SideID sideID, PlayerInfo* pPlayerInfo, QuitSideReason reason, const char* szMessageParam)
  188. {
  189. // this might be someone in our list.
  190. RebuildList(pPlayerInfo->GetShip());
  191. }
  192. void OnStationCaptured(StationID stationID, SideID sideID)
  193. {
  194. // this might be or have been one of our stations
  195. RebuildList();
  196. }
  197. void OnTechTreeChanged(SideID sideID)
  198. {
  199. if (sideID == trekClient.GetSideID())
  200. {
  201. RebuildList();
  202. }
  203. }
  204. void OnModelTerminated(ImodelIGC* pmodel)
  205. {
  206. // this might a station being destroyed
  207. if (pmodel->GetSide() == trekClient.GetSide())
  208. RebuildList(pmodel);
  209. }
  210. void OnDiscoveredStation(IstationIGC* pstation)
  211. {
  212. // this might be a new station we have built
  213. if (pstation->GetSide() == trekClient.GetSide())
  214. RebuildList();
  215. }
  216. bool OnSelectDestination(ItemID pitem)
  217. {
  218. return true;
  219. }
  220. void OnBoardFailed(IshipIGC* pshipRequestedParent)
  221. {
  222. if (trekClient.GetShip()->GetCluster() == NULL)
  223. {
  224. if (pshipRequestedParent
  225. && MannedTurrets(pshipRequestedParent) < NumTurrets(pshipRequestedParent))
  226. {
  227. TRef<IMessageBox> pmsgBox = CreateMessageBox(
  228. "Teleportation canceled - no turret positions were open.");
  229. trekClient.PlaySoundEffect(noTurretsSound);
  230. GetWindow()->GetPopupContainer()->OpenPopup(pmsgBox, false);
  231. }
  232. else
  233. {
  234. TRef<IMessageBox> pmsgBox = CreateMessageBox("Teleportation failed.");
  235. GetWindow()->GetPopupContainer()->OpenPopup(pmsgBox, false);
  236. }
  237. }
  238. }
  239. bool OnButtonTeleport()
  240. {
  241. ImodelIGC* pDestination = (ImodelIGC*)(m_plistPaneDestinations->GetSelection());
  242. IstationIGC* pstation = NULL;
  243. if (pDestination == NULL || trekClient.MyMission() == NULL
  244. || trekClient.GetShip()->GetCluster() != NULL)
  245. return true;
  246. if (pDestination->GetObjectType() == OT_ship)
  247. {
  248. // if this is a ship, try boarding it.
  249. IshipIGC* pship;
  250. CastTo(pship, pDestination);
  251. if (trekClient.GetShip()->GetParentShip() != NULL)
  252. {
  253. trekClient.DisembarkAndBoard(pship);
  254. }
  255. else
  256. {
  257. trekClient.BoardShip(pship);
  258. }
  259. trekClient.PlaySoundEffect(personalJumpSound);
  260. }
  261. else
  262. {
  263. // try teleporting to that station
  264. IstationIGC* pstation;
  265. CastTo(pstation, pDestination);
  266. if (trekClient.GetShip()->GetParentShip() != NULL)
  267. {
  268. // if they are already there, just get off of the ship
  269. if (pstation == trekClient.GetShip()->GetParentShip()->GetStation())
  270. {
  271. trekClient.BoardShip(NULL);
  272. }
  273. else
  274. {
  275. trekClient.DisembarkAndTeleport(pstation);
  276. }
  277. }
  278. else
  279. {
  280. if (pstation != trekClient.GetShip()->GetStation())
  281. {
  282. trekClient.SetMessageType(BaseClient::c_mtGuaranteed);
  283. BEGIN_PFM_CREATE(trekClient.m_fm, pfmDocked, C, DOCKED)
  284. END_PFM_CREATE
  285. pfmDocked->stationID = pstation->GetObjectID();
  286. trekClient.StartLockDown(
  287. "Teleporting to " + ZString(pstation->GetName()) + "....",
  288. BaseClient::lockdownTeleporting);
  289. }
  290. }
  291. }
  292. // dismiss the teleport pane.
  293. GetWindow()->TurnOffOverlayFlags(ofTeleportPane);
  294. return true;
  295. }
  296. bool OnButtonBack()
  297. {
  298. GetWindow()->TurnOffOverlayFlags(ofTeleportPane);
  299. return true;
  300. }
  301. bool OnButtonBar(int iButton)
  302. {
  303. return true;
  304. }
  305. static bool CanBoard(IshipIGC* pship)
  306. {
  307. PlayerInfo* pPlayer = (PlayerInfo*)pship->GetPrivateData();
  308. if (pship != trekClient.GetShip()
  309. && pship->GetSide() == trekClient.GetSide()
  310. && pPlayer->LastSeenState() == c_ssDocked
  311. && pship->GetPilotType() >= c_ptPlayer)
  312. {
  313. HullID hid = pPlayer->LastSeenShipType();
  314. assert (hid != NA);
  315. IhullTypeIGC* pht = trekClient.m_pCoreIGC->GetHullType(hid);
  316. assert (pht);
  317. if ((trekClient.GetShip()->GetPilotType() == c_ptCheatPlayer) ||
  318. (pht->GetMaxFixedWeapons() != pht->GetMaxWeapons()))
  319. {
  320. return true;
  321. }
  322. }
  323. return false;
  324. }
  325. void RebuildList(ImodelIGC* pmodelExclude = NULL)
  326. {
  327. TRef<TListListWrapper<ImodelIGC*> > plistNew = new TListListWrapper<ImodelIGC*>();
  328. IsideIGC* pside = trekClient.GetSide();
  329. if (pside && trekClient.GetSideID() != SIDE_TEAMLOBBY)
  330. {
  331. for (ShipLinkIGC* pshiplink = pside->GetShips()->first();
  332. (pshiplink != NULL);
  333. pshiplink = pshiplink->next())
  334. {
  335. IshipIGC* pship = pshiplink->data();
  336. if (pship != pmodelExclude && CanBoard(pship))
  337. {
  338. plistNew->PushEnd(pship);
  339. }
  340. }
  341. for (StationLinkIGC* pstationlink = pside->GetStations()->first();
  342. (pstationlink != NULL);
  343. pstationlink = pstationlink->next())
  344. {
  345. IstationIGC* pstation = pstationlink->data();
  346. if (pstation != pmodelExclude
  347. && pstation->GetBaseStationType()->HasCapability(c_sabmRestart))
  348. {
  349. plistNew->PushEnd(pstation);
  350. }
  351. }
  352. }
  353. m_plistDestinations = plistNew;
  354. m_plistPaneDestinations->SetList(m_plistDestinations);
  355. }
  356. static ZString HullName(ImodelIGC* pmodel)
  357. {
  358. if (pmodel->GetObjectType() == OT_ship)
  359. {
  360. PlayerInfo* pPlayer = (PlayerInfo*)((IshipIGC*)pmodel)->GetPrivateData();
  361. IhullTypeIGC* phull = trekClient.GetCore()->GetHullType(pPlayer->LastSeenShipType());
  362. if (phull == NULL)
  363. return "<bug>";
  364. else
  365. return phull->GetName();
  366. }
  367. else
  368. {
  369. return "";
  370. }
  371. }
  372. static bool CanKeepCurrentShip(ImodelIGC* pmodel)
  373. {
  374. if (pmodel->GetObjectType() == OT_ship)
  375. {
  376. // they can keep their ship only if they don't have one or are in a lifepod
  377. return trekClient.GetShip()->GetParentShip() != NULL
  378. || trekClient.GetShip()->GetBaseHullType()->HasCapability(c_habmLifepod);
  379. }
  380. else
  381. {
  382. IstationIGC* pstation;
  383. CastTo(pstation, pmodel);
  384. IhullTypeIGC* pht = trekClient.GetShip()->GetBaseHullType();
  385. // they can keep their current ship if they are already in this station or
  386. // if the station can build the hull which they currently have.
  387. return trekClient.GetShip()->GetStation() == pstation
  388. || pht && pstation->CanBuy(pht);
  389. }
  390. }
  391. static ZString SectorName(ImodelIGC* pmodel)
  392. {
  393. IclusterIGC* psector;
  394. if (pmodel->GetObjectType() == OT_ship)
  395. {
  396. PlayerInfo* pplayer = (PlayerInfo*)((IshipIGC*)pmodel)->GetPrivateData();
  397. psector = trekClient.GetCore()->GetCluster(pplayer->LastSeenSector());
  398. }
  399. else
  400. {
  401. psector = pmodel->GetCluster();
  402. }
  403. if (psector == NULL)
  404. return "unknown";
  405. else
  406. return psector->GetName();
  407. }
  408. static int MannedTurrets(IshipIGC* pshipParent)
  409. {
  410. // loop through all of the ships on that side and count the ones
  411. // that are turrets of this.
  412. int cMannedTurrets = 0;
  413. const ShipListIGC* shipList = pshipParent->GetSide()->GetShips();
  414. for (const ShipLinkIGC* lShip = shipList->first(); lShip; lShip = lShip->next())
  415. {
  416. IshipIGC* pship = lShip->data();
  417. PlayerInfo* pplayer = (PlayerInfo*)pship->GetPrivateData();
  418. if (pplayer->LastSeenState() == c_ssTurret)
  419. {
  420. PlayerInfo* pplayerParent = trekClient.FindPlayer(pplayer->LastSeenParent());
  421. if (pplayerParent && pplayerParent->GetShip() == pshipParent)
  422. cMannedTurrets++;
  423. }
  424. }
  425. return cMannedTurrets;
  426. }
  427. static int NumTurrets(IshipIGC* pship)
  428. {
  429. PlayerInfo* pPlayer = (PlayerInfo*)pship->GetPrivateData();
  430. const IhullTypeIGC* pHullType = trekClient.GetCore()->GetHullType(pPlayer->LastSeenShipType());
  431. // if we have a real ship...
  432. if (pHullType)
  433. return pHullType->GetMaxWeapons() - pHullType->GetMaxFixedWeapons();
  434. else
  435. return 0;
  436. }
  437. };
  438. //////////////////////////////////////////////////////////////////////////////
  439. //
  440. // Constructor
  441. //
  442. //////////////////////////////////////////////////////////////////////////////
  443. TRef<IObject> CreateTeleportPane(Modeler* pmodeler)
  444. {
  445. return (IItemEvent::Sink*)new TeleportPane(pmodeler);
  446. }