PICOWARS - self study
- explanations generally reference the javascript world, specifically react
in pico 8, the main game loop is stored in 3 global callbacks:
- _init()
- first run, like componentDidMount
- _update()
- componentWillUpdate()
- main task in the js event loop
- _draw()
- componentDidUpdate()
- rendering task in the js event loop
in picowars by lambdanaut
below are all functions as defined in the game, and notes to make sense of them
main functions
_init()
-
calls load_assets()
-
what load assets does:
- loop twice, doing:
- peek_increment() to load:
- player key
- player co name (CO = commanding officer)
- player color palette
- player icon
- team icon
- player music
- within the loop, loop 7 times, doing:
- load assets for units (infantry, mechs, tanks etc)
- includes theur types, travel, cost, attack range etc
- TODO: check damage_chart
- peek_increment() to load:
- loop twice, doing:
function load_assets()
for i=1, 2 do
-- load player settings
-- icons, color palletes, etc
players_human[i] = peek_increment() == 1
players_co_name[i] = load_string(10)
players[i] = team_index_to_palette[peek_increment()]
players_co_icon[i] = peek_increment()
team_icon[i] = peek_increment()
players_music[i] = peek_increment()
for j=1, 7 do
-- loads their units and unit configs
local u = {}
u.index = peek_increment()
u.type = load_string(10)
u.sprite = peek_increment()
u.mobility_type = peek_increment()
u.travel = peek_increment()
u.cost = peek_increment()
u.range_min = peek_increment()
u.range_max = peek_increment()
u.ranged = u.range_min > 0
u.luck_max = peek_increment()
u.capture_bonus = peek_increment()
u.struct_heal_bonus = peek2(memory_i)
memory_i += 2
u.ai_unit_ratio = peek_increment()
u.moveout_sfx = peek_increment()
u.combat_sfx = peek_increment()
u.damage_chart = {}
for k=1, 7 do
local v = peek4(memory_i)
memory_i += 4
add(u.damage_chart, v)
end
add(players_unit_types[i], u)
end
end
players_reversed = {}
players_reversed[players[1]] = 1
players_reversed[players[2]] = 2
current_map = make_war_map({peek_increment(), peek_increment(), peek_increment(), peek_increment()})
map_bg_color = peek_increment()
peek_increment()
end
_update()
function _update()
-- In the case of btnp() the ones associated with U, D, L, R, O, and X (⬆️, ⬇️, ⬅️, ➡️, 🅾️, and ❎) are useful, because they represent the corresponding button indices 0 through 5.
t = time()
delta_time = t - last_checked_time
last_checked_time = t
attack_timer += delta_time
end_turn_timer += delta_time
btnp4 = btnp(4) -- btn 🅾️
btnp5 = btnp(5) -- btn ❎
-- in-menu logic
if in_splash_screen then
if splash_option_selected == 0 then
if btnp(0) then
map_index_selected -= 1
sfx(6)
elseif btnp(1) then
map_index_selected += 1
sfx(6)
end
elseif btnp(0) or btnp(1) then -- if ⬆️, ⬇️
ai_index_selected += 1
sfx(6)
end
if btnp(2) or btnp(3) then -- if ⬅️, ➡️
splash_option_selected += 1
sfx(6)
end
map_index_selected = map_index_selected % 6
ai_index_selected = ai_index_selected % 2
splash_option_selected = splash_option_selected % 2
if btnp4 or btnp5 then -- if 🅾️, ❎
in_splash_screen = false
current_map = make_war_map(map_index_mapping[map_index_selected+1][1])
map_bg_color = map_index_mapping[map_index_selected+1][2]
players_human[1] = not (btnp(1) and btnp(2))
players_human[2] = ai_index_selected == 1 and players_human[1]
current_map:load()
make_cam()
end_turn()
selector_init()
players_turn_team = players[players_turn]
end
else
-- game update loop
selector_update() -- icon logic
cam:update()
for unit in all(units) do
unit:update()
end
end
end
_draw()
larger functions (e.g. parent components in react)
load_assets()
make_war_map()
-
loops twice, to:
AI functions
ai_update()
ai_coroutine()
ai_move()
ai_pathfinding()
ai_calculate_attack_value()
Util functions
ui
selector_update()
- only on player turn
- icon logic i think
logic
peek_increment()
- peek(), and then increment
- used to load the item, and them move to the next item
- mutates a variable
memory_iglobal variable, starting from 0x1000 ()
loads items one by one from the sprite sheet when called multiple times
manhattan_distance(p, target)
- ya know, distance. but pixellated. because manhattan is a grid city, and navigating a diagonal involves going in zigzags of horizontals and verticals
- https://aigents.co/data-science-blog/publication/distance-metrics-for-machine-learning
get_unit_at_pos()
get_struct_at_pos()
get_selection()
references (picowars)
what is memory_i?
uhh it references sprite number 128, which is: (image not archived: ”./images/sprite128.png”)
references (pico8)
- numbers
- decimal - most numbers are represented as decimals
- binary - 0B prefix, e.g. 0B1010
- hexa - 0X prefix, e.g. 0XFF
- peek()
- get() item at any memory address
- takes a hex number as input for memory address
references (lua)
- for loop:
for i=1, 2 domeans start from 1, end at 2 (loop twice)
- peek: get()
- poke: set()