preliminary portal gun mod

This commit is contained in:
Ben Russell (300178622) 2015-10-01 16:51:40 +13:00
parent ec715c4ee2
commit 300c11ff34
7 changed files with 1000 additions and 1 deletions

View File

@ -75,7 +75,7 @@ function trace_gap(x,y,z)
xlen,ylen,zlen = common.map_get_dims()
local l = common.map_pillar_get(math.floor(x), math.floor(z))
i = 1
local i = 1
local h1,h2
h1 = nil
while true do

View File

@ -0,0 +1,265 @@
--[[
This file is derived from code from Ice Lua Components.
Ice Lua Components is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Ice Lua Components is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Ice Lua Components. If not, see <http://www.gnu.org/licenses/>.
]]
local thisid = ...
if client then
weapon_models[thisid] = model_load({
kv6 = {
bdir = DIR_PKG_KV6,
name = "rifle.kv6",
scale = 1.0/128.0,
},
pmf = {
bdir = DIR_PKG_PMF,
name = "rifle.pmf",
},
}, {"kv6", "pmf"})
end
weapon_names[thisid] = "Portal Gun"
return function (plr)
local this = tpl_gun(plr, {
dmg = {
head = 0,
body = 0,
legs = 0,
},
block_damage = 0,
ammo_clip = 10,
ammo_reserve = 50,
time_fire = 1/3,
time_reload = 1/3,
recoil_x = 0.0001,
recoil_y = -0.05,
model = client and (weapon_models[thisid] {}),
name = "Rifle",
})
function this.reload()
end
local s_tick = this.tick
function this.tick(sec_current, sec_delta, ...)
this.ammo_clip = 10
return s_tick(sec_current, sec_delta, ...)
end
function this.textgen()
local cols
col = 0xFFC0C0C0
return col, "- -"
end
function this.click(button, state)
if button == 1 or button == 3 then
-- LMB
if this.ammo_clip > 0 then
if state then
this.portal_select = (button==1 and 1) or 2
end
this.firing = state
else
-- Shouldn't happen!
this.firing = false
client.wav_play_global(wav_pin, plr.x, plr.y, plr.z)
plr.reload_msg.visible = true
plr.reload_msg.static_alarm{name='reloadviz',
time=0.5, on_trigger=function() plr.reload_msg.visible = false end}
end
elseif button == 2 then
-- RMB
if hold_to_zoom then
plr.zooming = state and not this.reloading
else
if state and not this.reloading then
plr.zooming = not plr.zooming
end
end
end
end
function this.prv_fire(sec_current)
local xlen, ylen, zlen
xlen, ylen, zlen = common.map_get_dims()
if client then
client.wav_play_global(this.cfg.shot_sound, plr.x, plr.y, plr.z)
bcase_part_mdl = bcase_part_mdl or new_particle_model(250, 215, 0)
particles_add(new_particle{
x = plr.x,
y = plr.y,
z = plr.z,
vx = math.sin(plr.angy - math.pi / 4) / 2 + math.random() * 0.25,
vy = 0.1 + math.random() * 0.25,
vz = math.cos(plr.angy - math.pi / 4) / 2 + math.random() * 0.25,
model = bcase_part_mdl,
size = 8,
lifetime = 2
})
end
net_send(nil, common.net_pack("BBB", PKT_PLR_GUN_SHOT, 0, 1))
-- TODO: Better spread
-- spread
local angy = plr.angy + (this.cfg.spread * (math.random() - 0.5))
local angx = plr.angx + (this.cfg.spread * (math.random() - 0.5))
local sya = math.sin(angy)
local cya = math.cos(angy)
local sxa = math.sin(angx)
local cxa = math.cos(angx)
local fwx,fwy,fwz
fwx,fwy,fwz = sya*cxa, sxa, cya*cxa
-- tracer
if client then
tracer_add(plr.x, plr.y, plr.z, angy, angx)
end
-- perform a trace
portal_traces_enabled = false
local d,cx1,cy1,cz1,cx2,cy2,cz2
d,cx1,cy1,cz1,cx2,cy2,cz2
= trace_map_ray_dist(plr.x+sya*0.4,plr.y,plr.z+cya*0.4, fwx,fwy,fwz, this.cfg.range)
d = d or this.cfg.range
portal_traces_enabled = true
-- see if there's anyone we can kill
local hurt_idx = nil
local hurt_part = nil
local hurt_part_idx = 0
local hurt_dist = d*d
local i,j
for i=1,players.max do
local p = players[i]
if p and p ~= plr and p.alive then
local dx = p.x-plr.x
local dy = p.y-plr.y+0.1
local dz = p.z-plr.z
for j=1,3 do
local dot, dd = isect_line_sphere_delta(dx,dy,dz,fwx,fwy,fwz)
if dot and dot < 0.55 and dd < hurt_dist then
hurt_idx = i
hurt_dist = dd
hurt_part_idx = j
hurt_part = ({"head","body","legs"})[j]
break
end
dy = dy + 1.0
end
end
end
if hurt_idx then
if server then
--[[
players[hurt_idx].gun_damage(
hurt_part, this.cfg.dmg[hurt_part], plr)
]]
else
--[[
net_send(nil, common.net_pack("BBB"
, PKT_PLR_GUN_HIT, hurt_idx, hurt_part_idx))
]]
plr.show_hit()
end
else
if client then
--[[
net_send(nil, common.net_pack("BBB"
, PKT_PLR_GUN_HIT, 0, 0))
]]
end
if cx2 and cy2 <= ylen-2 and cx2 >= 0 and cx2 < xlen and cz2 >= 0 and cz2 < zlen then
print("Portal hit ("..cx2..", "..cy2..", "..cz2..") type "..this.portal_select)
local dx,dy,dz = cx2-cx1, cy2-cy1, cz2-cz1
dx = math.floor(dx + 0.5)
dy = math.floor(dy + 0.5)
dz = math.floor(dz + 0.5)
print("Direction ("..dx..", "..dy..", "..dz..")")
-- Check if valid...
local valid = true
-- CHECK: Ensure back is all solid, front is all clear
local cx = math.floor(cx2)
local cy = math.floor(cy2)
local cz = math.floor(cz2)
if valid then
local i,j,k
for i=-1,1 do
for j=-1,1 do
for k=-1,0 do
if not valid then break end
local x = cx+k*dx+i*dy+j*dz
local y = cy+k*dy+i*dz+j*dx
local z = cz+k*dz+i*dx+j*dy
if k == 0 then
if map_block_get(x,y,z) == nil then valid = false end
else
if map_block_get(x,y,z) ~= nil then valid = false end
end
end
end
end
end
if valid and dy == 0 and (dx ~= 0 or dz ~= 0) then
net_send(nil, common.net_pack("BBBhhhbbbbbb", PKT_PORTALGUN_SET,
0, this.portal_select, cx, cy, cz,
dx, dy, dz, 0, 1, 0))
elseif valid and dy ~= 0 and (dx == 0 and dz == 0) then
local sx, sz = 0, 0
if math.abs(fwx) > math.abs(fwz) then
sx = (fwx < 0 and -1) or 1
else
sz = (fwz < 0 and -1) or 1
end
net_send(nil, common.net_pack("BBBhhhbbbbbb", PKT_PORTALGUN_SET,
0, this.portal_select, cx, cy, cz,
dx, dy, dz, sx, 0, sz))
end
end
end -- if hurt_idx
-- apply recoil
-- attempting to emulate classic behaviour provided i have it right
plr.recoil(sec_current, this.cfg.recoil_y, this.cfg.recoil_x)
end
return this
end

