9 Commits

2 changed files with 314 additions and 74 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

@@ -1407,71 +1407,6 @@ shield will
return ret return ret
end 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 -- flotilla ships
ship_f = mknew(ship_m.new{ ship_f = mknew(ship_m.new{
@@ -1501,13 +1436,13 @@ ship_f = mknew(ship_m.new{
diamond_loop = segment.new{ diamond_loop = segment.new{
dests = { dests = {
destination.new{ destination.new{
x_off_frac=0.25 x_off_frac=0.125
}, destination.new{ }, destination.new{
y_off_frac=0.25 y_off_frac=0.125
}, destination.new { }, destination.new {
x_off_frac = -0.25 x_off_frac = -0.125
}, destination.new { }, destination.new {
y_off_frac = -0.25 y_off_frac = -0.125
}, },
}, },
} }
@@ -1529,6 +1464,37 @@ diamond_bounce = path.new{
}, },
} }
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{ ship_mook = mknew(ship_f.new{
sprite=103 sprite=103
}) })
@@ -1545,12 +1511,13 @@ ship_turret = mknew(ship_f.new{
ship_skirmisher = mknew(ship_f.new{ ship_skirmisher = mknew(ship_f.new{
sprite=107, sprite=107,
xp = 0x0.0004, xp = 0x0.0004,
hp = 1.5,
sparks = smokespark, sparks = smokespark,
sparkodds = 3, sparkodds = 3,
fire_off_y = 7, fire_off_y = 7,
xmin = -8, xmin = -8,
xmax = 112, xmax = 112,
path = diamond_bounce, path = swoop_loop,
}) })
function ship_skirmisher:reset_bounds() function ship_skirmisher:reset_bounds()
@@ -1675,7 +1642,7 @@ flotilla = mknew{
[8]=mknew(ship_turret.new{ship_t=8}), [8]=mknew(ship_turret.new{ship_t=8}),
[9]=mknew(ship_turret.new{ship_t=9, sprite=4}), [9]=mknew(ship_turret.new{ship_t=9, sprite=4}),
[12]=mknew(ship_skirmisher.new{ship_t=12}), [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, end,
} }
@@ -1691,8 +1658,9 @@ function flotilla:load(ulc_cx, ulc_cy, lvl)
[12]=0, [12]=0,
[13]=0, [13]=0,
} }
local maxcol = 0
repeat 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 for i,v in ipairs(self.opt_odds) do
opt[i*4-4]=rnd()<v opt[i*4-4]=rnd()<v
end end
@@ -1702,7 +1670,7 @@ function flotilla:load(ulc_cx, ulc_cy, lvl)
mode = f&0x03 mode = f&0x03
if mode==2 then if mode==2 then
-- bits 0x0c: ship class -- bits 0x0c: ship class
local ship_t = f&0x0c local ship_t, col = f&0x0c, cx-ulc_cx
-- bit 0x20: optional? -- bit 0x20: optional?
if f&0x20 == 0 or opt[ship_t] then if f&0x20 == 0 or opt[ship_t] then
-- bit 0x10: alternate ship? -- bit 0x10: alternate ship?
@@ -1711,7 +1679,8 @@ function flotilla:load(ulc_cx, ulc_cy, lvl)
-- and we allow alternates -- and we allow alternates
-- for this type of ship -- for this type of ship
ship_t+=(uv>>ship_t&0x1)&((f&0x10)>>4) 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
end end
cx += 1 cx += 1
@@ -1731,6 +1700,13 @@ function flotilla:load(ulc_cx, ulc_cy, lvl)
-- control mark bit 0x04: end of flotilla -- control mark bit 0x04: end of flotilla
until f&0x04 > 0 until f&0x04 > 0
self.rows=rows 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) self:statisfy(counts)
end end