This commit is contained in:
2022-05-21 00:39:58 -04:00
parent f03a48b0a6
commit b950cc3cff
5 changed files with 173 additions and 124 deletions

View File

@@ -20,6 +20,7 @@ setmetatable(player,army)
--
-- New Data Structure:
-- Player.Stack={ 1=kind, 2=qty, 3=kind, 4=qty,... }
-- Player.Food[kind]=Number
-- orchard.crop='r'
-- orchard.size=integer -- Max size it can grow to
-- orchard.rate=integer -- How many grow per unit time()
@@ -70,9 +71,10 @@ 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
for i=1,#self.stack,2 do if self.stack[i]==k then self.stack[i+1]=self.stack[i+1]+c self:cb_map() return end end
self.stack[#self.stack+1]=k
self.stack[#self.stack+1]=c
self:cb_map()
end
@@ -160,6 +162,15 @@ function fight0(ak,ac,dk,dc,tweekdefender) -- returns number of attacker casualt
end
function player:near_iter(rad,kind)
local lis=tangible.near(self,rad,true,true)
local i=1
return function()
while i<=#lis do
i=i+1
if not kind or isa(lis[i-1],kind) then return lis[i-1] end end end
end
function stack_iter(t1)
local i=0
return function()
@@ -191,67 +202,23 @@ function army:counttroops()
end
function army:die()
print("This enemy is dying!!!")
print("You defeat the enemy!")
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
if enemy:counttroops()==0 then enemy:die() return false
else return true end
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)
local rval={}
for ak,ac in pairs(self.Count) do -- Should randomize the order
local enemyk, enemyc
local useloss0
local useloss1=-1
local loss0,loss1
local whichdk, whichdc
for dk,dc in pairs(enemy.Count) do
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
enemyk=dk
enemyc=dc
useloss1=loss1
useloss0=loss0
end end
if enemyk then
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
end
end
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
end
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
@@ -265,20 +232,23 @@ function player:droparmy(actor,kind)
t.Count[kind]=t.Count[kind]+1
end
function orchard:gather(actor)
actor.food[self.kind]=actor.inventory[self.kind]+self.count
function player:pickfruit(ft)
local kind=ft.crop
local qty=ft:harvest(ft:available())
self.food[kind]=self.food[kind]+qty
print("You harvest "..qty.." "..foodnames[kind])
end
function player:newlocation()
function player:newlocation() -- return false if the player should bounce back, else return true
local lis=tangible.near(self,0,true,true)
local count={} setmetatable(count,NilIsZero)
local count0=0
local result={}
for _,t in ipairs(lis) do if isa(t,army) then
result[1+#result]=self:fight(t)
end end
pprint(result)
return result
local bounce=false
for _,t in ipairs(lis) do
if isa(t,army) then bounce=bounce or self:fight(t)
elseif isa(t,orchard) then self:pickfruit(t) end
end
return bounce
end
function player:printanimstate()
@@ -288,16 +258,8 @@ function player:printanimstate()
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
tangible.animate(self,{action='walk',dx=-dx,dy=-dy}) end
if self:newlocation() then tangible.animate(self,{action='walk',dx=-dx,dy=-dy}) end
self:cb_map()
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
function player:cb_north()
@@ -337,6 +299,7 @@ function where()
print("You are at "..x.." "..y)
end
function mapcelltext(lis)
if lis==nil then return ' ' end
if #lis>1 then return '++++++' end
@@ -348,14 +311,15 @@ function mapcelltext(lis)
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))
elseif isa(lis[1],orchard) then
rval=rval.." OO "
prefix="\27[32;1m" suffix="\27[0m"
rval=rval..lis[1].crop..string.format("%-5d",lis[1]:available())
end
return prefix..rval..suffix
end
function player.cb_map(actor,place,dialog)
print("\27c\27[38;5;9mMap:\27[0m")
print("\27[s\27[0;0f")
local rad=4
local ax,ay=tangible.xyz(actor)
scratch={}
@@ -378,62 +342,8 @@ function player.cb_map(actor,place,dialog)
lbuf=lbuf..'|'
print(lbuf)
end
print("\27[u")
end
function player.xcb_map(actor,place,dialog)
local rad=4
local ax,ay=tangible.xyz(actor)
local scratch={}
local lis=tangible.near(actor,1.5*rad,true,false)
print("\27c\27[38;5;9mMap:\27[0m")
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]={} 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
end
-- pprint(scratch)
for dy=-rad,rad do for line=0,1 do
local lbuf="|"
for dx=-rad,rad do
local offset=(dy+rad)*(rad*2+1)+dx+rad
if line==0 then lbuf=lbuf.."----"
elseif line==1 then
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.."-"
end end
print(lbuf)
end end
lbuf=""
for dx=-rad,rad do lbuf=lbuf.."----" end
lbuf=lbuf.."-"
print(lbuf)
-- print("In Player "..tangible.id(actor)..":")
-- 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
-- print("In Army "..tangible.id(v)..":")
-- for k2,v2 in pairs(v.Count) do print(" "..k2.." "..v2) end
-- end
end
function counttoten(p1,p2,p3)
for i=1,10 do print(i) end
end