PICOWARS - self study

in pico 8, the main game loop is stored in 3 global callbacks:

in picowars by lambdanaut

below are all functions as defined in the game, and notes to make sense of them

main functions

_init()

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()

AI functions

ai_update()

ai_coroutine()

ai_move()

ai_pathfinding()

ai_calculate_attack_value()

Util functions

ui

selector_update()

logic

peek_increment()

loads items one by one from the sprite sheet when called multiple times

manhattan_distance(p, target)

get_unit_at_pos()

get_struct_at_pos()

get_selection()

references (picowars)

what is memory_i?

uhh it references sprite number 128, which is: sprite128

references (pico8)

references (lua)