Files
integration/luprex/core/lua/horps.lua

400 lines
12 KiB
Lua
Raw Normal View History

2022-02-03 11:20:28 -05:00
makeclass('player')
2022-03-05 19:17:22 -05:00
makeclass('army')
2022-04-25 12:20:29 -04:00
makeclass('orchard')
2022-03-31 14:41:19 -04:00
2022-05-18 13:20:48 -04:00
setmetatable(player,army)
2022-03-05 19:17:22 -05:00
--
-- HORPS game: Walk around the board collecting armies and buffs
-- Captured armies eat each turn
-- Attacking armies select order (random by default)
-- Defending armies are stronger
-- Command: Tame Creature Stack
-- Command: Free Creature Stack
--
2022-02-03 11:20:28 -05:00
2022-02-03 16:23:14 -05:00
--
2022-05-18 13:20:48 -04:00
-- Old Data Structure
2022-03-05 19:17:22 -05:00
-- Army.Count[kind]=number Where kind is r,p,s
2022-03-23 17:03:07 -04:00
-- Player.Count[kind]=number Where kind is r,p,s
2022-02-03 16:23:14 -05:00
--
2022-05-18 13:20:48 -04:00
-- New Data Structure:
-- Player.Stack={ 1=kind, 2=qty, 3=kind, 4=qty,... }
--
2022-02-03 16:23:14 -05:00
2022-02-03 11:20:28 -05:00
function player.interface(actor, place)
2022-03-05 19:17:22 -05:00
gui.menu_item("cb_north" ,"Go North")
gui.menu_item("cb_south" ,"Go South")
gui.menu_item("cb_east" ,"Go East")
gui.menu_item("cb_west" ,"Go West")
gui.menu_item("cb_map" ,"Show the Map")
gui.menu_item("cb_conjurerock" ,"Conjure Rock")
gui.menu_item("cb_conjurepaper" ,"Conjure Paper")
gui.menu_item("cb_conjurescissor" ,"Conjure Scissor")
gui.menu_item("cb_droprock" ,"Drop Rock")
gui.menu_item("cb_droppaper" ,"Drop Paper")
gui.menu_item("cb_dropscissor" ,"Drop Scissor")
end
2022-03-16 15:47:46 -04:00
2022-05-18 13:20:48 -04:00
function player:cb_conjurerock() self:conjure('r',1) end
function player:cb_conjurepaper() self:conjure('p',1) end
function player:cb_conjurescissor() self:conjure('s',1) end
function player:conjure(k,c)
for i=1,#self.stack,2 do if self.stack[i]==k then self.stack[i+1]=self.stack[i+1]+c return end end
self.stack[#self.stack+1]=k
self.stack[#self.stack+1]=c
end
2022-03-05 19:17:22 -05:00
function army.interface(actor,place)
if place.owner==actor then gui_menu_item("cb_recruit","Recruit");
2022-03-16 15:47:46 -04:00
else gui.menu_item("cb_fight" ,"Fight");
2022-03-05 19:17:22 -05:00
end end
-- Rock dulls Scissors 3
-- Rock cripples Lizard 2
-- Paper disproves Spock 3
-- Paper covers Rock 2
-- Scissors decapitates Lizard 3
-- Scissors cuts Paper 2
-- Lizard eats Paper 3
-- Lizard poisons Spock 2
-- Spock vaporizes Rock 3
-- Spock dismantles Scissors 2
armytypes={'r','p','s'}
2022-04-25 12:20:29 -04:00
armynames={r="Rock Golem" ,p="Paper Dragon",s="Scissor Beast",l="Fire Lizzard",v="Mr. Spock"}
foodnames={r="Raspberry" ,p="Pomegranite" ,s="Strawberry" ,l="Lemon" ,v="Mango" }
2022-03-05 19:17:22 -05:00
--advantage={ r={ r=1, p=1/2, s=3, l=2, v=1/3 },
-- p={ r=2, p=1, s=1/2, l=1/3, v=3 },
2022-03-16 15:47:46 -04:00
-- s={ r=1/3, p=2, s=1, l=3, v=1/2 },
-- l={ r=1/2, p=3, s=1/3, l=1, v=2 },
-- v={ r=3, p=1/3, s=2, l=1/2, v=1 } }
2022-03-05 19:17:22 -05:00
advantage={ r={ r=1, p=1/2, s=2 },
p={ r=2, p=1, s=1/2 },
s={ r=1/2, p=2, s=1 } }
function shuffle(t)
local s={}
for i=1, #t do s[i]=t[i] end
for i=#t,2,-1 do
local j=math.random(i)
s[i],s[j]=s[j],s[i]
end
return s
end
2022-03-23 17:03:07 -04:00
function MakeMap()
2022-03-29 15:47:44 -04:00
local rad=6
for x=-rad,rad do for y=-rad,rad do if math.random(1,5)==1 then
2022-03-23 17:03:07 -04:00
t={class='army',x=x,y=y,z=0,plane='main',graphic='army'}
local nt=tangible.build(t)
2022-05-18 13:20:48 -04:00
local stack={}
local shuf=shuffle(armytypes)
for i=1,#shuf do if math.random(1,2)==1 then
stack[1+#stack]=shuf[i]
stack[1+#stack]=math.random(1,3)
2022-03-23 17:03:07 -04:00
end end
2022-05-18 13:20:48 -04:00
nt.stack=stack
2022-03-29 15:47:44 -04:00
end end end
2022-03-23 17:03:07 -04:00
end
2022-03-05 19:17:22 -05:00
2022-04-25 12:20:29 -04:00
2022-03-05 19:17:22 -05:00
--
-- For each creature type, select the optimal target. Select randomly among identical targets.
--
2022-05-18 13:20:48 -04:00
function fight0(ak,ac,dk,dc,tweekdefender) -- returns number of attacker casualties, defender casualties
2022-03-05 19:17:22 -05:00
local adv0=advantage[ak][dk]
local adv1=advantage[dk][ak]
2022-04-25 12:20:29 -04:00
if not tweekdefender then tweekdefender=function(adv) return adv end end
adv1=tweekdefender(adv1)
local losstotal=(ac+dc)/2
local power0=ac*adv0
local power1=dc*adv1
local powertotal=power0+power1
local loss0=math.min(ac,math.floor(losstotal*power1/powertotal))
local loss1=math.min(dc,math.floor(losstotal*power0/powertotal))
2022-05-18 13:20:48 -04:00
if loss0+loss1==0 then loss0=loss0+1 end
2022-04-25 12:20:29 -04:00
return loss0,loss1
2022-03-05 19:17:22 -05:00
end
2022-04-25 12:20:29 -04:00
2022-05-18 13:20:48 -04:00
function stack_iter(t1)
local i=0
return function()
if i+2>#t1 then return end
i=i+2
return t1[i-1],t1[i]
end end
function fight_iter(t1,t2) -- Returns quads: AttackerKind,AttackerCount,DefenderKind,DefenderCount
local i=0
return function()
if i+2>#t1 or i+2>#t2 then return end
i=i+2
return t1[i-1],t1[i],t2[i-1],t2[i]
end end
2022-04-25 12:20:29 -04:00
2022-05-18 13:20:48 -04:00
function compactstack(s)
local index=1
while index<=#s do
if s[index+1]==0 then table.remove(s,index) table.remove(s,index)
else index=index+2 end
end
end
function army:counttroops()
local rval=0
for k,c in stack_iter(self.stack) do rval=rval+c end
return rval
end
function army:die()
print("This enemy is dying!!!")
tangible.delete(self)
print("And now it is dead, so sad")
end
function army:fight(enemy)
print("Fight!!!")
local rval={}
for i=1,math.min(#self.stack,#enemy.stack),2 do
loss1,loss2=fight0(self.stack[i],self.stack[i+1],enemy.stack[i],enemy.stack[i+1])
print("When "..self.stack[i+1].." "..self.stack[i].." fight "..enemy.stack[i+1].." "..enemy.stack[i].." they kill "..loss2.." and suffer "..loss1)
rval[1+#rval]={'fight',self.stack[i],self.stack[i+1],loss1,enemy.stack[i],enemy.stack[i+1],loss2}
self.stack[i+1]=self.stack[i+1]-loss1
enemy.stack[i+1]=enemy.stack[i+1]-loss2
end
compactstack(self.stack)
compactstack(enemy.stack)
if enemy:counttroops()==0 then enemy:die() end
return rval
end
-- for ak,ac,dk,dc in fight_iter(self.stack,enemy.stack) do
-- loss1,loss2=fight0(ak,ac,dk,dc)
-- print("When "..ac.." "..ak.." fight "..dc.." "..dk.." they kill "..loss2.." and suffer "..loss1)
-- rval[1+#rval]={'fight',ak,ac,loss1,dk,dc,loss2}
-- end
function player:xfight(enemy)
2022-04-25 12:20:29 -04:00
local rval={}
2022-03-31 14:41:19 -04:00
for ak,ac in pairs(self.Count) do -- Should randomize the order
2022-03-16 15:47:46 -04:00
local enemyk, enemyc
2022-04-25 12:20:29 -04:00
local useloss0
local useloss1=-1
local loss0,loss1
2022-03-16 15:47:46 -04:00
local whichdk, whichdc
2022-03-31 14:41:19 -04:00
for dk,dc in pairs(enemy.Count) do
2022-04-25 12:20:29 -04:00
loss0,loss1=army.fight0(ak,ac,dk,dc)
print("When "..ac.." "..ak.." fight "..dc.." "..dk.." they kill "..loss1.." and suffer "..loss0)
if loss1>useloss1 then
2022-03-16 15:47:46 -04:00
enemyk=dk
enemyc=dc
2022-04-25 12:20:29 -04:00
useloss1=loss1
useloss0=loss0
2022-03-05 19:17:22 -05:00
end end
2022-03-16 15:47:46 -04:00
if enemyk then
2022-04-25 12:20:29 -04:00
print("Army of "..ac.." "..ak.." fights "..enemyc.." "..enemyk..", killing "..useloss1.." and suffering "..useloss0)
rval[1+#rval]={'fight',ak,ac,useloss0,enemyk,enemyc,useloss1}
enemy.Count[enemyk]=enemy.Count[enemyk]-useloss1
self.Count[ak]=self.Count[ak]-useloss0
2022-03-05 19:17:22 -05:00
end
end
2022-04-25 12:20:29 -04:00
local enemyleft=0
local playerleft=0
for dk,dc in pairs(enemy.Count) do enemyleft=enemyleft+dc end
for ak,ac in pairs(self.Count) do playerleft=playerleft+ac end
rval.enemyleft=enemyleft
return rval
2022-03-05 19:17:22 -05:00
end
2022-04-25 12:20:29 -04:00
2022-03-05 19:17:22 -05:00
function player:cb_droprock(actor) player:droparmy(actor,'r') end
function player:cb_droppaper(actor) player:droparmy(actor,'p') end
function player:cb_dropscissor(actor) player:droparmy(actor,'s') end
function player:droparmy(actor,kind)
2022-03-23 17:03:07 -04:00
if actor.Count[kind]<=0 then print("No armies of type "..kind.." available") return end
2022-03-05 19:17:22 -05:00
local lis=tangible.near(actor,0,true,true)
if #lis>1 then print("Multiple Tangibles Nearby") return end
local t=lis[1]
if t==nil then t=player:buildarmy(actor) end
t.Count[kind]=t.Count[kind]+1
end
2022-04-25 12:20:29 -04:00
function orchard:gather(actor)
actor.food[self.kind]=actor.inventory[self.kind]+self.count
end
2022-03-05 19:17:22 -05:00
function player:newlocation()
2022-03-31 14:41:19 -04:00
local lis=tangible.near(self,0,true,true)
2022-04-25 12:20:29 -04:00
local count={} setmetatable(count,NilIsZero)
local count0=0
local result={}
2022-05-18 13:20:48 -04:00
for _,t in ipairs(lis) do if isa(t,army) then
result[1+#result]=self:fight(t)
end end
pprint(result)
2022-04-25 12:20:29 -04:00
return result
2022-03-05 19:17:22 -05:00
end
2022-02-03 11:20:28 -05:00
function player:printanimstate()
2022-03-05 19:17:22 -05:00
local graphic,plane,x,y,z,facing = tangible.animstate(self)
print("Resulting state: ", graphic, plane, x, y, z, facing)
end
2022-02-03 11:20:28 -05:00
2022-04-25 12:20:29 -04:00
function player:walk(dx,dy)
tangible.animate(self,{action='walk',dx=dx,dy=dy})
local result=self:newlocation()
if #result>1 then print("Error: multiple armies at one location")
elseif #result==1 then
2022-05-18 13:20:48 -04:00
tangible.animate(self,{action='walk',dx=-dx,dy=-dy}) end
self:cb_map()
2022-04-25 12:20:29 -04:00
if #result==1 then
for i,v in ipairs(result[1]) do
if v[1]=='fight' then print(v[3]..v[2].." attacks "..v[6]..v[5]..", killing "..v[7].." while losing "..v[4]) end
end
end
end
2022-03-05 19:17:22 -05:00
function player:cb_north()
2022-04-25 12:20:29 -04:00
self:walk(0,1)
2022-03-05 19:17:22 -05:00
end
2022-02-03 11:20:28 -05:00
2022-03-31 14:41:19 -04:00
function player:cb_south()
2022-04-25 12:20:29 -04:00
self:walk(0,-1)
2022-03-05 19:17:22 -05:00
end
2022-02-03 11:20:28 -05:00
2022-03-31 14:41:19 -04:00
function player:cb_east()
2022-04-25 12:20:29 -04:00
self:walk(1,0)
2022-03-05 19:17:22 -05:00
end
2022-03-31 14:41:19 -04:00
function player:cb_west()
2022-05-18 13:20:48 -04:00
self:walk(-1,0)
2022-03-05 19:17:22 -05:00
end
function player:buildarmy(actor)
local _,pl,ax,ay=tangible.animstate(actor)
t={class='army',x=ax,y=ay,z=0,plane=pl,graphic='army'}
local nt=tangible.build(t)
nt.Count={}
setmetatable(nt.Count,{__index=function(t,k) return 0 end,__newindex=function(t,k,v) if v~=nil and v~=0 then rawset(t,k,v) end end})
return nt
end
2022-02-03 11:20:28 -05:00
2022-05-18 13:20:48 -04:00
makeclass('army')
function seq(a,b,c) return a<=b and b<=c or false end
function num2(a) if a<=9 then return " "..a else return a end end
function where()
local x,y,z=tangible.xyz(tangible.actor())
print("You are at "..x.." "..y)
2022-03-05 19:17:22 -05:00
end
2022-02-03 11:20:28 -05:00
2022-05-18 13:20:48 -04:00
function mapcelltext(lis)
if lis==nil then return ' ' end
if #lis>1 then return '++++++' end
local acc={} setmetatable(acc,NilIsZero)
local rval=""
local prefix=""
local suffix=""
if isa(lis[1],player) then prefix="\27[91;7m" suffix="\27[0m" end
for k,c in stack_iter(lis[1].stack) do rval=rval..k..(c>9 and '+' or tostring(c)) end
rval=rval..string.sub(' ',1,6-string.len(rval))
return prefix..rval..suffix
2022-03-05 19:17:22 -05:00
end
2022-02-03 11:20:28 -05:00
2022-05-18 13:20:48 -04:00
function player.cb_map(actor,place,dialog)
print("\27c\27[38;5;9mMap:\27[0m")
local rad=4
local ax,ay=tangible.xyz(actor)
scratch={}
local lis=tangible.near(actor,1.5*rad,true,false)
for _,t in pairs(lis) do
local c=tangible.getclass(t)
local dx,dy=tangible.xyz(t)
local offset=(-dy+ay+rad)*(rad*2+1)+dx-ax+rad
local cl=tangible.getclass(t)
if seq(ax-rad,dx,ax+rad) and seq(ay-rad,dy,ay+rad) and (cl=='player' or cl=='army') then
if not scratch[offset] then scratch[offset]={} end
scratch[offset][1+#scratch[offset]]=t
end end
for dy=-rad,rad do
local lbuf=""
for dx=-rad,rad do
local offset=(dy+rad)*(rad*2+1)+dx+rad
lbuf=lbuf..'|'..mapcelltext(scratch[offset])
end
lbuf=lbuf..'|'
print(lbuf)
end
end
2022-04-25 12:20:29 -04:00
2022-02-03 11:20:28 -05:00
2022-05-18 13:20:48 -04:00
function player.xcb_map(actor,place,dialog)
2022-03-29 15:47:44 -04:00
local rad=4
2022-03-31 14:41:19 -04:00
local ax,ay=tangible.xyz(actor)
local scratch={}
2022-03-23 17:03:07 -04:00
local lis=tangible.near(actor,1.5*rad,true,false)
2022-03-31 14:41:19 -04:00
print("\27c\27[38;5;9mMap:\27[0m")
2022-02-03 11:20:28 -05:00
for _,t in pairs(lis) do
local c=tangible.getclass(t)
local dx,dy=tangible.xyz(t)
2022-03-31 14:41:19 -04:00
local offset=(-dy+ay+rad)*(rad*2+1)+dx-ax+rad
2022-02-03 11:20:28 -05:00
local cl=tangible.getclass(t)
2022-03-31 14:41:19 -04:00
if seq(ax-rad,dx,ax+rad) and seq(ay-rad,dy,ay+rad) and (cl=='player' or cl=='army') then
2022-03-23 17:03:07 -04:00
if not scratch[offset] then scratch[offset]={} scratch[offset].Count={} setmetatable(scratch[offset].Count,NilIsZero) end
if t.Count then
for i,k in ipairs(armytypes) do
local a0=scratch[offset]
local a1=a0.Count
local a2=a1[i]
local a3=t.Count
local a4=a3[k]
if not a4 then print("a4 is nil") pprint(t) end
scratch[offset].Count[i]=a2+a4
end end
end
2022-02-03 11:20:28 -05:00
end
2022-03-29 16:37:32 -04:00
-- pprint(scratch)
2022-02-03 16:23:14 -05:00
for dy=-rad,rad do for line=0,1 do
2022-03-31 14:41:19 -04:00
local lbuf="|"
2022-02-03 16:23:14 -05:00
for dx=-rad,rad do
2022-03-05 19:17:22 -05:00
local offset=(dy+rad)*(rad*2+1)+dx+rad
if line==0 then lbuf=lbuf.."----"
2022-02-03 16:23:14 -05:00
elseif line==1 then
2022-03-23 17:03:07 -04:00
if scratch[offset] then for i,k in ipairs(armytypes) do lbuf=lbuf..scratch[offset].Count[i] end
else for i=1,#armytypes do lbuf=lbuf..' ' end end
lbuf=lbuf.."-"
2022-02-03 16:23:14 -05:00
end end
2022-02-03 11:20:28 -05:00
print(lbuf)
end end
2022-03-05 19:17:22 -05:00
lbuf=""
for dx=-rad,rad do lbuf=lbuf.."----" end
lbuf=lbuf.."-"
print(lbuf)
2022-03-29 16:37:32 -04:00
-- print("In Player "..tangible.id(actor)..":")
2022-03-31 14:41:19 -04:00
-- for k,v in pairs(actor.Count) do print(" "..k.." "..v) end
-- local lis=tangible.near(actor,0,true,true)
-- for k,v in pairs(lis) do
2022-03-29 16:37:32 -04:00
-- print("In Army "..tangible.id(v)..":")
2022-03-31 14:41:19 -04:00
-- for k2,v2 in pairs(v.Count) do print(" "..k2.." "..v2) end
-- end
2022-02-03 11:20:28 -05:00
end
2022-05-18 13:20:48 -04:00
function counttoten(p1,p2,p3)
for i=1,10 do print(i) end
end