+++ /dev/null
-#define BUILDING_ROAD_NORTH_SOUTH 0x77
-#define BUILDING_ROAD_WEAT_EAST 0x78
-#define BUILDING_UNDER_CONSTRUCTIOn 0x67
-
- ; places a building at the current cursor position
- ; performs pre-checks and displays an error
- ; if pre-conditions for building fail
- ; e.g. not enough resources, not enough space
- ; executes cursor_state state machine update
-building_build:
- ld de, building_state
- call st_update
- ret
-
-build_warehouse:
- ldnull bc
- ret
-
- ; builds a road
- ; roads transfer connections
-build_road:
- call cursor_get_cell
- ; hl = cell
- ld de, cell_template_road
- ld bc, c_size
- call memcpy
-
- ; placeholder tile
- call cursor_get_tile
- ld a, BUILDING_UNDER_CONSTRUCTIOn
- call bg_update_queue_push
-
- ldnull bc
- ret
-
- ; constructs a 2x2 capital fortress
- ; is always flagged as connected
- ; only one fortress can be built
-build_fortress:
- ret
-
-build_farm:
- ldnull bc
- ret
-
-build_lumber:
- ldnull bc
- ret
-
+++ /dev/null
-
- ; updates a cell
- ; does not call c_st_routine if c_head_offset != 0
- ; inputs:
- ; de: cell ptr
- ; bc: screen ptr
-cell_update:
- push de
- inc de
- inc de
- inc de ; de = flags
- ld a, [de] ; a = head offset
- and a, CF_HEAD
- pop de
- ; if not head cell return
- ret z
-
- ; save cell ptr
- push de
-
- ; check timer
- ld a, [de] ; a = timer
- cp a, 0
- jr z, @update REL ; if timer is 0 update
- dec a ; otherwise timer--
- ld [de], a
- pop de
- ret
-@update:
- inc de ; de = c_st_routine
-
- ld a, [de]
- ld l, a
- inc de
- ld a, [de]
- ld h, a ; hl = update routine
-
- ; cell ptr has to be passed
- ; to call
- pop de ; de is cell ptr
- push de ; save again
- call call_hl
-
- ; restore cell ptr
- pop de
-
- ; write new s_st_routine
- ; if bc is not 0000
- ld a, b
- or a, c
- ret z
-
- inc de
- ; de = c_st_routine
-
- ; store next routine
- ld a, c
- ld [de], a
- ld a, b
- inc de
- ld [de], a
-
- ret
-
- ; default road update state
-cell_road_update:
- ldnull bc
- ret
-
- ; sets up a road cell
- ; updates the tiles
- ; by pushing tile updates
-cell_road_init:
- push bc
- pop hl ; hl = ptr to tile
- ld a, BUILDING_ROAD_NORTH_SOUTH
-
- call bg_update_queue_push
-
- ld bc, cell_road_update
- ret
-
-cell_template_road:
- c_def 0, cell_road_init, CF_HEAD
#define WINDOW_Y 120
#define WINDOW_X 1
- ; tile flags
-.se 1
- ; is connected to road
- ; if tile is a road indicates if
- ; it is connected to a warehouse
- ; a warehouse is always connected
-.de CF_CONNECTED, 1
- ; if this flag is set, the cell in question is a head cell
- ; head cells contain update code and can own many tiles
- ; head cells are always in the top left corner of a multi cell
- ; structure
- ; so if a cell needs to look up its head
- ; (e.g. when being selected for deletion)
- ; it can walk the cell map up and left until a head is found
-.de CF_HEAD, 2
- ; mark a cell as used
-.de CF_USED, 4
-
- ; cell types
-.se 0
-.de C_ROAD, 1
-
- ; game state cell struct
- ; this holds all the information
- ; a cell on the map has
- ; cells are a reduced version of states
- ; without a default next value and additional flags
-.se 0
- ; cell delay timer only used for head cell
- ; when it reaches 0 the next state is called
-.de c_time, 1
- ; type/gfx or custom data for non-head cells
-.de c_type, 0
-.de c_st_data, 0
- ; this is the same as st routine,
- ; but tiles do not have a timer or
- ; a next flag
- ; only head cells have an update routine
- ; if a c_st_rotuine returns a non-zero value in bc
- ; the rotuine pointed to will be used during the next
- ; cell update
- ; additionally bc will point to the current screen
- ; pointer for the cell
- ; inputs:
- ; de: cell ptr
- ; bc: screen ptr
-.de c_st_routine, 2
-.de c_flags, 1
- ; if c_st_rotuine
- ; is unsued (true for all non-head cells)
- ; this space can be used by the head cell
- ; to store data
-.de c_size, 0
-
- ; cell type enum
-.se 0
-.de CT_NONE, 1
-.de CT_GROUND, 1
-.de CT_WATER, 1
-
; defines end of scroll location
#define CURSOR_MIN_X 0
#define CURSOR_MAX_X 0xF8
#define CURSOR_MIN_Y 0
#define CURSOR_MAX_Y 0xB8
+ ; cells struct
+.se 0
+.de c_tile, 1
+.de c_flags, 1
+.de c_size, 0
+
; state struct
; states are intended to
; be used as part of a larger
#include "ui.s"
#include "audio.s"
#include "map.s"
-#include "simulation.s"
#include "state.s"
-#include "cells.s"
-#include "buildings.s"
#include "tiles.inc"
; fill bank
ret
- ; TODO: this does not match the actual cell structure anymore
; loads a tile map into cells
; and to cell memory
; inputs:
ret
- ; TODO: this does not match the actual cell structure anymore
; draws all cells currently loaded to the screen
; only call during blank!
cells_draw_all:
; set up game mode
call game_init
- call sim_init
call mbc1_init
ret
input_held BTNA
jr z, @not_a REL
- ; next state: building state
- call building_build
@not_a:
; movement keys with cooldown
input_just BTNSELECT
jr z, @not_select REL
; select goes to build menu
-
- ld bc, st_ui_building_selector
+
+ ldnull bc
ret
@not_select:
@done:
+++ /dev/null
- ; loads de and bc with initial values and stores them
-sim_init:
- ld de, state_cells
- ld bc, SCRN0
-
- ; stores the values of bc and de in
- ; cell_ptr and screen_ptr
- sim_store:
- ld a, e
- ld [sim_cell_ptr], a
- ld a, d
- ld [sim_cell_ptr+1], a
-
- ld a, c
- ld [sim_cell_screen_ptr], a
- ld a, b
- ld [sim_cell_screen_ptr+1], a
-
- ret
-
- ; updates all cells
- ; eihter until the end of the
- ; end of the cell list is reached
- ; or CELLS_MAX_FRAME is reached
- ; only head cells count towards the cell update limit
- ; increments cell_idx every iteration
- ; resets cell_idx to 0 when end of cells is reached
-sim_update:
-
- ; load sim update state
- ld a, [sim_cell_ptr]
- ld e, a
- ld a, [sim_cell_ptr+1]
- ld d, a
-
- ld a, [sim_cell_screen_ptr]
- ld c, a
- ld a, [sim_cell_screen_ptr+1]
- ld b, a
-
- ld a, SIM_UPDATE_MAX+1
- push af
-@loop:
- push de
- push bc
- call cell_update
- pop bc
- pop de
- pop af
-
- ; advance to next cell
- .rep i, c_size, 1, inc de
- inc bc
-
- ; loop counter--
- dec a
- cp a, 0
- ; jp and ret
- jp z, sim_store
-
- push af
- ; check if we need to exit
- ld a, state_cells_end LO
- cp a, e
- jp nz, @loop
- ld a, state_cells_end HI
- cp a, d
- jp nz, @loop
- ; if not reset and ret
- pop af
- jp sim_init
st_cursor_delay:
st_def CURSOR_MOVE_TIMER, st_null_fn, st_cursor
-st_ui_building_selector:
- st_def 0, ui_building_selector_update, st_ui_building_selector
-st_ui_building_selector_delay:
- st_def CURSOR_MOVE_TIMER, st_null_fn, st_ui_building_selector
-st_ui_buildung_selector_exit:
- st_def 8, ui_building_selector_exit, st_cursor
-
st_update_game:
st_def 0x00, update_game, st_update_game
st_update_pause:
st_update_game_over:
st_def 0x00, update_game_over, st_update_game_over
-; building states
-
-st_build_warehouse:
- st_def 0x00, build_warehouse, st_null
-st_build_road:
- st_def 0x00, build_road, st_build_road
-st_build_farm:
- st_def 0x00, build_farm, st_null
-st_build_lumber:
- st_def 0x00, build_lumber, st_null
-#define UI_TILE_WAREHOUSE 128
-#define UI_TILE_ROAD 129
-#define UI_TILE_FARM 130
-#define UI_TILE_LUMBER 131
-
-#define UI_BUILDING_TEXT_X 2
-#define UI_BUILDING_TEXT_Y 120
-
-#define UI_CURSOR_BUILDING_BASE_X 2
-#define UI_CURSOR_BUILDING_BASE_Y 128
-
-#define UI_TEXT_SPRITE_LEN oamsize*10
-#define UI_TEXT_SPRITE_0 shadow_oam+oamsize
-
- ; lookup table for
- ; labels displayed when hovering
- ; over a building
-ui_cursor_label_table:
- dw STR_WAREHOUSE
- dw STR_ROAD
- dw STR_FARM
- dw STR_LUMBER
-
- ; tile to display for each selection
-ui_cursor_tile_table:
- .db UI_TILE_WAREHOUSE
- .db UI_TILE_ROAD
- .db UI_TILE_FARM
- .db UI_TILE_LUMBER
-
- ; converts from UI cursor position
- ; to building state
-ui_to_building_selecction_table:
- dw st_build_warehouse
- dw st_build_road
- dw st_build_farm
- dw st_build_lumber
; inits UI
ui_init:
; this should only be called
; during blanking
ui_draw:
- ld hl, SCRN1+32
- inc hl
-
- ld a, UI_TILE_WAREHOUSE
- ld [hl+], a
-
- ld a, UI_TILE_ROAD
- ld [hl+], a
-
- ld a, UI_TILE_FARM
- ld [hl+], a
-
- ld a, UI_TILE_LUMBER
- ld [hl+], a
-
- ret
-
- ; ui building selector state
-ui_building_selector_update:
- call ui_building_selector_inputs
- push bc
- call ui_building_selector_draw
- pop bc
- ret
-
- ; draw building selector cursor
- ; and text
-ui_building_selector_draw:
- ; draw cursor
- ld hl, PLAYER_SPRITE1
-
- ld a, UI_CURSOR_BUILDING_BASE_Y
- add a, OBJ_OFF_Y
- ld [hl+], a
-
- ld a, [ui_cursor_pos]
- mul8 a
- ld b, a ; b = curosr * 8
-
- ld a, UI_CURSOR_BUILDING_BASE_X
- add a, OBJ_OFF_X
- ; add cursor pos * 8
- add a, b
- ld [hl+], a
-
- ld a, CURSOR_TILE
- ld [hl+], a
-
- ; clear text objects
- ld hl, UI_TEXT_SPRITE_0
- ld bc, UI_TEXT_SPRITE_LEN
- ld d, 0
- call memset
-
- ; draw text
- ld a, [ui_cursor_pos]
- sla a ; * 2
- ld hl, ui_cursor_label_table
- ld d, 0
- ld e, a
- add hl, de
- push hl
- pop de ; de = pointing to label ptr
- ld a, [de]
- ld l, a
- inc de
- ld a, [de]
- ld h, a
- ; hl = string ptr
-
- ld b, UI_BUILDING_TEXT_Y
- ld c, UI_BUILDING_TEXT_X
- ld de, UI_TEXT_SPRITE_0
- call objputs
-
- ret
-
- ; exit state for build menu
- ; clears shadow oam
-ui_building_selector_exit:
- call shadow_oam_clear
-
- ldnull bc
- ret
-
- ; building selectr input reader
-ui_building_selector_inputs:
- input_just BTNSELECT
- jr z, @not_select REL
- ; select back to regular state
-
- ld bc, st_ui_buildung_selector_exit
- ret
-@not_select:
-
- input_held BTNRIGHT
- jr z, @not_right REL
-
- ld a, [ui_cursor_pos]
- inc a
- ld [ui_cursor_pos], a
-
- ld bc, st_ui_building_selector_delay
- ret
-@not_right:
-
- input_held BTNLEFT
- jr z, @not_left REL
-
- ld a, [ui_cursor_pos]
- dec a
- ld [ui_cursor_pos], a
-
- ld bc, st_ui_building_selector_delay
- ret
-@not_left:
-
- input_just BTNA
- jr z, @not_a REL
-
- ; load building to be built into building selector state
- ld a, [ui_cursor_pos]
- sla a ; * 2 to get ptr offset
- ld d, 0
- ld e, a
-
- ld hl, ui_to_building_selecction_table
- add hl, de ; hl = correct value
- ld a, [hl+]
- ld e, a
- ld a, [hl]
- ld d, a ; de = source of state ptr
-
- ld hl, building_state
- ld bc, st_size
- call memcpy
-
- ; load tile for preview
- ld hl, ui_cursor_tile_table
- ld d, 0
- ld a, [ui_cursor_pos]
- ld e, a
- add hl, de
- ld a, [hl]
- ld [cursor_preview_tile], a
-
- ld bc, st_ui_buildung_selector_exit
- ret
-@not_a:
-
- ldnull bc
ret
ld de, actor_player
call st_update
- call sim_update
ldnull bc
ret