18 Commits

Author SHA1 Message Date
03b4d86e47 require embedded filenames 2025-08-03 16:13:03 -07:00
3cc1287dc4 implement t, the last simple type 2025-08-03 15:48:18 -07:00
fbd75700d5 Implement fixed length arrays 2025-08-03 15:19:27 -07:00
42494142bb major code reuse improvement, more ops 2025-08-03 13:14:55 -07:00
550856cfba start of item writing 2025-07-26 19:02:52 -07:00
b534cb9b16 start of splubp prototype 2025-07-20 22:21:51 -07:00
6b5748dae7 bye, chasey!
chasey can be replaced by a path segment now
2025-07-06 22:28:41 -07:00
1d816e0c6a test player-relative paths 2025-07-06 21:07:36 -07:00
be321db355 mirror paths for skirmishers in the right half 2025-07-06 20:49:05 -07:00
7c3fc833df fix paths
segment/path update logic was somewhat backwards.

the result is definitely good enough for the kind of movements I want!
2025-07-06 20:32:43 -07:00
10aef3b1ed paths sort of work but not really
figure out why they do not show the expected patterns
2025-07-06 20:02:21 -07:00
6efd05febe try using a path for a skirmisher
slightly stabilized but the length operator does not work in the presence of Lua Crimes like this so it needs more work
2025-07-06 19:25:43 -07:00
383491fc5d add load-bearing commas 2025-07-06 18:10:27 -07:00
8533720114 paths and segments are now factories instead
the factory/instance deliniation is not clean but it _is_ efficient.
2025-07-06 18:09:56 -07:00
797818214a fix lingering stuff from a previous iteration 2025-07-06 14:42:23 -07:00
ee24adf8b2 path prototype 2025-07-05 20:27:14 -07:00
0b98ee540d segment prototype 2025-07-05 20:18:31 -07:00
9d02a1f570 destination prototype
also ship bounds resets
2025-07-05 19:43:15 -07:00
2 changed files with 559 additions and 75 deletions

264
splubp.p8 Normal file
View File