View File

@ -0,0 +1,529 @@
--[[
This file is derived from code from Ice Lua Components.
Ice Lua Components is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Ice Lua Components is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Ice Lua Components. If not, see <http://www.gnu.org/licenses/>.
]]
local portal_transforms = {}
local portal_traces = {}
local portal_traces_mcache = {}
portal_traces_enabled = true
portal_transforms_performed = {}
local function trace_portal_get_transform(cx, cy, cz)
if not(portal_traces[cz] and portal_traces[cz][cx] and portal_traces[cz][cx][cy]) then
return nil
end
local tidx = portal_traces[cz][cx][cy]
local tf = portal_transforms[tidx]
return tf
end
function trace_portal_transform(tf, cx, cy, cz, vx, vy, vz)
print("ENTRY", cx, cy, cz, vx, vy, vz)
-- Get origins
local cx1 = tf[1][1] + 0.5 + tf[1][4]*0.5
local cy1 = tf[1][2] + 0.5 + tf[1][5]*0.5
local cz1 = tf[1][3] + 0.5 + tf[1][6]*0.5
local cx2 = tf[2][1] + 0.5 + tf[2][4]*0.5
local cy2 = tf[2][2] + 0.5 + tf[2][5]*0.5
local cz2 = tf[2][3] + 0.5 + tf[2][6]*0.5
-- Get normals
local nx1 = tf[1][4]
local ny1 = tf[1][5]
local nz1 = tf[1][6]
local nx2 = tf[2][4]
local ny2 = tf[2][5]
local nz2 = tf[2][6]
-- Get sky vectors
local sx1 = tf[1][7]
local sy1 = tf[1][8]
local sz1 = tf[1][9]
local sx2 = tf[2][7]
local sy2 = tf[2][8]
local sz2 = tf[2][9]
-- Get horiz vectors
local hx1 = ny1*sz1-nz1*sy1
local hy1 = nz1*sx1-nx1*sz1
local hz1 = nx1*sy1-ny1*sx1
local hx2 = ny2*sz2-nz2*sy2
local hy2 = nz2*sx2-nx2*sz2
local hz2 = nx2*sy2-ny2*sx2
-- Get offsets
local no1 = nx1*cx1 + ny1*cy1 + nz1*cz1
local no2 = nx2*cx2 + ny2*cy2 + nz2*cz2
local so1 = sx1*cx1 + sy1*cy1 + sz1*cz1
local so2 = sx2*cx2 + sy2*cy2 + sz2*cz2
local ho1 = hx1*cx1 + hy1*cy1 + hz1*cz1
local ho2 = hx2*cx2 + hy2*cy2 + hz2*cz2
print("NORMS", nx1, ny1, nz1, nx2, ny2, nz2, no1)
print("SKIES", sx1, sy1, sz1, sx2, sy2, sz2, so1)
print("HORIZ", hx1, hy1, hz1, hx2, hy2, hz2, ho1)
-- Get source offsets
local noP = nx1*cx + ny1*cy + nz1*cz - no1
local soP = sx1*cx + sy1*cy + sz1*cz - so1
local hoP = hx1*cx + hy1*cy + hz1*cz - ho1
-- Update position
cx = nx2*noP + sx2*soP + hx2*hoP + no2*nx2 + so2*sx2 + ho2*hx2
cy = ny2*noP + sy2*soP + hy2*hoP + no2*ny2 + so2*sy2 + ho2*hy2
cz = nz2*noP + sz2*soP + hz2*hoP + no2*nz2 + so2*sz2 + ho2*hz2
-- Get direction offsets
local noV = nx1*vx + ny1*vy + nz1*vz
local soV = sx1*vx + sy1*vy + sz1*vz
local hoV = hx1*vx + hy1*vy + hz1*vz
-- Update direction
vx = -(nx2*noV + sx2*soV + hx2*hoV)
vy = -(ny2*noV + sy2*soV + hy2*hoV)
vz = -(nz2*noV + sz2*soV + hz2*hoV)
-- Return!
print("EXIT ", cx, cy, cz, vx, vy, vz)
return cx, cy, cz, vx, vy, vz
end
local function trace_portal_set_mark(pid, portal_select, cx, cy, cz)
local l = portal_traces
if not l[cz] then l[cz] = {} end
if not l[cz][cx] then l[cz][cx] = {} end
l[cz][cx][cy] = pid*2+portal_select
--print(cx, cy, cz)
end
local function trace_portal_insert(p, pid, portal_select)
local cx, cy, cz = p[1], p[2], p[3]
local dx, dy, dz = p[4], p[5], p[6]
local sx, sy, sz = p[7], p[8], p[9]
local hx = dy*sz-dz*sy
local hy = dz*sx-dx*sz
local hz = dx*sy-dy*sx
-- Mark area
trace_portal_set_mark(pid, portal_select, cx , cy , cz )
trace_portal_set_mark(pid, portal_select, cx-sx, cy-sy, cz-sz)
trace_portal_set_mark(pid, portal_select, cx+sx, cy+sy, cz+sz)
trace_portal_set_mark(pid, portal_select, cx +hx, cy +hy, cz +hz)
trace_portal_set_mark(pid, portal_select, cx-sx+hx, cy-sy+hy, cz-sz+hz)
trace_portal_set_mark(pid, portal_select, cx+sx+hx, cy+sy+hy, cz+sz+hz)
trace_portal_set_mark(pid, portal_select, cx -hx, cy -hy, cz -hz)
trace_portal_set_mark(pid, portal_select, cx-sx-hx, cy-sy-hy, cz-sz-hz)
trace_portal_set_mark(pid, portal_select, cx+sx-hx, cy+sy-hy, cz+sz-hz)
end
local function trace_portal_setup()
local i
portal_traces = {}
portal_transforms = {}
if not portal_traces_enabled then return end
-- Gather portal info
for i=1,players.max do
local plr = players[i]
if plr then
local p1 = plr.portal_list[1]
local p2 = plr.portal_list[2]
if p1 and p2 then
-- Create transformation!
local t1 = {p1, p2}
local t2 = {p2, p1}
-- Save transformation!
portal_transforms[i*2+1] = t1
portal_transforms[i*2+2] = t2
-- TODO! (instead we're drilling the map)
-- Place marks which indicate the transformation thing!
trace_portal_insert(p1, i, 1)
trace_portal_insert(p2, i, 2)
end
end
end
end
local function map_pillar_get_fake(x, z)
-- Check if we have a portal list
if not (portal_traces[z] and portal_traces[z][x]) then
return common.map_pillar_get(x, z)
end
-- Check if we have a cache
if portal_traces_mcache[z] and portal_traces_mcache[z][x] then
return portal_traces_mcache[z][x]
end
--print("TRACES EXIST", x, z)
-- We have to edit because of this portal list
local l = common.map_pillar_get(x, z)
local pl = portal_traces[z][x]
-- Unpack
local ul = map_pillar_raw_unpack(l)
-- Replace things
local xlen,ylen,zlen
xlen,ylen,zlen = common.map_get_dims()
local i
for i=1,ylen do
if pl[i-1] then
--print("DELETE", x, i, z)
ul[i] = nil
end
end
-- Repack
l = map_pillar_raw_pack(ul)
-- Dump this into the cache
if not portal_traces_mcache[z] then portal_traces_mcache[z] = {} end
portal_traces_mcache[z][x] = l
-- Return
return l
end
-- need to copy-paste these functions from lib_vector.lua in order to mangle them
-- Map helpers
function trace_gap(x,y,z)
local xlen,ylen,zlen
xlen,ylen,zlen = common.map_get_dims()
local l = map_pillar_get_fake(math.floor(x), math.floor(z))
local i = 1
local h1,h2
h1 = nil
while true do
h2 = l[i+1]
if h2 == ylen-1 then h2 = ylen end
if y < l[i+1] or l[i] == 0 then return h1, h2 end
i = i + l[i]*4
if y < l[i+3] then return h1, h2 end
h1 = l[i+3]
h2 = l[i+1]
end
end
function box_is_clear(x1,y1,z1,x2,y2,z2,canwrap)
local x,z,i
x1 = math.floor(x1)
y1 = math.floor(y1)
z1 = math.floor(z1)
x2 = math.floor(x2)
y2 = math.floor(y2)
z2 = math.floor(z2)
local xlen,ylen,zlen
xlen,ylen,zlen = common.map_get_dims()
if not canwrap then
if x1 < 0 or z1 < 0 then
return false
elseif x2 >= xlen or z2 >= zlen then
return false
end
end
for z=z1,z2 do
for x=x1,x2 do
local l = map_pillar_get_fake(x, z)
i = 1
while true do
if l[i+1] == ylen-1 and y2 < ylen then break end
if y2 < l[i+1] then break end
if l[i] == 0 then return false end
i = i + l[i]*4
if y1 < l[i+3] then return false end
end
end
end
return true
end
-- Ray tracing
function trace_map_ray_dist(x1,y1,z1, vx,vy,vz, maxdist, nil_on_maxdist)
if nil_on_maxdist == nil then nil_on_maxdist = true end
local function depsilon(d)
if d < 0.0000001 then
return 0.0000001
else
return d
end
end
local xlen,ylen,zlen
xlen,ylen,zlen = common.map_get_dims()
-- direction
local gx,gy,gz
if vx < 0 then gx = -1 else gx = 1 end
if vy < 0 then gy = -1 else gy = 1 end
if vz < 0 then gz = -1 else gz = 1 end
vx = vx * gx
vy = vy * gy
vz = vz * gz
-- cell
local cx,cy,cz
if not x1 then return nil end
cx = math.floor(x1)
cy = math.floor(y1)
cz = math.floor(z1)
-- subpos
local sx,sy,sz
sx = x1-cx
sy = y1-cy
sz = z1-cz
if gx >= 0 then sx = 1-sx end
if gy >= 0 then sy = 1-sy end
if gz >= 0 then sz = 1-sz end
local dist = 0
local pillar, npillar
npillar = map_pillar_get_fake(cx,cz)
pillar = npillar
trace_portal_setup() -- Have to do this!
while true do
local tx = sx/depsilon(vx)
local ty = sy/depsilon(vy)
local tz = sz/depsilon(vz)
local t,d
local ncx,ncy,ncz
ncx,ncy,ncz = cx,cy,cz
if tx < ty and tx < tz then
t = tx
d = 0
ncx = cx + gx
npillar = map_pillar_get_fake(ncx,ncz)
elseif ty < tx and ty < tz then
t = ty
d = 1
ncy = cy + gy
else
t = tz
d = 2
ncz = cz + gz
npillar = map_pillar_get_fake(ncx,ncz)
end
dist = dist + t
if dist > maxdist and nil_on_maxdist then
return nil, nil, nil, nil, nil, nil, nil
elseif dist > maxdist and not nil_on_maxdist then
return dist, cx, cy, cz, ncx, ncy, ncz
end
local i=1
while true do
if ncy < npillar[i+1] then break end
if npillar[i] == 0 then return dist, cx, cy, cz, ncx, ncy, ncz end
i = i + npillar[i]*4
if ncy < npillar[i+3] then return dist, cx, cy, cz, ncx, ncy, ncz end
end
sx = sx - vx*t
sy = sy - vy*t
sz = sz - vz*t
cx,cy,cz = ncx,ncy,ncz
if d == 0 then sx = 1
elseif d == 1 then sy = 1
else sz = 1 end
pillar = npillar
end
end
function trace_map_box(x1,y1,z1, x2,y2,z2, bx1,by1,bz1, bx2,by2,bz2, canwrap)
local function depsilon(d)
if d < 0.0000001 then
return 0.0000001
else
return d
end
end
-- delta
local dx,dy,dz
dx = x2-x1
dy = y2-y1
dz = z2-z1
-- offsets
local fx,fy,fz
if dx < 0 then fx = bx1 else fx = bx2 end
if dy < 0 then fy = by1 else fy = by2 end
if dz < 0 then fz = bz1 else fz = bz2 end
-- direction
local gx,gy,gz
if dx < 0 then gx = -1 else gx = 1 end
if dy < 0 then gy = -1 else gy = 1 end
if dz < 0 then gz = -1 else gz = 1 end
dx = dx * gx
dy = dy * gy
dz = dz * gz
-- combined box size
local bcx,bcy,bcz
bcx = (bx2-bx1)
bcy = (by2-by1)
bcz = (bz2-bz1)
-- top left offset (note, incorrect name!)
local tlx,tly,tlz
if gx >= 0 then tlx = 0.999 else tlx = 0.001 end
if gy >= 0 then tly = 0.999 else tly = 0.001 end
if gz >= 0 then tlz = 0.999 else tlz = 0.001 end
-- apply offset
x1 = x1 + fx
y1 = y1 + fy
z1 = z1 + fz
bx1 = bx1 - fx
by1 = by1 - fy
bz1 = bz1 - fz
bx2 = bx2 - fx
by2 = by2 - fy
bz2 = bz2 - fz
-- cell
local cx,cy,cz
cx = math.floor(x1)
cy = math.floor(y1)
cz = math.floor(z1)
-- target cell
local tcx,tcy,tcz
tcx = math.floor(x2+fx+gx*0.002)
tcy = math.floor(y2+fy+gy*0.002)
tcz = math.floor(z2+fz+gz*0.002)
-- sub deltas
local sx, sy, sz
sx = (x1 % 1.0) - 0.001
sy = (y1 % 1.0) - 0.001
sz = (z1 % 1.0) - 0.001
if gx >= 0 then sx = 1-sx end
if gy >= 0 then sy = 1-sy end
if gz >= 0 then sz = 1-sz end
-- restricted x/y/z
local rx,ry,rz
rx = nil
ry = nil
rz = nil
trace_portal_setup() -- Have to do this!
-- TODO: unset these when another boundary is crossed
local i
local iend = (
math.abs(tcx-cx)
+ math.abs(tcy-cy)
+ math.abs(tcz-cz)
)
portal_transforms_performed = {}
for i=1,iend do
-- get the time it takes to hit the boundary
local tx = sx/depsilon(dx)
local ty = sy/depsilon(dy)
local tz = sz/depsilon(dz)
local t, d, ck
if tx < ty and tx < tz then
-- X first
d = 0
t = tx
elseif ty < tx and ty < tz then
-- Y first
d = 1
t = ty
else
-- Z first
d = 2
t = tz
end
sx = sx - t*dx
sy = sy - t*dy
sz = sz - t*dz
x1 = rx or x1 + t*dx*gx
y1 = ry or y1 + t*dy*gy
z1 = rz or z1 + t*dz*gz
if d == 0 then
-- X first
sx = 1.0
ck = rx or box_is_clear(cx+gx,y1+by1,z1+bz1,cx+gx,y1+by2,z1+bz2,canwrap)
if not ck then rx = cx + tlx end
if not rx then cx = cx + gx end
elseif d == 1 then
-- Y first
sy = 1.0
ck = ry or box_is_clear(x1+bx1,cy+gy,z1+bz1,x1+bx2,cy+gy,z1+bz2,canwrap)
if not ck then ry = cy + tly end
if not ry then cy = cy + gy end
else
-- Z first
sz = 1.0
ck = rz or box_is_clear(x1+bx1,y1+by1,cz+gz,x1+bx2,y1+by2,cz+gz,canwrap)
if not ck then rz = cz + tlz end
if not rz then cz = cz + gz end
end
--if not ck then return x1-bx1, y1-by1, z1-bz1 end
local tf = trace_portal_get_transform(cx, cy, cz)
if tf then
table.insert(portal_transforms_performed, tf)
cx, cy, cz, dx, dy, dz = trace_portal_transform(tf, cx, cy, cz, dx, dy, dz)
x1, y1, z1, gx, gy, gz = trace_portal_transform(tf, x1, y1, z1, gx, gy, gz)
x2, y2, z2 = trace_portal_transform(tf, x2, y2, z2, 0, 0, 0)
if dx < 0 then sx = 1.0 - sx; dx = -dx end
if dy < 0 then sy = 1.0 - sy; dy = -dy end
if dz < 0 then sz = 1.0 - sz; dz = -dz end
end
end
--
if rx then rx = rx - fx end
if ry then ry = ry - fy end
if rz then rz = rz - fz end
return rx or x2, ry or y2, rz or z2
end

View File

@ -0,0 +1,117 @@
do
local s_new_player = new_player
print(new_player)
function new_player(settings, ...)
local this = s_new_player(settings, ...)
this.portal_list = {}
this.portal_list_va = {}
local s_calc_motion_trace = this.calc_motion_trace
function this.calc_motion_trace(sec_current, sec_delta, ox, oy, oz, nx, ny, nz, ...)
local tx1, ty1, tz1 = s_calc_motion_trace(sec_current, sec_delta, ox, oy, oz, nx, ny, nz, ...)
if #portal_transforms_performed == 0 then
return tx1, ty1, tz1
end
local k, v, _
for k, v in pairs(portal_transforms_performed) do
-- Apply to velocity
_, _, _, this.vx, this.vy, this.vz = trace_portal_transform(
v, 0, 0, 0, this.vx, this.vy, this.vz)
-- Get camera direction
local sya = math.sin(this.angy)
local cya = math.cos(this.angy)
local sxa = math.sin(this.angx)
local cxa = math.cos(this.angx)
local fwx,fwy,fwz = sya*cxa, sxa, cya*cxa
-- Apply to camera direction
_, _, _, fwx, fwy, fwz = trace_portal_transform(
v, 0, 0, 0, fwx, fwy, fwz)
-- Set camera direction
-- TODO: angx
this.angy = math.atan2(fwx, fwz)
end
return tx1, ty1, tz1
end
local function render_portals()
local i
for i=1,2 do
local p = this.portal_list[i]
if p then
local cx, cy, cz = p[1], p[2], p[3]
local dx, dy, dz = p[4], p[5], p[6]
local sx, sy, sz = p[7], p[8], p[9]
--print("PORTAL", cx, cy, cz, dx, dy, dz, sx, sy, sz, i)
if not p.va then
local hx = dy*sz-dz*sy
local hy = dz*sx-dx*sz
local hz = dx*sy-dy*sx
local cr,cg,cb
if i == 1 then
cr,cg,cb = 0,0.5,1
else
cr,cg,cb = 1,0.5,0
end
cx = cx + 0.5
cy = cy + 0.5
cz = cz + 0.5
local doffs = 0.5+0.02
local rh = 1.5-0.12
local rs = 1.5-0.12
local x1 = cx - doffs*dx - rh*hx - rs*sx
local y1 = cy - doffs*dy - rh*hy - rs*sy
local z1 = cz - doffs*dz - rh*hz - rs*sz
local x2 = cx - doffs*dx + rh*hx - rs*sx
local y2 = cy - doffs*dy + rh*hy - rs*sy
local z2 = cz - doffs*dz + rh*hz - rs*sz
local x3 = cx - doffs*dx - rh*hx + rs*sx
local y3 = cy - doffs*dy - rh*hy + rs*sy
local z3 = cz - doffs*dz - rh*hz + rs*sz
local x4 = cx - doffs*dx + rh*hx + rs*sx
local y4 = cy - doffs*dy + rh*hy + rs*sy
local z4 = cz - doffs*dz + rh*hz + rs*sz
p.va = common.va_make({
{x1,y1,z1,cr,cg,cb},
{x3,y3,z3,cr,cg,cb},
{x2,y2,z2,cr,cg,cb},
{x4,y4,z4,cr,cg,cb},
{x2,y2,z2,cr,cg,cb},
{x3,y3,z3,cr,cg,cb},
}, this.portal_list_va[i], "3v,3c")
this.portal_list_va[i] = p.va
end
client.va_render_global(p.va, 0, 0, 0, 0, 0, 0, 1)
end
end
end
local s_render = this.render
function this.render(...)
local ret = s_render(...)
render_portals()
return ret
end
return this
end
end

72
pkg/gm/portalgun/main.lua Normal file
View File

@ -0,0 +1,72 @@
PKT_PORTALGUN_SET = network.sys_alloc_packet()
DIR_PORTALGUN = "pkg/gm/portalgun/"
WPN_PORTALGUN = weapon_add(DIR_PORTALGUN.."/gun_portal.lua")
weapons_enabled[WPN_PORTALGUN] = true
network.sys_handle_c2s(PKT_PORTALGUN_SET, "BBhhhbbbbbb", nwdec_plrset(
function (neth, cli, plr, sec_current, pid,
portal_select, cx, cy, cz, dx, dy, dz, sx, sy, sz)
print("RECEIVED")
if not (plr and plr.has_permission("build")) then return end
print("Got player")
if portal_select ~= 1 and portal_select ~= 2 then return end
print("Got valid portal")
local xlen,ylen,zlen
xlen,ylen,zlen = common.map_get_dims()
if not (cx >= 0 and cx < xlen and cz >= 0 and cz < zlen and cy >= 0 and cy < ylen) then return end
print("Got valid coords")
if dx == 0 and dy == 0 and dz == 0 then
print("SERVER DELETE PORTAL "..portal_select)
plr.portal_list[portal_select] = nil
net_broadcast(nil, common.net_pack("BBBhhhbbbbbb",
PKT_PORTALGUN_SET, cli.plrid, portal_select,
cx, cy, cz, dx, dy, dz, sx, sy, sz))
else
print("SERVER CREATE PORTAL "..portal_select.." AT ("..cx..", "..cy..", "..cz..")")
plr.portal_list[portal_select] = {cx, cy, cz, dx, dy, dz, sx, sy, sz}
net_broadcast(nil, common.net_pack("BBBhhhbbbbbb",
PKT_PORTALGUN_SET, cli.plrid, portal_select,
cx, cy, cz, dx, dy, dz, sx, sy, sz))
end
end))
network.sys_handle_s2c(PKT_PORTALGUN_SET, "BBhhhbbbbbb",
function (neth, cli, plr, sec_current, pid,
portal_select, cx, cy, cz, dx, dy, dz, sx, sy, sz)
print("CLIENT RECEIVED", pid)
local plr = players[pid]
if not plr then return end
print("Got player")
if portal_select ~= 1 and portal_select ~= 2 then return end
print("Got valid portal")
local xlen,ylen,zlen
xlen,ylen,zlen = common.map_get_dims()
if not (cx >= 0 and cx < xlen and cz >= 0 and cz < zlen and cy >= 0 and cy < ylen) then return end
print("Got valid coords")
if dx == 0 and dy == 0 and dz == 0 then
print("CLIENT DELETE PORTAL "..pid..":"..portal_select)
plr.portal_list[portal_select] = nil
else
print("CLIENT CREATE PORTAL "..pid..":"..portal_select.." AT ("..cx..", "..cy..", "..cz..")")
plr.portal_list[portal_select] = {cx, cy, cz, dx, dy, dz, sx, sy, sz}
end
end)
if server then
-- TODO more elegant method
local s_slot_add = slot_add
function slot_add(neth, tidx, wpn, name, ...)
wpn = WPN_PORTALGUN
return s_slot_add(neth, tidx, wpn, name, ...)
end
end

View File

@ -0,0 +1,4 @@
{
"preload": ["preload.lua"],
"load": ["main.lua"],
}

View File

@ -0,0 +1,12 @@
do
local s_dofile = dofile
function dofile(fname)
s_dofile(fname)
if fname == "pkg/base/common.lua" then
-- can't grab libs via dofile, so use the next dofile on the list
dofile("pkg/gm/portalgun/hook_player.lua")
dofile("pkg/gm/portalgun/hook_map.lua")
end
end
end