castleraider

changeset 319:5b852b0c64bc

Added a game over screen, consolidating common code in an attempt to save memory. Started to organise the text that is copied into the working area in the loader in preparation for writing the completion code which will also be copied into the free space there.
author David Boddie <david@boddie.org.uk>
date Wed Oct 01 00:27:48 2014 +0200
parents c92f31bf59a5
children a73fcec69709
files build.py code.oph loader.oph plotting.oph
diffstat 4 files changed, 81 insertions(+), 102 deletions(-) [+]
line diff
     1.1 --- a/build.py	Mon Sep 29 00:35:39 2014 +0200
     1.2 +++ b/build.py	Wed Oct 01 00:27:48 2014 +0200
     1.3 @@ -37,72 +37,6 @@
     1.4      t = "\r".join(lines) + "\r"
     1.5      return t
     1.6  
     1.7 -def encode_text(text):
     1.8 -
     1.9 -    words = text.split(" ")
    1.10 -    word_dict = {}
    1.11 -    
    1.12 -    # Count the number of occurrences of each word.
    1.13 -    for word in words:
    1.14 -        word_dict.setdefault(word, 0)
    1.15 -        word_dict[word] += 1
    1.16 -    
    1.17 -    # Sort the words in order of decreasing frequency.
    1.18 -    frequencies = map(lambda x: (x[1], x[0]), word_dict.items())
    1.19 -    frequencies.sort()
    1.20 -    frequencies.reverse()
    1.21 -    
    1.22 -    # Create encoding and decoding look up tables.
    1.23 -    decoding_lookup = {}
    1.24 -    encoding_lookup = {}
    1.25 -    
    1.26 -    i = 0
    1.27 -    for count, word in frequencies:
    1.28 -    
    1.29 -        if i >= 128:
    1.30 -            j = 1 + i * 2
    1.31 -        else:
    1.32 -            j = i * 2
    1.33 -        
    1.34 -        encoding_lookup[word] = j
    1.35 -        decoding_lookup[j] = word
    1.36 -        
    1.37 -        i += 1
    1.38 -    
    1.39 -    # Encode the text.
    1.40 -    encoded = []
    1.41 -    for word in words:
    1.42 -    
    1.43 -        encoded.append(encoding_lookup[word])
    1.44 -    
    1.45 -    encoded_string = ""
    1.46 -    for value in encoded:
    1.47 -    
    1.48 -        if value & 1 == 0:
    1.49 -            encoded_string += chr(value)
    1.50 -        else:
    1.51 -            encoded_string += struct.pack("<H", value)
    1.52 -    
    1.53 -    return decoding_lookup, encoded_string
    1.54 -
    1.55 -def decode_text(data, lookup):
    1.56 -
    1.57 -    words = ""
    1.58 -    i = 0
    1.59 -    while i < len(data):
    1.60 -    
    1.61 -        value = ord(data[i])
    1.62 -        if value & 1 != 0:
    1.63 -            value += ord(data[i+1]) << 8
    1.64 -            i += 2
    1.65 -        else:
    1.66 -            i += 1
    1.67 -        
    1.68 -        words += lookup[value]
    1.69 -        words += " "
    1.70 -    
    1.71 -    return words[:-1]
    1.72 -
    1.73  def address_length_end(address, data):
    1.74  
    1.75      address_low = address & 0xff
    1.76 @@ -115,6 +49,33 @@
    1.77      end_high = end >> 8
    1.78      return address_low, address_high, length_low, length_high, end_low, end_high
    1.79  
    1.80 +def encode_text(lines):
    1.81 +
    1.82 +    # Store the text reversed to reduce the number of instructions needed to
    1.83 +    # print it.
    1.84 +    
    1.85 +    text_length = 0
    1.86 +    data_oph = ""
    1.87 +    lines.reverse()
    1.88 +    
    1.89 +    for line in lines:
    1.90 +    
    1.91 +        text_values = []
    1.92 +        
    1.93 +        for piece in line:
    1.94 +        
    1.95 +            if type(piece) == int:
    1.96 +                text_length += 1
    1.97 +                text_values.append(str(piece))
    1.98 +            else:
    1.99 +                text_length += len(piece)
   1.100 +                text_values.append('"' + piece[::-1] + '"')
   1.101 +        
   1.102 +        text_values.reverse()
   1.103 +        data_oph += ".byte " + ", ".join(text_values) + "\n"
   1.104 +    
   1.105 +    return data_oph, text_length
   1.106 +
   1.107  
   1.108  tiles = map(lambda tile: makelevels.tile_ref[tile], makelevels.tile_order)
   1.109  
   1.110 @@ -188,24 +149,17 @@
   1.111                        (31, 6, 29, 17, 2, "to play")]
   1.112      
   1.113      title_data_oph += 'game_title_text:\n'
   1.114 -    in_game_title_text_length = 0
   1.115 +    title_data, in_game_title_text_length = encode_text(title_text)
   1.116 +    title_data_oph += title_data
   1.117 +    title_data_oph += 'game_title_text_end:\n'
   1.118      
   1.119 -    for line in title_text:
   1.120 +    game_over_text = [(26, 31, 1, 16, 17, 3, "Your quest is over"),
   1.121 +                          (31, 4, 29, 17, 1, "Press  SPACE")]
   1.122      
   1.123 -        in_game_title_text_values = []
   1.124 -        
   1.125 -        for piece in line:
   1.126 -        
   1.127 -            if type(piece) == int:
   1.128 -                in_game_title_text_length += 1
   1.129 -                in_game_title_text_values.append(str(piece))
   1.130 -            else:
   1.131 -                in_game_title_text_length += len(piece)
   1.132 -                in_game_title_text_values.append('"' + piece + '"')
   1.133 -        
   1.134 -        title_data_oph += ".byte " + ", ".join(in_game_title_text_values) + "\n"
   1.135 -    
   1.136 -    title_data_oph += 'game_title_text_end:\n'
   1.137 +    title_data_oph += 'game_over_text:\n'
   1.138 +    game_over_data, in_game_game_over_text_length = encode_text(game_over_text)
   1.139 +    title_data_oph += game_over_data
   1.140 +    title_data_oph += 'game_over_text_end:\n'
   1.141      
   1.142      open("title-data.oph", "w").write(title_data_oph)
   1.143      
   1.144 @@ -280,13 +234,15 @@
   1.145      # Initial displacements for the rows.
   1.146      initial_row_offsets           = row_indices + 0x10
   1.147      
   1.148 -    # Store the in-game title data above the working data - this is done in the
   1.149 -    # loader.
   1.150 +    # Store the in-game text data and the completion code above the working
   1.151 +    # data - this is done in the loader.
   1.152      title_data_address = initial_row_offsets + 0x10
   1.153      
   1.154      in_game_title_text_address = title_data_address + (title_rows * 10)
   1.155 +    in_game_game_over_text_address = in_game_title_text_address + in_game_title_text_length
   1.156 +    in_game_completion_address = in_game_game_over_text_address + in_game_game_over_text_length
   1.157      
   1.158 -    working_end = in_game_title_text_address + in_game_title_text_length
   1.159 +    working_end = in_game_completion_address
   1.160      
   1.161      # Permanent data
   1.162      
   1.163 @@ -600,9 +556,14 @@
   1.164          ".alias title_rows                      %i\n"
   1.165          ".alias in_game_title_text_address      $%x\n"
   1.166          ".alias in_game_title_text_length       %i\n"
   1.167 +        ".alias in_game_game_over_text_address  $%x\n"
   1.168 +        ".alias in_game_game_over_text_length   %i\n"
   1.169 +        ".alias in_game_total_text_length       %i\n"
   1.170          "\n"
   1.171 -        ) % (title_data_address, title_rows, in_game_title_text_address,
   1.172 -             in_game_title_text_length)
   1.173 +        ) % (title_data_address, title_rows,
   1.174 +             in_game_title_text_address, in_game_title_text_length,
   1.175 +             in_game_game_over_text_address, in_game_game_over_text_length,
   1.176 +             in_game_title_text_length + in_game_game_over_text_length)
   1.177      
   1.178      scenery_rows = 16
   1.179      extra_rows = 7
     2.1 --- a/code.oph	Mon Sep 29 00:35:39 2014 +0200
     2.2 +++ b/code.oph	Wed Oct 01 00:27:48 2014 +0200
     2.3 @@ -30,24 +30,20 @@
     2.4      jsr show_bank2          ; Show bank 2 since this is where the output of OS
     2.5                              ; calls will appear.
     2.6  
     2.7 -    ldx #0
     2.8 +    ldx #[in_game_title_text_length - 1]
     2.9      title_text_loop:
    2.10          lda in_game_title_text_address,x
    2.11          jsr $ffee
    2.12 -        inx
    2.13 -        cpx #in_game_title_text_length
    2.14 -        bne title_text_loop
    2.15 +        dex
    2.16 +        bpl title_text_loop
    2.17  
    2.18      ; Wait for the SPACE key or fire button to be pressed.
    2.19      main_start_wait_loop:
    2.20  
    2.21          jsr read_joystick_fire
    2.22 -        bcc main_start_wait_loop_key_check
    2.23  
    2.24          lda #1
    2.25 -        sta using_joystick
    2.26 -        clc
    2.27 -        bcc main_init
    2.28 +        bcs main_init
    2.29  
    2.30          main_start_wait_loop_key_check:
    2.31  
    2.32 @@ -56,9 +52,10 @@
    2.33          bne main_start_wait_loop
    2.34  
    2.35      lda #0
    2.36 -    sta using_joystick
    2.37  
    2.38      main_init:
    2.39 +    clc
    2.40 +    sta using_joystick
    2.41  
    2.42      ; Set character's initial position and status.
    2.43  
    2.44 @@ -218,11 +215,8 @@
    2.45          jsr check_key
    2.46          bne main_loop_no_pause
    2.47  
    2.48 -        main_loop_pause_loop:
    2.49 -        
    2.50 -            ldx #201        ; (O)
    2.51 -            jsr check_key
    2.52 -            bne main_loop_pause_loop
    2.53 +        lda #201            ; (O)
    2.54 +        jsr wait_for_key
    2.55  
    2.56          main_loop_no_pause:
    2.57  
    2.58 @@ -252,8 +246,31 @@
    2.59  
    2.60      ; Do something to indicate the game is over.
    2.61  
    2.62 +    jsr clear_banks
    2.63 +    jsr show_bank2          ; Show bank 2 since this is where the output of OS
    2.64 +                            ; calls will appear.
    2.65 +
    2.66 +    ldx #[in_game_game_over_text_length - 1]
    2.67 +    game_over_text_loop:
    2.68 +        lda in_game_game_over_text_address,x
    2.69 +        jsr $ffee
    2.70 +        dex
    2.71 +        bpl game_over_text_loop
    2.72 +
    2.73 +    lda #157        ; (SPACE)
    2.74 +    jsr wait_for_key
    2.75 +
    2.76      jmp main
    2.77  
    2.78 +wait_for_key:   ; A=key
    2.79 +
    2.80 +    sta $70
    2.81 +    wait_for_key_loop:
    2.82 +        ldx $70
    2.83 +        jsr check_key
    2.84 +        bne wait_for_key_loop
    2.85 +    rts
    2.86 +
    2.87  player_move:
    2.88  
    2.89      lda player_jumping
     3.1 --- a/loader.oph	Mon Sep 29 00:35:39 2014 +0200
     3.2 +++ b/loader.oph	Wed Oct 01 00:27:48 2014 +0200
     3.3 @@ -526,7 +526,7 @@
     3.4          clc
     3.5  
     3.6          inx
     3.7 -        cpx #[title_length + in_game_title_text_length]
     3.8 +        cpx #[title_length + in_game_total_text_length]
     3.9          bne move_title_data_loop
    3.10  
    3.11      clc
     4.1 --- a/plotting.oph	Mon Sep 29 00:35:39 2014 +0200
     4.2 +++ b/plotting.oph	Wed Oct 01 00:27:48 2014 +0200
     4.3 @@ -15,6 +15,7 @@
     4.4  
     4.5  clear_banks:
     4.6  
     4.7 +    clc
     4.8      jsr clear_bank1
     4.9      jsr clear_bank2
    4.10      jmp show_bank1