From a6213917e7a7f5f2fb9936e51c6013772e3c84d3 Mon Sep 17 00:00:00 2001 From: "teppy@egenesis.com" Date: Thu, 31 Mar 2022 14:41:19 -0400 Subject: [PATCH] changes --- luprex/core/lua/horps.lua | 84 +++++++++++++++++++++------------ luprex/core/lua/prng.lua | 7 +++ luprex/core/lua/uglyglobals.lua | 20 ++++++++ 3 files changed, 81 insertions(+), 30 deletions(-) create mode 100644 luprex/core/lua/prng.lua create mode 100644 luprex/core/lua/uglyglobals.lua diff --git a/luprex/core/lua/horps.lua b/luprex/core/lua/horps.lua index 91915927..0020bf0c 100644 --- a/luprex/core/lua/horps.lua +++ b/luprex/core/lua/horps.lua @@ -1,6 +1,7 @@ makeclass('player') makeclass('army') + -- -- HORPS game: Walk around the board collecting armies and buffs -- Captured armies eat each turn @@ -71,11 +72,33 @@ function shuffle(t) return s end +function qbuildarmy(x,y) + t={class='army',x=x,y=y,z=0,plane='main',graphic='army'} + local nt=tangible.build(t) + nt.Count={} + setmetatable(nt.Count,NilIsZero) + nt.Count['r']=1 + return nt + end + +function MakeMap2() + for i=-2,2 do + qbuildarmy( i, 3) + qbuildarmy( i,-3) + qbuildarmy(-3, i) + qbuildarmy( 3, i) + end + qbuildarmy(-3, 3) + qbuildarmy( 3,-3) + qbuildarmy(-3,-3) + qbuildarmy( 3, 3) + end + function MakeMap() local rad=6 for x=-rad,rad do for y=-rad,rad do if math.random(1,5)==1 then t={class='army',x=x,y=y,z=0,plane='main',graphic='army'} - print("Building an army:") +-- print("Building an army:") -- pprint(t) local nt=tangible.build(t) nt.Count={} @@ -84,7 +107,7 @@ function MakeMap() local type=armytypes[math.random(1,#armytypes)] nt.Count[type]=nt.Count[type]+math.random(1,3) end end - print("Here's what was built:") +-- print("Here's what was built:") -- pprint(nt) end end end end @@ -93,6 +116,7 @@ function MakeMap() -- For each creature type, select the optimal target. Select randomly among identical targets. -- function army.fight0(ak,ac,dk,dc,rules) -- returns number of attacker casualties, defender casualties + if not rules then rules={} end local adv0=advantage[ak][dk] local adv1=advantage[dk][ak] if rules.counterattack=='one' then adv1=1 @@ -104,17 +128,15 @@ function army.fight0(ak,ac,dk,dc,rules) -- returns number of attacker casualties return rval0,rval1 end -function army.cb_fight(actor,place,dialog) - for ak,ac in pairs(actor.Count) do -- Should randomize the order +function player:fight(enemy) + for ak,ac in pairs(self.Count) do -- Should randomize the order local enemyk, enemyc local acas local maxcas=-1 local Loss0,Loss1 local whichdk, whichdc - for dk,dc in pairs(place.Count) do - local rem0,rem1=army.fight0(ak,ac,dk,dc) - Loss0=ac-rem0 - Loss1=dc-rem1 + 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>maxcas then enemyk=dk @@ -144,9 +166,8 @@ function player:droparmy(actor,kind) end function player:newlocation() - local lis=tangible.near(self,0,false,false) --- print("New Location") --- pprint(lis) + local lis=tangible.near(self,0,true,true) + for _,t in ipairs(lis) do self:fight(t) end end function player:printanimstate() @@ -156,21 +177,25 @@ function player:printanimstate() function player:cb_north() tangible.animate(self, {action="walk", dy=1}) + self:cb_map() self:newlocation() end -function player:cb_south(place, dialog) - tangible.animate(place, {action="walk", dy=-1}) +function player:cb_south() + tangible.animate(self, {action="walk", dy=-1}) + self:cb_map() self:newlocation() end -function player.cb_east(actor, place, dialog) - tangible.animate(actor, {action="walk", dx=1}) +function player:cb_east() + tangible.animate(self, {action="walk", dx=1}) + self:cb_map() self:newlocation() end -function player:cb_west(actor, place, dialog) - tangible.animate(actor, {action="walk", dx=-1}) +function player:cb_west() + tangible.animate(self, {action="walk", dx=-1}) + self:cb_map() self:newlocation() end @@ -210,16 +235,17 @@ function num2(a) if a<=9 then return " "..a else return a end end function player.cb_map(actor,place,dialog) local rad=4 - scratch={} + local ax,ay=tangible.xyz(actor) + local scratch={} local lis=tangible.near(actor,1.5*rad,true,false) - print("\27[38;5;9mMap:\27[0m") + print("\27c\27[38;5;9mMap:\27[0m") for _,t in pairs(lis) do - local graphic,plane,x,y,z,facing = tangible.animstate(t) +-- local graphic,plane,x,y,z,facing = tangible.animstate(t) local c=tangible.getclass(t) local dx,dy=tangible.xyz(t) - local offset=(dy+rad)*(rad*2+1)+dx+rad + local offset=(-dy+ay+rad)*(rad*2+1)+dx-ax+rad local cl=tangible.getclass(t) - if cl=='player' or cl=='army' then + 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 @@ -235,7 +261,7 @@ function player.cb_map(actor,place,dialog) end -- pprint(scratch) for dy=-rad,rad do for line=0,1 do - local lbuf="" + local lbuf="|" for dx=-rad,rad do local offset=(dy+rad)*(rad*2+1)+dx+rad if line==0 then lbuf=lbuf.."----" @@ -251,13 +277,11 @@ function player.cb_map(actor,place,dialog) 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 +-- 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 +-- for k2,v2 in pairs(v.Count) do print(" "..k2.." "..v2) end +-- end end - - diff --git a/luprex/core/lua/prng.lua b/luprex/core/lua/prng.lua new file mode 100644 index 00000000..b0bd98dd --- /dev/null +++ b/luprex/core/lua/prng.lua @@ -0,0 +1,7 @@ +makeclass('prng') + +function prng.seed(n) + ug.set(prngseed,n) + end + +function \ No newline at end of file diff --git a/luprex/core/lua/uglyglobals.lua b/luprex/core/lua/uglyglobals.lua new file mode 100644 index 00000000..a6a6d2d5 --- /dev/null +++ b/luprex/core/lua/uglyglobals.lua @@ -0,0 +1,20 @@ +makeclass('ug') + +function ug.the() + return tangible.scan('globals',0,0,0,true)[1] + end + +lis=tangible.scan('globals',0,0,0,true) +if #lis==0 then + local ugid=tangible.build{class='ug',x=0,y=0,z=0,plane='globals',graphic='box'} + print("The global table is "..tangible.id(ugid)) + end + +function ug.set(var,val) + ug.the()[var]=val + end + +function ug.get(var) + return ug.the()[var] + end +