BuyShipEvent.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /***********************************************************************
  2. *
  3. * SPACE TRADER 1.2.0
  4. *
  5. * BuyShipEvent.c
  6. *
  7. * Copyright (C) 2000-2002 Pieter Spronck, All Rights Reserved
  8. *
  9. * Additional coding by Sam Anderson (rulez2@home.com)
  10. * Additional coding by Samuel Goldstein (palm@fogbound.net)
  11. *
  12. * Some code of Matt Lee's Dope Wars program has been used.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. *
  28. * You can contact the author at space_trader@hotmail.com
  29. *
  30. * For those who are familiar with the classic game Elite: many of the
  31. * ideas in Space Trader are heavily inspired by Elite.
  32. *
  33. **********************************************************************/
  34. #include "external.h"
  35. // *************************************************************************
  36. // Create a new ship.
  37. // *************************************************************************
  38. static void CreateShip( int Index )
  39. {
  40. int i;
  41. Ship.Type = Index;
  42. for (i=0; i<MAXWEAPON; ++i)
  43. {
  44. Ship.Weapon[i] = -1;
  45. }
  46. for (i=0; i<MAXSHIELD; ++i)
  47. {
  48. Ship.Shield[i] = -1;
  49. Ship.ShieldStrength[i] = 0;
  50. }
  51. for (i=0; i<MAXGADGET; ++i)
  52. {
  53. Ship.Gadget[i] = -1;
  54. }
  55. for (i=0; i<MAXTRADEITEM; ++i)
  56. {
  57. Ship.Cargo[i] = 0;
  58. BuyingPrice[i] = 0;
  59. }
  60. Ship.Fuel = GetFuelTanks();
  61. Ship.Hull = Shiptype[Ship.Type].HullStrength;
  62. }
  63. // *************************************************************************
  64. // Buy a new ship.
  65. // *************************************************************************
  66. static void BuyShip( int Index )
  67. {
  68. CreateShip( Index );
  69. Credits -= ShipPrice[Index];
  70. if (ScarabStatus == 3)
  71. ScarabStatus = 0;
  72. }
  73. // *************************************************************************
  74. // Determine Ship Prices depending on tech level of current system.
  75. // *************************************************************************
  76. static void DetermineShipPrices( void )
  77. {
  78. int i;
  79. for (i=0; i<MAXSHIPTYPE; ++i)
  80. {
  81. if (Shiptype[i].MinTechLevel <= CURSYSTEM.TechLevel)
  82. {
  83. ShipPrice[i] = BASESHIPPRICE( i ) - CurrentShipPrice( false );
  84. if (ShipPrice[i] == 0)
  85. ShipPrice[i] = 1;
  86. }
  87. else
  88. ShipPrice[i] = 0;
  89. }
  90. }
  91. // *************************************************************************
  92. // You get a Flea
  93. // *************************************************************************
  94. void CreateFlea( void )
  95. {
  96. int i;
  97. CreateShip( 0 );
  98. for (i=1; i<MAXCREW; ++i)
  99. Ship.Crew[i] = -1;
  100. EscapePod = false;
  101. Insurance = false;
  102. NoClaim = 0;
  103. }
  104. static void DrawBuyShipForm()
  105. {
  106. FormPtr frmP = FrmGetActiveForm();
  107. int i, j;
  108. RectangularShortcuts( frmP, BuyShipBButton );
  109. DetermineShipPrices();
  110. for (i=0; i<MAXSHIPTYPE; ++i)
  111. {
  112. RectangularButton( frmP, BuyShipInfo0Button + i );
  113. RectangularButton( frmP, BuyShipBuy0Button + i );
  114. if (ShipPrice[i] == 0 || Ship.Type == i)
  115. FrmHideObject( frmP, FrmGetObjectIndex( frmP, BuyShipBuy0Button + i ) );
  116. else
  117. FrmShowObject( frmP, FrmGetObjectIndex( frmP, BuyShipBuy0Button + i ) );
  118. FrmShowObject( frmP, FrmGetObjectIndex( frmP, BuyShipInfo0Button + i ) );
  119. }
  120. FrmDrawForm( frmP );
  121. for (i=0; i<MAXSHIPTYPE; ++i)
  122. {
  123. FntSetFont( stdFont );
  124. DrawChars( Shiptype[i].Name, 30, 17+i*13 );
  125. EraseRectangle( 110, 17+i*13, 56, 9 );
  126. StrIToA( SBuf, ShipPrice[i] );
  127. StrCat( SBuf, " cr." );
  128. j = MAXDIGITS - StrLen( SBuf );
  129. if (ShipPrice[i] == 0)
  130. DrawChars( "not sold", 122, 17+i*13 );
  131. else if (Ship.Type == i)
  132. DrawChars( "got one", 123, 17+i*13 );
  133. else
  134. DrawChars( SBuf, 124+j*5+(ShipPrice[i] < 0 ? 1 : 0), 17+i*13 );
  135. }
  136. DisplayTradeCredits();
  137. if (Ship.Tribbles > 0 && !TribbleMessage)
  138. {
  139. FrmAlert( ShipNotWorthMuchAlert );
  140. TribbleMessage = true;
  141. }
  142. }
  143. // *************************************************************************
  144. // Handling of the events of the Buy Ship form.
  145. // *************************************************************************
  146. Boolean BuyShipFormHandleEvent( EventPtr eventP )
  147. {
  148. Boolean handled = false;
  149. Boolean addLightning, addCompactor, addMorganLaser;
  150. Boolean hasLightning, hasCompactor, hasMorganLaser;
  151. long extra;
  152. int d, i, j;
  153. switch (eventP->eType)
  154. {
  155. case frmOpenEvent:
  156. case frmUpdateEvent:
  157. DrawBuyShipForm();
  158. handled = true;
  159. break;
  160. case ctlSelectEvent:
  161. if (eventP->data.ctlSelect.controlID >= BuyShipInfo0Button &&
  162. eventP->data.ctlSelect.controlID <= BuyShipInfo9Button)
  163. {
  164. SelectedShipType = eventP->data.ctlSelect.controlID - BuyShipInfo0Button;
  165. CurForm = ShiptypeInfoForm;
  166. FrmGotoForm( CurForm );
  167. }
  168. else if (eventP->data.ctlSelect.controlID >= BuyShipBuy0Button &&
  169. eventP->data.ctlSelect.controlID <= BuyShipBuy9Button)
  170. {
  171. j = 0;
  172. for (i=0; i<MAXCREW; ++i)
  173. if (Ship.Crew[i] >= 0)
  174. ++j;
  175. if (ShipPrice[eventP->data.ctlSelect.controlID - BuyShipBuy0Button] == 0)
  176. FrmAlert( ShipNotAvailableAlert );
  177. else if ((ShipPrice[eventP->data.ctlSelect.controlID - BuyShipBuy0Button] > 0) &&
  178. (Debt > 0))
  179. FrmAlert( YoureInDebtAlert );
  180. else if (ShipPrice[eventP->data.ctlSelect.controlID - BuyShipBuy0Button] > ToSpend())
  181. FrmAlert( CantBuyShipAlert );
  182. else if ((JarekStatus == 1) && (Shiptype[eventP->data.ctlSelect.controlID -
  183. BuyShipBuy0Button].CrewQuarters < 2))
  184. FrmCustomAlert( PassengerNeedsQuartersAlert, "Ambassador Jarek", NULL, NULL );
  185. else if ((WildStatus == 1) && (Shiptype[eventP->data.ctlSelect.controlID -
  186. BuyShipBuy0Button].CrewQuarters < 2))
  187. FrmCustomAlert( PassengerNeedsQuartersAlert, "Jonathan Wild", NULL, NULL );
  188. else if (ReactorStatus > 0 && ReactorStatus < 21)
  189. FrmAlert ( CantSellShipWithReactorAlert );
  190. else
  191. {
  192. extra = 0;
  193. hasLightning = false;
  194. hasCompactor = false;
  195. hasMorganLaser = false;
  196. addLightning = false;
  197. addCompactor = false;
  198. addMorganLaser = false;
  199. if (HasShield( &Ship, LIGHTNINGSHIELD ))
  200. {
  201. if (Shiptype[eventP->data.ctlSelect.controlID - BuyShipBuy0Button].ShieldSlots == 0)
  202. {
  203. // can't transfer the Lightning Shields. How often would this happen?
  204. FrmCustomAlert(CantTransferSlotAlert, Shiptype[eventP->data.ctlSelect.controlID -
  205. BuyShipBuy0Button].Name, "Lightning Shield", "Shield");
  206. }
  207. hasLightning = true;
  208. extra += 30000;
  209. }
  210. if (HasGadget( &Ship, FUELCOMPACTOR ))
  211. {
  212. if (Shiptype[eventP->data.ctlSelect.controlID - BuyShipBuy0Button].GadgetSlots == 0)
  213. {
  214. // can't transfer the Fuel Compactor
  215. FrmCustomAlert(CantTransferSlotAlert, Shiptype[eventP->data.ctlSelect.controlID -
  216. BuyShipBuy0Button].Name, "Fuel Compactor", "Gadget");
  217. }
  218. hasCompactor = true;
  219. extra += 20000;
  220. }
  221. if (HasWeapon( &Ship, MORGANLASERWEAPON, true ))
  222. {
  223. if (Shiptype[eventP->data.ctlSelect.controlID - BuyShipBuy0Button].WeaponSlots == 0)
  224. {
  225. // can't transfer the Laser
  226. FrmCustomAlert(CantTransferSlotAlert, Shiptype[eventP->data.ctlSelect.controlID -
  227. BuyShipBuy0Button].Name, "Morgan's Laser", "Weapon");
  228. }
  229. extra += 33333;
  230. hasMorganLaser = true;
  231. }
  232. if (ShipPrice[eventP->data.ctlSelect.controlID - BuyShipBuy0Button] + extra > ToSpend())
  233. FrmCustomAlert( CantBuyShipWithEquipmentAlert, SBuf, NULL, NULL );
  234. extra = 0;
  235. if (hasLightning && Shiptype[eventP->data.ctlSelect.controlID - BuyShipBuy0Button].ShieldSlots > 0)
  236. {
  237. if (ShipPrice[eventP->data.ctlSelect.controlID - BuyShipBuy0Button] + extra <= ToSpend())
  238. {
  239. d = FrmAlert( TransferLightningShieldAlert );
  240. if (d == 0)
  241. {
  242. addLightning = true;
  243. extra += 30000;
  244. }
  245. }
  246. else
  247. {
  248. FrmCustomAlert ( CantTransferAlert, "Lightning Shield", NULL, NULL );
  249. }
  250. }
  251. if (hasCompactor && Shiptype[eventP->data.ctlSelect.controlID - BuyShipBuy0Button].GadgetSlots > 0)
  252. {
  253. if (ShipPrice[eventP->data.ctlSelect.controlID - BuyShipBuy0Button] + extra <= ToSpend())
  254. {
  255. d = FrmAlert( TransferFuelCompactorAlert );
  256. if (d == 0)
  257. {
  258. addCompactor = true;
  259. extra += 20000;
  260. }
  261. }
  262. else
  263. {
  264. FrmCustomAlert( CantTransferAlert, "Fuel Compactor", NULL, NULL);
  265. }
  266. }
  267. if (hasMorganLaser && Shiptype[eventP->data.ctlSelect.controlID - BuyShipBuy0Button].WeaponSlots > 0)
  268. {
  269. if (ShipPrice[eventP->data.ctlSelect.controlID - BuyShipBuy0Button] + extra <= ToSpend())
  270. {
  271. d = FrmAlert( TransferMorganLaserAlert );
  272. if (d == 0)
  273. {
  274. addMorganLaser = true;
  275. extra += 33333;
  276. }
  277. }
  278. else
  279. {
  280. FrmCustomAlert( CantTransferAlert, "Morgan's Laser", NULL, NULL);
  281. }
  282. }
  283. /*
  284. if (ShipPrice[eventP->data.ctlSelect.controlID - BuyShipBuy0Button] + extra > ToSpend())
  285. FrmCustomAlert( CantBuyShipWithEquipmentAlert, SBuf, NULL, NULL );
  286. */
  287. if (j > Shiptype[eventP->data.ctlSelect.controlID -
  288. BuyShipBuy0Button].CrewQuarters)
  289. FrmAlert( TooManyCrewmembersAlert );
  290. else
  291. {
  292. /*
  293. frmP = FrmInitForm( TradeInShipForm );
  294. StrCopy( SBuf, Shiptype[Ship.Type].Name );
  295. StrCat( SBuf, " for a new " );
  296. StrCat( SBuf, Shiptype[eventP->data.ctlSelect.controlID -
  297. BuyShipBuy0Button].Name );
  298. StrCat( SBuf, "?" );
  299. setLabelText( frmP, TradeInShipTradeInShipLabel, SBuf );
  300. d = FrmDoDialog( frmP );
  301. FrmDeleteForm( frmP );
  302. */
  303. if (addCompactor || addLightning || addMorganLaser)
  304. {
  305. StrCopy(SBuf, ", and transfer your unique equipment to the new ship?");
  306. }
  307. else
  308. {
  309. StrCopy(SBuf, "?");
  310. }
  311. d = FrmCustomAlert( TradeShipAlert, Shiptype[Ship.Type].Name,
  312. Shiptype[eventP->data.ctlSelect.controlID - BuyShipBuy0Button].Name,
  313. SBuf);
  314. if (d == TradeShipYes)
  315. {
  316. BuyShip( eventP->data.ctlSelect.controlID - BuyShipBuy0Button );
  317. Credits -= extra;
  318. if (addCompactor)
  319. Ship.Gadget[0] = FUELCOMPACTOR;
  320. if (addLightning)
  321. Ship.Shield[0] = LIGHTNINGSHIELD;
  322. if (addMorganLaser)
  323. Ship.Weapon[0] = MORGANLASERWEAPON;
  324. Ship.Tribbles = 0;
  325. CurForm = BuyShipForm;
  326. FrmGotoForm( CurForm );
  327. }
  328. }
  329. }
  330. }
  331. handled = true;
  332. break;
  333. default:
  334. break;
  335. }
  336. return handled;
  337. }