Shipyard.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /***********************************************************************
  2. *
  3. * SPACE TRADER 1.2.0
  4. *
  5. * Shipyard.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. // *************************************************************************
  35. // Shipyard.c - Shipyard Module.
  36. //
  37. // Boolean ShipYardFormHandleEvent( EventPtr eventP )
  38. // void BuyRepairs( int Amount )
  39. //
  40. // Static Local Functions
  41. // -------------------------------
  42. // int GetAmountForRepairs( void )
  43. // int GetAmountForFuel( void )
  44. // void ShowShipYard( void )
  45. //
  46. // *************************************************************************
  47. #include "external.h"
  48. // *************************************************************************
  49. // Let the commander indicate how much he wants to spend on repairs
  50. // *************************************************************************
  51. static int GetAmountForRepairs( void )
  52. {
  53. FormPtr frm;
  54. int d, Amount;
  55. Handle AmountH;
  56. frm = FrmInitForm( BuyRepairsForm );
  57. AmountH = (Handle) SetField( frm, BuyRepairsForRepairsField, "", 5, true );
  58. d = FrmDoDialog( frm );
  59. GetField( frm, BuyRepairsForRepairsField, SBuf, AmountH );
  60. if (SBuf[0] == '\0')
  61. Amount = 0;
  62. else
  63. Amount = StrAToI( SBuf );
  64. FrmDeleteForm( frm );
  65. if (d == BuyFuelAllButton)
  66. return( 9999 );
  67. else if (d == BuyFuelNoneButton)
  68. return( 0 );
  69. return( Amount );
  70. }
  71. // *************************************************************************
  72. // Let the commander indicate how much he wants to spend on fuel
  73. // *************************************************************************
  74. static int GetAmountForFuel( void )
  75. {
  76. FormPtr frm;
  77. int d, Amount;
  78. Handle AmountH;
  79. frm = FrmInitForm( BuyFuelForm );
  80. AmountH = (Handle) SetField( frm, BuyFuelForFuelField, "", 4, true );
  81. d = FrmDoDialog( frm );
  82. GetField( frm, BuyFuelForFuelField, SBuf, AmountH );
  83. if (SBuf[0] == '\0')
  84. Amount = 0;
  85. else
  86. Amount = StrAToI( SBuf );
  87. FrmDeleteForm( frm );
  88. if (d == BuyFuelAllButton)
  89. return( 999 );
  90. else if (d == BuyFuelNoneButton)
  91. return( 0 );
  92. return( Amount );
  93. }
  94. // *************************************************************************
  95. // Determine ship's hull strength
  96. // *************************************************************************
  97. long GetHullStrength(void)
  98. {
  99. if (ScarabStatus == 3)
  100. return Shiptype[Ship.Type].HullStrength + UPGRADEDHULL;
  101. else
  102. return Shiptype[Ship.Type].HullStrength;
  103. }
  104. // *************************************************************************
  105. // Display the Ship Yard form.
  106. // Modified by SRA 04/19/01 - DisplayTradeCredits if Enabled
  107. // *************************************************************************
  108. static void ShowShipYard( void )
  109. {
  110. FormPtr frmP;
  111. frmP = FrmGetActiveForm();
  112. RectangularShortcuts( frmP, ShipYardBButton );
  113. if (GetFuel() < GetFuelTanks())
  114. {
  115. FrmShowObject( frmP, FrmGetObjectIndex( frmP, ShipYardBuyFuelButton ) );
  116. FrmShowObject( frmP, FrmGetObjectIndex( frmP, ShipYardFullTankButton ) );
  117. }
  118. else
  119. {
  120. FrmHideObject( frmP, FrmGetObjectIndex( frmP, ShipYardBuyFuelButton ) );
  121. FrmHideObject( frmP, FrmGetObjectIndex( frmP, ShipYardFullTankButton ) );
  122. }
  123. if (Ship.Hull < GetHullStrength())
  124. {
  125. FrmShowObject( frmP, FrmGetObjectIndex( frmP, ShipYardRepairButton ) );
  126. FrmShowObject( frmP, FrmGetObjectIndex( frmP, ShipYardFullRepairsButton ) );
  127. }
  128. else
  129. {
  130. FrmHideObject( frmP, FrmGetObjectIndex( frmP, ShipYardRepairButton ) );
  131. FrmHideObject( frmP, FrmGetObjectIndex( frmP, ShipYardFullRepairsButton ) );
  132. }
  133. if (CURSYSTEM.TechLevel >= Shiptype[0].MinTechLevel)
  134. {
  135. FrmHideObject( frmP, FrmGetObjectIndex( frmP, ShipYardShipInformationButton ) );
  136. FrmShowObject( frmP, FrmGetObjectIndex( frmP, ShipYardBuyNewShipButton ) );
  137. }
  138. else
  139. {
  140. FrmHideObject( frmP, FrmGetObjectIndex( frmP, ShipYardBuyNewShipButton ) );
  141. FrmShowObject( frmP, FrmGetObjectIndex( frmP, ShipYardShipInformationButton ) );
  142. }
  143. if (EscapePod || ToSpend() < 2000 || CURSYSTEM.TechLevel < Shiptype[0].MinTechLevel)
  144. FrmHideObject( frmP, FrmGetObjectIndex( frmP, ShipYardEscapePodButton ) );
  145. else
  146. FrmShowObject( frmP, FrmGetObjectIndex( frmP, ShipYardEscapePodButton ) );
  147. FrmDrawForm( frmP );
  148. FntSetFont( stdFont );
  149. EraseRectangle( 0, 18, 160, 25 );
  150. StrCopy( SBuf, "You have fuel to fly " );
  151. SBufMultiples( GetFuel(), "parsec" );
  152. StrCat( SBuf, "." );
  153. DrawChars( SBuf, 0, 18 );
  154. if (GetFuel() < GetFuelTanks())
  155. {
  156. StrCopy( SBuf, "A full tank costs " );
  157. StrIToA( SBuf2, (GetFuelTanks() - GetFuel()) * Shiptype[Ship.Type].CostOfFuel );
  158. StrCat( SBuf, SBuf2 );
  159. StrCat( SBuf, " cr." );
  160. DrawChars( SBuf, 0, 30 );
  161. }
  162. else
  163. DrawChars( "Your tank cannot hold more fuel.", 0, 30 );
  164. EraseRectangle( 0, 60, 160, 25 );
  165. StrCopy( SBuf, "Your hull strength is at " );
  166. StrIToA( SBuf2, (Ship.Hull * 100) / GetHullStrength() );
  167. StrCat( SBuf, SBuf2 );
  168. StrCat( SBuf, "%." );
  169. DrawChars( SBuf, 0, 60 );
  170. if (Ship.Hull < GetHullStrength())
  171. {
  172. StrCopy( SBuf, "Full repair will cost " );
  173. StrIToA( SBuf2, (GetHullStrength() - Ship.Hull) *
  174. Shiptype[Ship.Type].RepairCosts );
  175. StrCat( SBuf, SBuf2 );
  176. StrCat( SBuf, " cr." );
  177. DrawChars( SBuf, 0, 72 );
  178. }
  179. else
  180. DrawChars( "No repairs are needed.", 0, 72 );
  181. EraseRectangle( 0, 102, 160, 12 );
  182. if (CURSYSTEM.TechLevel >= Shiptype[0].MinTechLevel)
  183. DrawChars( "There are new ships for sale.", 0, 102 );
  184. else
  185. DrawChars( "No new ships are for sale.", 0, 102 );
  186. #ifdef _STRA_SHIPYARDCREDITS_
  187. DisplayTradeCredits(); // SRA 04/19/01
  188. #endif
  189. EraseRectangle( 0, 132, 160, 12 );
  190. if (EscapePod)
  191. DrawChars( "You have an escape pod installed.", 0, 132 );
  192. else if (CURSYSTEM.TechLevel < Shiptype[0].MinTechLevel)
  193. DrawChars( "No escape pods are for sale.", 0, 132 );
  194. else if (ToSpend() < 2000)
  195. DrawChars( "You need 2000 cr. for an escape pod.", 0, 132 );
  196. else
  197. DrawChars( "You can buy an escape pod for 2000 cr.", 0, 132 );
  198. }
  199. // *************************************************************************
  200. // Repair Ship for Amount credits
  201. // *************************************************************************
  202. void BuyRepairs( int Amount )
  203. {
  204. int MaxRepairs;
  205. int Percentage;
  206. MaxRepairs = (GetHullStrength() - Ship.Hull) *
  207. Shiptype[Ship.Type].RepairCosts;
  208. if (Amount > MaxRepairs)
  209. Amount = MaxRepairs;
  210. if (Amount > Credits)
  211. Amount = Credits;
  212. Percentage = Amount / Shiptype[Ship.Type].RepairCosts;
  213. Ship.Hull += Percentage;
  214. Credits -= Percentage * Shiptype[Ship.Type].RepairCosts;
  215. }
  216. // *************************************************************************
  217. // Ship Yard Form Event Handler.
  218. // *************************************************************************
  219. Boolean ShipYardFormHandleEvent( EventPtr eventP )
  220. {
  221. Boolean handled = false;
  222. int Amount;
  223. switch (eventP->eType)
  224. {
  225. case frmOpenEvent:
  226. ShowShipYard();
  227. handled = true;
  228. break;
  229. case ctlSelectEvent:
  230. switch (eventP->data.ctlSelect.controlID)
  231. {
  232. case ShipYardBuyFuelButton:
  233. Amount = GetAmountForFuel();
  234. if (Amount > 0)
  235. BuyFuel( Amount );
  236. ShowShipYard();
  237. break;
  238. case ShipYardFullTankButton:
  239. BuyFuel( 999 );
  240. ShowShipYard();
  241. break;
  242. case ShipYardRepairButton:
  243. Amount = GetAmountForRepairs();
  244. if (Amount > 0)
  245. BuyRepairs( Amount );
  246. ShowShipYard();
  247. break;
  248. case ShipYardFullRepairsButton:
  249. BuyRepairs( 9999 );
  250. ShowShipYard();
  251. break;
  252. case ShipYardBuyNewShipButton:
  253. CurForm = BuyShipForm;
  254. FrmGotoForm( CurForm );
  255. break;
  256. case ShipYardShipInformationButton:
  257. CurForm = BuyShipForm;
  258. FrmGotoForm( CurForm );
  259. break;
  260. case ShipYardEscapePodButton:
  261. if (FrmAlert( BuyEscapePodAlert ) == BuyEscapePodYes)
  262. {
  263. Credits -= 2000;
  264. EscapePod = true;
  265. ShowShipYard();
  266. }
  267. break;
  268. }
  269. handled = true;
  270. break;
  271. default:
  272. break;
  273. }
  274. return handled;
  275. }