فهرست منبع

Lag compensation rough draft; Slot fix;

Bjorn 11 سال پیش
والد
کامیت
92d657035a
5فایلهای تغییر یافته به همراه45 افزوده شده و 19 حذف شده
  1. 2 0
      lib/player.lua
  2. 2 3
      lib/playerdummy.lua
  3. 1 1
      lib/playermain.lua
  4. 1 1
      lib/players.lua
  5. 39 14
      lib/playerserver.lua

+ 2 - 0
lib/player.lua

@@ -25,6 +25,8 @@ Player.collision = {
 -- Core
 ----------------
 function Player:init()
+  self.meta = {__index = self}
+  
   self.id = nil
   self.username = ''
   self.class = nil

+ 2 - 3
lib/playerdummy.lua

@@ -4,7 +4,6 @@ local function drawTick() return tick - (interp / tickRate) end
 
 function PlayerDummy:activate()
   self.history = {}
-  self.historyMeta = {__index = self}
 
   Player.activate(self)
 end
@@ -23,7 +22,7 @@ function PlayerDummy:get(t)
       y = self.y,
       angle = self.angle,
       tick = tick,
-    }, self.historyMeta)
+    }, self.meta)
   end
 
   while self.history[1].tick < tick - 1 / tickRate and #self.history > 2 do
@@ -61,7 +60,7 @@ function PlayerDummy:trace(data)
     y = data.y,
     angle = data.angle,
     tick = data.tick
-  }, self.historyMeta))
+  }, self.meta))
 
   self.x, self.y, self.angle = data.x, data.y, data.angle
   self.health, self.shield = data.health or self.health, data.shield or self.shield

+ 1 - 1
lib/playermain.lua

@@ -1,7 +1,7 @@
 PlayerMain = extend(Player)
 
 function PlayerMain:activate()
-  self.prev = setmetatable({}, {__index = self})
+  self.prev = setmetatable({}, self.meta)
   self.inputs = {}
 
   self.alpha = 1

+ 1 - 1
lib/players.lua

@@ -19,7 +19,7 @@ function Players:init()
   end)
   
   ctx.event:on(evtFire, function(data)
-    if data.id ~= ctx.id then
+    if ctx.id and data.id ~= ctx.id then
       local p = self:get(data.id)
       local slot = p.slots[data.slot]
       slot:fire(p)

+ 39 - 14
lib/playerserver.lua

@@ -6,14 +6,30 @@ function PlayerServer:activate()
   self.lastHurt = tick
   self.hurtHistory = {}
   self.helpHistory = {}
-  
+
+  self.history = {}
+
   self.ack = tick
 
   Player.activate(self)
 end
 
 function PlayerServer:get(t)
-  return self
+  if not self.history then return self end
+
+  while self.history[1] and self.history[1].tick < tick - 2 / tickRate do
+    table.remove(self.history, 1)
+  end
+
+  if #self.history == 0 then return self end
+
+  if self.history[#self.history].tick < t then return self end
+
+  for i = #self.history, 1, -1 do
+    if self.history[i].tick <= t then return self.history[i] end
+  end
+  
+  return self.history[1]
 end
 
 function PlayerServer:update()
@@ -32,22 +48,25 @@ function PlayerServer:update()
 end
 
 function PlayerServer:trace(data, ping)
-  local rewindTo = data.tick - ((ping / 1000) + interp) / tickRate
   if data.tick > self.ack then
     
     -- Lag compensation
-    --[[local oldPos = {}
+    local target = data.tick - ((ping / 1000) + interp) / tickRate
+    local t1 = math.floor(target)
+    local factor = target - t1
+    local oldPos = {}
     ctx.players:each(function(p)
       if p.id ~= self.id then
         oldPos[p.id] = {p.x, p.y}
-        local lerpd = ctx.players:get(p.id, rewindTo)
-        if lerpd then
-          p.x = lerpd.x
-          p.y = lerpd.y
-          p.shape:moveTo(p.x, p.y)
-        end
+        local s1, s2 = p:get(t1), p:get(t1 + 1)
+        s1 = {x = s1.x, y = s1.y}
+        s2 = {x = s2.x, y = s2.y}
+        local lerpd = table.interpolate(s1, s2, factor)
+        p.x = lerpd.x
+        p.y = lerpd.y
+        ctx.event:emit('collision.move', {object = p, x = p.x, y = p.y})
       end
-    end)]]
+    end)
 
     self.ack = data.tick
     
@@ -57,6 +76,12 @@ function PlayerServer:trace(data, ping)
       self:slot(data)
     end
 
+    table.insert(self.history, setmetatable({
+      x = self.x,
+      y = self.y,
+      tick = data.tick
+    }, self.meta))
+
     -- sync
     local msg = {}
     msg.x = math.round(self.x)
@@ -79,12 +104,12 @@ function PlayerServer:trace(data, ping)
     ctx.net:emit(evtSync, msg)
 
     -- Undo lag compensation
-    --[[ctx.players:each(function(p)
+    ctx.players:each(function(p)
       if oldPos[p.id] then
         p.x, p.y = unpack(oldPos[p.id])
-        p.shape:moveTo(p.x, p.y)
+        ctx.event:emit('collision.move', {object = p, x = p.x, y = p.y})
       end
-    end)]]
+    end)
   end
 end