From 93e1ab63b932ef2198541319b56396575dcfbdce Mon Sep 17 00:00:00 2001 From: "teppy@egenesis.com" Date: Wed, 23 Mar 2022 17:03:07 -0400 Subject: [PATCH] changes --- luprex/core/cpp/lpxclient.cpp | 2 +- luprex/core/lua/control.lst | 1 + luprex/core/lua/horps.lua | 78 ++++++++++++++++++++++++----------- luprex/core/lua/login.lua | 3 +- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/luprex/core/cpp/lpxclient.cpp b/luprex/core/cpp/lpxclient.cpp index a87da0a3..9385aa9e 100644 --- a/luprex/core/cpp/lpxclient.cpp +++ b/luprex/core/cpp/lpxclient.cpp @@ -203,7 +203,7 @@ public: void receive_diff_from_server(StreamBuffer *sb) { world_to_synchronous(); try { - DebugCollector dbc(""); + DebugCollector dbc(""); int64_t nactor = world_->patch_everything(sb, &dbc); if (nactor != actor_id_) change_actor_id(nactor); dbc.dump(stdostream()); diff --git a/luprex/core/lua/control.lst b/luprex/core/lua/control.lst index 1e392a1f..d4e9d236 100644 --- a/luprex/core/lua/control.lst +++ b/luprex/core/lua/control.lst @@ -6,6 +6,7 @@ ut-table.lua ut-globaldb.lua ut-tablecmp.lua +basics.lua login.lua horps.lua diff --git a/luprex/core/lua/horps.lua b/luprex/core/lua/horps.lua index 2254a6ad..5389b91a 100644 --- a/luprex/core/lua/horps.lua +++ b/luprex/core/lua/horps.lua @@ -13,6 +13,7 @@ makeclass('army') -- -- Data Structure -- Army.Count[kind]=number Where kind is r,p,s +-- Player.Count[kind]=number Where kind is r,p,s -- function player.interface(actor, place) @@ -70,13 +71,41 @@ function shuffle(t) return s end +NilIsZero={ + __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 } + +function MakeMap() + local rad=2 + for x=-rad,rad do for y=-rad,rad do + t={class='army',x=x,y=y,z=0,plane='main',graphic='army'} + print("Building an army:") + pprint(t) + local nt=tangible.build(t) + nt.Count={} + setmetatable(nt.Count,NilIsZero) + for i=1,#armytypes do if math.random(1,2)==1 then + 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:") + pprint(nt) + end end + end -- -- For each creature type, select the optimal target. Select randomly among identical targets. -- -function army.fight0(ak,ac,dk,dc) -- returns number of attacker casualties, defender casualties +function army.fight0(ak,ac,dk,dc,rules) -- returns number of attacker casualties, defender casualties local adv0=advantage[ak][dk] local adv1=advantage[dk][ak] + if rules.counterattack=='one' then adv1=1 + elseif rules.counterattack=='best' then adv1=adv1<1 and 1 or 1 + end local rval0=(ac*adv0-dc)/adv0 if rval0<0 then rval0=0 end local rval1=(dc*adv1-rval0)/adv1 @@ -113,7 +142,7 @@ 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) - if actor.Count[kind]<=0 then print("No armies of typem "..kind.." available") return end + if actor.Count[kind]<=0 then print("No armies of type "..kind.." available") return end local lis=tangible.near(actor,0,true,true) if #lis>1 then print("Multiple Tangibles Nearby") return end local t=lis[1] @@ -124,8 +153,8 @@ function player:droparmy(actor,kind) function player:newlocation() local lis=tangible.near(self,0,false,false) - print("New Location") - pprint(lis) +-- print("New Location") +-- pprint(lis) end function player:printanimstate() @@ -171,7 +200,7 @@ function player.cb_emit_army(actor,place,dialog) local nt=tangible.build(t) setmetatable(nt,{r=0,p=0,s=0}) nt.kind='r' - nt.count=1 + nt.Count=1 end function player.cb_emit_buff(actor,place,dialog) @@ -188,40 +217,41 @@ 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 player.cb_map(actor,place,dialog) - local rad=2 + local rad=1 scratch={} - local lis=tangible.near(actor,rad,true,false) - pprint("cb_map ",lis) + local lis=tangible.near(actor,1.5*rad,true,false) + print("\27[38;5;9mMap:\27[0m") for _,t in pairs(lis) do - if t~=actor then print(tangible.getclass(t)) end 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 cl=tangible.getclass(t) - local kind=t.kind or 'Nil' - if not scratch[offset] then scratch[offset]={} end - if not scratch[offset].count then scratch[offset].count={} end - if not scratch[offset].count[kind] then scratch[offset].count[kind]=0 end - if t.count then scratch[offset].count[kind]=scratch[offset].count[kind]+t.count end + if 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 - local kc=0 - local tc=0 - local kstr=' ' - if scratch[offset] then for k,c in pairs(scratch[offset].count) do if c>0 then - kc=kc+1 - tc=tc+c - if kc>1 then kstr='M' else kstr=k end - end end end - lbuf=lbuf.."-"..kstr..(tc>0 and num2(tc) or " ") + 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 - lbuf=lbuf.."-" print(lbuf) end end lbuf="" diff --git a/luprex/core/lua/login.lua b/luprex/core/lua/login.lua index e40f1956..255c8b25 100644 --- a/luprex/core/lua/login.lua +++ b/luprex/core/lua/login.lua @@ -8,7 +8,8 @@ end function login.cb_becomeplayer(actor, place, dialog) actor.kind='P' actor.Count={} - setmetatable(actor.Count,{jones=5,__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}) + actor.Count.Foobar=42 + setmetatable(actor.Count,NilIsZero) tangible.setclass(actor, player) tangible.animate(actor,{action="warp",plane="main",x=0,y=0,z=0}) end