Skip to navigation

Market: gnum

[Elite Demonstration Disc]

Name: gnum [Show more] Type: Subroutine Category: Market Summary: Get a number from the keyboard
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * EQSHP calls gnum * TT219 calls gnum

Get a number from the keyboard, up to the maximum number in QQ25, for the buying and selling of cargo and equipment. Pressing a key with an ASCII code less than ASCII "0" will return a 0 in A (so that includes pressing Space or Return), while pressing a key with an ASCII code greater than ASCII "9" will jump to the Inventory screen (so that includes all letters and most punctuation).
Arguments: QQ25 The maximum number allowed
Returns: A The number entered R Also contains the number entered C flag Set if the number is too large (> QQ25), clear otherwise
.gnum \ --- Mod: Code removed for Demonstration Disc: -------> \LDX #0 \ We will build the number entered in R, so initialise \STX R \ it with 0 \ \LDX #12 \ We will check for up to 12 key presses, so set a \STX T1 \ counter in T1 \ \.TT223 \ \JSR TT217 \ Scan the keyboard until a key is pressed, and return \ \ the key's ASCII code in A (and X) \ \STA Q \ Store the key pressed in Q \ \SEC \ Subtract ASCII "0" from the key pressed, to leave the \SBC #'0' \ numeric value of the key in A (if it was a number key) \ \BCC OUT \ If A < 0, jump to OUT to load the current number and \ \ return from the subroutine, as the key pressed was \ \ RETURN (or some other character with a value less than \ \ ASCII "0") \ \CMP #10 \ If A >= 10, jump to BAY2 to display the Inventory \BCS BAY2 \ screen, as the key pressed was a letter or other \ \ non-digit and is greater than ASCII "9" \ \STA S \ Store the numeric value of the key pressed in S \ \LDA R \ Fetch the result so far into A \ \CMP #26 \ If A >= 26, where A is the number entered so far, then \BCS OUT \ adding a further digit will make it bigger than 256, \ \ so jump to OUT to return from the subroutine with the \ \ result in R (i.e. ignore the last key press) \ \ASL A \ Set A = (A * 2) + (A * 8) = A * 10 \STA T \ASL A \ASL A \ADC T \ \ADC S \ Add the pressed digit to A and store in R, so R now \STA R \ contains its previous value with the new key press \ \ tacked onto the end \ \CMP QQ25 \ If the result in R = the maximum allowed in QQ25, jump \BEQ TT226 \ to TT226 to print the key press and keep looping (the \ \ BEQ is needed because the BCS below would jump to OUT \ \ if R >= QQ25, which we don't want) \ \BCS OUT \ If the result in R > QQ25, jump to OUT to return from \ \ the subroutine with the result in R \ \.TT226 \ \LDA Q \ Print the character in Q (i.e. the key that was \JSR TT26 \ pressed, as we stored the ASCII value in Q earlier) \ \DEC T1 \ Decrement the loop counter \ \BNE TT223 \ Loop back to TT223 until we have checked for 12 digits \ --- And replaced by: --------------------------------> \ Instead of waiting for the player to choose a menu \ item, we now choose a random menu item instead, \ returning a value in R in the range 0 to QQ25, with 0 \ denoting that we are not choosing to buy anything JSR DORND \ Set A and X to random numbers CPX #160 \ Clear the C flag if X < 160 AND #7 \ Reduce A to a random number in the range 0 to 7 BCC gnum1 \ If X < 160 (62.5% chance), skip the following \ instruction LDA #0 \ Set A = 0, so A is a random number in the range 0 to 7 \ with a reasonably large chance of it being zero .gnum1 AND QQ25 \ Reduce the random number in A into the range 0 to QQ25 STA R \ Store the reduced result in R BEQ gnum2 \ If the result is zero then the choice is not to buy \ anything, so skip printing the number CLC \ The item number is in A, so add ASCII "0" to get the ADC #'0' \ ASCII character number to print JSR TT26 \ Call TT26 to print the character in A so it looks like \ we have typed it in .gnum2 LDY #50 \ Wait for 50/50 of a second (1 second) JSR DELAY LDA R \ Set R to the chosen menu item to return to the CMP QQ25 \ Set the C flag if the chosen menu item is too large \ (i.e. it is greater than or equal to QQ25), so we can \ check this on returning \ --- End of replacement ------------------------------> .OUT LDA R \ Set A to the result we have been building in R RTS \ Return from the subroutine