Elite on the BBC Micro

# Market: tnpr [Elite-A, Parasite]

```       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 entry point 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

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

CLC                    \ Clear the C flag for the addition below

.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

\ from the subroutine with the C flag set, as 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 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

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

RTS                    \ Return from the subroutine
```