Arms Dealer Init.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851
  1. #ifdef PRECOMPILEDHEADERS
  2. #include "Tactical All.h"
  3. #else
  4. #include "Types.h"
  5. #include "stdlib.h"
  6. #include "Arms Dealer Init.h"
  7. #include "String.h"
  8. #include "Debug.h"
  9. #include "Random.h"
  10. #include "Weapons.h"
  11. #include "FileMan.h"
  12. #include "Game Clock.h"
  13. #include "ArmsDealerInvInit.h"
  14. #include "Message.h"
  15. #include "soldier profile.h"
  16. #include "Handle Items.h"
  17. #endif
  18. // To reduce memory fragmentation from frequent MemRealloc(), we allocate memory for more than one special slot each
  19. // time we run out of space. Odds are that if we need one, we'll need another soon.
  20. #define SPECIAL_ITEMS_ALLOCED_AT_ONCE 3
  21. // Once allocated, the special item slots remain allocated for the duration of the game, or until the dealer dies.
  22. // This is a little bit wasteful, but saves an awful lot of hassles, and avoid unnecessary memory fragmentation
  23. #define MIN_REPAIR_TIME_IN_MINUTES 15 // minutes
  24. #define MIN_REPAIR_COST 10 // dollars
  25. // price classes
  26. #define PRICE_CLASS_JUNK 0
  27. #define PRICE_CLASS_CHEAP 1
  28. #define PRICE_CLASS_EXPENSIVE 2
  29. void ConvertCreatureBloodToElixir( void );
  30. UINT8 gubLastSpecialItemAddedAtElement = 255;
  31. // THIS STRUCTURE HAS UNCHANGING INFO THAT DOESN'T GET SAVED/RESTORED/RESET
  32. ARMS_DEALER_INFO ArmsDealerInfo[ NUM_ARMS_DEALERS ] =
  33. {
  34. //Buying Selling Merc ID# Type Initial Flags
  35. //Price Price Of Cash
  36. //Modifier Modifier Dealer
  37. /* Tony */ { 0.75f, 1.25f, TONY, ARMS_DEALER_BUYS_SELLS, 15000, ARMS_DEALER_SOME_USED_ITEMS | ARMS_DEALER_GIVES_CHANGE },
  38. /* Franz Hinkle */ { 1.0f, 1.5f, FRANZ, ARMS_DEALER_BUYS_SELLS, 5000, ARMS_DEALER_SOME_USED_ITEMS | ARMS_DEALER_GIVES_CHANGE },
  39. /* Keith Hemps */ { 0.75f, 1.0f, KEITH, ARMS_DEALER_BUYS_SELLS, 1500, ARMS_DEALER_ONLY_USED_ITEMS | ARMS_DEALER_GIVES_CHANGE },
  40. /* Jake Cameron */ { 0.8f, 1.1f, JAKE, ARMS_DEALER_BUYS_SELLS, 2500, ARMS_DEALER_ONLY_USED_ITEMS | ARMS_DEALER_GIVES_CHANGE },
  41. /* Gabby Mulnick*/ { 1.0f, 1.0f, GABBY, ARMS_DEALER_BUYS_SELLS, 3000, ARMS_DEALER_GIVES_CHANGE },
  42. /* Devin Connell*/ { 0.75f, 1.25f, DEVIN, ARMS_DEALER_SELLS_ONLY, 5000, ARMS_DEALER_GIVES_CHANGE },
  43. /* Howard Filmore*/ { 1.0f, 1.0f, HOWARD, ARMS_DEALER_SELLS_ONLY, 3000, ARMS_DEALER_GIVES_CHANGE },
  44. /* Sam Rozen */ { 1.0f, 1.0f, SAM, ARMS_DEALER_SELLS_ONLY, 3000, ARMS_DEALER_GIVES_CHANGE },
  45. /* Frank */ { 1.0f, 1.0f, FRANK, ARMS_DEALER_SELLS_ONLY, 500, ARMS_DEALER_ACCEPTS_GIFTS },
  46. /* Bar Bro 1 */ { 1.0f, 1.0f, HERVE, ARMS_DEALER_SELLS_ONLY, 250, ARMS_DEALER_ACCEPTS_GIFTS },
  47. /* Bar Bro 2 */ { 1.0f, 1.0f, PETER, ARMS_DEALER_SELLS_ONLY, 250, ARMS_DEALER_ACCEPTS_GIFTS },
  48. /* Bar Bro 3 */ { 1.0f, 1.0f, ALBERTO, ARMS_DEALER_SELLS_ONLY, 250, ARMS_DEALER_ACCEPTS_GIFTS },
  49. /* Bar Bro 4 */ { 1.0f, 1.0f, CARLO, ARMS_DEALER_SELLS_ONLY, 250, ARMS_DEALER_ACCEPTS_GIFTS },
  50. /* Micky O'Brien*/ { 1.0f, 1.4f, MICKY, ARMS_DEALER_BUYS_ONLY, 10000, ARMS_DEALER_HAS_NO_INVENTORY | ARMS_DEALER_GIVES_CHANGE },
  51. //Repair Repair
  52. //Speed Cost
  53. /* Arnie Brunzwell*/{ 0.1f, 0.8f, ARNIE, ARMS_DEALER_REPAIRS, 1500, ARMS_DEALER_HAS_NO_INVENTORY | ARMS_DEALER_GIVES_CHANGE },
  54. /* Fredo */ { 0.6f, 0.6f, FREDO, ARMS_DEALER_REPAIRS, 1000, ARMS_DEALER_HAS_NO_INVENTORY | ARMS_DEALER_GIVES_CHANGE },
  55. /* Perko */ { 1.0f, 0.4f, PERKO, ARMS_DEALER_REPAIRS, 1000, ARMS_DEALER_HAS_NO_INVENTORY | ARMS_DEALER_GIVES_CHANGE },
  56. /* Elgin */ { 1.0f, 1.0f, DRUGGIST, ARMS_DEALER_SELLS_ONLY, 500, ARMS_DEALER_ACCEPTS_GIFTS },
  57. /* Manny */ { 1.0f, 1.0f, MANNY, ARMS_DEALER_SELLS_ONLY, 500, ARMS_DEALER_ACCEPTS_GIFTS },
  58. };
  59. // THESE GET SAVED/RESTORED/RESET
  60. ARMS_DEALER_STATUS gArmsDealerStatus[ NUM_ARMS_DEALERS ];
  61. DEALER_ITEM_HEADER gArmsDealersInventory[ NUM_ARMS_DEALERS ][ MAXITEMS ];
  62. void InitializeOneArmsDealer( UINT8 ubArmsDealer );
  63. void AddAmmoToArmsDealerInventory( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubShotsLeft );
  64. void AddItemToArmsDealerInventory( UINT8 ubArmsDealer, UINT16 usItemIndex, SPECIAL_ITEM_INFO *pSpclItemInfo, UINT8 ubHowMany );
  65. void AddSpecialItemToArmsDealerInventoryAtElement( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubElement, SPECIAL_ITEM_INFO *pSpclItemInfo );
  66. void RemoveRandomItemFromArmsDealerInventory( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubHowMany );
  67. void DailyCheckOnItemQuantities();
  68. void SimulateArmsDealerCustomer();
  69. BOOLEAN AdjustCertainDealersInventory();
  70. void LimitArmsDealersInventory( UINT8 ubArmsDealer, UINT32 uDealerItemType, UINT8 ubMaxNumberOfItemType );
  71. void GuaranteeAtLeastOneItemOfType( UINT8 ubArmsDealer, UINT32 uiDealerItemType );
  72. void GuaranteeAtLeastXItemsOfIndex( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubHowMany );
  73. BOOLEAN AllocMemsetSpecialItemArray( DEALER_ITEM_HEADER *pDealerItem, UINT8 ubElementsNeeded );
  74. BOOLEAN ResizeSpecialItemArray( DEALER_ITEM_HEADER *pDealerItem, UINT8 ubElementsNeeded );
  75. void FreeSpecialItemArray( DEALER_ITEM_HEADER *pDealerItem );
  76. void ArmsDealerGetsFreshStock( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubNumItems );
  77. BOOLEAN ItemContainsLiquid( UINT16 usItemIndex );
  78. UINT8 DetermineDealerItemCondition( UINT8 ubArmsDealer, UINT16 usItemIndex );
  79. BOOLEAN IsItemInfoSpecial( SPECIAL_ITEM_INFO *pSpclItemInfo );
  80. BOOLEAN DoesItemAppearInDealerInventoryList( UINT8 ubArmsDealer, UINT16 usItemIndex, BOOLEAN fPurchaseFromPlayer );
  81. BOOLEAN LoadIncompleteArmsDealersStatus( HWFILE hFile, BOOLEAN fIncludesElgin, BOOLEAN fIncludesManny );
  82. //INT16 GetSpecialItemFromArmsDealerInventory( UINT8 ubArmsDealer, UINT16 usItemIndex, SPECIAL_ITEM_INFO *pSpclItemInfo );
  83. void GuaranteeMinimumAlcohol( UINT8 ubArmsDealer );
  84. BOOLEAN ItemIsARocketRifle( INT16 sItemIndex );
  85. BOOLEAN GetArmsDealerShopHours( UINT8 ubArmsDealer, UINT32 *puiOpeningTime, UINT32 *puiClosingTime );
  86. void InitAllArmsDealers()
  87. {
  88. UINT8 ubArmsDealer;
  89. //Memset all dealers' status tables to zeroes
  90. memset( gArmsDealerStatus, 0, sizeof( gArmsDealerStatus ) );
  91. //Memset all dealers' inventory tables to zeroes
  92. memset( gArmsDealersInventory, 0, sizeof( gArmsDealersInventory ) );
  93. //Initialize the initial status & inventory for each of the arms dealers
  94. for( ubArmsDealer = 0; ubArmsDealer < NUM_ARMS_DEALERS; ubArmsDealer++ )
  95. {
  96. InitializeOneArmsDealer( ubArmsDealer );
  97. }
  98. //make sure certain items are in stock and certain limits are respected
  99. AdjustCertainDealersInventory( );
  100. }
  101. void InitializeOneArmsDealer( UINT8 ubArmsDealer )
  102. {
  103. UINT16 usItemIndex;
  104. UINT8 ubNumItems=0;
  105. #ifdef JA2DEMO
  106. if ( ubArmsDealer != ARMS_DEALER_JAKE)
  107. {
  108. return;
  109. }
  110. #endif
  111. memset( &( gArmsDealerStatus[ ubArmsDealer ] ), 0, sizeof( ARMS_DEALER_STATUS ) );
  112. memset( &( gArmsDealersInventory[ ubArmsDealer ] ), 0, sizeof( DEALER_ITEM_HEADER ) * MAXITEMS );
  113. //Reset the arms dealers cash on hand to the default initial value
  114. gArmsDealerStatus[ ubArmsDealer ].uiArmsDealersCash = ArmsDealerInfo[ ubArmsDealer ].iInitialCash;
  115. //if the arms dealer isn't supposed to have any items (includes all repairmen)
  116. if( ArmsDealerInfo[ ubArmsDealer ].uiFlags & ARMS_DEALER_HAS_NO_INVENTORY )
  117. {
  118. return;
  119. }
  120. //loop through all the item types
  121. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  122. {
  123. //Can the item be sold by the arms dealer
  124. if( CanDealerTransactItem( ubArmsDealer, usItemIndex, FALSE ) )
  125. {
  126. //Setup an initial amount for the items (treat items as new, how many are used isn't known yet)
  127. ubNumItems = DetermineInitialInvItems( ubArmsDealer, usItemIndex, GetDealersMaxItemAmount( ubArmsDealer, usItemIndex ), FALSE );
  128. //if there are any initial items
  129. if( ubNumItems > 0 )
  130. {
  131. ArmsDealerGetsFreshStock( ubArmsDealer, usItemIndex, ubNumItems );
  132. }
  133. }
  134. }
  135. }
  136. void ShutDownArmsDealers()
  137. {
  138. UINT8 ubArmsDealer;
  139. UINT16 usItemIndex;
  140. // loop through all the dealers
  141. for( ubArmsDealer=0; ubArmsDealer<NUM_ARMS_DEALERS; ubArmsDealer++ )
  142. {
  143. #ifdef JA2DEMO
  144. if ( ubArmsDealer != ARMS_DEALER_JAKE)
  145. {
  146. continue;
  147. }
  148. #endif
  149. //loop through all the item types
  150. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  151. {
  152. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced > 0 )
  153. {
  154. FreeSpecialItemArray( &gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ] );
  155. }
  156. }
  157. }
  158. }
  159. BOOLEAN SaveArmsDealerInventoryToSaveGameFile( HWFILE hFile )
  160. {
  161. UINT32 uiNumBytesWritten;
  162. UINT8 ubArmsDealer;
  163. UINT16 usItemIndex;
  164. //Save the arms dealers status
  165. if (!FileWrite( hFile, gArmsDealerStatus, sizeof( gArmsDealerStatus ), &uiNumBytesWritten ))
  166. {
  167. return( FALSE );
  168. }
  169. //save the dealers inventory item headers (all at once)
  170. if (!FileWrite( hFile, gArmsDealersInventory, sizeof( gArmsDealersInventory ), &uiNumBytesWritten ))
  171. {
  172. return( FALSE );
  173. }
  174. //loop through all the dealers inventories
  175. for( ubArmsDealer=0; ubArmsDealer<NUM_ARMS_DEALERS; ubArmsDealer++ )
  176. {
  177. //loop through this dealer's individual items
  178. for(usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  179. {
  180. //if there are any special item elements allocated for this item, save them
  181. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced > 0 )
  182. {
  183. if (!FileWrite( hFile, &gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[0], sizeof( DEALER_SPECIAL_ITEM ) * gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced, &uiNumBytesWritten ))
  184. {
  185. return( FALSE );
  186. }
  187. }
  188. }
  189. }
  190. return( TRUE );
  191. }
  192. BOOLEAN LoadArmsDealerInventoryFromSavedGameFile( HWFILE hFile, BOOLEAN fIncludesElgin, BOOLEAN fIncludesManny )
  193. {
  194. UINT32 uiNumBytesRead;
  195. UINT8 ubArmsDealer;
  196. UINT16 usItemIndex;
  197. //Free all the dealers special inventory arrays
  198. ShutDownArmsDealers();
  199. // Elgin was added to the dealers list in Game Version #54, enlarging these 2 tables...
  200. // Manny was added to the dealers list in Game Version #55, enlarging these 2 tables...
  201. if ( fIncludesElgin && fIncludesManny )
  202. {
  203. // info for all dealers is in the save file
  204. //Load the arms dealers status
  205. if ( !FileRead( hFile, gArmsDealerStatus, sizeof( gArmsDealerStatus ), &uiNumBytesRead ))
  206. {
  207. return( FALSE );
  208. }
  209. //load the dealers inventory item headers (all at once)
  210. if ( !FileRead( hFile, gArmsDealersInventory, sizeof( gArmsDealersInventory ), &uiNumBytesRead ))
  211. {
  212. return( FALSE );
  213. }
  214. }
  215. else
  216. {
  217. if ( !LoadIncompleteArmsDealersStatus( hFile, fIncludesElgin, fIncludesManny ) )
  218. {
  219. return( FALSE );
  220. }
  221. }
  222. //loop through all the dealers inventories
  223. for( ubArmsDealer=0; ubArmsDealer<NUM_ARMS_DEALERS; ubArmsDealer++ )
  224. {
  225. //loop through this dealer's individual items
  226. for(usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  227. {
  228. //if there are any elements allocated for this item, load them
  229. if( gArmsDealersInventory[ubArmsDealer][usItemIndex].ubElementsAlloced > 0 )
  230. {
  231. //Allocate memory for the inventory
  232. if ( !AllocMemsetSpecialItemArray( &gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ], gArmsDealersInventory[ubArmsDealer][usItemIndex].ubElementsAlloced ))
  233. return(FALSE);
  234. if (!FileRead( hFile, &gArmsDealersInventory[ubArmsDealer][usItemIndex].SpecialItem[0], sizeof( DEALER_SPECIAL_ITEM ) * gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced, &uiNumBytesRead ))
  235. {
  236. return( FALSE );
  237. }
  238. }
  239. }
  240. }
  241. return( TRUE );
  242. }
  243. void DailyUpdateOfArmsDealersInventory()
  244. {
  245. // if Gabby has creature blood, start turning it into extra elixir
  246. ConvertCreatureBloodToElixir();
  247. //Simulate other customers buying inventory from the dealer
  248. SimulateArmsDealerCustomer();
  249. //if there are some items that are out of stock, order some more
  250. DailyCheckOnItemQuantities();
  251. //make sure certain items are in stock and certain limits are respected
  252. AdjustCertainDealersInventory( );
  253. }
  254. // Once a day, loop through each dealer's inventory items and possibly sell some
  255. void SimulateArmsDealerCustomer()
  256. {
  257. UINT8 ubArmsDealer=0;
  258. UINT16 usItemIndex;
  259. UINT8 ubItemsSold=0;
  260. UINT8 ubElement;
  261. SPECIAL_ITEM_INFO SpclItemInfo;
  262. //loop through all the arms dealers
  263. for( ubArmsDealer=0;ubArmsDealer<NUM_ARMS_DEALERS;ubArmsDealer++ )
  264. {
  265. if( gArmsDealerStatus[ ubArmsDealer ].fOutOfBusiness )
  266. continue;
  267. //if the arms dealer isn't supposed to have any items (includes all repairmen)
  268. if( ArmsDealerInfo[ ubArmsDealer ].uiFlags & ARMS_DEALER_HAS_NO_INVENTORY )
  269. continue;
  270. //loop through all items of the same type
  271. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  272. {
  273. //if there are some of these in stock
  274. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems > 0)
  275. {
  276. // first, try to sell all the new (perfect) ones
  277. if ( usItemIndex == JAR_ELIXIR )
  278. {
  279. // only allow selling of standard # of items so those converted from blood given by player will be available
  280. ubItemsSold = HowManyItemsAreSold( ubArmsDealer, usItemIndex, (UINT8) __min( 3, gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubPerfectItems), FALSE);
  281. }
  282. else
  283. {
  284. ubItemsSold = HowManyItemsAreSold( ubArmsDealer, usItemIndex, gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubPerfectItems, FALSE);
  285. }
  286. if ( ubItemsSold > 0)
  287. {
  288. // create item info describing a perfect item
  289. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  290. //Now remove that many NEW ones (condition 100) of that item
  291. RemoveItemFromArmsDealerInventory( ubArmsDealer, usItemIndex, &SpclItemInfo, ubItemsSold);
  292. }
  293. // next, try to sell all the used ones, gotta do these one at a time so we can remove them by element
  294. for ( ubElement = 0; ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  295. {
  296. // don't worry about negative condition, repairmen can't come this far, they don't sell!
  297. if ( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive )
  298. {
  299. // try selling just this one
  300. if (HowManyItemsAreSold( ubArmsDealer, usItemIndex, 1, TRUE) > 0)
  301. {
  302. //Sold, now remove that particular USED one!
  303. RemoveSpecialItemFromArmsDealerInventoryAtElement( ubArmsDealer, usItemIndex, ubElement );
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. void DailyCheckOnItemQuantities()
  312. {
  313. UINT8 ubArmsDealer;
  314. UINT16 usItemIndex;
  315. UINT8 ubMaxSupply;
  316. UINT8 ubNumItems;
  317. UINT32 uiArrivalDay;
  318. BOOLEAN fPrevElig;
  319. UINT8 ubReorderDays;
  320. //loop through all the arms dealers
  321. for( ubArmsDealer=0;ubArmsDealer<NUM_ARMS_DEALERS;ubArmsDealer++ )
  322. {
  323. if( gArmsDealerStatus[ ubArmsDealer ].fOutOfBusiness )
  324. continue;
  325. //Reset the arms dealers cash on hand to the default initial value
  326. gArmsDealerStatus[ ubArmsDealer ].uiArmsDealersCash = ArmsDealerInfo[ ubArmsDealer ].iInitialCash;
  327. //if the arms dealer isn't supposed to have any items (includes all repairmen)
  328. if( ArmsDealerInfo[ ubArmsDealer ].uiFlags & ARMS_DEALER_HAS_NO_INVENTORY )
  329. continue;
  330. //loop through all items of the same type
  331. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  332. {
  333. //if the dealer can sell the item type
  334. if( CanDealerTransactItem( ubArmsDealer, usItemIndex, FALSE ) )
  335. {
  336. //if there are no items on order
  337. if ( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubQtyOnOrder == 0 )
  338. {
  339. ubMaxSupply = GetDealersMaxItemAmount( ubArmsDealer, usItemIndex );
  340. //if the qty on hand is half the desired amount or fewer
  341. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems <= (UINT32)( ubMaxSupply / 2 ) )
  342. {
  343. // remember value of the "previously eligible" flag
  344. fPrevElig = gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].fPreviouslyEligible;
  345. //determine if the item can be restocked (assume new, use items aren't checked for until the stuff arrives)
  346. if (ItemTransactionOccurs( ubArmsDealer, usItemIndex, DEALER_BUYING, FALSE ))
  347. {
  348. // figure out how many items to reorder (items are reordered an entire batch at a time)
  349. ubNumItems = HowManyItemsToReorder( ubMaxSupply, gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems );
  350. // if this is the first day the player is eligible to have access to this thing
  351. if ( !fPrevElig )
  352. {
  353. // eliminate the ordering delay and stock the items instantly!
  354. // This is just a way to reward the player right away for making progress without the reordering lag...
  355. ArmsDealerGetsFreshStock( ubArmsDealer, usItemIndex, ubNumItems );
  356. }
  357. else
  358. {
  359. if ( ( ubArmsDealer == ARMS_DEALER_TONY ) || ( ubArmsDealer == ARMS_DEALER_DEVIN ) )
  360. {
  361. // the stuff Tony and Devin sell is imported, so it takes longer to arrive (for game balance)
  362. ubReorderDays = ( UINT8) ( 2 + Random( 2 ) ); // 2-3 days
  363. }
  364. else
  365. {
  366. ubReorderDays = ( UINT8) ( 1 + Random( 2 ) ); // 1-2 days
  367. }
  368. //Determine when the inventory should arrive
  369. uiArrivalDay = GetWorldDay() + ubReorderDays; // consider changing this to minutes
  370. // post new order
  371. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubQtyOnOrder = ubNumItems;
  372. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].uiOrderArrivalTime = uiArrivalDay;
  373. }
  374. }
  375. }
  376. }
  377. else //items are on order
  378. {
  379. //and today is the day the items come in
  380. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].uiOrderArrivalTime >= GetWorldDay() )
  381. {
  382. ArmsDealerGetsFreshStock( ubArmsDealer, usItemIndex, gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubQtyOnOrder);
  383. //reset order
  384. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubQtyOnOrder = 0;
  385. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].uiOrderArrivalTime = 0;
  386. }
  387. }
  388. }
  389. }
  390. }
  391. }
  392. void ConvertCreatureBloodToElixir( void )
  393. {
  394. UINT8 ubBloodAvailable;
  395. UINT8 ubAmountToConvert;
  396. SPECIAL_ITEM_INFO SpclItemInfo;
  397. ubBloodAvailable = gArmsDealersInventory[ ARMS_DEALER_GABBY ][ JAR_CREATURE_BLOOD ].ubTotalItems;
  398. if ( ubBloodAvailable )
  399. {
  400. // start converting blood into elixir!
  401. //ubAmountToConvert = (UINT8) __min( 5 + Random( 3 ), ubBloodAvailable );
  402. ubAmountToConvert = ubBloodAvailable;
  403. // create item info describing a perfect item
  404. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  405. //Now remove that many NEW ones (condition 100) of that item
  406. RemoveItemFromArmsDealerInventory( ARMS_DEALER_GABBY, JAR_CREATURE_BLOOD, &SpclItemInfo, ubAmountToConvert );
  407. ArmsDealerGetsFreshStock( ARMS_DEALER_GABBY, JAR_ELIXIR, ubAmountToConvert );
  408. }
  409. }
  410. BOOLEAN AdjustCertainDealersInventory( )
  411. {
  412. //Adjust Tony's items (this restocks *instantly* 1/day, doesn't use the reorder system)
  413. GuaranteeAtLeastOneItemOfType( ARMS_DEALER_TONY, ARMS_DEALER_BIG_GUNS );
  414. LimitArmsDealersInventory( ARMS_DEALER_TONY, ARMS_DEALER_BIG_GUNS, 2 );
  415. LimitArmsDealersInventory( ARMS_DEALER_TONY, ARMS_DEALER_HANDGUNCLASS, 3 );
  416. LimitArmsDealersInventory( ARMS_DEALER_TONY, ARMS_DEALER_AMMO, 8 );
  417. //Adjust all bartenders' alcohol levels to a minimum
  418. GuaranteeMinimumAlcohol( ARMS_DEALER_FRANK );
  419. GuaranteeMinimumAlcohol( ARMS_DEALER_BAR_BRO_1 );
  420. GuaranteeMinimumAlcohol( ARMS_DEALER_BAR_BRO_2 );
  421. GuaranteeMinimumAlcohol( ARMS_DEALER_BAR_BRO_3 );
  422. GuaranteeMinimumAlcohol( ARMS_DEALER_BAR_BRO_4 );
  423. GuaranteeMinimumAlcohol( ARMS_DEALER_ELGIN );
  424. GuaranteeMinimumAlcohol( ARMS_DEALER_MANNY );
  425. //make sure Sam (hardware guy) has at least one empty jar
  426. GuaranteeAtLeastXItemsOfIndex( ARMS_DEALER_SAM, JAR, 1 );
  427. if ( CheckFact( FACT_ESTONI_REFUELLING_POSSIBLE, 0 ) )
  428. {
  429. // gas is restocked regularly, unlike most items
  430. GuaranteeAtLeastXItemsOfIndex( ARMS_DEALER_JAKE, GAS_CAN, ( UINT8 ) ( 4 + Random( 3 ) ) );
  431. }
  432. //If the player hasn't bought a video camera from Franz yet, make sure Franz has one to sell
  433. if( !( gArmsDealerStatus[ ARMS_DEALER_FRANZ ].ubSpecificDealerFlags & ARMS_DEALER_FLAG__FRANZ_HAS_SOLD_VIDEO_CAMERA_TO_PLAYER ) )
  434. {
  435. GuaranteeAtLeastXItemsOfIndex( ARMS_DEALER_FRANZ, VIDEO_CAMERA, 1 );
  436. }
  437. #ifdef JA2DEMO
  438. {
  439. // Adjust Jake's inventory (for demo only)
  440. SPECIAL_ITEM_INFO SpclItemInfo;
  441. // create item info describing a perfect item
  442. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  443. // These items are to be in perfect working order (even though he's a junk dealer)
  444. AddItemToArmsDealerInventory( ARMS_DEALER_JAKE, SILENCER, &SpclItemInfo, (UINT8)( 2 + Random( 2 ) ) );
  445. AddItemToArmsDealerInventory( ARMS_DEALER_JAKE, LOCKSMITHKIT, &SpclItemInfo, 1 );
  446. // ArmsDealerGetsFreshStock( ARMS_DEALER_JAKE, SILENCER, (UINT8)( 2 + Random( 2 ) ) );
  447. // ArmsDealerGetsFreshStock( ARMS_DEALER_JAKE, LOCKSMITHKIT, 1);
  448. }
  449. #endif
  450. return( TRUE );
  451. }
  452. void LimitArmsDealersInventory( UINT8 ubArmsDealer, UINT32 uiDealerItemType, UINT8 ubMaxNumberOfItemType )
  453. {
  454. UINT16 usItemIndex=0;
  455. UINT32 uiItemsToRemove=0;
  456. SPECIAL_ITEM_INFO SpclItemInfo;
  457. UINT16 usAvailableItem[ MAXITEMS ] = { NOTHING };
  458. UINT8 ubNumberOfAvailableItem[ MAXITEMS ] = { 0 };
  459. UINT32 uiTotalNumberOfItems = 0, uiRandomChoice;
  460. UINT32 uiNumAvailableItems = 0, uiIndex;
  461. // not permitted for repair dealers - would take extra code to avoid counting items under repair!
  462. Assert( !DoesDealerDoRepairs( ubArmsDealer ) );
  463. if( gArmsDealerStatus[ ubArmsDealer ].fOutOfBusiness )
  464. return;
  465. //loop through all items of the same class and count the number in stock
  466. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  467. {
  468. //if there is some items in stock
  469. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems > 0)
  470. {
  471. //if the item is of the same dealer item type
  472. if( uiDealerItemType & GetArmsDealerItemTypeFromItemNumber( usItemIndex ) )
  473. {
  474. usAvailableItem[ uiNumAvailableItems ] = usItemIndex;
  475. //if the dealer item type is ammo
  476. if( uiDealerItemType == ARMS_DEALER_AMMO )
  477. {
  478. // all ammo of same type counts as only one item
  479. ubNumberOfAvailableItem[ uiNumAvailableItems ] = 1;
  480. uiTotalNumberOfItems++;
  481. }
  482. else
  483. {
  484. // items being repaired don't count against the limit
  485. ubNumberOfAvailableItem[ uiNumAvailableItems ] = gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems;
  486. uiTotalNumberOfItems += ubNumberOfAvailableItem[ uiNumAvailableItems ];
  487. }
  488. uiNumAvailableItems++;
  489. }
  490. }
  491. }
  492. //if there is more of the given type than we want
  493. if( uiNumAvailableItems > ubMaxNumberOfItemType )
  494. {
  495. uiItemsToRemove = uiNumAvailableItems - ubMaxNumberOfItemType;
  496. do
  497. {
  498. uiRandomChoice = Random( uiTotalNumberOfItems );
  499. for ( uiIndex = 0; uiIndex < uiNumAvailableItems; uiIndex++ )
  500. {
  501. if ( uiRandomChoice <= ubNumberOfAvailableItem[ uiIndex ] )
  502. {
  503. usItemIndex = usAvailableItem[ uiIndex ];
  504. if ( uiDealerItemType == ARMS_DEALER_AMMO )
  505. {
  506. // remove all of them, since each ammo item counts as only one "item" here
  507. // create item info describing a perfect item
  508. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  509. // ammo will always be only condition 100, there's never any in special slots
  510. RemoveItemFromArmsDealerInventory( ubArmsDealer, usItemIndex, &SpclItemInfo, gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems );
  511. }
  512. else
  513. {
  514. // pick 1 random one, don't care about its condition
  515. RemoveRandomItemFromArmsDealerInventory( ubArmsDealer, usItemIndex, 1 );
  516. }
  517. // now remove entry from the array by replacing it with the last and decrementing
  518. // the size of the array
  519. usAvailableItem[ uiIndex ] = usAvailableItem[ uiNumAvailableItems - 1 ];
  520. ubNumberOfAvailableItem[ uiIndex ] = ubNumberOfAvailableItem[ uiNumAvailableItems - 1 ];
  521. uiNumAvailableItems--;
  522. // decrement count of # of items to remove
  523. uiItemsToRemove--;
  524. break; // and out of 'for' loop
  525. }
  526. else
  527. {
  528. // next item!
  529. uiRandomChoice -= ubNumberOfAvailableItem[ uiIndex ];
  530. }
  531. }
  532. /*
  533. //loop through all items of the same type
  534. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  535. {
  536. //if there are some non-repairing items in stock
  537. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems )
  538. {
  539. //if the item is of the same dealer item type
  540. if( uiDealerItemType & GetArmsDealerItemTypeFromItemNumber( usItemIndex ) )
  541. {
  542. // a random chance that the item will be removed
  543. if( Random( 100 ) < 30 )
  544. {
  545. //remove the item
  546. //if the dealer item type is ammo
  547. if( uiDealerItemType == ARMS_DEALER_AMMO )
  548. {
  549. // remove all of them, since each ammo item counts as only one "item" here
  550. // create item info describing a perfect item
  551. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  552. // ammo will always be only condition 100, there's never any in special slots
  553. RemoveItemFromArmsDealerInventory( ubArmsDealer, usItemIndex, &SpclItemInfo, gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems );
  554. }
  555. else
  556. {
  557. // pick 1 random one, don't care about its condition
  558. RemoveRandomItemFromArmsDealerInventory( ubArmsDealer, usItemIndex, 1 );
  559. }
  560. uiItemsToRemove--;
  561. if( uiItemsToRemove == 0)
  562. break;
  563. }
  564. }
  565. }
  566. }
  567. */
  568. } while (uiItemsToRemove > 0);
  569. }
  570. }
  571. void GuaranteeAtLeastOneItemOfType( UINT8 ubArmsDealer, UINT32 uiDealerItemType )
  572. {
  573. UINT16 usItemIndex;
  574. UINT8 ubChance;
  575. BOOLEAN fFoundEligibleItemOfSameType = FALSE;
  576. BOOLEAN fItemHasBeenAdded = FALSE;
  577. BOOLEAN fFailedOnce = FALSE;
  578. UINT16 usAvailableItem[ MAXITEMS ] = { NOTHING };
  579. UINT8 ubChanceForAvailableItem[ MAXITEMS ] = { 0 };
  580. UINT32 uiTotalChances = 0;
  581. UINT32 uiNumAvailableItems = 0, uiIndex, uiRandomChoice;
  582. // not permitted for repair dealers - would take extra code to avoid counting items under repair!
  583. Assert( !DoesDealerDoRepairs( ubArmsDealer ) );
  584. if( gArmsDealerStatus[ ubArmsDealer ].fOutOfBusiness )
  585. return;
  586. //loop through all items of the same type
  587. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  588. {
  589. //if the item is of the same dealer item type
  590. if( uiDealerItemType & GetArmsDealerItemTypeFromItemNumber( usItemIndex ) )
  591. {
  592. //if there are any of these in stock
  593. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems > 0 )
  594. {
  595. //there is already at least 1 item of that type, return
  596. return;
  597. }
  598. // if he can stock it (it appears in his inventory list)
  599. if( GetDealersMaxItemAmount( ubArmsDealer, usItemIndex ) > 0)
  600. {
  601. // and the stage of the game gives him a chance to have it (assume new)
  602. ubChance = ChanceOfItemTransaction( ubArmsDealer, usItemIndex, DEALER_BUYING, FALSE );
  603. if ( ubChance > 0 )
  604. {
  605. usAvailableItem[ uiNumAvailableItems ] = usItemIndex;
  606. ubChanceForAvailableItem[ uiNumAvailableItems ] = ubChance;
  607. uiNumAvailableItems++;
  608. uiTotalChances += ubChance;
  609. }
  610. }
  611. }
  612. }
  613. // if there aren't any such items, the following loop would never finish, so quit before trying it!
  614. if (uiNumAvailableItems == 0)
  615. {
  616. return;
  617. }
  618. // CJC: randomly pick one of available items by weighted random selection.
  619. // randomize number within uiTotalChances and then loop forwards till we find that item
  620. uiRandomChoice = Random( uiTotalChances );
  621. for ( uiIndex = 0; uiIndex < uiNumAvailableItems; uiIndex++ )
  622. {
  623. if ( uiRandomChoice <= ubChanceForAvailableItem[ uiIndex ] )
  624. {
  625. ArmsDealerGetsFreshStock( ubArmsDealer, usAvailableItem[ uiIndex ], 1 );
  626. return;
  627. }
  628. else
  629. {
  630. // next item!
  631. uiRandomChoice -= ubChanceForAvailableItem[ uiIndex ];
  632. }
  633. }
  634. // internal logic failure!
  635. }
  636. void GuaranteeAtLeastXItemsOfIndex( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubHowMany )
  637. {
  638. // not permitted for repair dealers - would take extra code to avoid counting items under repair!
  639. Assert( !DoesDealerDoRepairs( ubArmsDealer ) );
  640. if( gArmsDealerStatus[ ubArmsDealer ].fOutOfBusiness )
  641. return;
  642. //if there are any of these in stock
  643. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems >= ubHowMany )
  644. {
  645. // have what we need...
  646. return;
  647. }
  648. // if he can stock it (it appears in his inventory list)
  649. // RESTRICTION REMOVED: Jake must be able to guarantee GAS even though it's not in his list, it's presence is conditional
  650. // if( GetDealersMaxItemAmount( ubArmsDealer, usItemIndex ) > 0)
  651. {
  652. //add the item
  653. ArmsDealerGetsFreshStock( ubArmsDealer, usItemIndex, (UINT8)( ubHowMany - gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems ) );
  654. }
  655. }
  656. UINT32 GetArmsDealerItemTypeFromItemNumber( UINT16 usItem )
  657. {
  658. switch( Item[ usItem ].usItemClass )
  659. {
  660. case IC_NONE:
  661. return( 0 );
  662. break;
  663. case IC_GUN:
  664. switch( Weapon[ Item[ usItem ].ubClassIndex ].ubWeaponClass )
  665. {
  666. case HANDGUNCLASS:
  667. return( ARMS_DEALER_HANDGUNCLASS );
  668. break;
  669. case RIFLECLASS:
  670. if ( ItemIsARocketRifle( usItem ) )
  671. return( ARMS_DEALER_ROCKET_RIFLE );
  672. else
  673. return( ARMS_DEALER_RIFLECLASS );
  674. break;
  675. case SHOTGUNCLASS:
  676. return( ARMS_DEALER_SHOTGUNCLASS );
  677. break;
  678. case SMGCLASS:
  679. return( ARMS_DEALER_SMGCLASS );
  680. break;
  681. case MGCLASS:
  682. return( ARMS_DEALER_MGCLASS );
  683. break;
  684. case MONSTERCLASS:
  685. return( 0 );
  686. break;
  687. case KNIFECLASS:
  688. return( ARMS_DEALER_KNIFECLASS );
  689. break;
  690. }
  691. break;
  692. case IC_PUNCH:
  693. if (usItem == NOTHING)
  694. {
  695. return( 0 );
  696. }
  697. // else treat as blade
  698. case IC_BLADE:
  699. case IC_THROWING_KNIFE:
  700. return( ARMS_DEALER_BLADE );
  701. break;
  702. case IC_LAUNCHER:
  703. return( ARMS_DEALER_LAUNCHER );
  704. break;
  705. case IC_ARMOUR:
  706. return( ARMS_DEALER_ARMOUR );
  707. break;
  708. case IC_MEDKIT:
  709. return( ARMS_DEALER_MEDKIT );
  710. break;
  711. case IC_KIT:
  712. return( ARMS_DEALER_KIT );
  713. break;
  714. case IC_MISC:
  715. {
  716. //switch on the type of item
  717. switch( usItem )
  718. {
  719. case BEER:
  720. case WINE:
  721. case ALCOHOL:
  722. return( ARMS_DEALER_ALCOHOL );
  723. break;
  724. case METALDETECTOR:
  725. case LASERSCOPE:
  726. // case REMDETONATOR:
  727. return( ARMS_DEALER_ELECTRONICS );
  728. break;
  729. case CANTEEN:
  730. case CROWBAR:
  731. case WIRECUTTERS:
  732. return( ARMS_DEALER_HARDWARE );
  733. break;
  734. case ADRENALINE_BOOSTER:
  735. case REGEN_BOOSTER:
  736. case SYRINGE_3:
  737. case SYRINGE_4:
  738. case SYRINGE_5:
  739. return( ARMS_DEALER_MEDICAL );
  740. break;
  741. case SILENCER:
  742. case SNIPERSCOPE:
  743. case BIPOD:
  744. case DUCKBILL:
  745. return( ARMS_DEALER_ATTACHMENTS );
  746. break;
  747. case DETONATOR:
  748. case REMDETONATOR:
  749. case REMOTEBOMBTRIGGER:
  750. return( ARMS_DEALER_DETONATORS );
  751. break;
  752. default:
  753. return( ARMS_DEALER_MISC );
  754. }
  755. }
  756. break;
  757. case IC_AMMO:
  758. return( ARMS_DEALER_AMMO );
  759. break;
  760. case IC_FACE:
  761. switch( usItem )
  762. {
  763. case EXTENDEDEAR:
  764. case NIGHTGOGGLES:
  765. case ROBOT_REMOTE_CONTROL:
  766. return( ARMS_DEALER_ELECTRONICS );
  767. break;
  768. default:
  769. return( ARMS_DEALER_FACE );
  770. }
  771. break;
  772. case IC_THROWN:
  773. return( 0 );
  774. // return( ARMS_DEALER_THROWN );
  775. break;
  776. case IC_KEY:
  777. return( 0 );
  778. // return( ARMS_DEALER_KEY );
  779. break;
  780. case IC_GRENADE:
  781. return( ARMS_DEALER_GRENADE );
  782. break;
  783. case IC_BOMB:
  784. return( ARMS_DEALER_BOMB );
  785. break;
  786. case IC_EXPLOSV:
  787. return( ARMS_DEALER_EXPLOSV );
  788. break;
  789. case IC_TENTACLES:
  790. case IC_MONEY:
  791. return( 0 );
  792. break;
  793. // case IC_APPLIABLE:
  794. break;
  795. default:
  796. AssertMsg( FALSE, String( "GetArmsDealerItemTypeFromItemNumber(), invalid class %d for item %d. DF 0.", Item[ usItem ].usItemClass, usItem ) );
  797. break;
  798. }
  799. return( 0 );
  800. }
  801. BOOLEAN IsMercADealer( UINT8 ubMercID )
  802. {
  803. UINT8 cnt;
  804. #ifdef JA2DEMO // Gabby is not a dealer in the demo, but is one in the game
  805. if( ubMercID == GABBY )
  806. return( FALSE );
  807. #endif
  808. // Manny is not actually a valid dealer unless a particular event sets that fact
  809. if( ( ubMercID == MANNY ) && !CheckFact( FACT_MANNY_IS_BARTENDER, 0 ) )
  810. {
  811. return( FALSE );
  812. }
  813. //loop through the list of arms dealers
  814. for( cnt=0; cnt<NUM_ARMS_DEALERS; cnt++ )
  815. {
  816. if( ArmsDealerInfo[ cnt ].ubShopKeeperID == ubMercID )
  817. return( TRUE );
  818. }
  819. return( FALSE );
  820. }
  821. INT8 GetArmsDealerIDFromMercID( UINT8 ubMercID )
  822. {
  823. INT8 cnt;
  824. #ifdef JA2DEMO // Gabby is not a dealer in the demo, but is one in the game
  825. if( ubMercID == GABBY )
  826. return( -1 );
  827. #endif
  828. //loop through the list of arms dealers
  829. for( cnt=0; cnt<NUM_ARMS_DEALERS; cnt++ )
  830. {
  831. if( ArmsDealerInfo[ cnt ].ubShopKeeperID == ubMercID )
  832. return( cnt );
  833. }
  834. return( -1 );
  835. }
  836. UINT8 GetTypeOfArmsDealer( UINT8 ubDealerID )
  837. {
  838. return( ArmsDealerInfo[ ubDealerID ].ubTypeOfArmsDealer );
  839. }
  840. BOOLEAN DoesDealerDoRepairs( UINT8 ubArmsDealer )
  841. {
  842. if( ArmsDealerInfo[ ubArmsDealer ].ubTypeOfArmsDealer == ARMS_DEALER_REPAIRS )
  843. return( TRUE );
  844. else
  845. return( FALSE );
  846. }
  847. /*
  848. INT16 GetSpecialItemFromArmsDealerInventory( UINT8 ubArmsDealer, UINT16 usItemIndex, SPECIAL_ITEM_INFO *pSpclItemInfo )
  849. {
  850. UINT8 ubElement;
  851. // this function won't find perfect items!
  852. Assert( IsItemInfoSpecial( pSpclItemInfo ) );
  853. for( ubElement = 0; ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  854. {
  855. // if this is the one we're looking for
  856. if( memcmp( &(gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].Info), pSpclItemInfo, sizeof( SPECIAL_ITEM_INFO ) ) == 0 )
  857. {
  858. return( ubElement );
  859. }
  860. }
  861. // not found!
  862. return( -1 );
  863. }
  864. */
  865. BOOLEAN RepairmanIsFixingItemsButNoneAreDoneYet( UINT8 ubProfileID )
  866. {
  867. INT8 bArmsDealer;
  868. BOOLEAN fHaveOnlyUnRepairedItems=FALSE;
  869. UINT8 ubElement;
  870. UINT16 usItemIndex;
  871. bArmsDealer = GetArmsDealerIDFromMercID( ubProfileID );
  872. if( bArmsDealer == -1 )
  873. return( FALSE );
  874. //if the dealer is not a repair dealer, return
  875. if( !DoesDealerDoRepairs( bArmsDealer ) )
  876. return( FALSE );
  877. //loop through the dealers inventory and check if there are only unrepaired items
  878. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  879. {
  880. //if there is some items in stock
  881. if( gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].ubTotalItems )
  882. {
  883. //loop through the array of items
  884. for( ubElement=0; ubElement< gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  885. {
  886. if ( gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive )
  887. {
  888. //if the items status is below 0, the item is being repaired
  889. if( gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].Info.bItemCondition < 0 )
  890. {
  891. //if the item has been repaired
  892. if( gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].uiRepairDoneTime <= GetWorldTotalMin() )
  893. {
  894. //A repair item is ready, therefore, return false
  895. return( FALSE );
  896. }
  897. else
  898. {
  899. fHaveOnlyUnRepairedItems = TRUE;
  900. }
  901. }
  902. }
  903. }
  904. }
  905. }
  906. return( fHaveOnlyUnRepairedItems );
  907. }
  908. UINT32 GetTimeToFixItemBeingRepaired( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubElement )
  909. {
  910. //dealer must be a repair dealer
  911. Assert( DoesDealerDoRepairs( ubArmsDealer ) );
  912. // element index must be valid
  913. Assert( ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced );
  914. // that item must be active
  915. Assert( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive );
  916. // that item must be in repair
  917. Assert( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].Info.bItemCondition < 0 );
  918. //if the item has already been repaired
  919. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].uiRepairDoneTime <= GetWorldTotalMin() )
  920. return( 0 );
  921. //Return how many more minutes it will take to fix the item
  922. return( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].uiRepairDoneTime - GetWorldTotalMin() );
  923. }
  924. BOOLEAN CanDealerTransactItem( UINT8 ubArmsDealer, UINT16 usItemIndex, BOOLEAN fPurchaseFromPlayer )
  925. {
  926. switch ( ArmsDealerInfo[ ubArmsDealer ].ubTypeOfArmsDealer )
  927. {
  928. case ARMS_DEALER_SELLS_ONLY:
  929. if ( fPurchaseFromPlayer )
  930. {
  931. // this dealer only sells stuff to player, so he can't buy anything from him
  932. return( FALSE );
  933. }
  934. break;
  935. case ARMS_DEALER_BUYS_ONLY:
  936. if ( !fPurchaseFromPlayer )
  937. {
  938. // this dealer only buys stuff from player, so he can't sell anything to him
  939. return( FALSE );
  940. }
  941. break;
  942. case ARMS_DEALER_BUYS_SELLS:
  943. switch ( ubArmsDealer )
  944. {
  945. case ARMS_DEALER_JAKE:
  946. case ARMS_DEALER_KEITH:
  947. case ARMS_DEALER_FRANZ:
  948. if ( fPurchaseFromPlayer )
  949. {
  950. // these guys will buy nearly anything from the player, regardless of what they carry for sale!
  951. return( CalcValueOfItemToDealer( ubArmsDealer, usItemIndex, FALSE ) > 0 );
  952. }
  953. //else selling inventory uses their inventory list
  954. break;
  955. default:
  956. // the others go by their inventory list
  957. break;
  958. }
  959. break;
  960. case ARMS_DEALER_REPAIRS:
  961. // repairmen don't have a complete list of what they'll repair in their inventory,
  962. // so we must check the item's properties instead.
  963. return( CanDealerRepairItem( ubArmsDealer, usItemIndex ) );
  964. default:
  965. AssertMsg( FALSE, String( "CanDealerTransactItem(), type of dealer %d. AM 0.", ArmsDealerInfo[ ubArmsDealer ].ubTypeOfArmsDealer ) );
  966. return(FALSE);
  967. }
  968. return( DoesItemAppearInDealerInventoryList( ubArmsDealer, usItemIndex, fPurchaseFromPlayer ) );
  969. }
  970. BOOLEAN CanDealerRepairItem( UINT8 ubArmsDealer, UINT16 usItemIndex )
  971. {
  972. UINT32 uiFlags;
  973. uiFlags = Item[ usItemIndex ].fFlags;
  974. // can't repair anything that's not repairable!
  975. if ( !( uiFlags & ITEM_REPAIRABLE ) )
  976. {
  977. return(FALSE);
  978. }
  979. switch ( ubArmsDealer )
  980. {
  981. case ARMS_DEALER_ARNIE:
  982. case ARMS_DEALER_PERKO:
  983. // repairs ANYTHING non-electronic
  984. if ( !( uiFlags & ITEM_ELECTRONIC ) )
  985. {
  986. return(TRUE);
  987. }
  988. break;
  989. case ARMS_DEALER_FREDO:
  990. // repairs ONLY electronics
  991. if ( uiFlags & ITEM_ELECTRONIC )
  992. {
  993. return(TRUE);
  994. }
  995. break;
  996. default:
  997. AssertMsg( FALSE, String( "CanDealerRepairItem(), Arms Dealer %d is not a recognized repairman!. AM 1.", ubArmsDealer ) );
  998. }
  999. // can't repair this...
  1000. return(FALSE);
  1001. }
  1002. BOOLEAN AllocMemsetSpecialItemArray( DEALER_ITEM_HEADER *pDealerItem, UINT8 ubElementsNeeded )
  1003. {
  1004. Assert(pDealerItem);
  1005. Assert( ubElementsNeeded > 0);
  1006. pDealerItem->SpecialItem = MemAlloc( sizeof( DEALER_SPECIAL_ITEM ) * ubElementsNeeded );
  1007. if( pDealerItem->SpecialItem == NULL )
  1008. {
  1009. Assert( 0 );
  1010. return(FALSE);
  1011. }
  1012. // zero them out (they're inactive until an item is actually added)
  1013. memset( pDealerItem->SpecialItem, 0, sizeof( DEALER_SPECIAL_ITEM ) * ubElementsNeeded );
  1014. pDealerItem->ubElementsAlloced = ubElementsNeeded;
  1015. return(TRUE);
  1016. }
  1017. BOOLEAN ResizeSpecialItemArray( DEALER_ITEM_HEADER *pDealerItem, UINT8 ubElementsNeeded )
  1018. {
  1019. Assert(pDealerItem);
  1020. // must already have a ptr allocated!
  1021. Assert(pDealerItem->SpecialItem);
  1022. if ( ubElementsNeeded == pDealerItem->ubElementsAlloced)
  1023. {
  1024. // shouldn't have been called, but what they hey, it's not exactly a problem
  1025. return(TRUE);
  1026. }
  1027. // already allocated, but change its size
  1028. pDealerItem->SpecialItem = MemRealloc( pDealerItem->SpecialItem, sizeof( DEALER_SPECIAL_ITEM ) * ubElementsNeeded );
  1029. if( pDealerItem->SpecialItem == NULL )
  1030. {
  1031. Assert( 0 );
  1032. return(FALSE);
  1033. }
  1034. // if adding more elements
  1035. if ( ubElementsNeeded > pDealerItem->ubElementsAlloced)
  1036. {
  1037. // zero them out (they're inactive until an item is actually added)
  1038. memset( &(pDealerItem->SpecialItem[pDealerItem->ubElementsAlloced]), 0, sizeof( DEALER_SPECIAL_ITEM ) * ( ubElementsNeeded - pDealerItem->ubElementsAlloced) );
  1039. }
  1040. pDealerItem->ubElementsAlloced = ubElementsNeeded;
  1041. return(TRUE);
  1042. }
  1043. void FreeSpecialItemArray( DEALER_ITEM_HEADER *pDealerItem)
  1044. {
  1045. Assert(pDealerItem);
  1046. // must already have a ptr allocated!
  1047. Assert(pDealerItem->SpecialItem);
  1048. MemFree( pDealerItem->SpecialItem );
  1049. pDealerItem->SpecialItem = NULL;
  1050. pDealerItem->ubElementsAlloced = 0;
  1051. pDealerItem->ubTotalItems = pDealerItem->ubPerfectItems;
  1052. // doesn't effect perfect items, orders or stray bullets!
  1053. }
  1054. void ArmsDealerGetsFreshStock( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubNumItems )
  1055. {
  1056. UINT8 ubCnt;
  1057. UINT8 ubItemCondition;
  1058. UINT8 ubPerfectOnes = 0;
  1059. SPECIAL_ITEM_INFO SpclItemInfo;
  1060. // create item info describing a perfect item
  1061. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  1062. // determine the condition of each one, counting up new ones, but adding damaged ones right away
  1063. for ( ubCnt = 0; ubCnt < ubNumItems; ubCnt++ )
  1064. {
  1065. ubItemCondition = DetermineDealerItemCondition( ubArmsDealer, usItemIndex);
  1066. // if the item is brand new
  1067. if ( ubItemCondition == 100)
  1068. {
  1069. ubPerfectOnes++;
  1070. }
  1071. else
  1072. {
  1073. // add a used item with that condition to his inventory
  1074. SpclItemInfo.bItemCondition = (INT8) ubItemCondition;
  1075. AddItemToArmsDealerInventory( ubArmsDealer, usItemIndex, &SpclItemInfo, 1 );
  1076. }
  1077. }
  1078. // now add all the perfect ones, in one shot
  1079. if ( ubPerfectOnes > 0)
  1080. {
  1081. SpclItemInfo.bItemCondition = 100;
  1082. AddItemToArmsDealerInventory( ubArmsDealer, usItemIndex, &SpclItemInfo, ubPerfectOnes );
  1083. }
  1084. }
  1085. UINT8 DetermineDealerItemCondition( UINT8 ubArmsDealer, UINT16 usItemIndex )
  1086. {
  1087. UINT8 ubCondition = 100;
  1088. // if it's a damagable item, and not a liquid (those are always sold full)
  1089. if ( ( Item[ usItemIndex ].fFlags & ITEM_DAMAGEABLE ) && !ItemContainsLiquid( usItemIndex ) )
  1090. {
  1091. // if he ONLY has used items, or 50% of the time if he carries both used & new items
  1092. if ( ( ArmsDealerInfo[ ubArmsDealer ].uiFlags & ARMS_DEALER_ONLY_USED_ITEMS ) ||
  1093. ( ( ArmsDealerInfo[ ubArmsDealer ].uiFlags & ARMS_DEALER_SOME_USED_ITEMS ) && ( Random( 100 ) < 50 ) ) )
  1094. {
  1095. // make the item a used one
  1096. ubCondition = (UINT8)(20 + Random( 60 ));
  1097. }
  1098. }
  1099. return( ubCondition);
  1100. }
  1101. BOOLEAN ItemContainsLiquid( UINT16 usItemIndex )
  1102. {
  1103. switch ( usItemIndex )
  1104. {
  1105. case CANTEEN:
  1106. case BEER:
  1107. case ALCOHOL:
  1108. case JAR_HUMAN_BLOOD:
  1109. case JAR_CREATURE_BLOOD:
  1110. case JAR_QUEEN_CREATURE_BLOOD:
  1111. case JAR_ELIXIR:
  1112. case GAS_CAN:
  1113. return( TRUE );
  1114. }
  1115. return( FALSE );
  1116. }
  1117. /*
  1118. UINT32 CountTotalItemsInArmsDealersInventory( UINT8 ubArmsDealer )
  1119. {
  1120. UINT32 uiNumOfItems=0;
  1121. UINT16 usItemIndex;
  1122. //loop through all the items in this dealer's inventory
  1123. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  1124. {
  1125. // This counts each pack of ammo or stacked item as one. See the "distinct" version of this for an alternate version
  1126. uiNumOfItems += gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems;
  1127. }
  1128. return( uiNumOfItems );
  1129. }
  1130. */
  1131. UINT32 CountDistinctItemsInArmsDealersInventory( UINT8 ubArmsDealer )
  1132. {
  1133. UINT32 uiNumOfItems=0;
  1134. UINT16 usItemIndex;
  1135. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  1136. {
  1137. //if there are any items
  1138. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems > 0 )
  1139. {
  1140. // if there are any items in perfect condition
  1141. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubPerfectItems > 0 )
  1142. {
  1143. // if the items can be stacked
  1144. // NOTE: This test must match the one inside AddItemsToTempDealerInventory() exactly!
  1145. if ( DealerItemIsSafeToStack( usItemIndex ) )
  1146. {
  1147. // regardless of how many there are, they count as 1 *distinct* item! They will all be together in one box...
  1148. uiNumOfItems++;
  1149. }
  1150. else
  1151. {
  1152. // non-stacking items must be stored in one / box , because each may have unique fields besides bStatus[]
  1153. // Example: guns all have ammo, ammo type, etc. We need these uniquely represented for pricing & manipulation
  1154. uiNumOfItems += gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubPerfectItems;
  1155. }
  1156. }
  1157. // each *active* special item counts as one additional distinct item (each one occupied a separate shopkeeper box!)
  1158. // NOTE: This is including items being repaired!!!
  1159. uiNumOfItems += CountActiveSpecialItemsInArmsDealersInventory( ubArmsDealer, usItemIndex);
  1160. }
  1161. }
  1162. return( uiNumOfItems );
  1163. }
  1164. UINT8 CountActiveSpecialItemsInArmsDealersInventory( UINT8 ubArmsDealer, UINT16 usItemIndex )
  1165. {
  1166. UINT8 ubActiveSpecialItems = 0;
  1167. UINT8 ubElement;
  1168. // next, try to sell all the used ones, gotta do these one at a time so we can remove them by element
  1169. for ( ubElement = 0; ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  1170. {
  1171. // don't worry about negative condition, repairmen can't come this far, they don't sell!
  1172. if ( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive )
  1173. {
  1174. ubActiveSpecialItems++;
  1175. }
  1176. }
  1177. return( ubActiveSpecialItems );
  1178. }
  1179. UINT16 CountTotalItemsRepairDealerHasInForRepairs( UINT8 ubArmsDealer )
  1180. {
  1181. UINT16 usItemIndex;
  1182. UINT16 usHowManyInForRepairs = 0;
  1183. //if the dealer is not a repair dealer, no need to count, return 0
  1184. if( !DoesDealerDoRepairs( ubArmsDealer ) )
  1185. return( 0 );
  1186. //loop through the dealers inventory and count the number of items in for repairs
  1187. for( usItemIndex=0; usItemIndex < MAXITEMS; usItemIndex++ )
  1188. {
  1189. usHowManyInForRepairs += CountSpecificItemsRepairDealerHasInForRepairs( ubArmsDealer, usItemIndex );
  1190. }
  1191. return( usHowManyInForRepairs );
  1192. }
  1193. UINT8 CountSpecificItemsRepairDealerHasInForRepairs( UINT8 ubArmsDealer, UINT16 usItemIndex )
  1194. {
  1195. UINT8 ubElement;
  1196. UINT8 ubHowManyInForRepairs = 0;
  1197. //if the dealer is not a repair dealer, no need to count, return 0
  1198. if( !DoesDealerDoRepairs( ubArmsDealer ) )
  1199. return( 0 );
  1200. //if there is some items in stock
  1201. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems )
  1202. {
  1203. //loop through the array of items
  1204. for( ubElement = 0; ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  1205. {
  1206. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive)
  1207. {
  1208. //if the item's status is below 0, the item is being repaired
  1209. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].Info.bItemCondition < 0 )
  1210. {
  1211. ubHowManyInForRepairs++;
  1212. }
  1213. }
  1214. }
  1215. }
  1216. return( ubHowManyInForRepairs );
  1217. }
  1218. void AddObjectToArmsDealerInventory( UINT8 ubArmsDealer, OBJECTTYPE *pObject )
  1219. {
  1220. UINT8 ubCnt;
  1221. SPECIAL_ITEM_INFO SpclItemInfo;
  1222. SetSpecialItemInfoFromObject( &SpclItemInfo, pObject );
  1223. // split up all the components of an objecttype and add them as seperate items into the dealer's inventory
  1224. switch ( Item [ pObject->usItem ].usItemClass )
  1225. {
  1226. case IC_GUN:
  1227. // add the gun (keeps the object's status and imprintID)
  1228. // if the gun was jammed, this will forget about the jam (i.e. dealer immediately unjams anything he buys)
  1229. AddItemToArmsDealerInventory( ubArmsDealer, pObject->usItem, &SpclItemInfo, 1 );
  1230. // if any GunAmmoItem is specified
  1231. if( pObject->usGunAmmoItem != NONE)
  1232. {
  1233. // if it's regular ammo
  1234. if( Item[ pObject->usGunAmmoItem ].usItemClass == IC_AMMO )
  1235. {
  1236. // and there are some remaining
  1237. if ( pObject->ubGunShotsLeft > 0 )
  1238. {
  1239. // add the bullets of its remaining ammo
  1240. AddAmmoToArmsDealerInventory( ubArmsDealer, pObject->usGunAmmoItem, pObject->ubGunShotsLeft );
  1241. }
  1242. }
  1243. else // assume it's attached ammo (mortar shells, grenades)
  1244. {
  1245. // add the launchable item (can't be imprinted, or have attachments!)
  1246. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  1247. SpclItemInfo.bItemCondition = pObject->bGunAmmoStatus;
  1248. // if the gun it was in was jammed, get rid of the negative status now
  1249. if ( SpclItemInfo.bItemCondition < 0 )
  1250. {
  1251. SpclItemInfo.bItemCondition *= -1;
  1252. }
  1253. AddItemToArmsDealerInventory( ubArmsDealer, pObject->usGunAmmoItem, &SpclItemInfo, 1 );
  1254. }
  1255. }
  1256. break;
  1257. case IC_AMMO:
  1258. // add the contents of each magazine (multiple mags may have vastly different #bullets left)
  1259. for ( ubCnt = 0; ubCnt < pObject->ubNumberOfObjects; ubCnt++ )
  1260. {
  1261. AddAmmoToArmsDealerInventory( ubArmsDealer, pObject->usItem, pObject->ubShotsLeft[ ubCnt ] );
  1262. }
  1263. break;
  1264. default:
  1265. // add each object seperately (multiple objects may have vastly different statuses, keep any imprintID)
  1266. for ( ubCnt = 0; ubCnt < pObject->ubNumberOfObjects; ubCnt++ )
  1267. {
  1268. SpclItemInfo.bItemCondition = pObject->bStatus[ ubCnt ];
  1269. AddItemToArmsDealerInventory( ubArmsDealer, pObject->usItem, &SpclItemInfo, 1 );
  1270. }
  1271. break;
  1272. }
  1273. // loop through any detachable attachments and add them as seperate items
  1274. for( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1275. {
  1276. if( pObject->usAttachItem[ ubCnt ] != NONE )
  1277. {
  1278. // ARM: Note: this is only used for selling, not repairs, so attachmentes are seperated when sold to a dealer
  1279. // If the attachment is detachable
  1280. if (! (Item[ pObject->usAttachItem[ubCnt] ].fFlags & ITEM_INSEPARABLE ) )
  1281. {
  1282. // add this particular attachment (they can't be imprinted, or themselves have attachments!)
  1283. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  1284. SpclItemInfo.bItemCondition = pObject->bAttachStatus[ ubCnt ];
  1285. AddItemToArmsDealerInventory( ubArmsDealer, pObject->usAttachItem[ ubCnt ], &SpclItemInfo, 1 );
  1286. }
  1287. }
  1288. }
  1289. // nuke the original object to prevent any possible item duplication
  1290. memset( pObject, 0, sizeof( OBJECTTYPE ) );
  1291. }
  1292. void AddAmmoToArmsDealerInventory( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubShotsLeft )
  1293. {
  1294. UINT8 ubMagCapacity;
  1295. UINT8 *pubStrayAmmo;
  1296. SPECIAL_ITEM_INFO SpclItemInfo;
  1297. // Ammo only, please!!!
  1298. if (Item [ usItemIndex ].usItemClass != IC_AMMO )
  1299. {
  1300. Assert(0);
  1301. return;
  1302. }
  1303. if ( ubShotsLeft == 0)
  1304. {
  1305. return;
  1306. }
  1307. ubMagCapacity = Magazine[ Item[ usItemIndex ].ubClassIndex ].ubMagSize;
  1308. if ( ubShotsLeft >= ubMagCapacity )
  1309. {
  1310. // add however many FULL magazines the #shot left represents
  1311. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  1312. AddItemToArmsDealerInventory( ubArmsDealer, usItemIndex, &SpclItemInfo, ( UINT8 ) ( ubShotsLeft / ubMagCapacity ) );
  1313. ubShotsLeft %= ubMagCapacity;
  1314. }
  1315. // any shots left now are "strays" - not enough to completely fill a magazine of this type
  1316. if ( ubShotsLeft > 0 )
  1317. {
  1318. // handle "stray" ammo - add it to the dealer's stray pile
  1319. pubStrayAmmo = &(gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubStrayAmmo);
  1320. *pubStrayAmmo += ubShotsLeft;
  1321. // if dealer has accumulated enough stray ammo to make another full magazine, convert it!
  1322. if ( *pubStrayAmmo >= ubMagCapacity )
  1323. {
  1324. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  1325. AddItemToArmsDealerInventory( ubArmsDealer, usItemIndex, &SpclItemInfo, ( UINT8 ) ( *pubStrayAmmo / ubMagCapacity ) );
  1326. *pubStrayAmmo = *pubStrayAmmo % ubMagCapacity;
  1327. }
  1328. // I know, I know, this is getting pretty anal... But what the hell, it was easy enough to do. ARM.
  1329. }
  1330. }
  1331. //Use AddObjectToArmsDealerInventory() instead of this when converting a complex item in OBJECTTYPE format.
  1332. void AddItemToArmsDealerInventory( UINT8 ubArmsDealer, UINT16 usItemIndex, SPECIAL_ITEM_INFO *pSpclItemInfo, UINT8 ubHowMany )
  1333. {
  1334. UINT8 ubRoomLeft;
  1335. UINT8 ubElement;
  1336. UINT8 ubElementsToAdd;
  1337. BOOLEAN fFoundOne;
  1338. BOOLEAN fSuccess;
  1339. Assert( ubHowMany > 0);
  1340. ubRoomLeft = 255 - gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems;
  1341. if ( ubHowMany > ubRoomLeft)
  1342. {
  1343. // not enough room to store that many, any extras vanish into thin air!
  1344. ubHowMany = ubRoomLeft;
  1345. }
  1346. if ( ubHowMany == 0)
  1347. {
  1348. return;
  1349. }
  1350. // decide whether this item is "special" or not
  1351. if ( IsItemInfoSpecial( pSpclItemInfo ) )
  1352. {
  1353. // Anything that's used/damaged or imprinted is store as a special item in the SpecialItem array,
  1354. // exactly one item per element. We (re)allocate memory dynamically as necessary to hold the additional items.
  1355. do
  1356. {
  1357. // search for an already allocated, empty element in the special item array
  1358. fFoundOne = FALSE;
  1359. for ( ubElement = 0; ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  1360. {
  1361. if ( !( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive ) )
  1362. {
  1363. //Great! Store it here, then.
  1364. AddSpecialItemToArmsDealerInventoryAtElement( ubArmsDealer, usItemIndex, ubElement, pSpclItemInfo );
  1365. fFoundOne = TRUE;
  1366. break;
  1367. }
  1368. }
  1369. // if we didn't find any inactive elements already allocated
  1370. if (!fFoundOne)
  1371. {
  1372. // then we're going to have to allocate some more space...
  1373. ubElementsToAdd = max( SPECIAL_ITEMS_ALLOCED_AT_ONCE, ubHowMany);
  1374. // if there aren't any allocated at all right now
  1375. if ( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced == 0 )
  1376. {
  1377. // allocate new memory for the real buffer
  1378. fSuccess = AllocMemsetSpecialItemArray( &gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ], ubElementsToAdd );
  1379. }
  1380. else
  1381. {
  1382. // we have some allocated, but they're all full and we need more. MemRealloc existing amount + # addition elements
  1383. fSuccess = ResizeSpecialItemArray( &gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ], (UINT8)( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced + ubElementsToAdd ) );
  1384. }
  1385. if ( !fSuccess )
  1386. {
  1387. return;
  1388. }
  1389. // now add the special item at the first of the newly added elements (still stored in ubElement!)
  1390. AddSpecialItemToArmsDealerInventoryAtElement( ubArmsDealer, usItemIndex, ubElement, pSpclItemInfo );
  1391. }
  1392. // store the # of the element it was placed in globally so anyone who needs that can grab it there
  1393. gubLastSpecialItemAddedAtElement = ubElement;
  1394. ubHowMany--;
  1395. } while ( ubHowMany > 0);
  1396. }
  1397. else // adding perfect item(s)
  1398. {
  1399. // then it's stored as a "perfect" item, simply add it to that counter!
  1400. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubPerfectItems += ubHowMany;
  1401. // increase total items of this type
  1402. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems += ubHowMany;
  1403. }
  1404. }
  1405. void AddSpecialItemToArmsDealerInventoryAtElement( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubElement, SPECIAL_ITEM_INFO *pSpclItemInfo )
  1406. {
  1407. Assert( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems < 255 );
  1408. Assert( ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced );
  1409. Assert( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive == FALSE );
  1410. Assert( IsItemInfoSpecial( pSpclItemInfo ) );
  1411. //Store the special values in that element, and make it active
  1412. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive = TRUE;
  1413. memcpy( &(gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].Info), pSpclItemInfo, sizeof( SPECIAL_ITEM_INFO ) );
  1414. // increase the total items
  1415. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems++;
  1416. }
  1417. // removes ubHowMany items of usItemIndex with the matching Info from dealer ubArmsDealer
  1418. void RemoveItemFromArmsDealerInventory( UINT8 ubArmsDealer, UINT16 usItemIndex, SPECIAL_ITEM_INFO *pSpclItemInfo, UINT8 ubHowMany )
  1419. {
  1420. DEALER_SPECIAL_ITEM *pSpecialItem;
  1421. UINT8 ubElement;
  1422. Assert( ubHowMany <= gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems );
  1423. if ( ubHowMany == 0)
  1424. {
  1425. return;
  1426. }
  1427. // decide whether this item is "special" or not
  1428. if ( IsItemInfoSpecial( pSpclItemInfo ) )
  1429. {
  1430. // look through the elements, trying to find special items matching the specifications
  1431. for ( ubElement = 0; ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  1432. {
  1433. pSpecialItem = &(gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ]);
  1434. // if this element is in use
  1435. if ( pSpecialItem->fActive )
  1436. {
  1437. // and its contents are exactly what we're looking for
  1438. if( memcmp( &(gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].Info), pSpclItemInfo, sizeof( SPECIAL_ITEM_INFO ) ) == 0 )
  1439. {
  1440. // Got one! Remove it
  1441. RemoveSpecialItemFromArmsDealerInventoryAtElement( ubArmsDealer, usItemIndex, ubElement );
  1442. ubHowMany--;
  1443. if ( ubHowMany == 0)
  1444. {
  1445. break;
  1446. }
  1447. }
  1448. }
  1449. }
  1450. // when we've searched all the special item elements, we'd better not have any more items to remove!
  1451. Assert( ubHowMany == 0);
  1452. }
  1453. else // removing perfect item(s)
  1454. {
  1455. // then it's stored as a "perfect" item, simply subtract from tha counter!
  1456. Assert( ubHowMany <= gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubPerfectItems );
  1457. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubPerfectItems -= ubHowMany;
  1458. // decrease total items of this type
  1459. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems -= ubHowMany;
  1460. }
  1461. }
  1462. void RemoveRandomItemFromArmsDealerInventory( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubHowMany )
  1463. {
  1464. UINT8 ubWhichOne;
  1465. UINT8 ubSkippedAlready;
  1466. BOOLEAN fFoundIt;
  1467. UINT8 ubElement;
  1468. SPECIAL_ITEM_INFO SpclItemInfo;
  1469. // not permitted for repair dealers - would take extra code to subtract items under repair from ubTotalItems!!!
  1470. Assert( !DoesDealerDoRepairs( ubArmsDealer ) );
  1471. // Can't remove any items in for repair, though!
  1472. Assert( ubHowMany <= gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems );
  1473. while ( ubHowMany > 0)
  1474. {
  1475. // pick a random one to get rid of
  1476. ubWhichOne = (UINT8)Random(gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems );
  1477. // if we picked one of the perfect ones...
  1478. if ( ubWhichOne < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubPerfectItems )
  1479. {
  1480. // create item info describing a perfect item
  1481. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  1482. // then that's easy, its condition is 100, so remove one of those
  1483. RemoveItemFromArmsDealerInventory( ubArmsDealer, usItemIndex, &SpclItemInfo, 1 );
  1484. }
  1485. else
  1486. {
  1487. // Yikes! Gotta look through the special items. We already know it's not any of the perfect ones, subtract those
  1488. ubWhichOne -= gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubPerfectItems;
  1489. ubSkippedAlready = 0;
  1490. fFoundIt = FALSE;
  1491. for ( ubElement = 0; ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  1492. {
  1493. // if this is an active special item, not in repair
  1494. if ( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive ) // &&
  1495. // ( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].Info.bItemCondition > 0 ) )
  1496. {
  1497. // if we skipped the right amount of them
  1498. if ( ubSkippedAlready == ubWhichOne )
  1499. {
  1500. // then this one is it! That's the one we're gonna remove
  1501. RemoveSpecialItemFromArmsDealerInventoryAtElement( ubArmsDealer, usItemIndex, ubElement );
  1502. fFoundIt = TRUE;
  1503. break;
  1504. }
  1505. else
  1506. {
  1507. // keep looking...
  1508. ubSkippedAlready++;
  1509. }
  1510. }
  1511. }
  1512. // this HAS to work, or the data structure is corrupt!
  1513. Assert(fFoundIt);
  1514. }
  1515. ubHowMany--;
  1516. }
  1517. }
  1518. void RemoveSpecialItemFromArmsDealerInventoryAtElement( UINT8 ubArmsDealer, UINT16 usItemIndex, UINT8 ubElement )
  1519. {
  1520. Assert( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems > 0 );
  1521. Assert( ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced );
  1522. Assert( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive == TRUE );
  1523. // wipe it out (turning off fActive)
  1524. memset( &( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ] ), 0, sizeof( DEALER_SPECIAL_ITEM ) );
  1525. // one fewer item remains...
  1526. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems--;
  1527. }
  1528. BOOLEAN AddDeadArmsDealerItemsToWorld( UINT8 ubMercID )
  1529. {
  1530. INT8 bArmsDealer;
  1531. SOLDIERTYPE *pSoldier;
  1532. UINT16 usItemIndex;
  1533. UINT8 ubElement;
  1534. UINT8 ubHowManyMaxAtATime;
  1535. UINT8 ubLeftToDrop;
  1536. UINT8 ubNowDropping;
  1537. OBJECTTYPE TempObject;
  1538. DEALER_SPECIAL_ITEM *pSpecialItem;
  1539. SPECIAL_ITEM_INFO SpclItemInfo;
  1540. //Get Dealer ID from from merc Id
  1541. bArmsDealer = GetArmsDealerIDFromMercID( ubMercID );
  1542. if( bArmsDealer == -1 )
  1543. {
  1544. // not a dealer, that's ok, we get called for every dude that croaks.
  1545. return( FALSE );
  1546. }
  1547. // mark the dealer as being out of business!
  1548. gArmsDealerStatus[ bArmsDealer ].fOutOfBusiness = TRUE;
  1549. //Get a pointer to the dealer
  1550. pSoldier = FindSoldierByProfileID( ubMercID, FALSE );
  1551. if( pSoldier == NULL )
  1552. {
  1553. // This should never happen, a dealer getting knocked off without the sector being loaded, should it?
  1554. // If it's possible, we should modify code below to dump his belongings into the sector without using pSoldier->sGridNo
  1555. Assert(0);
  1556. return( FALSE );
  1557. }
  1558. //loop through all the items in the dealer's inventory, and drop them all where the dealer was set up.
  1559. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  1560. {
  1561. //if the dealer has any items of this type
  1562. if( gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].ubTotalItems > 0)
  1563. {
  1564. // if he has any perfect items of this time
  1565. if ( gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].ubPerfectItems > 0 )
  1566. {
  1567. // drop all the perfect items first
  1568. // drop stackable items like ammo in stacks of whatever will fit into a large pocket instead of one at a time
  1569. ubHowManyMaxAtATime = ItemSlotLimit( usItemIndex, BIGPOCK1POS );
  1570. if ( ubHowManyMaxAtATime < 1 )
  1571. {
  1572. ubHowManyMaxAtATime = 1;
  1573. }
  1574. // create item info describing a perfect item
  1575. SetSpecialItemInfoToDefaults( &SpclItemInfo );
  1576. ubLeftToDrop = gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].ubPerfectItems;
  1577. // ATE: While it IS leagal here to use pSoldier->sInitialGridNo, cause of where this
  1578. // function is called, there are times when we're not guarenteed that sGridNo is good
  1579. while ( ubLeftToDrop > 0)
  1580. {
  1581. ubNowDropping = min( ubLeftToDrop, ubHowManyMaxAtATime );
  1582. MakeObjectOutOfDealerItems( usItemIndex, &SpclItemInfo, &TempObject, ubNowDropping );
  1583. AddItemToPool( pSoldier->sInitialGridNo, &TempObject, INVISIBLE, 0, 0, 0 );
  1584. ubLeftToDrop -= ubNowDropping;
  1585. }
  1586. // remove them all from his inventory
  1587. RemoveItemFromArmsDealerInventory( bArmsDealer, usItemIndex, &SpclItemInfo, gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].ubPerfectItems );
  1588. }
  1589. // then drop all the special items
  1590. for ( ubElement = 0; ubElement < gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  1591. {
  1592. pSpecialItem = &(gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ]);
  1593. if ( pSpecialItem->fActive )
  1594. {
  1595. MakeObjectOutOfDealerItems(usItemIndex, &(pSpecialItem->Info), &TempObject, 1 );
  1596. AddItemToPool( pSoldier->sInitialGridNo, &TempObject, INVISIBLE, 0, 0, 0 );
  1597. RemoveItemFromArmsDealerInventory( bArmsDealer, usItemIndex, &(pSpecialItem->Info), 1 );
  1598. }
  1599. }
  1600. // release any memory allocated for special items, he won't need it now...
  1601. if( gArmsDealersInventory[ bArmsDealer ][ usItemIndex ].ubElementsAlloced > 0 )
  1602. {
  1603. FreeSpecialItemArray( &gArmsDealersInventory[ bArmsDealer ][ usItemIndex ] );
  1604. }
  1605. }
  1606. }
  1607. //if the dealer has money
  1608. if( gArmsDealerStatus[ bArmsDealer ].uiArmsDealersCash > 0 )
  1609. {
  1610. //Create the object
  1611. memset( &TempObject, 0, sizeof( OBJECTTYPE ) );
  1612. if( !CreateMoney( gArmsDealerStatus[ bArmsDealer ].uiArmsDealersCash, &TempObject ) )
  1613. {
  1614. return( FALSE );
  1615. }
  1616. //add the money item to the dealers feet
  1617. AddItemToPool( pSoldier->sInitialGridNo, &TempObject, INVISIBLE, 0, 0, 0 );
  1618. gArmsDealerStatus[ bArmsDealer ].uiArmsDealersCash = 0;
  1619. }
  1620. return( TRUE );
  1621. }
  1622. void MakeObjectOutOfDealerItems( UINT16 usItemIndex, SPECIAL_ITEM_INFO *pSpclItemInfo, OBJECTTYPE *pObject, UINT8 ubHowMany )
  1623. {
  1624. INT8 bItemCondition;
  1625. UINT8 ubCnt;
  1626. bItemCondition = pSpclItemInfo->bItemCondition;
  1627. //if the item condition is below 0, the item is in for repairs, so flip the sign
  1628. if( bItemCondition < 0 )
  1629. {
  1630. bItemCondition *= -1;
  1631. }
  1632. memset( pObject, 0, sizeof( OBJECTTYPE ) );
  1633. //Create the item object
  1634. CreateItems( usItemIndex, bItemCondition, ubHowMany, pObject );
  1635. // set the ImprintID
  1636. pObject->ubImprintID = pSpclItemInfo->ubImprintID;
  1637. // add any attachments we've been storing
  1638. for ( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1639. {
  1640. if ( pSpclItemInfo->usAttachment[ ubCnt ] != NONE )
  1641. {
  1642. // store what it is, and its condition
  1643. pObject->usAttachItem[ ubCnt ] = pSpclItemInfo->usAttachment[ ubCnt ];
  1644. pObject->bAttachStatus[ ubCnt ] = pSpclItemInfo->bAttachmentStatus[ ubCnt ];
  1645. }
  1646. }
  1647. // if it's a gun
  1648. if (Item [ pObject->usItem ].usItemClass == IC_GUN )
  1649. {
  1650. // Empty out the bullets put in by CreateItem(). We now sell all guns empty of bullets. This is so that we don't
  1651. // have to keep track of #bullets in a gun throughout dealer inventory. Without this, players could "reload" guns
  1652. // they don't have ammo for by selling them to Tony & buying them right back fully loaded! One could repeat this
  1653. // ad nauseum (empty the gun between visits) as a (really expensive) way to get unlimited special ammo like rockets.
  1654. pObject->ubGunShotsLeft = 0;
  1655. }
  1656. }
  1657. void GiveObjectToArmsDealerForRepair( UINT8 ubArmsDealer, OBJECTTYPE *pObject, UINT8 ubOwnerProfileId )
  1658. {
  1659. // UINT8 ubCnt;
  1660. SPECIAL_ITEM_INFO SpclItemInfo;
  1661. Assert( DoesDealerDoRepairs( ubArmsDealer ) );
  1662. // Any object passed into here must already be:
  1663. // a) Unstacked
  1664. Assert( pObject->ubNumberOfObjects == 1 );
  1665. // b) Repairable
  1666. Assert( CanDealerRepairItem( ubArmsDealer, pObject->usItem ) );
  1667. // c) Actually damaged, or a rocket rifle (being reset)
  1668. Assert( ( pObject->bStatus[ 0 ] < 100 ) || ItemIsARocketRifle( pObject->usItem ) );
  1669. /* ARM: Can now repair with removeable attachments still attached...
  1670. // d) Already stripped of all *detachable* attachments
  1671. for( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1672. {
  1673. if ( pObject->usAttachItem[ ubCnt ] != NONE )
  1674. {
  1675. // If the attachment is detachable
  1676. if (! (Item[ pObject->usAttachItem[ubCnt] ].fFlags & ITEM_INSEPARABLE ) )
  1677. {
  1678. Assert( 0 );
  1679. }
  1680. }
  1681. }
  1682. */
  1683. // e) If a gun, stripped of any non-ammo-class GunAmmoItems, and bullets
  1684. if (Item [ pObject->usItem ].usItemClass == IC_GUN )
  1685. {
  1686. // if any GunAmmoItem is specified
  1687. if( pObject->usGunAmmoItem != NONE)
  1688. {
  1689. // it better be regular ammo, and empty
  1690. Assert( Item[ pObject->usGunAmmoItem ].usItemClass == IC_AMMO );
  1691. Assert( pObject->ubGunShotsLeft == 0 );
  1692. }
  1693. }
  1694. SetSpecialItemInfoFromObject( &SpclItemInfo, pObject );
  1695. // ok, given all that, now everything is easy!
  1696. // if the gun was jammed, this will forget about the jam (i.e. dealer immediately unjams anything he will be repairing)
  1697. GiveItemToArmsDealerforRepair( ubArmsDealer, pObject->usItem, &SpclItemInfo, ubOwnerProfileId );
  1698. }
  1699. //PLEASE: Use GiveObjectToArmsDealerForRepair() instead of this when repairing a item in OBJECTTYPE format.
  1700. void GiveItemToArmsDealerforRepair( UINT8 ubArmsDealer, UINT16 usItemIndex, SPECIAL_ITEM_INFO *pSpclItemInfo, UINT8 ubOwnerProfileId )
  1701. {
  1702. UINT32 uiTimeWhenFreeToStartIt;
  1703. UINT32 uiMinutesToFix;
  1704. UINT32 uiMinutesShopClosedBeforeItsDone;
  1705. UINT32 uiDoneWhen;
  1706. Assert( DoesDealerDoRepairs( ubArmsDealer ) );
  1707. Assert( pSpclItemInfo->bItemCondition > 0 );
  1708. Assert( ( pSpclItemInfo->bItemCondition < 100 ) || ItemIsARocketRifle( usItemIndex ) );
  1709. // figure out the earliest the repairman will be free to start repairing this item
  1710. uiTimeWhenFreeToStartIt = WhenWillRepairmanBeAllDoneRepairing( ubArmsDealer );
  1711. //Determine how long it will take to fix
  1712. uiMinutesToFix = CalculateSpecialItemRepairTime( ubArmsDealer, usItemIndex, pSpclItemInfo );
  1713. uiMinutesShopClosedBeforeItsDone = CalculateOvernightRepairDelay( ubArmsDealer, uiTimeWhenFreeToStartIt, uiMinutesToFix );
  1714. // clock time when this will finally be ready
  1715. uiDoneWhen = uiTimeWhenFreeToStartIt + uiMinutesToFix + uiMinutesShopClosedBeforeItsDone;
  1716. // Negate the status
  1717. pSpclItemInfo->bItemCondition *= -1;
  1718. // give it to the dealer
  1719. AddItemToArmsDealerInventory( ubArmsDealer, usItemIndex, pSpclItemInfo, 1 );
  1720. //Set the time at which item will be fixed
  1721. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ gubLastSpecialItemAddedAtElement ].uiRepairDoneTime = uiDoneWhen;
  1722. //Remember the original owner of the item
  1723. gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ gubLastSpecialItemAddedAtElement ].ubOwnerProfileId = ubOwnerProfileId;
  1724. }
  1725. UINT32 WhenWillRepairmanBeAllDoneRepairing( UINT8 ubArmsDealer )
  1726. {
  1727. UINT32 uiWhenFree;
  1728. UINT16 usItemIndex;
  1729. UINT8 ubElement;
  1730. Assert( DoesDealerDoRepairs( ubArmsDealer ) );
  1731. // if nothing is in for repairs, he'll be free RIGHT NOW!
  1732. uiWhenFree = GetWorldTotalMin();
  1733. //loop through the dealers inventory
  1734. for( usItemIndex = 1; usItemIndex < MAXITEMS; usItemIndex++ )
  1735. {
  1736. //if there is some items in stock
  1737. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubTotalItems > 0 )
  1738. {
  1739. for ( ubElement = 0; ubElement < gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].ubElementsAlloced; ubElement++ )
  1740. {
  1741. if ( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].fActive )
  1742. {
  1743. //if the item is in for repairs
  1744. if( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].Info.bItemCondition < 0 )
  1745. {
  1746. // if this item will be done later than the latest we've found so far
  1747. if ( gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].uiRepairDoneTime > uiWhenFree )
  1748. {
  1749. // then we're busy til then!
  1750. uiWhenFree = gArmsDealersInventory[ ubArmsDealer ][ usItemIndex ].SpecialItem[ ubElement ].uiRepairDoneTime;
  1751. }
  1752. }
  1753. }
  1754. }
  1755. }
  1756. }
  1757. return( uiWhenFree );
  1758. }
  1759. UINT32 CalculateSpecialItemRepairTime( UINT8 ubArmsDealer, UINT16 usItemIndex, SPECIAL_ITEM_INFO *pSpclItemInfo )
  1760. {
  1761. UINT32 uiRepairTime;
  1762. UINT8 ubCnt;
  1763. uiRepairTime = CalculateSimpleItemRepairTime( ubArmsDealer, usItemIndex, pSpclItemInfo->bItemCondition );
  1764. // add time to repair any attachments on it
  1765. for ( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1766. {
  1767. if ( pSpclItemInfo->usAttachment[ ubCnt ] != NONE )
  1768. {
  1769. // if damaged and repairable
  1770. if ( ( pSpclItemInfo->bAttachmentStatus[ ubCnt ] < 100 ) && CanDealerRepairItem( ubArmsDealer, pSpclItemInfo->usAttachment[ ubCnt ] ) )
  1771. {
  1772. uiRepairTime += CalculateSimpleItemRepairTime( ubArmsDealer, pSpclItemInfo->usAttachment[ ubCnt ], pSpclItemInfo->bAttachmentStatus[ ubCnt ] );
  1773. }
  1774. }
  1775. }
  1776. return( uiRepairTime );
  1777. }
  1778. UINT32 CalculateObjectItemRepairTime( UINT8 ubArmsDealer, OBJECTTYPE *pItemObject )
  1779. {
  1780. UINT32 uiRepairTime;
  1781. UINT8 ubCnt;
  1782. uiRepairTime = CalculateSimpleItemRepairTime( ubArmsDealer, pItemObject->usItem, pItemObject->bStatus[ 0 ] );
  1783. // add time to repair any attachments on it
  1784. for ( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1785. {
  1786. if ( pItemObject->usAttachItem[ ubCnt ] != NONE )
  1787. {
  1788. // if damaged and repairable
  1789. if ( ( pItemObject->bAttachStatus[ ubCnt ] < 100 ) && CanDealerRepairItem( ubArmsDealer, pItemObject->usAttachItem[ ubCnt ] ) )
  1790. {
  1791. uiRepairTime += CalculateSimpleItemRepairTime( ubArmsDealer, pItemObject->usAttachItem[ ubCnt ], pItemObject->bAttachStatus[ ubCnt ] );
  1792. }
  1793. }
  1794. }
  1795. return( uiRepairTime );
  1796. }
  1797. UINT32 CalculateSimpleItemRepairTime( UINT8 ubArmsDealer, UINT16 usItemIndex, INT8 bItemCondition )
  1798. {
  1799. UINT32 uiTimeToRepair = 0;
  1800. UINT32 uiRepairCost = 0;
  1801. Assert( DoesDealerDoRepairs( ubArmsDealer ) );
  1802. // first calc what he'll charge - that takes care of item condition, repair ease, and his repair cost "markup"
  1803. uiRepairCost = CalculateSimpleItemRepairCost( ubArmsDealer, usItemIndex, bItemCondition );
  1804. // Now adjust that for the repairman's individual repair speed.
  1805. // For a repairman, his BUY modifier controls his REPAIR SPEED (1.0 means minutes to repair = price in $)
  1806. // with a REPAIR SPEED of 1.0, typical gun price of $2000, and a REPAIR COST of 0.5 this works out to 16.6 hrs
  1807. // for a full 100% status repair... Not bad.
  1808. uiTimeToRepair = (UINT32)( uiRepairCost * ArmsDealerInfo[ ubArmsDealer ].dRepairSpeed );
  1809. // repairs on electronic items take twice as long if the guy doesn't have the skill
  1810. // for dealers, this means anyone but Fredo the Electronics guy takes twice as long (but doesn't charge double)
  1811. // (Mind you, current he's the ONLY one who CAN repair Electronics at all! Oh well.)
  1812. if( ( Item[ usItemIndex ].fFlags & ITEM_ELECTRONIC ) && ( ubArmsDealer != ARMS_DEALER_FREDO ) )
  1813. {
  1814. uiTimeToRepair *= 2;
  1815. }
  1816. // avoid "instant" repairs on really cheap, barely damaged crap...
  1817. if (uiTimeToRepair < MIN_REPAIR_TIME_IN_MINUTES)
  1818. {
  1819. uiTimeToRepair = MIN_REPAIR_TIME_IN_MINUTES;
  1820. }
  1821. return( uiTimeToRepair );
  1822. }
  1823. UINT32 CalculateSpecialItemRepairCost( UINT8 ubArmsDealer, UINT16 usItemIndex, SPECIAL_ITEM_INFO *pSpclItemInfo )
  1824. {
  1825. UINT32 uiRepairCost;
  1826. UINT8 ubCnt;
  1827. uiRepairCost = CalculateSimpleItemRepairCost( ubArmsDealer, usItemIndex, pSpclItemInfo->bItemCondition );
  1828. // add cost of repairing any attachments on it
  1829. for ( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1830. {
  1831. if ( pSpclItemInfo->usAttachment[ ubCnt ] != NONE )
  1832. {
  1833. // if damaged and repairable
  1834. if ( ( pSpclItemInfo->bAttachmentStatus[ ubCnt ] < 100 ) && CanDealerRepairItem( ubArmsDealer, pSpclItemInfo->usAttachment[ ubCnt ] ) )
  1835. {
  1836. uiRepairCost += CalculateSimpleItemRepairCost( ubArmsDealer, pSpclItemInfo->usAttachment[ ubCnt ], pSpclItemInfo->bAttachmentStatus[ ubCnt ] );
  1837. }
  1838. }
  1839. }
  1840. return( uiRepairCost );
  1841. }
  1842. UINT32 CalculateObjectItemRepairCost( UINT8 ubArmsDealer, OBJECTTYPE *pItemObject )
  1843. {
  1844. UINT32 uiRepairCost;
  1845. UINT8 ubCnt;
  1846. uiRepairCost = CalculateSimpleItemRepairCost( ubArmsDealer, pItemObject->usItem, pItemObject->bStatus[ 0 ] );
  1847. // add cost of repairing any attachments on it
  1848. for ( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1849. {
  1850. if ( pItemObject->usAttachItem[ ubCnt ] != NONE )
  1851. {
  1852. // if damaged and repairable
  1853. if ( ( pItemObject->bAttachStatus[ ubCnt ] < 100 ) && CanDealerRepairItem( ubArmsDealer, pItemObject->usAttachItem[ ubCnt ] ) )
  1854. {
  1855. uiRepairCost += CalculateSimpleItemRepairCost( ubArmsDealer, pItemObject->usAttachItem[ ubCnt ], pItemObject->bAttachStatus[ ubCnt ] );
  1856. }
  1857. }
  1858. }
  1859. return( uiRepairCost );
  1860. }
  1861. UINT32 CalculateSimpleItemRepairCost( UINT8 ubArmsDealer, UINT16 usItemIndex, INT8 bItemCondition )
  1862. {
  1863. UINT32 uiItemCost = 0;
  1864. UINT32 uiRepairCost = 0;
  1865. INT16 sRepairCostAdj = 0;
  1866. // UINT32 uiDifFrom10=0;
  1867. // figure out the full value of the item, modified by this dealer's personal Sell (i.e. repair cost) modifier
  1868. // don't use CalcShopKeeperItemPrice - we want FULL value!!!
  1869. uiItemCost = (UINT32)(( Item[ usItemIndex ].usPrice * ArmsDealerInfo[ ubArmsDealer ].dRepairCost ) );
  1870. // get item's repair ease, for each + point is 10% easier, each - point is 10% harder to repair
  1871. sRepairCostAdj = 100 - ( 10 * Item[ usItemIndex ].bRepairEase );
  1872. // make sure it ain't somehow gone too low!
  1873. if (sRepairCostAdj < 10)
  1874. {
  1875. sRepairCostAdj = 10;
  1876. }
  1877. // calculate repair cost, the more broken it is the more it costs, and the difficulty of repair it is also a factor
  1878. uiRepairCost = (UINT32)( uiItemCost * ( sRepairCostAdj * (100 - bItemCondition) / ((FLOAT)100 * 100) ));
  1879. /*
  1880. //if the price is not diviseble by 10, make it so
  1881. uiDifFrom10 = 10 - uiRepairCost % 10;
  1882. if( uiDifFrom10 != 0 )
  1883. {
  1884. uiRepairCost += uiDifFrom10;
  1885. }
  1886. */
  1887. if ( ItemIsARocketRifle( usItemIndex ) )
  1888. {
  1889. // resetting imprinting for a rocket rifle costs something extra even if rifle is at 100%
  1890. uiRepairCost += 100;
  1891. }
  1892. // anything repairable has to have a minimum price
  1893. if ( uiRepairCost < MIN_REPAIR_COST )
  1894. {
  1895. uiRepairCost = MIN_REPAIR_COST;
  1896. }
  1897. return( uiRepairCost );
  1898. }
  1899. void SetSpecialItemInfoToDefaults( SPECIAL_ITEM_INFO *pSpclItemInfo )
  1900. {
  1901. UINT8 ubCnt;
  1902. memset( pSpclItemInfo, 0, sizeof( SPECIAL_ITEM_INFO ) );
  1903. pSpclItemInfo->bItemCondition = 100;
  1904. pSpclItemInfo->ubImprintID = NO_PROFILE;
  1905. for ( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1906. {
  1907. pSpclItemInfo->usAttachment[ ubCnt ] = NONE;
  1908. pSpclItemInfo->bAttachmentStatus[ ubCnt ] = 0;
  1909. }
  1910. }
  1911. void SetSpecialItemInfoFromObject( SPECIAL_ITEM_INFO *pSpclItemInfo, OBJECTTYPE *pObject )
  1912. {
  1913. UINT8 ubCnt;
  1914. memset(pSpclItemInfo, 0, sizeof( SPECIAL_ITEM_INFO ) );
  1915. if( Item[ pObject->usItem ].usItemClass == IC_AMMO )
  1916. {
  1917. // ammo condition is always 100, don't use status, which holds the #bullets
  1918. pSpclItemInfo->bItemCondition = 100;
  1919. }
  1920. else
  1921. {
  1922. pSpclItemInfo->bItemCondition = pObject->bStatus[ 0 ];
  1923. }
  1924. // only guns currently have imprintID properly initialized...
  1925. if ( Item[ pObject->usItem ].usItemClass == IC_GUN)
  1926. {
  1927. pSpclItemInfo->ubImprintID = pObject->ubImprintID;
  1928. }
  1929. else
  1930. {
  1931. // override garbage imprintIDs (generally 0) for non-guns
  1932. pSpclItemInfo->ubImprintID = NO_PROFILE;
  1933. }
  1934. for ( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1935. {
  1936. if( pObject->usAttachItem[ ubCnt ] != NONE )
  1937. {
  1938. // store what it is
  1939. pSpclItemInfo->usAttachment[ ubCnt ] = pObject->usAttachItem[ ubCnt ];
  1940. pSpclItemInfo->bAttachmentStatus[ ubCnt ] = pObject->bAttachStatus[ ubCnt ];
  1941. }
  1942. else
  1943. {
  1944. pSpclItemInfo->usAttachment[ ubCnt ] = NONE;
  1945. pSpclItemInfo->bAttachmentStatus[ ubCnt ] = 0;
  1946. }
  1947. }
  1948. }
  1949. BOOLEAN IsItemInfoSpecial( SPECIAL_ITEM_INFO *pSpclItemInfo )
  1950. {
  1951. UINT8 ubCnt;
  1952. // being damaged / in repairs makes an item special
  1953. if ( pSpclItemInfo->bItemCondition != 100 )
  1954. {
  1955. return(TRUE);
  1956. }
  1957. // being imprinted makes an item special
  1958. if (pSpclItemInfo->ubImprintID != NO_PROFILE)
  1959. {
  1960. return(TRUE);
  1961. }
  1962. // having an attachment makes an item special
  1963. for ( ubCnt = 0; ubCnt < MAX_ATTACHMENTS; ubCnt++ )
  1964. {
  1965. if ( pSpclItemInfo->usAttachment[ ubCnt ] != NONE )
  1966. {
  1967. return(TRUE);
  1968. }
  1969. }
  1970. // otherwise, it's just a "perfect" item, nothing special about it
  1971. return(FALSE);
  1972. }
  1973. BOOLEAN DoesItemAppearInDealerInventoryList( UINT8 ubArmsDealer, UINT16 usItemIndex, BOOLEAN fPurchaseFromPlayer )
  1974. {
  1975. DEALER_POSSIBLE_INV *pDealerInv=NULL;
  1976. UINT16 usCnt;
  1977. // the others will buy only things that appear in their own "for sale" inventory lists
  1978. pDealerInv = GetPointerToDealersPossibleInventory( ubArmsDealer );
  1979. Assert( pDealerInv != NULL );
  1980. // loop through the dealers' possible inventory and see if the item exists there
  1981. usCnt = 0;
  1982. while( pDealerInv[ usCnt ].sItemIndex != LAST_DEALER_ITEM )
  1983. {
  1984. //if the initial dealer inv contains the required item, the dealer can sell the item
  1985. if( pDealerInv[ usCnt ].sItemIndex == usItemIndex )
  1986. {
  1987. // if optimal quantity listed is 0, it means dealer won't sell it himself, but will buy it from the player!
  1988. if ( ( pDealerInv[ usCnt ].ubOptimalNumber > 0 ) || fPurchaseFromPlayer )
  1989. {
  1990. return( TRUE );
  1991. }
  1992. }
  1993. usCnt++;
  1994. }
  1995. return( FALSE );
  1996. }
  1997. UINT16 CalcValueOfItemToDealer( UINT8 ubArmsDealer, UINT16 usItemIndex, BOOLEAN fDealerSelling )
  1998. {
  1999. UINT16 usBasePrice;
  2000. UINT8 ubItemPriceClass;
  2001. UINT8 ubDealerPriceClass;
  2002. UINT16 usValueToThisDealer;
  2003. usBasePrice = Item[ usItemIndex ].usPrice;
  2004. if ( usBasePrice == 0 )
  2005. {
  2006. // worthless to any dealer
  2007. return( 0 );
  2008. }
  2009. // figure out the price class this dealer prefers
  2010. switch ( ubArmsDealer )
  2011. {
  2012. case ARMS_DEALER_JAKE:
  2013. ubDealerPriceClass = PRICE_CLASS_JUNK;
  2014. break;
  2015. case ARMS_DEALER_KEITH:
  2016. ubDealerPriceClass = PRICE_CLASS_CHEAP;
  2017. break;
  2018. case ARMS_DEALER_FRANZ:
  2019. ubDealerPriceClass = PRICE_CLASS_EXPENSIVE;
  2020. break;
  2021. // other dealers don't use this system
  2022. default:
  2023. if ( DoesItemAppearInDealerInventoryList( ubArmsDealer, usItemIndex, TRUE ) )
  2024. {
  2025. return( usBasePrice );
  2026. }
  2027. else
  2028. {
  2029. return( 0 );
  2030. }
  2031. }
  2032. // the rest of this function applies only to the "general" dealers ( Jake, Keith, and Franz )
  2033. // Micky & Gabby specialize in creature parts & such, the others don't buy these at all (exception: jars)
  2034. if ( ( usItemIndex != JAR ) &&
  2035. ( DoesItemAppearInDealerInventoryList( ARMS_DEALER_MICKY, usItemIndex, TRUE ) ||
  2036. DoesItemAppearInDealerInventoryList( ARMS_DEALER_GABBY, usItemIndex, TRUE ) ) )
  2037. {
  2038. return( 0 );
  2039. }
  2040. if ( ( ubArmsDealer == ARMS_DEALER_KEITH ) && ( Item [ usItemIndex].usItemClass & ( IC_GUN | IC_LAUNCHER ) ) )
  2041. {
  2042. // Keith won't buy guns until the Hillbillies are vanquished
  2043. if( CheckFact( FACT_HILLBILLIES_KILLED, KEITH ) == FALSE )
  2044. {
  2045. return( 0 );
  2046. }
  2047. }
  2048. // figure out which price class it belongs to
  2049. if ( usBasePrice < 100 )
  2050. {
  2051. ubItemPriceClass = PRICE_CLASS_JUNK;
  2052. }
  2053. else
  2054. if ( usBasePrice < 1000 )
  2055. {
  2056. ubItemPriceClass = PRICE_CLASS_CHEAP;
  2057. }
  2058. else
  2059. {
  2060. ubItemPriceClass = PRICE_CLASS_EXPENSIVE;
  2061. }
  2062. if( !fDealerSelling )
  2063. {
  2064. // junk dealer won't buy expensive stuff at all, expensive dealer won't buy junk at all
  2065. if ( abs( (INT8) ubDealerPriceClass - (INT8) ubItemPriceClass ) == 2 )
  2066. {
  2067. return( 0 );
  2068. }
  2069. }
  2070. // start with the base price
  2071. usValueToThisDealer = usBasePrice;
  2072. // if it's out of their preferred price class
  2073. if ( ubDealerPriceClass != ubItemPriceClass )
  2074. {
  2075. // exception: Gas (Jake's)
  2076. if ( usItemIndex != GAS_CAN )
  2077. {
  2078. // they pay only 1/3 of true value!
  2079. usValueToThisDealer /= 3;
  2080. }
  2081. }
  2082. #ifndef JA2DEMO // don't halve the gun/silencer prices in the demo...
  2083. // Tony specializes in guns, weapons, and ammo, so make others pay much less for that kind of stuff
  2084. if ( DoesItemAppearInDealerInventoryList( ARMS_DEALER_TONY, usItemIndex, TRUE ) )
  2085. {
  2086. // others pay only 1/2 of that value!
  2087. usValueToThisDealer /= 2;
  2088. }
  2089. #endif
  2090. // minimum bet $1 !
  2091. if ( usValueToThisDealer == 0 )
  2092. {
  2093. usValueToThisDealer = 1;
  2094. }
  2095. return( usValueToThisDealer );
  2096. }
  2097. // this only exists to support saves made with game versions < 54 or 55!
  2098. BOOLEAN LoadIncompleteArmsDealersStatus( HWFILE hFile, BOOLEAN fIncludesElgin, BOOLEAN fIncludesManny )
  2099. {
  2100. UINT32 uiDealersSaved;
  2101. UINT32 uiNumBytesRead;
  2102. Assert( !fIncludesElgin || !fIncludesManny );
  2103. if ( !fIncludesElgin )
  2104. {
  2105. // read 2 fewer element without Elgin or Manny in there...
  2106. uiDealersSaved = NUM_ARMS_DEALERS - 2;
  2107. }
  2108. else
  2109. {
  2110. // read one fewer element without Elgin in there...
  2111. uiDealersSaved = NUM_ARMS_DEALERS - 1;
  2112. }
  2113. // read in all other dealer's status
  2114. if (!FileRead( hFile, gArmsDealerStatus, uiDealersSaved * sizeof( ARMS_DEALER_STATUS ), &uiNumBytesRead ))
  2115. {
  2116. return( FALSE );
  2117. }
  2118. // read in all other dealer's inventory
  2119. if (!FileRead( hFile, gArmsDealersInventory, uiDealersSaved * sizeof( DEALER_ITEM_HEADER ) * MAXITEMS, &uiNumBytesRead ))
  2120. {
  2121. return( FALSE );
  2122. }
  2123. if ( !fIncludesElgin )
  2124. {
  2125. // initialize Elgin now...
  2126. InitializeOneArmsDealer( ARMS_DEALER_ELGIN );
  2127. }
  2128. if ( !fIncludesManny )
  2129. {
  2130. // initialize Manny now...
  2131. InitializeOneArmsDealer( ARMS_DEALER_MANNY );
  2132. }
  2133. return(TRUE);
  2134. }
  2135. BOOLEAN DealerItemIsSafeToStack( UINT16 usItemIndex )
  2136. {
  2137. // basically any item type with nothing unique about it besides its status can be stacked in dealer's inventory boxes...
  2138. // NOTE: This test is only applied to items already KNOWN to be perfect - special items are obviously not-stackable
  2139. if ( Item[ usItemIndex ].usItemClass == IC_GUN )
  2140. {
  2141. return( FALSE );
  2142. }
  2143. /*
  2144. if ( ItemSlotLimit( usItemIndex, BIGPOCK1POS ) > 1 )
  2145. {
  2146. return( TRUE );
  2147. }
  2148. */
  2149. return( TRUE );
  2150. }
  2151. void GuaranteeMinimumAlcohol( UINT8 ubArmsDealer )
  2152. {
  2153. GuaranteeAtLeastXItemsOfIndex( ubArmsDealer, BEER, ( UINT8 ) ( GetDealersMaxItemAmount( ubArmsDealer, BEER ) / 3 ) );
  2154. GuaranteeAtLeastXItemsOfIndex( ubArmsDealer, WINE, ( UINT8 ) ( GetDealersMaxItemAmount( ubArmsDealer, WINE ) / 3 ) );
  2155. GuaranteeAtLeastXItemsOfIndex( ubArmsDealer, ALCOHOL, ( UINT8 ) ( GetDealersMaxItemAmount( ubArmsDealer, ALCOHOL ) / 3 ) );
  2156. }
  2157. BOOLEAN ItemIsARocketRifle( INT16 sItemIndex )
  2158. {
  2159. if ( ( sItemIndex == ROCKET_RIFLE ) || ( sItemIndex == AUTO_ROCKET_RIFLE ) )
  2160. {
  2161. return( TRUE );
  2162. }
  2163. else
  2164. {
  2165. return( FALSE );
  2166. }
  2167. }
  2168. BOOLEAN GetArmsDealerShopHours( UINT8 ubArmsDealer, UINT32 *puiOpeningTime, UINT32 *puiClosingTime )
  2169. {
  2170. SOLDIERTYPE *pSoldier;
  2171. pSoldier = FindSoldierByProfileID( ArmsDealerInfo[ ubArmsDealer ].ubShopKeeperID, FALSE );
  2172. if ( pSoldier == NULL )
  2173. {
  2174. return( FALSE );
  2175. }
  2176. if ( ExtractScheduleDoorLockAndUnlockInfo( pSoldier, puiOpeningTime, puiClosingTime ) == FALSE )
  2177. {
  2178. return( FALSE );
  2179. }
  2180. Assert( *puiOpeningTime < *puiClosingTime );
  2181. return( TRUE );
  2182. }
  2183. UINT32 CalculateOvernightRepairDelay( UINT8 ubArmsDealer, UINT32 uiTimeWhenFreeToStartIt, UINT32 uiMinutesToFix )
  2184. {
  2185. UINT32 uiOpeningTime, uiClosingTime;
  2186. UINT32 uiMinutesClosedOvernight;
  2187. UINT32 uiDelayInDays = 0;
  2188. UINT32 uiDoneToday;
  2189. Assert( uiMinutesToFix > 0 );
  2190. // convert world time into 24hr military time for the day he's gonna start on it
  2191. uiTimeWhenFreeToStartIt = uiTimeWhenFreeToStartIt % NUM_MIN_IN_DAY;
  2192. if ( GetArmsDealerShopHours( ubArmsDealer, &uiOpeningTime, &uiClosingTime ) == FALSE )
  2193. {
  2194. return( 0 );
  2195. }
  2196. // if it won't get done by the end of a day
  2197. while ( ( uiTimeWhenFreeToStartIt + uiMinutesToFix ) > uiClosingTime )
  2198. {
  2199. // this is to handle existing saves with overnight repairs
  2200. if ( uiTimeWhenFreeToStartIt < uiClosingTime )
  2201. {
  2202. // he gets this much done before closing
  2203. uiDoneToday = uiClosingTime - uiTimeWhenFreeToStartIt;
  2204. // subtract how much he got done
  2205. uiMinutesToFix -= uiDoneToday;
  2206. Assert( uiMinutesToFix > 0 );
  2207. }
  2208. // he starts back at it first thing in the morning
  2209. uiTimeWhenFreeToStartIt = uiOpeningTime;
  2210. uiDelayInDays++;
  2211. }
  2212. uiMinutesClosedOvernight = NUM_MIN_IN_DAY - ( uiClosingTime - uiOpeningTime );
  2213. return ( uiDelayInDays * uiMinutesClosedOvernight );
  2214. }
  2215. UINT32 CalculateMinutesClosedBetween( UINT8 ubArmsDealer, UINT32 uiStartTime, UINT32 uiEndTime )
  2216. {
  2217. UINT32 uiOpeningTime, uiClosingTime;
  2218. UINT32 uiMinutesClosedOvernight;
  2219. UINT32 uiDaysDifference = 0;
  2220. UINT32 uiMinutesClosed = 0;
  2221. Assert( uiStartTime <= uiEndTime );
  2222. if ( GetArmsDealerShopHours( ubArmsDealer, &uiOpeningTime, &uiClosingTime ) == FALSE )
  2223. {
  2224. return( 0 );
  2225. }
  2226. uiMinutesClosedOvernight = NUM_MIN_IN_DAY - ( uiClosingTime - uiOpeningTime );
  2227. // NOTE: this assumes stored are only closed overnight, so all we have to do is compare the day portion
  2228. uiDaysDifference = ( uiEndTime / NUM_MIN_IN_DAY ) - ( uiStartTime / NUM_MIN_IN_DAY );
  2229. if ( uiDaysDifference >= 2 )
  2230. {
  2231. // close for 1 less than that many full nights...
  2232. uiMinutesClosed = ( uiDaysDifference - 1 ) * uiMinutesClosedOvernight;
  2233. }
  2234. // add partial day's closing
  2235. // convert start and end times into 24hr military time
  2236. uiStartTime = uiStartTime % NUM_MIN_IN_DAY;
  2237. uiEndTime = uiEndTime % NUM_MIN_IN_DAY;
  2238. // treat end time of midnight as 24:00 hours to prevent indefinite recursion and make formulas work
  2239. if ( uiEndTime == 0 )
  2240. {
  2241. uiEndTime = NUM_MIN_IN_DAY;
  2242. }
  2243. if ( uiStartTime == uiEndTime )
  2244. {
  2245. if ( uiDaysDifference == 0 )
  2246. {
  2247. return( 0 );
  2248. }
  2249. else
  2250. {
  2251. uiMinutesClosed += uiMinutesClosedOvernight;
  2252. }
  2253. }
  2254. if ( uiStartTime < uiEndTime )
  2255. {
  2256. if ( uiStartTime < uiOpeningTime )
  2257. {
  2258. // add how many minutes in the time range BEFORE the store opened that day
  2259. uiMinutesClosed += ( min( uiOpeningTime, uiEndTime ) - uiStartTime );
  2260. }
  2261. if ( uiEndTime > uiClosingTime )
  2262. {
  2263. // add how many minutes in the time range AFTER the store closed that day
  2264. uiMinutesClosed += ( uiEndTime - max( uiClosingTime, uiStartTime ) );
  2265. }
  2266. }
  2267. else
  2268. {
  2269. Assert( uiEndTime < uiStartTime );
  2270. // recursive calls! Add two separate times: before midnight, and after midnight
  2271. uiMinutesClosed += CalculateMinutesClosedBetween( ubArmsDealer, uiStartTime, NUM_MIN_IN_DAY );
  2272. uiMinutesClosed += CalculateMinutesClosedBetween( ubArmsDealer, 0, uiEndTime );
  2273. }
  2274. return ( uiMinutesClosed );
  2275. }