; level to cell map
level_def_to_tile:
dw tile_grass
- dw tile_player_hive
- dw tile_enemy_hive
- dw tile_food
; tile grass
.def int TGS = TI_EMPTY
- ; tile player hive
-.def int TPH = TI_PLAYER_HIVE
-
- ; tile enemy hive
-.def int TEH = TI_ENEMY_HIVE
-
- ; tile food
-.def int TFD = TI_FOOD
; level definitions
; levels always have a header
; with flags, tilesets to load
- ; followed by a 16x16 tilemap
+ ; followed by a MAP_WxMAP_H tilemap
; the tilemap is an index into a tile lookup
- ; table. The tile defs are then copied to a map_tiles table (16x16)
+ ; table. The tile defs are then copied to a map_tiles table (MAP_W*MAP_H)
; where each tile has values and its current state. The map can be drawn from this.
l1:
mapdef 0, map_r_nop, bank8000, bank8800, bank8C00, bank9000
- .db TPH, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TFD, TFD
- .db TFD, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TFD
- .db TFD, TFD, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
.db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
.db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
.db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
.db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
.db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
- .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TFD, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
.db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
- .db TGS, TFD, TFD, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TFD, TGS, TGS, TGS, TFD, TFD
- .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TFD
- .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TFD
- .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TFD
- .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TFD
- .db TGS, TGS, TGS, TFD, TFD, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TEH
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
+ .db TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS, TGS
; tile definitions
#define GFX_GRASS 0x20
-#define GFX_PHIVE 0x26
-#define GFX_EHIVE 0x46
-#define GFX_FOOD 0x08
-#define GFX_PWORKERS 0x28
-#define GFX_EWORKERS 0x48
- ; resets update tile to 0/0
-tile_update_reset:
- xor a, a
- ld [update_tile_y], a
- ld [update_tile_x], a
-
- call tile_update_get
-
- ret
-
- ; inits the next tile
- ; increments update_tile_y/x
- ; resets when out of bounds
-tile_update_get_next:
- ld a, [update_tile_x]
- inc a
- cp a, MAP_W
- jr nz, @not_y REL
- ld a, [update_tile_y]
- inc a
- and a, MAP_H-1
- ld [update_tile_y], a
- xor a, a ; reset x
-@not_y:
- ld [update_tile_x], a
-
- call tile_update_get
- ret
-
- ; sets update_tile to the current selected tile
- ; bsed on y/x position
-tile_update_get:
- ld a, [update_tile_y]
- ld b, a
- ld a, [update_tile_x]
- ld c, a
- call map_get_tile
-
- ld a, h
- ld [update_tile], a
- ld a, l
- ld [update_tile+1], a
- ret
-
- ; updates the next tile
- ; in the queue
- ; inputs:
- ; next_update_tile
- ; sets next_update_tile to next tile
- ; wraps to first tile if end is reached
-tile_update_selected:
- call tile_update_get_next
-
- ld a, [update_tile]
- ld d, a
- ld a, [update_tile+1]
- ld e, a
-
- call tile_update
-
- ; prepare the tile for drawing
- ld a, [update_tile_y]
- ld b, a
- ld a, [update_tile_x]
- ld c, a
- call map_draw_tile_prep
-
- ret
-
- ; draws the recently updated tile
- ; based on update_tile prep set in previous frame
-tile_update_draw:
- jp map_draw_tile
-
-
- ; updates a tile with its
- ; routine
- ; inputs:
- ; de: tile
-tile_update:
- push de
- call tile_get_adjacent
- pop de
-
- ld a, [de] ; load tile type
- add a, a ; * 2 offset into table
-
- ld b, 0
- ld c, a ; bc = offset
-
- ld hl, tile_update_table
- add hl, bc
-
- ; load ptr
- ld a, [hl+]
- ld b, a
- ld a, [hl]
- ld h, a
- ld l, b
- jp hl
-
- ; table of update routines
- ; for each tile
-tile_update_table:
- dw tile_update_empty
- dw tile_update_used
- dw tile_update_hive
- dw tile_update_food
-
-strz str_empty, "EMPTY "
-strz str_hive, "HIVE "
-strz str_used, "HILL "
-strz str_food, "FOOD "
-
- ; mapping of tile ids to
- ; the tile names
-tile_str_table:
- dw str_empty
- dw str_used
- dw str_hive
- dw str_food
-
- ; gets the controlling actor of a tile
- ; returns NULL if TO_NULL is set
- ; inputs:
- ; de: tile
- ; returns:
- ; bc: actor
-tile_get_actor:
- ld bc, NULL
- ; TODO:
- ret
-
- ; stores an adjacent tile
- ; inputs:
- ; $1: skip if x position equal
- ; $2: tile offset based on bc
- ; bc: current tile ptr
- ; de: destination
- ; returns:
- ; de: next destination
-#macro tile_adjacent_store
-.beginscope
- ; check if x is 0
- ; if so skip left tile
- ld a, [update_tile_x]
- cp a, $1
- jr z, @skip REL
-
- ; store left tile
- ld hl, $2
- add hl, bc
- ; move to new position
-
- ; store tile
- ld a, h
- ld [de], a
- inc de
- ld a, l
- ld [de], a
- inc de
-
- jr @done REL
-@skip:
- inc de
- inc de
-@done:
-.endscope
-#endmacro
-
- ; stores pointers to
- ; all adjacent tiles
- ; stores NULL if adjacent tile is out
- ; of bounds
- ; also counts the amount of seen types
- ; in tiles_adjacen_types
-tile_get_adjacent:
- ; clear adjacent tiles
- ; stored by previous frame
- ld hl, tiles_adjacent
- ld bc, ta_size * TILES_ADJACENT_MAX
- ld de, 0
- call memset
-
- ; bc = y/x
- ld a, [update_tile_y]
- ld b, a
- ld a, [update_tile_x]
- ld c, a
- call map_get_tile
-
- ; hl = current tile
- push hl
- pop bc ; bc = current tile
-
- ; de = destination
- ld de, tiles_adjacent
-
- ; now get all tiles adjacent
-
- ; 1) get tiles above (up to 3 tiles)
-
- ; check if y is already 0, if so skip
- ld a, [update_tile_y]
- cp a, 0
- jp z, @skip_above
- ; up/center tile
- tile_adjacent_store 0xFF, (-1 * (t_size * MAP_W)) & 0xFFFF
- ; up/left tile
- tile_adjacent_store 0, (-1 * (t_size * (MAP_W+1))) & 0xFFFF
- ; up/right tile
- tile_adjacent_store (MAP_W-1), (-1 * (t_size * (MAP_W-1))) & 0xFFFF
-
-@skip_above:
-
- ; 2) get tiles left and right (up to 2 tiles)
-
- ; left
- tile_adjacent_store 0, (-1 * t_size) & 0xFFFF
- ; right
- tile_adjacent_store (MAP_W-1), t_size
-
- ; 3) get tiles below (up to 3 tiles)
-
- ; check if y is already MAP_H-1 if so skip
- ld a, [update_tile_y]
- cp a, MAP_H-1
- jp z, @skip_below
-
- ; down/center tile
- tile_adjacent_store 0xFF, (t_size * MAP_W)
- ; down/left tile
- tile_adjacent_store 0, (t_size * (MAP_W+1))
- ; down/right tile
- tile_adjacent_store (MAP_W-1), (t_size * (MAP_W-1))
-@skip_below:
-
- ret
-
- ; checks one tile for a type match
- ; inputs:
- ; hl: ptr to next tile
- ; a: expected tile type
- ; b: current tile match count
- ; returns:
- ; incremnets b if type matches
- ; hl+=2 (next ptr)
- ; preserves:
- ; a
-#macro tile_count_adjacent_check
-.beginscope
- push af
- ld a, [hl+]
- ld d, a
- ld a, [hl+]
- ld e, a
- ; hl = next ptr
-
- ; de = ptr to tile
- push hl
- ld hl, t_type
- add hl, de
- ; hl = tile type
- ld a, [hl]
- ld c, a ; c = tile type
- pop hl
-
- pop af
- cp a, c
- ; check if match
- jr nz, @no_match REL
- inc b ; result++
-@no_match:
-.endscope
-#endmacro
-
- ; counts adjacent tiles of specific type
- ; inputs:
- ; a: expected type
- ; returns:
- ; a: count of type
-tile_count_adjacent:
- ; b = result
- ld b, 0
-
- ; hl = ptrs to tiles
- ld hl, tiles_adjacent
-
- ; check 8 tiles
- tile_count_adjacent_check
- tile_count_adjacent_check
- tile_count_adjacent_check
- tile_count_adjacent_check
-
- tile_count_adjacent_check
- tile_count_adjacent_check
- tile_count_adjacent_check
- tile_count_adjacent_check
-
- ; result is in a now
- ld a, b
- ret
-
-
- ; updates empty tile
- ; inputs:
- ; de: tile
-tile_update_empty:
- ld a, TT_FOOD
- push de
- call tile_count_adjacent
- pop de
-
- ; if 2 adjacnet food tiles
- ; turn into food
- cp a, 2
- ret c
-
- push de
- pop hl ; hl = dst
- ld de, tile_food
- ld bc, t_size
- call memcpy
- ret
-
- ; updates controlled tile
- ; inputs:
- ; de: tile
-tile_update_used:
- ret
-
- ; updates hive
- ; inputs:
- ; de: tile
-tile_update_hive:
- ret
-
- ; updates food tile
- ; inputs:
- ; de: tile
-tile_update_food:
- inc de ; flags
- ld a, [de]
- inc a
- ld [de], a
- ret
-
; maps from tile ids
; to tile presets (tile index)
tile_id_table:
dw tile_grass
- dw tile_grass
- dw tile_player_hive
- dw tile_grass
- dw tile_enemy_hive
- dw tile_food
+ dw tile_grass
tile_grass:
- tiledef TT_EMPTY, 0, TO_NULL, 0, GFX_GRASS
-
-tile_player_hive:
- tiledef TT_HIVE, 0, TO_PLAYER, 0, GFX_PHIVE
-
-tile_enemy_hive:
- tiledef TT_HIVE, 0, TO_ENEMY, 0, GFX_EHIVE
-
-tile_food:
- tiledef TT_FOOD, 0, 0, TO_NULL, GFX_FOOD
-
-tile_player_workers:
- tiledef TT_WORKERS, 0, TO_PLAYER, 0, GFX_PWORKERS
-
-tile_enemy_workers:
- tiledef TT_WORKERS, 0, TO_ENEMY, 0, GFX_EWORKERS
+ tiledef TT_EMPTY, 0, GFX_GRASS