Skip to content

Instantly share code, notes, and snippets.

@amiantos
Created November 29, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiantos/b3a80e8aec534f768b687a53028e4d19 to your computer and use it in GitHub Desktop.
Save amiantos/b3a80e8aec534f768b687a53028e4d19 to your computer and use it in GitHub Desktop.
alpha verson of comms.lua from back of beyond, a pico-8 space game
comms_open=false
comms_mode=0
--modes: 0=planet,1=jobs,2=pre_battle,3=buy
--init
function init_comms()
comms_open=false
comms_mode=0
comms_planet=nil
comms_pirate=nil
comms_selected=1
comms_title=""
comms_str=""
comms_options={}
end
--update
function update_comms()
if #comms_options>0 then
local comms_changed=false
if btnp(5) then
comms_str_update(true)
comms_options[comms_selected][2](comms_options[comms_selected][3])
end
if btnp(2) then
comms_selected+=1
comms_changed=true
elseif btnp(3) then
comms_selected-=1
comms_changed=true
end
if (comms_selected<1) comms_selected=#comms_options
if (comms_selected>#comms_options) comms_selected=1
if (comms_changed) comms_str_update(false)
else
if (btnp(5)) close_comms()
end
end
--draw
function draw_comms()
--show action prompt
if (comms_planet and not comms_open) then
ow_print("❎open comms",4,120)
end
--comms
if comms_open then
rectfill(2,94,101,126,0)
rect(3,95,100,125,1)
line(5,104,98,104,1)
ow_print(comms_title,5,97)
comms_print(comms_str)
if #comms_options>0 then
--print options ui chrome
option_spacing={87,80,73,66,59,52,45,38}
rectfill(32,option_spacing[#comms_options]-1,127,97,0)
rect(33,option_spacing[#comms_options],126,96,1)
--print comms options
opt_spacing={89,82,75,68,61,54,47,40}
-- selected comms
local spacing=opt_spacing[comms_selected]
rectfill(35,spacing+2,124,spacing+3,1)
local i=1
for menu_item in all(comms_options) do
ow_print(menu_item[1],35,opt_spacing[i])
if menu_item[3] then
if (menu_item[3].cost) ow_print("$" .. menu_item[3].cost,105,opt_spacing[i])
if (menu_item[3].scu) ow_print("^" .. menu_item[3].scu,85,opt_spacing[i])
end
i+=1
end
else
ow_print("❎close",71,97)
end
end
end
function close_comms()
comms_open=false
comms_options={}
end
--comms options
--format:
--{
-- menu label: str,
-- function to call: func,
-- functions args: table,
-- hover comms: str or nil
-- post-selection comms: str or nil
--}
-- battle
function pre_battle_comms(pirate)
ow_ship.dx=0
ow_ship.dy=0
comms_options={{
"let's fight!",
begin_battle,
{enemy=pirate},
nil,
nil,
}}
shmup_pirate=pirate
comms_pirate=pirate
comms_mode=2
comms_selected=1
comms_title="pirate"
local comms_strs={
"i'm gonna mash you good!",
"hope you don't like living, 'cause you're gonna die now!",
"that's a nice ship, it'd be terrible if i blew it up right now.",
}
comms_str=comms_strs[rndb(1,#comms_strs)]
comms_open=true
end
--planet comms
function planet_comms()
comms_open=true
comms_selected=1
comms_mode=0
comms_title=comms_planet.name
comms_str="hey there, what can we do for you?"
local recharge_cost=min(ow_ship.cash,ceil((ow_ship.max_charge-ow_ship.charge)))
comms_options={
{
"goodbye",
close_comms,
nil,
nil,
nil
},
{
"charge ship",
charge_ship,
{cost=recharge_cost},
"we can top off your charge, if you're in need.",
"no problem, we'll get you charged up, good as new. enjoy!"
},
{
"repair shields",
repair_ship,
{cost=1000},
"you look like you could use some repairs.",
"i'll get my people to work on your ship immediately."
},
{
"ship upgrades",
buy_comms,
nil,
"we've got a mechanic on hand... if you got the cash to pay her!",
"this is what the mechanic can do for you."
},
{
"delivery jobs",
jobs_comms,
nil,
"we've got jobs for you, if you want them.",
"can you deliver these goods? good money in it for you, if so."
},
-- {
-- "sell",
-- sell_menu,
-- nil,
-- nil,
-- "i'll take a look, i may be buying...",
-- "show me what you got!"
-- },
}
for job in all(ow_ship.jobs) do
if job.planet.name==comms_planet.name then
add(comms_options,{
"make deliveries",
make_deliveries,
nil,
"ah, you brought some goods for us. well, hand 'em over!",
"thank you kindly. here's your pay, as agreed."
})
break
end
end
end
-- deliveries
function jobs_comms()
comms_mode=1
comms_selected=1
--generate possible deliveries
if #comms_planet.jobs==1 or comms_planet.jobs_age > 2000 then
local jobs=create_jobs(comms_planet)
comms_options=jobs
comms_planet.jobs=jobs
comms_planet.jobs_age=0
else
comms_options=comms_planet.jobs
end
end
function buy_comms()
comms_mode=3
comms_selected=1
-- if #comms_planet.shop==1 then
-- local items=create_shop(comms_planet)
-- comms_options=items
-- comms_planet.shop=items
-- else
comms_options=create_shop(comms_planet)
-- end
end
function create_shop(p)
local items=default_options()
--make scu booster
local amt=min(1000, 100+((ow_ship.max_scu-100)*1))
local cost=min(9999,1000+((ow_ship.max_scu-100)*12))
add(items,{
"scu retrofit",
buy_scu_boost,
{amount=amt,cost=cost},
"this will expand your maximum cargo storage by " .. amt .. " scu.",
"my boys are done wrenchin', you're good to go!"
})
--make charge booster
local amt=min(1000, 100+((ow_ship.max_charge-400)*1))
local cost=min(9999,1000+((ow_ship.max_charge-400)*12))
add(items,{
"extra batteries",
buy_charge_boost,
{amount=amt,cost=cost},
"boost your maximum charge by " .. amt .. " units, includes a free charge!",
"we've got your new batteries installed, good luck and stay frosty."
})
--make speed booster
local amt=min(100, 5+flr((ow_ship.speed-100)*0.5))
local cost=min(9999,1000+((ow_ship.speed-100)*50))
add(items,{
"engine upgrade",
buy_speed_boost,
{amount=amt,cost=cost},
"boost your speed by " .. amt .. " units, also increases charge comsumption.",
"whoa-ho! i'm gonna start calling you greased lightning!"
})
--make shield booster
local amt=min(100, 25+flr((ow_ship.max_shield-100)*0.05))
local cost=min(9999,1000+((ow_ship.max_shield-100)*10))
add(items,{
"shield booster",
buy_shield_boost,
{amount=amt,cost=cost},
"boost your shields by " .. amt .. " units. includes free repair.",
"looking good... give those pirates hell out there!"
})
return items
end
function create_jobs(p)
local jobs=default_options()
--calculate distances to all planets
local distances={}
local d_keys={}
for pl in all(ow_planets) do
distances[distanceFrom(p.x,p.y,pl.x,pl.y)]=pl
end
for k in pairs(distances) do add(d_keys,k) end
sort(d_keys)
local numbers={}
for i=#d_keys/5,#d_keys,#d_keys/5 do
add(numbers,d_keys[ceil(i)])
end
local i=1
for num in all(numbers) do
local planet=distances[num]
local scu=rndb(10,999)
if (i<=2) scu=rndb(10,100)
if (i==3) scu=rndb(10,200)
add(jobs,{
planet.name,
take_job,
{
cost=min(9999,ceil(distanceFrom(p.x,p.y,planet.x,planet.y)*1.5)+5*scu),
planet=planet,
scu=scu
},
"take this package to " .. planet.name,
"the job is yours!",
})
i+=1
end
return jobs
end
-- utils
function default_options()
return {{
"nevermind",
planet_comms,
nil,
nil,
nil,
nil
}}
end
function comms_str_update(selected)
--update comms text if selection needs it
if (selected and comms_options[comms_selected][5]) then
comms_str=comms_options[comms_selected][5]
elseif (comms_options[comms_selected][4]) then
comms_str=comms_options[comms_selected][4]
end
end
function comms_print(string)
--split string by 3 and print in comms
local x=5
local y_spc={106,112,118}
if (#string<=23) return ow_print(string,x,y_spc[1],6)
local split_st=split(string," ")
local new_line=""
local poss_line=""
local p_lines={}
for i=1,#split_st do
local word=split_st[i]
poss_line=new_line .. word .. " "
if #poss_line<25 then
new_line=poss_line
else
add(p_lines, new_line)
new_line=word .. " "
end
if (i==#split_st) add(p_lines,new_line)
end
for i=1,#p_lines do
if (i<=3) ow_print(p_lines[i],x,y_spc[i],6)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment