Bank.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /***********************************************************************
  2. *
  3. * SPACE TRADER 1.2.0
  4. *
  5. * Bank.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. // Maximum loan
  37. // *************************************************************************
  38. static long MaxLoan( void )
  39. {
  40. return (PoliceRecordScore >= CLEANSCORE ?
  41. min( 25000L, max( 1000L, ((CurrentWorth() / 10L) / 500L) * 500L ) ) : 500L);
  42. }
  43. // *************************************************************************
  44. // Lending money
  45. // *************************************************************************
  46. static void GetLoan( long Loan )
  47. {
  48. long Amount;
  49. Amount = min( MaxLoan() - Debt, Loan );
  50. Credits += Amount;
  51. Debt += Amount;
  52. }
  53. // *************************************************************************
  54. // Paying back
  55. // *************************************************************************
  56. static void PayBack( long Cash )
  57. {
  58. long Amount;
  59. Amount = min( Debt, Cash );
  60. Amount = min( Amount, Credits );
  61. Credits -= Amount;
  62. Debt -= Amount;
  63. }
  64. // *************************************************************************
  65. // Show the Bank screen
  66. // *************************************************************************
  67. static void ShowBank( void )
  68. {
  69. FormPtr frmP;
  70. frmP = FrmGetActiveForm();
  71. RectangularShortcuts( frmP, BankBButton );
  72. if (Debt <= 0)
  73. FrmHideObject( frmP, FrmGetObjectIndex( frmP, BankPayBackButton ) );
  74. else
  75. FrmShowObject( frmP, FrmGetObjectIndex( frmP, BankPayBackButton ) );
  76. if (Insurance)
  77. {
  78. FrmHideObject( frmP, FrmGetObjectIndex( frmP, BankBuyInsuranceButton ) );
  79. FrmShowObject( frmP, FrmGetObjectIndex( frmP, BankStopInsuranceButton ) );
  80. }
  81. else
  82. {
  83. FrmShowObject( frmP, FrmGetObjectIndex( frmP, BankBuyInsuranceButton ) );
  84. FrmHideObject( frmP, FrmGetObjectIndex( frmP, BankStopInsuranceButton ) );
  85. }
  86. FrmDrawForm( frmP );
  87. EraseRectangle( 80, 32, 80, 26 );
  88. StrIToA( SBuf, Debt );
  89. StrCat( SBuf, " cr." );
  90. DrawChars( SBuf, 80, 32 );
  91. StrIToA( SBuf, MaxLoan() );
  92. StrCat( SBuf, " cr." );
  93. DrawChars( SBuf, 80, 46 );
  94. EraseRectangle( 80, 100, 80, 40 );
  95. StrIToA( SBuf, CurrentShipPriceWithoutCargo( true ) );
  96. StrCat( SBuf, " cr. " );
  97. DrawChars( SBuf, 80, 100 );
  98. StrIToA( SBuf, min( NoClaim, 90 ) );
  99. StrCat( SBuf, "%" );
  100. if (NoClaim == 90)
  101. StrCat( SBuf, " (maximum)" );
  102. DrawChars( SBuf, 80, 114 );
  103. StrIToA( SBuf, InsuranceMoney() );
  104. StrCat( SBuf, " cr. daily" );
  105. DrawChars( SBuf, 80, 128 );
  106. DisplayTradeCredits();
  107. }
  108. // *************************************************************************
  109. // Handling of events on the Bank screen
  110. // *************************************************************************
  111. Boolean BankFormHandleEvent(EventPtr eventP)
  112. {
  113. Boolean handled = false;
  114. FormPtr frmP;
  115. int d;
  116. long Amount;
  117. Handle AmountH;
  118. switch (eventP->eType)
  119. {
  120. case frmOpenEvent:
  121. case frmUpdateEvent:
  122. ShowBank();
  123. handled = true;
  124. break;
  125. case ctlSelectEvent:
  126. if (eventP->data.ctlSelect.controlID == BankGetLoanButton)
  127. {
  128. if (Debt >= MaxLoan())
  129. {
  130. FrmAlert( DebtTooHighAlert );
  131. handled = true;
  132. break;
  133. }
  134. frmP = FrmInitForm( GetLoanForm );
  135. AmountH = (Handle) SetField( frmP, GetLoanGetLoanField, "", 6, true );
  136. StrCopy( SBuf, "You can borrow up to " );
  137. StrIToA( SBuf2, (MaxLoan() - Debt) );
  138. StrCat( SBuf, SBuf2 );
  139. StrCat( SBuf, " credits." );
  140. setLabelText( frmP, GetLoanMaxLoanLabel, SBuf );
  141. d = FrmDoDialog( frmP );
  142. GetField( frmP, GetLoanGetLoanField, SBuf, AmountH );
  143. if (SBuf[0] == '\0')
  144. Amount = 0;
  145. else
  146. Amount = StrAToI( SBuf );
  147. FrmDeleteForm( frmP );
  148. if (d == GetLoanEverythingButton)
  149. GetLoan( MaxLoan() );
  150. else if (d != GetLoanNothingButton)
  151. GetLoan( Amount );
  152. }
  153. else if (eventP->data.ctlSelect.controlID == BankPayBackButton)
  154. {
  155. if (Debt <= 0)
  156. {
  157. FrmAlert( NoDebtAlert );
  158. handled = true;
  159. break;
  160. }
  161. frmP = FrmInitForm( PayBackForm );
  162. AmountH = (Handle) SetField( frmP, PayBackPayBackField, "", 6, true );
  163. StrCopy( SBuf, "You have a debt of " );
  164. StrIToA( SBuf2, Debt );
  165. StrCat( SBuf, SBuf2 );
  166. StrCat( SBuf, " credits." );
  167. setLabelText( frmP, PayBackMaxDebtLabel, SBuf );
  168. d = FrmDoDialog( frmP );
  169. GetField( frmP, PayBackPayBackField, SBuf, AmountH );
  170. if (SBuf[0] == '\0')
  171. Amount = 0;
  172. else
  173. Amount = StrAToI( SBuf );
  174. FrmDeleteForm( frmP );
  175. if (d == PayBackEverythingButton)
  176. PayBack( 99999 );
  177. else if (d != PayBackNothingButton)
  178. PayBack( Amount );
  179. }
  180. else if (eventP->data.ctlSelect.controlID == BankBuyInsuranceButton)
  181. {
  182. if (!EscapePod)
  183. {
  184. FrmAlert( NoEscapePodAlert );
  185. handled = true;
  186. break;
  187. }
  188. Insurance = true;
  189. }
  190. else if (eventP->data.ctlSelect.controlID == BankStopInsuranceButton)
  191. {
  192. if (FrmAlert( StopInsuranceAlert ) == StopInsuranceNo)
  193. {
  194. handled = true;
  195. break;
  196. }
  197. Insurance = false;
  198. NoClaim = 0;
  199. }
  200. ShowBank();
  201. handled = true;
  202. break;
  203. default:
  204. break;
  205. }
  206. return handled;
  207. }