@@ -0,0 +1,264 @@
pico-8 cartridge // http://www.pico-8.com
version 42
__lua__
-- splubp data transport
-- by kistaro windrider
-- shrinko-8 hints
DEBUG = true --[[const]]
-- >8
-- utilities
-- generate standard "overlay"
-- constructor for type tt.
-- if tt.init is defined, generated
-- new calls tt.init(ret) after
-- ret is definitely not nil,
-- after calling setmetatable.
-- use to initialize mutables.
--
-- if there was a previous new,
-- it is invoked before
-- setting tt's metatable, so
-- each new will see its
-- inheritance chain.
function mknew(tt)
local mt,oldinit,more = {__index=tt},tt.superinit,rawget(tt, "init")
tt.new=function(ret)
if(not ret) ret = {}
ret.new = false
setmetatable(ret, mt)
if(oldinit) oldinit(ret)
if (more) more(ret)
return ret
end
if oldinit and more then
tt.superinit = function(ret)
oldinit(ret)
more(ret)
end
elseif more then
tt.superinit = more
end
return tt
end
function trim(s)
local f, e = 1, #s
while (f <= e and s[f]==" ") f += 1
while (e >= f and s[e]==" ") e -= 1
if (f<e) return sub(s,f,e)
return ""
end
function shatter(s)
local ret ={}
for line in all(split(s, "\n")) do
local row = {}
for cell in all(split(split(line, "--")[1], " ")) do
cell = trim(cell)
if (#cell > 0) add(row, cell)
end
if (#row > 0) add(ret, row)
end
return ret
end
function crush(s)
ret = ""
for line in shatter(s) do
for tok in line do
ret..=tok.." "
end
ret = sub(ret, 1, -2).."\n"
end
if (#ret > 0) return sub(ret, 1, -2)
return ""
end
-- >8
-- common
--[[preserve-keys]] splubp = mknew{
ptr = 0x8000,
init = function(self)
-- fill from ffile or saved file
self.fmts = {}
if (not self.ffile) self.ffile, self.ptr = self:e()
for f in all(split(self.ffile, "===", false)) do
local tok = shatter(f)
if DEBUG then
assert(#tok > 1, "not enough rows in "..tostr(tok))
assert(#tok[1] == 1, "bad extension row: "..tostr(tok[1]))
end
self.fmts[deli(tok, 1)[1]] = tok
end
end,
}
function splubp:ppi(n)
self.ptr += n
return self.ptr - n
end
function splubp:op()
self.cri += 1
return self.row[self.cri]
end
function splubp:c(n)
n = n or 1
return peek(self:ppi(n), n)
end
function splubp:i()
return %self:ppi(2)
end
function splubp:n()
return $self:ppi(4)
end
function splubp:e()
return chr(self:c(self:i()))
end
-- >8
-- writer
function splubp:emit_fmts()
self:write_e(self.ffile)
end
-- execute next op (write data)
function splubp:wnx(...)
return self["write_"..self:op()](self, ...)
end
-- write an object of a known type.
-- item must have a field named
-- _file, containing a filename
-- with a correct extension
-- to save the file under.
function splubp:emit_item(obj)
if DEBUG then
--[[global]] splubp_last_emit = obj
end
-- todo: should _addr skip the
-- filename, instead?
obj._addr = self.ptr
self:write_s(obj._file)
for row in all(self.fmts[split(filename, ".")[2]]) do
local alt = self.wdirectives[row[1]]
if alt then
alt(self, obj, row)
else
self:write_fmt(obj[row[1]], row, 2)
end
end
end
-- TODO: splubp.wdirectives -- a
-- table mapping non-key
-- special values to
-- functions to write them
-- TODO: write_fmt: recursive
-- writing thing
-- Write each argument as one
-- byte at self.ptr, then
-- increment self.ptr by the
-- number of bytes written.
function splubp:write_c(...)
poke(self:ppi(#{...}), ...)
end
-- Like etch but it takes only
-- a single 2-byte int.
function splubp:write_i(i)
poke2(self:ppi(2), i)
end
-- Like etch but it takes only
-- a single 4-byte number.
function splubp:write_n(n)
poke4(self:ppi(4), n)
end
function splubp:write_s(s)
self:write_c(#s, ord(s, 1, #s))
end
function splubp:write_e(s)
self:write_i(#s)
self:write_c(ord(s,1,#s))
end
splubp.write_b = splubp.write_c
function splubp:write_shr(n)
n <<= self:op()
self:wnx(n)
end
function splubp:write_a(tbl)
if (DEBUG) assert(#tbl <= 255, "'a' can't write array of length "..#tbl)
self:write_c(#tbl)
local rpt = self.cri
for item in all(tbl) do
self.cri = rpt
self:wnx(item)
end
end
function splubp:write_t(tbl)
if DEBUG then
local want_len = self.row[self.cri]
assert(#tbl == want_len, "'t' wants "..tostr(want_len).." items, but got "..#tbl..": "..tostr(tbl))
end
for i=1,self:op() do
self:wnx(tbl[i])
end
end
-->8
-- loader
-- execute next op (read)
function splubp:rnx()
return self[self:op()](self)
end
function splubp:s()
return chr(self:c(self:c()))
end
function splubp:b()
local ret = self:c()
if (ret < 128) return ret
return ret-256
end
function splubp:shr()
local amt = self:op()
return self:rnx() >>> amt
end
function splubp:a()
local ret,rpt = {},self.cri
for i = 1,self:c() do
self.cri=rpt
add(ret, self:rnx())
end
return ret
end
function splubp:t()
local ret = {}
for i=1,self:op() do
add(ret, self:rnx())
end
return ret
end

View File

@@ -194,7 +194,9 @@ function updategame()
interlude -= 1
else
current_wave = flotilla.new()
current_wave:load(rnd() > 0.5 and 7 or 0, 0, min(ones(waves_complete)\2, 4))
--current_wave:load(rnd() > 0.5 and 7 or 0, 0, min(ones(waves_complete)\2, 4))
current_wave:load(0, 0, min(ones(waves_complete)\2, 4))
current_wave:update()
end
events = filtermoved(events)
appendmove(events, new_events)
@@ -466,9 +468,7 @@ ship_m = mknew{
-- xmin, xmax, ymin, ymax:
-- movement constraints
-- enforced by `constrain`.
xmin = 0, xmax = 104,
-- ymin, ymax default to nil
-- pship needs more constraint
xmin = 0, xmax = 104, ymin = 0, ymax = 120
}
function ship_m:die()
@@ -510,6 +510,10 @@ function ship_m:brake_dist(v0)
return (chunk_zone + overage * (tri_frames + 1)) * sgn(v0), (overage > 0) and tri_frames + 1 or tri_frames
end
function ship_m:reset_bounds()
self.xmin, self.xmax, self.ymin, self.ymax = 0, 104, 0, 120
end
function ship_m:constrain(p, dp, pmin, pmax, want)
if (not pmin) return want
local v1, bd, bf, bp
@@ -615,6 +619,190 @@ function ship_m:refresh_shield()
self.shield_refresh_ready = gframe + self.shieldcooldown
end
-->8
-- paths
-- destination: a point the
-- center of a ship approaches
destination = mknew{
-- 0..1: a point on a line
-- segment from the ship's
-- flotilla spot to the player;
-- -1 means center of play area
anchor_frac=0,
-- (0, 1]: relative to max
-- acceleration, how quickly
-- should the ship accelerate
-- to its destination?
-- 0: park here instead, using
-- constraint following
accel_frac=1,
-- how far to lerp between the
-- anchor and the screen bounds.
-- this is the "destination"-y
-- part of a destination.
x_off_frac = 0,
y_off_frac = 0
}
function lerp(a, b, f)
return (1-f)*a+b*f
end
function destination:anchor(fx, fy)
local af = self.anchor_frac
if (af == -1) return 51,59
return lerp(fx, primary_ship.x, af), lerp(fy, primary_ship.y, af)
end
function destination:target_from(fx, fy)
local rx, ry = self:anchor(fx, fy)
local xf, yf = self.x_off_frac, self.y_off_frac
if (xf < 0) rx = lerp(rx, 0, -xf)
if (xf > 0) rx = lerp(rx, 104, xf)
if (yf < 0) ry = lerp(ry, 0, -yf)
if (yf > 0) ry = lerp(ry, 120, yf)
return rx, ry, self.accel_frac
end
segment = mknew{
-- On loop, where to set index?
loop_idx = 1,
-- current index
current_idx = 1,
-- how long does this segment
-- last? 0 is erroneous,
-- positive values are a frame
-- count, negative values are
-- a number of loops.
limit = -1,
-- remain: remaining steps until end
-- prev_x, prev_y: stored previous
-- location to figure out
-- approach/depart
-- dest_x, dest_y: stored
-- current destination to
-- figure out approach/depart
-- dests[1..n]: destinations
init = function(x)
x.init = segment.reset
mknew(x)
if not x.mirror then
local mdests = {}
for i, v in ipairs(x.dests) do
mdests[i] = destination.new{
anchor_frac=v.anchor_frac,
accel_frac=v.accel_frac,
x_off_frac=-v.x_off_frac,
y_off_frac=v.y_off_frac,
}
end
x.mirror = mknew(x.new{mirror=x, init=segment.reset, dests=mdests})
end
end
}
function segment:reset()
self.current_idx = 1
self.remain = self.limit
self.prev_x = false
self.prev_y = false
self.was_approaching = false
end
function segment:target_from(fx, fy)
local rx, ry, af = self.dests[self.current_idx]:target_from(fx, fy)
self.dest_x, self.dest_y = rx, ry
return rx, ry, af
end
function segment:update(x, y)
local rem = self.remain
-- frame check
if (rem > 0) rem -= 1
if self:should_step(x, y) then
self.prev_x, self.prev_y, self.was_approaching = false, false, false
if self.current_idx == #self.dests then
self.current_idx = self.loop_idx
if (rem < 0) rem += 1
else
self.current_idx += 1
end
end
self.remain = rem
return rem != 0
end
function segment:should_step(x, y)
local dest_x, dest_y = self.dest_x, self.dest_y
local dx1, dy1 = x - dest_x, y-dest_y
if (abs(dx1) <= 1.5 and abs(dy1) <= 1.5) return true
if self.prev_x then
local dx0, dy0 = self.prev_x - dest_x, self.prev_y - dest_y
local d12, d02 = dx1 * dx1 + dy1 * dy1, dx0 * dx0 + dy0 * dy0
if d12 > d02 then
if (self.was_approaching) return true
else
if (d12 == d02 and self.was_approaching) return true
self.was_approaching = true
end
end
self.prev_x, self.prev_y = x, y
return false
end
path = mknew {
loop_idx = 1,
-- segs[1..n]: segments or subpaths
init=function(x)
if not x.mirror then
local msegs = {}
for i, v in ipairs(x.segs) do
msegs[i]=v.mirror
end
x.mirror = path.new{mirror=x, loop_idx=x.loop_idx, segs=msegs}
end
x.init = function(y)
local ysegs = {}
for i, v in ipairs(x.segs) do
ysegs[i]=v.new{}
end
y.segs = ysegs
y:reset()
end
mknew(x)
end,
}
function path:reset()
self.current_idx = 1
for s in all(self.segs) do
s:reset()
end
end
function path:target_from(fx, fy)
return self.segs[self.current_idx]:target_from(fx, fy)
end
function path:update(x, y)
local idx, segs = self.current_idx, self.segs
if not segs[idx]:update(x, y) then
segs[idx]:reset()
if idx == #segs then
self.current_idx = self.loop_idx
return
else
self.current_idx = idx+1
end
end
return true
end
-->8
-- bullet and gun behaviors
@@ -1089,7 +1277,6 @@ player = mknew(ship_m.new{
ymomentum = 0,
maxspd = 1.5, -- momentum cap
thrust = 0.1875, -- momentum added from button
ymin = 0, ymax = 120, -- stay on screen
drag = 0.0625, -- momentum lost per frame
act = function(self) -- fetch buttons
local b,th = btn(),self.thrust
@@ -1220,71 +1407,6 @@ shield will
return ret
end
-- original prototype leftover;
-- likely to be the basis of a
-- standard raider type, so
-- i am keeping it around
chasey = mknew(ship_m.new{
sprite = 5,
xp = 0x0.0004,
size = 1,
hurt = {
x_off = 1,
y_off = 2,
width = 6,
height = 5,
},
sparks = smokespark,
sparkodds = 8,
hp = 1.5,
shield = 1,
maxshield = 1,
fire_off_x = 4,
fire_off_y = 7,
maxspd = 2,
thrust = 0.2,
drag = 0.075,
init = function(ship)
--ship.main_gun=ship.main_gun or zap_gun_e.new{}
end
})
function chasey:act()
self.xmin = max(primary_ship.x-8, 0)
self.xmax = min(primary_ship.x + 8, 112 - 8*self.size)
return 0, 0, false, self.y > 10 and self.x - 16 < primary_ship.x and self.x + 16 > primary_ship.x
end
xl_chasey=mknew(chasey.new{
size=2,
xp = 0x0.000a,
maxspd=1.25,
hurt = {
x_off = 2,
y_off = 4,
width = 12,
height = 10
},
fire_off_x = 8,
fire_off_y = 15,
hp = 19.5,
shield = 5,
boss = true,
act = function(self)
local dx,dy,shoot_spec,shoot_main = chasey.act(self)
if (self.y < 4) dy=self.thrust
return dx,dy,shoot_spec,shoot_main
end,
draw = function(self)
if(self.fx_pal) pal(self.fx_pal)
sspr(40, 0, 8, 8, self.x, self.y, 16, 16)
pal()
end,
})
-- flotilla ships
ship_f = mknew(ship_m.new{
@@ -1303,13 +1425,76 @@ ship_f = mknew(ship_m.new{
act = function(self)
local wx,wy=self.want_x,self.want_y
self.xmin,self.xmax,self.ymin,self.ymax = wx,wx,wy,wy
return 0,0,false,false
return 0,0
end,
init = function(self)
if (self.gun_proto) self.main_gun = self.gun_proto.new()
if (self.path) self.path = self.path.new()
end
})
diamond_loop = segment.new{
dests = {
destination.new{
x_off_frac=0.125
}, destination.new{
y_off_frac=0.125
}, destination.new {
x_off_frac = -0.125
}, destination.new {
y_off_frac = -0.125
},
},
}
park = segment.new{
dests = {
destination.new{
accel_frac = 0
}
}
}
diamond_bounce = path.new{
segs = {
park,
diamond_loop,
park,
diamond_loop.mirror,
},
}
swoop = segment.new {
dests = {
destination.new{
accel_frac = 0
}, destination.new{
x_off_frac = -0.75,
anchor_frac = 0.25,
}, destination.new {
x_off_frac = -0.375,
anchor_frac = 0.625,
}, destination.new {
y_off_frac = -0.125,
anchor_frac = 1,
}, destination.new {
x_off_frac = 0.375,
anchor_frac = 0.625,
},
},
}
swoop_loop = path.new {
segs = {
diamond_loop,
swoop,
park,
diamond_loop.mirror,
swoop.mirror,
park,
}
}
ship_mook = mknew(ship_f.new{
sprite=103
})
@@ -1326,11 +1511,37 @@ ship_turret = mknew(ship_f.new{
ship_skirmisher = mknew(ship_f.new{
sprite=107,
xp = 0x0.0004,
hp = 1.5,
sparks = smokespark,
sparkodds = 3,
fire_off_y = 7,
xmin = -8,
xmax = 112,
path = swoop_loop,
})
function ship_skirmisher:reset_bounds()
self.xmin, self.xmax, self.ymin, self.ymax = -8, 112, 0, 128
end
function ship_skirmisher:act()
local tx, ty, af = self.path:target_from(self.want_x, self.want_y)
if af <= 0 then
self.xmin, self.xmax, self.ymin, self.ymax = tx,tx,ty,ty
return 0,0
end
self:reset_bounds()
local ax, ay, th = tx - self.x, ty - self.y, self.thrust*af
local ma = max(abs(ax), abs(ay))
return ax*th/ma, ay*th/ma
end
function ship_skirmisher:move()
if (not ship_f.move(self)) return
if (not self.path:update(self.x, self.y)) self.path:reset()
return true
end
function rnd_spawn_loc()
local x,y = flr(rnd(304)), flr(rnd(32))
if (x<184) return x-40,-y-8
@@ -1431,7 +1642,7 @@ flotilla = mknew{
[8]=mknew(ship_turret.new{ship_t=8}),
[9]=mknew(ship_turret.new{ship_t=9, sprite=4}),
[12]=mknew(ship_skirmisher.new{ship_t=12}),
[13]=mknew(ship_skirmisher.new{ship_t=13, sprite=26}),
[13]=mknew(ship_skirmisher.new{ship_t=13, sprite=26, path=diamond_bounce}),
}
end,
}
@@ -1447,8 +1658,9 @@ function flotilla:load(ulc_cx, ulc_cy, lvl)
[12]=0,
[13]=0,
}
local maxcol = 0
repeat
local row,cx,opt,f,mode= {},ulc_cx,{},0,0
local row,cx,opt,f,mode = {},ulc_cx,{},0,0
for i,v in ipairs(self.opt_odds) do
opt[i*4-4]=rnd()<v
end
@@ -1458,7 +1670,7 @@ function flotilla:load(ulc_cx, ulc_cy, lvl)
mode = f&0x03
if mode==2 then
-- bits 0x0c: ship class
local ship_t = f&0x0c
local ship_t, col = f&0x0c, cx-ulc_cx
-- bit 0x20: optional?
if f&0x20 == 0 or opt[ship_t] then
-- bit 0x10: alternate ship?
@@ -1467,7 +1679,8 @@ function flotilla:load(ulc_cx, ulc_cy, lvl)
-- and we allow alternates
-- for this type of ship
ship_t+=(uv>>ship_t&0x1)&((f&0x10)>>4)
add(row, self.ship_bases[ship_t].new{col=cx-ulc_cx})
add(row, self.ship_bases[ship_t].new{col=col})
if (col > maxcol) maxcol = col
end
end
cx += 1
@@ -1487,6 +1700,13 @@ function flotilla:load(ulc_cx, ulc_cy, lvl)
-- control mark bit 0x04: end of flotilla
until f&0x04 > 0
self.rows=rows
-- mirror right half paths; alternate center column if odd
local rh = maxcol>>1
for r, row in ipairs(rows) do
for s in all(row) do
if ((s.path) and (s.col > rh or (s.col == rh and r & 1 == 1))) s.path = s.path.mirror.new()
end
end
self:statisfy(counts)
end