test / test (push) Successful in 1m42s
New setting strata_world_size: small, medium (default) or large (2000, 3200 or 4200 columns) or infinite. The size is fixed when a world is created and kept in mod storage; worlds made before this (they already have env_meta.txt) stay infinite. - Past |x| = half every plane cell is strata_core:world_edge, an invisible, walkable, undiggable wall. is_protected refuses cells past it and creatures don't spawn there. - The last 220 columns are the ocean biome: land eases to a sand beach, then the sea floor falls to y = -17 (still the surface layer). Sea water fills to SEA_LEVEL only past the beach, so it is held in and doesn't flow when woken. - mg.set_world_size, mg.in_world, mg.ocean_t for other systems and tests. Tests: 3 new cases in tests/world_size.lua (the stored size, the edge, beach and sea shape in every size, and a generated shore that stays at rest, with reach refused past the edge). The test world runs infinite. 106/106 pass, no ERROR lines. A probe server confirmed new worlds get medium, keep it across restarts, and older worlds stay infinite. Closes #23 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
724 lines
23 KiB
Lua
724 lines
23 KiB
Lua
-- Strata player: side-view camera rig and avatar.
|
|
--
|
|
-- Luanti has no server-side way to place the camera freely, so each player is
|
|
-- attached to an invisible camera entity CAMERA_DIST nodes in front of an
|
|
-- "avatar" entity that lives on the gameplay plane. The camera entity trails
|
|
-- the avatar with a deadzone and look-ahead. The player's movement keys drive
|
|
-- the avatar; the player's own (hidden) body never moves on its own. The mouse
|
|
-- pans the view a little so the crosshair can aim at blocks near the avatar.
|
|
|
|
local PLANE_Z = strata.PLANE_Z
|
|
local CAMERA_DIST = strata.CAMERA_DIST
|
|
local REACH = strata.REACH
|
|
|
|
local WALK_SPEED = 6
|
|
local GROUND_ACCEL = 45
|
|
local AIR_ACCEL = 25
|
|
local GRAVITY = 22
|
|
local JUMP_SPEED = 10.5
|
|
local JUMP_CUT = 0.45 -- velocity kept when jump is released early
|
|
local FALL_SAFE = 15 -- landing speed that starts to hurt
|
|
local FALL_DMG = 4 -- HP per node/s above FALL_SAFE
|
|
local MAX_HP = 100
|
|
local DROP_TIME = 0.25 -- seconds platforms are ignored after pressing S
|
|
local DROP_SPEED = 2 -- initial downward speed when dropping through
|
|
local EYE_HEIGHT = 1.625 -- default player eye height
|
|
local AIM_TURN_MIN = 0.3 -- nodes the aim must be off-centre to turn the avatar
|
|
|
|
-- Camera follow. Set strata_smooth_camera = false for the old rigid camera.
|
|
local SMOOTH_CAMERA = core.settings:get_bool("strata_smooth_camera", true)
|
|
local FOLLOW_RATE = 9 -- 1/s; how quickly the camera closes on its goal
|
|
local LOOK_AHEAD = 1.5 -- nodes the view leads a walking avatar
|
|
local DEADZONE_UP = 2.75 -- rise in mid-air that doesn't move the view (above a full jump)
|
|
local DEADZONE_DOWN = 1 -- drop in mid-air before the view follows
|
|
local CAMERA_SNAP_DIST = 14 -- further than this from its goal, the camera jumps there
|
|
local CAMERA_Y_OFFSET = 0.3 - EYE_HEIGHT -- the client adds the eye height back
|
|
local CAMERA_MAX_OFFSET = 3.25 -- most the avatar sits off-centre when walking and jumping
|
|
-- Pan limit: must still reach REACH around an avatar that is off-centre.
|
|
local LOOK_LIMIT = math.atan((REACH + CAMERA_MAX_OFFSET) / CAMERA_DIST)
|
|
|
|
-- Teleport/respawn/join: hide the screen while the destination is emerged
|
|
-- and its visible mapblocks are pushed to the client, then fade back in.
|
|
local VIEW_HALF_W = 30 -- nodes visible either side of the avatar (with panning)
|
|
local VIEW_HALF_H = 20 -- nodes visible above/below the avatar
|
|
local FADE_TEXTURE = "[fill:1x1:#000000"
|
|
local FADE_Z = 1000 -- above every other HUD element
|
|
local FADE_HOLD = 0.3 -- seconds of black after the blocks are sent
|
|
local FADE_OUT_TIME = 0.3 -- seconds to fade back in
|
|
local FADE_OUT_STEPS = 5
|
|
local RESPAWN_RETRY = 0.5 -- seconds before retrying a failed avatar spawn
|
|
|
|
dofile(core.get_modpath("strata_player") .. "/appearance.lua")
|
|
dofile(core.get_modpath("strata_player") .. "/swim.lua")
|
|
|
|
local avatars = {}
|
|
strata.avatars = avatars
|
|
|
|
strata.camera = {
|
|
LOOK_LIMIT = LOOK_LIMIT,
|
|
MAX_OFFSET = CAMERA_MAX_OFFSET,
|
|
LOOK_AHEAD = LOOK_AHEAD,
|
|
DEADZONE_UP = DEADZONE_UP,
|
|
DEADZONE_DOWN = DEADZONE_DOWN,
|
|
SNAP_DIST = CAMERA_SNAP_DIST,
|
|
JUMP_APEX = JUMP_SPEED * JUMP_SPEED / (2 * GRAVITY),
|
|
}
|
|
|
|
local function get_avatar(name)
|
|
local obj = avatars[name]
|
|
if obj and obj:get_pos() then
|
|
return obj
|
|
end
|
|
avatars[name] = nil
|
|
end
|
|
strata.get_avatar = get_avatar
|
|
|
|
function strata.get_avatar_pos(name)
|
|
local obj = get_avatar(name)
|
|
return obj and obj:get_pos()
|
|
end
|
|
|
|
function strata.spawn_pos()
|
|
local y = strata.mapgen.surface_y(0)
|
|
return vector.new(0.5, y + 1.55, PLANE_Z)
|
|
end
|
|
|
|
--- The x on the plane that a player's crosshair points at. The player
|
|
-- object sits on the camera entity, CAMERA_DIST in front of the plane.
|
|
function strata.aim_x(player)
|
|
local yaw = player:get_look_horizontal()
|
|
if yaw > math.pi then
|
|
yaw = yaw - 2 * math.pi
|
|
end
|
|
return player:get_pos().x - math.tan(yaw) * CAMERA_DIST
|
|
end
|
|
|
|
--- The way an avatar at avatar_x should face to use an item at aim_x:
|
|
-- 1, -1, or nil when the aim is too close to the avatar's centre to tell.
|
|
function strata.aim_facing(aim_x, avatar_x)
|
|
local dx = aim_x - avatar_x
|
|
if math.abs(dx) < AIM_TURN_MIN then
|
|
return nil
|
|
end
|
|
return dx > 0 and 1 or -1
|
|
end
|
|
|
|
local AVATAR_BOX = {-0.35, -0.95, -0.3, 0.35, 0.95, 0.3}
|
|
|
|
core.register_entity("strata_player:avatar", {
|
|
initial_properties = {
|
|
physical = true,
|
|
collide_with_objects = false,
|
|
collisionbox = AVATAR_BOX,
|
|
selectionbox = AVATAR_BOX,
|
|
pointable = false,
|
|
visual = "upright_sprite",
|
|
visual_size = {x = 1, y = 2},
|
|
textures = {strata.avatar_texture(0, 1), strata.avatar_texture(0, 1)},
|
|
stepheight = 1.05,
|
|
static_save = false,
|
|
},
|
|
|
|
on_activate = function(self)
|
|
self.object:set_acceleration({x = 0, y = -GRAVITY, z = 0})
|
|
self._facing = 1
|
|
self._min_vy = 0
|
|
self._drop_timer = 0
|
|
end,
|
|
|
|
-- the texture follows in strata.update_avatar_appearance
|
|
set_facing = function(self, dir)
|
|
self._facing = dir
|
|
end,
|
|
|
|
on_step = function(self, dtime, moveresult)
|
|
local player = core.get_player_by_name(self._owner or "")
|
|
-- _test_ctrl lets the headless selftest drive an ownerless avatar
|
|
if not player and not self._test_ctrl then
|
|
self.object:remove()
|
|
return
|
|
end
|
|
local obj = self.object
|
|
local pos = obj:get_pos()
|
|
local vel = obj:get_velocity()
|
|
local alive = not player or player:get_hp() > 0
|
|
local ctrl = self._test_ctrl or (alive and player:get_player_control()) or {}
|
|
|
|
-- one-way platforms: S drops through the one we stand on
|
|
self._drop_timer = math.max(0, self._drop_timer - dtime)
|
|
if (ctrl.down or ctrl.sneak) and not ctrl.jump and self._supported then
|
|
self._drop_timer = DROP_TIME
|
|
vel.y = -DROP_SPEED
|
|
end
|
|
local on_platform = strata.platform_step(self, pos, vel, AVATAR_BOX, self._drop_timer > 0)
|
|
local on_ground = (moveresult and moveresult.touching_ground) or on_platform
|
|
local liquid = strata.body_liquid(pos)
|
|
|
|
-- horizontal movement
|
|
local dir = (ctrl.right and 1 or 0) - (ctrl.left and 1 or 0)
|
|
local target = dir * WALK_SPEED * (liquid and liquid.speed or 1)
|
|
local step = (on_ground and GROUND_ACCEL or AIR_ACCEL) * dtime
|
|
local dv = target - vel.x
|
|
if math.abs(dv) <= step then
|
|
vel.x = target
|
|
else
|
|
vel.x = vel.x + (dv > 0 and step or -step)
|
|
end
|
|
if dir ~= 0 then
|
|
self:set_facing(dir)
|
|
strata.open_door_ahead(pos, dir, AVATAR_BOX[4], AVATAR_BOX[5])
|
|
end
|
|
-- using an item turns the avatar towards what the player aims at
|
|
if ctrl.dig or ctrl.place or ctrl.LMB or ctrl.RMB then
|
|
local aim_x = self._test_aim_x or (player and strata.aim_x(player))
|
|
local aim_dir = aim_x and strata.aim_facing(aim_x, pos.x)
|
|
if aim_dir then
|
|
self:set_facing(aim_dir)
|
|
end
|
|
end
|
|
|
|
-- jumping, with variable height; swimming in a liquid
|
|
local gravity = GRAVITY
|
|
if liquid then
|
|
gravity = strata.swim_velocity(vel, liquid, strata.head_liquid(pos), ctrl, GRAVITY)
|
|
self._jumping = false
|
|
self._min_vy = 0 -- landing in a liquid breaks a fall
|
|
elseif on_ground then
|
|
self._jumping = false
|
|
if ctrl.jump then
|
|
vel.y = JUMP_SPEED
|
|
self._jumping = true
|
|
end
|
|
elseif self._jumping and not ctrl.jump and vel.y > 0 then
|
|
vel.y = vel.y * JUMP_CUT
|
|
self._jumping = false
|
|
end
|
|
|
|
-- fall damage
|
|
if on_ground then
|
|
local impact = -self._min_vy
|
|
if impact > FALL_SAFE and alive and player then
|
|
local dmg = math.floor((impact - FALL_SAFE) * FALL_DMG)
|
|
player:set_hp(player:get_hp() - dmg, {type = "fall"})
|
|
end
|
|
self._min_vy = 0
|
|
else
|
|
self._min_vy = math.min(self._min_vy, vel.y)
|
|
end
|
|
|
|
strata.avatar_liquid_step(self, pos, dtime, alive and player or nil)
|
|
|
|
strata.platform_support(self, on_platform and vel.y <= 0, gravity)
|
|
self._on_ground = on_ground
|
|
|
|
-- stay on the plane
|
|
vel.z = 0
|
|
if math.abs(pos.z - PLANE_Z) > 0.01 then
|
|
pos.z = PLANE_Z
|
|
obj:set_pos(pos)
|
|
end
|
|
obj:set_velocity(vel)
|
|
strata.update_avatar_appearance(self, dtime, ctrl, vel, on_ground, player)
|
|
end,
|
|
})
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Camera. The client puts an attached player's camera at the *parent's*
|
|
-- origin and ignores the attachment offset, so the player is attached to
|
|
-- this entity and the entity itself sits in front of the plane. The entity
|
|
-- moves by velocity rather than set_pos so the client interpolates it.
|
|
--------------------------------------------------------------------------
|
|
|
|
--- The y the camera should centre on. Standing on something re-centres it.
|
|
-- In the air it only moves once the avatar leaves the deadzone, so jumps
|
|
-- leave the screen still while falls are followed early.
|
|
function strata.camera_focus_y(focus, y, on_ground)
|
|
if on_ground or not focus then
|
|
return y
|
|
elseif y > focus + DEADZONE_UP then
|
|
return y - DEADZONE_UP
|
|
elseif y < focus - DEADZONE_DOWN then
|
|
return y + DEADZONE_DOWN
|
|
end
|
|
return focus
|
|
end
|
|
|
|
--- Where the camera entity wants to be, for an avatar at avatar_pos moving
|
|
-- at vel_x, with the vertical focus from strata.camera_focus_y.
|
|
function strata.camera_goal(avatar_pos, vel_x, focus)
|
|
local lead = math.max(-1, math.min(1, vel_x / WALK_SPEED)) * LOOK_AHEAD
|
|
return vector.new(avatar_pos.x + lead, focus + CAMERA_Y_OFFSET, PLANE_Z - CAMERA_DIST)
|
|
end
|
|
|
|
core.register_entity("strata_player:camera", {
|
|
initial_properties = {
|
|
physical = false,
|
|
pointable = false,
|
|
visual = "sprite",
|
|
visual_size = {x = 0, y = 0},
|
|
textures = {"blank.png"},
|
|
static_save = false,
|
|
},
|
|
-- _test_avatar lets the headless selftest drive an ownerless camera
|
|
on_step = function(self, dtime)
|
|
local avatar = self._test_avatar or get_avatar(self._owner or "")
|
|
local apos = avatar and avatar:get_pos()
|
|
if not apos then
|
|
self.object:remove()
|
|
return
|
|
end
|
|
if not SMOOTH_CAMERA then
|
|
return
|
|
end
|
|
local obj = self.object
|
|
self._focus = strata.camera_focus_y(self._focus, apos.y, avatar:get_luaentity()._on_ground)
|
|
local goal = strata.camera_goal(apos, avatar:get_velocity().x, self._focus)
|
|
local pos = obj:get_pos()
|
|
if vector.distance(pos, goal) > CAMERA_SNAP_DIST then
|
|
obj:set_pos(goal)
|
|
obj:set_velocity({x = 0, y = 0, z = 0})
|
|
return
|
|
end
|
|
-- capped so a long step can't overshoot the goal
|
|
local rate = math.min(FOLLOW_RATE, 1 / math.max(dtime, 0.001))
|
|
obj:set_velocity({x = (goal.x - pos.x) * rate, y = (goal.y - pos.y) * rate, z = 0})
|
|
end,
|
|
})
|
|
|
|
local cameras = {}
|
|
|
|
local function attach(player, avatar)
|
|
local name = player:get_player_name()
|
|
local apos = avatar:get_pos()
|
|
local goal = strata.camera_goal(apos, 0, apos.y)
|
|
local cam = cameras[name]
|
|
if not (cam and cam:get_pos()) then
|
|
cam = core.add_entity(goal, "strata_player:camera")
|
|
if not cam then
|
|
return
|
|
end
|
|
cam:get_luaentity()._owner = name
|
|
cameras[name] = cam
|
|
end
|
|
if SMOOTH_CAMERA then
|
|
cam:set_pos(goal)
|
|
cam:set_velocity({x = 0, y = 0, z = 0})
|
|
cam:get_luaentity()._focus = apos.y
|
|
else
|
|
cam:set_attach(avatar, "",
|
|
{x = 0, y = CAMERA_Y_OFFSET * 10, z = -CAMERA_DIST * 10},
|
|
{x = 0, y = 0, z = 0})
|
|
end
|
|
player:set_attach(cam, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
|
|
end
|
|
|
|
local function spawn_avatar(name, pos)
|
|
local player = core.get_player_by_name(name)
|
|
if not player then
|
|
return
|
|
end
|
|
local old = get_avatar(name)
|
|
if old then
|
|
old:remove()
|
|
end
|
|
local obj = core.add_entity(pos, "strata_player:avatar")
|
|
if not obj then
|
|
core.after(RESPAWN_RETRY, spawn_avatar, name, pos)
|
|
return
|
|
end
|
|
obj:get_luaentity()._owner = name
|
|
avatars[name] = obj
|
|
attach(player, obj)
|
|
player:set_look_horizontal(0)
|
|
player:set_look_vertical(0)
|
|
end
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Teleporting. The client only asks for mapblocks near the camera a few at
|
|
-- a time, so after a long jump it shows sky and holes for seconds. Instead
|
|
-- we cover the screen, emerge everything the camera can see, move the
|
|
-- avatar, push those mapblocks with send_mapblock and then fade back in.
|
|
--------------------------------------------------------------------------
|
|
|
|
--- Node area the side-view camera can see around an avatar at pos:
|
|
-- the view rectangle on the plane and wall layer plus the space between
|
|
-- the plane and the camera.
|
|
function strata.view_area(pos)
|
|
local p = vector.round(pos)
|
|
return vector.new(p.x - VIEW_HALF_W, p.y - VIEW_HALF_H, PLANE_Z - CAMERA_DIST - 2),
|
|
vector.new(p.x + VIEW_HALF_W, p.y + VIEW_HALF_H, strata.WALL_Z)
|
|
end
|
|
|
|
local fades = {} -- name -> {hud = id, token = n}
|
|
|
|
local function fade_cover(player)
|
|
local name = player:get_player_name()
|
|
local f = fades[name] or {token = 0}
|
|
fades[name] = f
|
|
f.token = f.token + 1
|
|
local def = {
|
|
type = "image",
|
|
position = {x = 0.5, y = 0.5},
|
|
alignment = {x = 0, y = 0},
|
|
scale = {x = -100, y = -100},
|
|
text = FADE_TEXTURE,
|
|
z_index = FADE_Z,
|
|
}
|
|
if f.hud then
|
|
player:hud_change(f.hud, "text", FADE_TEXTURE)
|
|
else
|
|
f.hud = player:hud_add(def)
|
|
end
|
|
end
|
|
|
|
local function fade_step(name, token, step)
|
|
local player = core.get_player_by_name(name)
|
|
local f = fades[name]
|
|
if not player or not f or f.token ~= token or not f.hud then
|
|
return
|
|
end
|
|
if step >= FADE_OUT_STEPS then
|
|
player:hud_remove(f.hud)
|
|
f.hud = nil
|
|
return
|
|
end
|
|
local opacity = math.floor(255 * (1 - step / FADE_OUT_STEPS))
|
|
player:hud_change(f.hud, "text", FADE_TEXTURE .. "^[opacity:" .. opacity)
|
|
core.after(FADE_OUT_TIME / FADE_OUT_STEPS, fade_step, name, token, step + 1)
|
|
end
|
|
|
|
local function fade_reveal(name, delay)
|
|
local f = fades[name]
|
|
if f then
|
|
core.after(delay, fade_step, name, f.token, 0)
|
|
end
|
|
end
|
|
|
|
--- Push every loaded mapblock in the camera's view to the player.
|
|
-- Returns the number of blocks sent.
|
|
function strata.send_view_blocks(player, pos)
|
|
local minp, maxp = strata.view_area(pos)
|
|
local bmin, bmax = vector.floor(vector.divide(minp, 16)), vector.floor(vector.divide(maxp, 16))
|
|
local sent = 0
|
|
for bz = bmin.z, bmax.z do
|
|
for by = bmin.y, bmax.y do
|
|
for bx = bmin.x, bmax.x do
|
|
if player:send_mapblock({x = bx, y = by, z = bz}) then
|
|
sent = sent + 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return sent
|
|
end
|
|
|
|
--- Emerge the camera's view around pos, then call callback(pos).
|
|
function strata.prepare_view(pos, callback)
|
|
local minp, maxp = strata.view_area(pos)
|
|
core.emerge_area(minp, maxp, function(_, _, remaining)
|
|
if remaining == 0 then
|
|
callback(pos)
|
|
end
|
|
end)
|
|
end
|
|
|
|
--- Move a player's avatar to pos (on the plane), hiding the screen until
|
|
-- the destination is loaded on the client. Creates the avatar if needed.
|
|
-- Returns false if the player is not online.
|
|
function strata.teleport(name, pos)
|
|
local player = core.get_player_by_name(name)
|
|
if not player then
|
|
return false
|
|
end
|
|
pos = vector.new(pos.x, pos.y, PLANE_Z)
|
|
fade_cover(player)
|
|
strata.prepare_view(pos, function()
|
|
player = core.get_player_by_name(name)
|
|
if not player then
|
|
return
|
|
end
|
|
local obj = get_avatar(name)
|
|
if obj then
|
|
obj:set_pos(pos)
|
|
obj:set_velocity({x = 0, y = 0, z = 0})
|
|
local ent = obj:get_luaentity()
|
|
ent._min_vy = 0 -- no fall damage from the jump
|
|
ent._prev_feet = nil -- don't land on platforms passed by the jump
|
|
attach(player, obj)
|
|
else
|
|
spawn_avatar(name, pos)
|
|
end
|
|
strata.send_view_blocks(player, pos)
|
|
fade_reveal(name, FADE_HOLD)
|
|
end)
|
|
return true
|
|
end
|
|
strata.place_avatar = strata.teleport
|
|
|
|
--------------------------------------------------------------------------
|
|
-- /teleport. The engine ignores set_pos on attached players, so the builtin
|
|
-- command does nothing here. This one moves the avatar and keeps the
|
|
-- builtin syntax; z is accepted and ignored, and "x,y" also works.
|
|
--------------------------------------------------------------------------
|
|
|
|
local COORD = "([%d.~-]+)"
|
|
local COORD_SEP = "[, ] *"
|
|
local XY = "^" .. COORD .. COORD_SEP .. COORD
|
|
local XYZ = XY .. COORD_SEP .. COORD .. "$"
|
|
|
|
local function parse_xy(text, mover)
|
|
local x, y = text:match(XYZ)
|
|
if not x then
|
|
x, y = text:match(XY .. "$")
|
|
end
|
|
if x then
|
|
return core.parse_coordinates(x, y, "0", strata.get_avatar_pos(mover))
|
|
end
|
|
end
|
|
|
|
--- Work out what a /teleport command means.
|
|
-- Returns the player to move and the destination, or nil and an error.
|
|
function strata.resolve_teleport(caller, param)
|
|
local S = strata.S
|
|
local dest = parse_xy(param, caller)
|
|
if dest then
|
|
return caller, dest
|
|
end
|
|
local mover, rest = param:match("^([^ ]+) +(.+)$")
|
|
if not mover then
|
|
mover, rest = caller, param
|
|
elseif not core.check_player_privs(caller, {bring = true}) then
|
|
return nil, S("You need the bring privilege to teleport other players.")
|
|
end
|
|
dest = parse_xy(rest, mover)
|
|
if dest then
|
|
return mover, dest
|
|
end
|
|
if rest == "" or rest:find(" ", 1, true) then
|
|
return nil, S("Usage: /teleport <x>,<y> | <to_name> | <name> <x>,<y> | <name> <to_name>")
|
|
end
|
|
dest = strata.get_avatar_pos(rest)
|
|
if not dest then
|
|
return nil, S("Player @1 is not online.", rest)
|
|
end
|
|
return mover, dest
|
|
end
|
|
|
|
core.override_chatcommand("teleport", {
|
|
params = "<x>,<y> | <to_name> | <name> <x>,<y> | <name> <to_name>",
|
|
func = function(name, param)
|
|
local mover, dest = strata.resolve_teleport(name, param)
|
|
if not mover then
|
|
return false, dest
|
|
end
|
|
if not strata.teleport(mover, dest) then
|
|
return false, strata.S("Player @1 is not online.", mover)
|
|
end
|
|
return true, strata.S("Teleported @1 to @2, @3.", mover, math.floor(dest.x), math.floor(dest.y))
|
|
end,
|
|
})
|
|
|
|
--- Force first-person view where the engine can (server and client 5.12+);
|
|
-- the rig has no meaningful third-person view. Older engines can't lock C.
|
|
function strata.lock_camera_mode(player)
|
|
if player.set_camera then
|
|
player:set_camera({mode = "first"})
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
local function setup_player(player)
|
|
player:set_properties({
|
|
visual_size = {x = 0, y = 0},
|
|
pointable = false,
|
|
makes_footstep_sound = false,
|
|
show_on_minimap = false,
|
|
hp_max = MAX_HP,
|
|
zoom_fov = 0, -- also blocks the creative-mode zoom
|
|
})
|
|
player:set_nametag_attributes({color = {a = 0, r = 255, g = 255, b = 255}})
|
|
player:set_physics_override({speed = 0, jump = 0, gravity = 0})
|
|
player:hud_set_flags({minimap = false, minimap_radar = false, wielditem = false, breathbar = false})
|
|
player:hud_set_hotbar_itemcount(10)
|
|
player:set_fov(strata.CAMERA_FOV, false)
|
|
strata.lock_camera_mode(player)
|
|
local inv = player:get_inventory()
|
|
inv:set_size("main", 40)
|
|
inv:set_size("craft", 0)
|
|
-- hp_max is only raised here, after on_newplayer, so top up new players once
|
|
local meta = player:get_meta()
|
|
if meta:get_int("strata:hp_init") == 0 then
|
|
meta:set_int("strata:hp_init", 1)
|
|
player:set_hp(MAX_HP)
|
|
end
|
|
end
|
|
|
|
core.register_on_newplayer(function(player)
|
|
local inv = player:get_inventory()
|
|
inv:set_size("main", 40)
|
|
for _, item in ipairs({"strata_core:copper_shortsword", "strata_core:copper_pickaxe",
|
|
"strata_core:copper_axe", "strata_core:torch 10"}) do
|
|
inv:add_item("main", item)
|
|
end
|
|
end)
|
|
|
|
core.register_on_joinplayer(function(player)
|
|
setup_player(player)
|
|
local name = player:get_player_name()
|
|
local saved = core.deserialize(player:get_meta():get_string("strata:avatar_pos"))
|
|
local pos = saved and vector.new(saved.x, saved.y, PLANE_Z) or strata.spawn_pos()
|
|
strata.teleport(name, pos)
|
|
end)
|
|
|
|
local function save_pos(player)
|
|
local pos = strata.get_avatar_pos(player:get_player_name())
|
|
if pos then
|
|
player:get_meta():set_string("strata:avatar_pos",
|
|
core.serialize({x = pos.x, y = pos.y}))
|
|
end
|
|
end
|
|
|
|
core.register_on_leaveplayer(function(player)
|
|
save_pos(player)
|
|
local name = player:get_player_name()
|
|
local obj = get_avatar(name)
|
|
if obj then
|
|
obj:remove()
|
|
end
|
|
avatars[name] = nil
|
|
if cameras[name] and cameras[name]:get_pos() then
|
|
cameras[name]:remove()
|
|
end
|
|
cameras[name] = nil
|
|
fades[name] = nil
|
|
end)
|
|
|
|
core.register_on_shutdown(function()
|
|
for _, player in ipairs(core.get_connected_players()) do
|
|
save_pos(player)
|
|
end
|
|
end)
|
|
|
|
core.register_on_respawnplayer(function(player)
|
|
strata.teleport(player:get_player_name(), strata.spawn_pos())
|
|
return true
|
|
end)
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Layer sky: below the surface layer the real sky, sun, moon, stars and
|
|
-- clouds are replaced by the layer's flat background colour, which is what
|
|
-- shows where a background wall is missing.
|
|
--------------------------------------------------------------------------
|
|
|
|
local sky_layer = {} -- player name -> name of the layer whose sky is set
|
|
|
|
--- Set a player's sky for a layer (a table from strata.mapgen.layers).
|
|
function strata.apply_layer_sky(player, layer)
|
|
local open = layer.sky == nil
|
|
if open then
|
|
player:set_sky({type = "regular", clouds = true})
|
|
else
|
|
player:set_sky({type = "plain", base_color = layer.sky, clouds = false})
|
|
end
|
|
player:set_sun({visible = open, sunrise_visible = open})
|
|
player:set_moon({visible = open})
|
|
player:set_stars({visible = open})
|
|
end
|
|
|
|
--- Apply the sky of the layer at row y if the player isn't already in it.
|
|
-- Returns true when the sky changed.
|
|
function strata.update_layer_sky(player, y)
|
|
local name = player:get_player_name()
|
|
local layer = strata.mapgen.layer_at(y)
|
|
if sky_layer[name] == layer.name then
|
|
return false
|
|
end
|
|
sky_layer[name] = layer.name
|
|
strata.apply_layer_sky(player, layer)
|
|
return true
|
|
end
|
|
|
|
core.register_on_leaveplayer(function(player)
|
|
sky_layer[player:get_player_name()] = nil
|
|
end)
|
|
|
|
-- Keep the camera pointed at the plane and re-attach if something detached us.
|
|
local timer, save_timer = 0, 0
|
|
core.register_globalstep(function(dtime)
|
|
timer = timer + dtime
|
|
save_timer = save_timer + dtime
|
|
if timer < 0.1 then
|
|
return
|
|
end
|
|
timer = 0
|
|
local do_save = save_timer > 10
|
|
if do_save then
|
|
save_timer = 0
|
|
end
|
|
for _, player in ipairs(core.get_connected_players()) do
|
|
local name = player:get_player_name()
|
|
local obj = get_avatar(name)
|
|
if obj and not player:get_attach() then
|
|
attach(player, obj)
|
|
end
|
|
if obj then
|
|
strata.update_layer_sky(player, obj:get_pos().y)
|
|
end
|
|
local yaw = player:get_look_horizontal()
|
|
if yaw > math.pi then
|
|
yaw = yaw - 2 * math.pi
|
|
end
|
|
if math.abs(yaw) > LOOK_LIMIT then
|
|
player:set_look_horizontal(yaw > 0 and LOOK_LIMIT or 2 * math.pi - LOOK_LIMIT)
|
|
end
|
|
local pitch = player:get_look_vertical()
|
|
if math.abs(pitch) > LOOK_LIMIT then
|
|
player:set_look_vertical(pitch > 0 and LOOK_LIMIT or -LOOK_LIMIT)
|
|
end
|
|
if do_save then
|
|
save_pos(player)
|
|
end
|
|
end
|
|
end)
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Reach rules: players may only dig/place on the plane or wall layer,
|
|
-- within REACH of their avatar. Implemented via is_protected so it covers
|
|
-- digging, placing and any mod that respects protection.
|
|
--------------------------------------------------------------------------
|
|
|
|
local old_is_protected = core.is_protected
|
|
function core.is_protected(pos, name)
|
|
if name and name ~= "" and core.get_player_by_name(name) then
|
|
if pos.z ~= PLANE_Z and pos.z ~= strata.WALL_Z then
|
|
return true
|
|
end
|
|
if strata.mapgen and not strata.mapgen.in_world(pos.x) then
|
|
return true -- past the edge of a finite world (#23)
|
|
end
|
|
local apos = strata.get_avatar_pos(name)
|
|
if not apos then
|
|
return true
|
|
end
|
|
if vector.distance(vector.new(apos.x, apos.y, pos.z), pos) > REACH then
|
|
return true
|
|
end
|
|
end
|
|
return old_is_protected(pos, name)
|
|
end
|
|
|
|
-- Don't let blocks be placed inside the avatar or a creature.
|
|
core.register_on_placenode(function(pos, newnode)
|
|
local def = core.registered_nodes[newnode.name]
|
|
if not def or def.walkable == false then
|
|
return
|
|
end
|
|
if #strata.objects_in_cell(pos) > 0 then
|
|
core.remove_node(pos)
|
|
return true -- don't consume the item
|
|
end
|
|
end)
|
|
|
|
dofile(core.get_modpath("strata_player") .. "/pickup.lua")
|