Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. network_init_stage:
  2. .(
  3.     ; Reinit frame counter
  4.     lda #$00
  5.     sta network_current_frame_byte0
  6.     sta network_current_frame_byte1
  7.     sta network_current_frame_byte2
  8.     sta network_current_frame_byte3
  9.  
  10.     ; Set client id
  11.     lda audio_music_enabled ; Hack to easilly configure the player number - activate music on player A's system
  12.     eor #%00000001
  13.     sta network_client_id_byte0
  14.     lda #0
  15.     sta network_client_id_byte1
  16.     sta network_client_id_byte2
  17.     sta network_client_id_byte3
  18.  
  19.     ; Clear rolling mode
  20.     lda #0
  21.     sta network_rollback_mode
  22.  
  23.     ; Clear input history
  24.     ; lda #0 ; ensured by above code
  25.     ldx #0
  26.     clear_one_input:
  27.         sta network_btns_history
  28.  
  29.         inx
  30.         cpx #32
  31.         bne clear_one_input
  32.  
  33.     rts
  34. .)
  35.  
  36. network_tick_ingame:
  37. .(
  38.     network_opponent_number = audio_music_enabled ; Hack to easilly configure the player number - activate music on player A's system
  39.  
  40.     .(
  41.         ; Do nothing in rollback mode, it would be recursive
  42.         lda network_rollback_mode
  43.         bne end
  44.  
  45.         ; Force opponent's buttons to not change
  46.         ldx network_opponent_number
  47.         lda controller_a_last_frame_btns, x
  48.         sta controller_a_btns, x
  49.  
  50.         ; Save local controller's history
  51.         jsr switch_selected_player
  52.         lda network_current_frame_byte0
  53.         and #%00011111
  54.         tay
  55.         lda controller_a_btns, x
  56.         sta network_btns_history, y
  57.  
  58.         ; Send controller's state
  59.         lda controller_a_btns, x
  60.         cmp controller_a_last_frame_btns, x
  61.         beq controller_sent
  62.  
  63.             sta $5009 ; buttons
  64.  
  65.             lda #$1    ; message_type
  66.             sta $5000
  67.  
  68.             lda network_client_id_byte0 ; client_id
  69.             sta $5001
  70.             lda network_client_id_byte1
  71.             sta $5002
  72.             lda network_client_id_byte2
  73.             sta $5003
  74.             lda network_client_id_byte3
  75.             sta $5004
  76.  
  77.             lda network_current_frame_byte0 ; timestamp
  78.             sta $5005
  79.             lda network_current_frame_byte1
  80.             sta $5006
  81.             lda network_current_frame_byte2
  82.             sta $5007
  83.             lda network_current_frame_byte3
  84.             sta $5008
  85.  
  86.             lda #9    ; Send the packet
  87.             sta $5101
  88.  
  89.         controller_sent:
  90.  
  91.         ; Receive new state
  92.         lda $5101
  93.         cmp #87
  94.         bne state_updated
  95.         lda $5000
  96.         cmp #2 ; TODO MESSAGE_TYPE_NEWSTATE
  97.         bne state_updated
  98.             jsr update_state
  99.         state_updated:
  100.  
  101.         ; Increment frame counter
  102.         inc network_current_frame_byte0
  103.         bne inc_ok
  104.         inc network_current_frame_byte1
  105.         bne inc_ok
  106.         inc network_current_frame_byte2
  107.         bne inc_ok
  108.         inc network_current_frame_byte3
  109.         inc_ok:
  110.  
  111.         end:
  112.         rts
  113.     .)
  114.  
  115.     update_state:
  116.     .(
  117.         //TODO reserve a place for it in mem_labels
  118.         server_current_frame_byte0 = $07d0
  119.         server_current_frame_byte1 = $07d1
  120.         server_current_frame_byte2 = $07d2
  121.         server_current_frame_byte3 = $07d3
  122.  
  123.         ; Copy gamestate
  124.         ldx #0
  125.         copy_one_byte:
  126.  
  127.             lda $5006, x ; 4 cycles
  128.             sta $00, x   ; 4 cycles
  129.  
  130.         inx ; 2 cycles
  131.         cpx #$4f ; 3 cycles
  132.         bne copy_one_byte ; 3 cycles
  133.  
  134.         ; Note
  135.         ;  Total - (4+4+2+3+3) * 79 = 16 * 79 = 1264
  136.         ;  Unroll - (4+4) * 79 = 8 * 79 = 632
  137.  
  138.         ; Copy controllers state, the game state shall have run one frame, last_frame_btns and btns became equal
  139.         lda $5006, x
  140.         sta controller_a_btns
  141.         sta controller_a_last_frame_btns
  142.         lda $5006+1, x
  143.         sta controller_b_btns
  144.         sta controller_b_last_frame_btns
  145.  
  146.         ; Extract frame counter
  147.         lda $5002
  148.         sta server_current_frame_byte0
  149.         lda $5003
  150.         sta server_current_frame_byte1
  151.         lda $5004
  152.         sta server_current_frame_byte2
  153.         lda $5005
  154.         sta server_current_frame_byte3
  155.  
  156.         ; Update game state until the current frame is at least equal to the one we where before reading the message
  157.         lda #1
  158.         sta network_rollback_mode
  159.         roll_forward_one_step:
  160.         .(
  161.             ; If sever's frame is inferior to local frame
  162.             lda server_current_frame_byte3
  163.             cmp network_current_frame_byte3
  164.             bcc do_it
  165.             lda server_current_frame_byte2
  166.             cmp network_current_frame_byte2
  167.             bcc do_it
  168.             lda server_current_frame_byte1
  169.             cmp network_current_frame_byte1
  170.             bcc do_it
  171.             lda server_current_frame_byte0
  172.             cmp network_current_frame_byte0
  173.             bcc do_it
  174.             jmp end_loop
  175.             do_it:
  176.  
  177.                 ; Set local player input according to history
  178.                 ldx network_opponent_number ; X = local player number
  179.                 jsr switch_selected_player
  180.  
  181.                 lda server_current_frame_byte0 ; Y = input offset in history
  182.                 and #%00011111
  183.                 tay
  184.  
  185.                 lda network_btns_history, y ; write current input
  186.                 sta controller_a_btns, x
  187.                 dey
  188.                 lda network_btns_history, y
  189.                 sta controller_a_last_frame_btns, x
  190.  
  191.                 ; Update game state
  192.                 jsr game_tick
  193.  
  194.                 ; Inc server_current_frame_byte
  195.                 inc server_current_frame_byte0
  196.                 bne end_inc
  197.                 inc server_current_frame_byte1
  198.                 bne end_inc
  199.                 inc server_current_frame_byte2
  200.                 bne end_inc
  201.                 inc server_current_frame_byte3
  202.                 end_inc:
  203.  
  204.                 ; Loop
  205.                 jmp roll_forward_one_step
  206.  
  207.             end_loop:
  208.         .)
  209.         lda #0
  210.         sta network_rollback_mode
  211.  
  212.         // Copy (updated) server frame number to the local one
  213.         lda server_current_frame_byte0
  214.         sta network_current_frame_byte0
  215.         lda server_current_frame_byte1
  216.         sta network_current_frame_byte1
  217.         lda server_current_frame_byte2
  218.         sta network_current_frame_byte2
  219.         lda server_current_frame_byte3
  220.         sta network_current_frame_byte3
  221.  
  222.         rts
  223.     .)
  224.  
  225. .)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement