Skip to navigation

Elite on the BBC Micro and NES

Market: tnpr

[Elite-A, Docked]

Name: tnpr [Show more] Type: Subroutine Category: Market Summary: Work out if we have space for a specific amount of cargo
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * TT219 calls tnpr * EQSHP calls via Tml

Given a market item and an amount, work out whether there is room in the cargo hold for this item. For standard tonne canisters, the limit is given by size of the cargo hold of our current ship. For items measured in kg (gold, platinum), g (gem-stones) and alien items, there is no limit.
Arguments: A The number of units of this market item QQ29 The type of market item (see QQ23 for a list of market item numbers)
Returns: C flag Returns the result: * Set if there is no room for this item * Clear if there is room for this item
Other entry points: Tml Calculate the sum of the following, returning the C flag according to whether this all fits into the hold: * The total tonnage of the first X items of cargo * The value in A * Plus one more tonne if the C flag is set on entry This is called with X = 12, A = the number of alien items in the hold, and the C flag set, to see if there is room for one more tonne in the hold
.tnpr \ --- Mod: Code removed for Elite-A: ------------------> \ PHA \ Store A on the stack \ --- End of removed code -----------------------------> LDX #12 \ If QQ29 > 12 then jump to kg below, as this cargo CPX QQ29 \ type is gold, platinum, gem-stones or alien items, BCC kg \ and they have different cargo limits to the standard \ tonne canisters \ --- Mod: Code added for Elite-A: --------------------> CLC \ Clear the C flag for the addition below \ --- End of added code -------------------------------> .Tml \ Here we count the tonne canisters we have in the hold \ and add to A to see if we have enough room for A more \ tonnes of cargo, using X as the loop counter, starting \ with X = 12 \ --- Mod: Code removed for Elite-A: ------------------> \ ADC QQ20,X \ Set A = A + the number of tonnes we have in the hold \ \ of market item number X. Note that the first time we \ \ go round this loop, the C flag is set (as we didn't \ \ branch with the BCC above, so the effect of this loop \ \ is to count the number of tonne canisters in the hold, \ \ and add 1 \ --- And replaced by: --------------------------------> ADC QQ20,X \ Set A = A + the number of tonnes we have in the hold \ of market item number X BCS n_over \ If the addition overflowed, jump to n_over to return \ from the subroutine with the C flag set, as the hold \ is already full \ --- End of replacement ------------------------------> DEX \ Decrement the loop counter BPL Tml \ Loop back to add in the next market item in the hold, \ until we have added up all market items from 12 \ (minerals) down to 0 (food) \ --- Mod: Code removed for Elite-A: ------------------> \ CMP CRGO \ If A < CRGO then the C flag will be clear (we have \ \ room in the hold) \ \ \ \ If A >= CRGO then the C flag will be set (we do not \ \ have room in the hold) \ \ \ \ This works because A contains the number of canisters \ \ plus 1, while CRGO contains our cargo capacity plus 2, \ \ so if we actually have "a" canisters and a capacity \ \ of "c", then: \ \ \ \ A < CRGO means: a+1 < c+2 \ \ a < c+1 \ \ a <= c \ \ \ \ So this is why the value in CRGO is 2 higher than the \ \ actual cargo bay size, i.e. it's 22 for the standard \ \ 20-tonne bay, and 37 for the large 35-tonne bay \ \ PLA \ Restore A from the stack \ --- And replaced by: --------------------------------> CMP new_hold \ If A < new_hold then the C flag will be clear (we have \ room in the hold) \ \ If A >= new_hold then the C flag will be set (we do \ not have room in the hold) .n_over \ --- End of replacement ------------------------------> RTS \ Return from the subroutine .kg \ Here we count the number of items of this type that \ we already have in the hold, and add to A to see if \ we have enough room for A more units LDY QQ29 \ Set Y to the item number we want to add ADC QQ20,Y \ Set A = A + the number of units of this item that we \ already have in the hold \ --- Mod: Code removed for Elite-A: ------------------> \ CMP #200 \ Is the result greater than 200 (the limit on \ \ individual stocks of gold, platinum, gem-stones and \ \ alien items)? \ \ \ \ If so, this sets the C flag (no room) \ \ \ \ Otherwise it is clear (we have room) \ \ PLA \ Restore A from the stack \ --- End of removed code -----------------------------> RTS \ Return from the subroutine