Elite on the BBC Micro and NES

# Market: tnpr

## [BBC Micro cassette version]

```       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
Variations: See code variations for this subroutine in the different versions
References: This subroutine is called as follows:
* Main flight loop (Part 8 of 16) calls tnpr
* TT219 calls tnpr

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 the type of cargo hold we
have, with a standard cargo hold having a capacity of 20t and an extended
cargo bay being 35t.

For items measured in kg (gold, platinum), g (gem-stones) and alien items,
the individual limit on each of these is 200 units.

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:

A                    A is preserved

C flag               Returns the result:

* Set if there is no room for this item

* Clear if there is room for this item

.tnpr

PHA                    \ Store A on the stack

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

.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

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,

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)

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

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

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

RTS                    \ Return from the subroutine
```