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>
214 lines
6.7 KiB
Lua
214 lines
6.7 KiB
Lua
-- Strata creatures. For now: the Green Slime, which hops toward players
|
|
-- during the day and drops gel (needed for torches).
|
|
|
|
local PLANE_Z = strata.PLANE_Z
|
|
local GRAVITY = 22
|
|
local MAX_PER_PLAYER = 3
|
|
local SPAWN_MIN, SPAWN_MAX = 18, 32
|
|
local DESPAWN_DIST = 64
|
|
local CONTACT_DAMAGE = 6
|
|
local INVULN_TIME = 0.66 -- Terraria-style i-frames after taking a hit
|
|
local DAY_START, DAY_END = 0.25, 0.78 -- time of day outside this is night
|
|
|
|
local last_hit = {}
|
|
|
|
local function nearest_avatar(pos, radius)
|
|
local best, best_d, best_name
|
|
for name, obj in pairs(strata.avatars) do
|
|
local apos = obj:get_pos()
|
|
if apos then
|
|
local d = vector.distance(pos, apos)
|
|
if d <= radius and (not best_d or d < best_d) then
|
|
best, best_d, best_name = apos, d, name
|
|
end
|
|
end
|
|
end
|
|
return best, best_d, best_name
|
|
end
|
|
|
|
local function hurt_player(name, amount, source)
|
|
local player = core.get_player_by_name(name)
|
|
if not player or player:get_hp() <= 0 then
|
|
return
|
|
end
|
|
local now = core.get_us_time() / 1e6
|
|
if last_hit[name] and now - last_hit[name] < INVULN_TIME then
|
|
return
|
|
end
|
|
last_hit[name] = now
|
|
player:set_hp(player:get_hp() - amount, {type = "punch", object = source})
|
|
local avatar = strata.get_avatar(name)
|
|
if avatar and source then
|
|
local dir = avatar:get_pos().x >= source:get_pos().x and 1 or -1
|
|
avatar:add_velocity({x = dir * 6, y = 4, z = 0})
|
|
end
|
|
end
|
|
strata.hurt_player = hurt_player
|
|
|
|
local SLIME_BOX = {-0.45, -0.4, -0.3, 0.45, 0.3, 0.3}
|
|
|
|
core.register_entity("strata_mobs:slime", {
|
|
initial_properties = {
|
|
hp_max = 14,
|
|
physical = true,
|
|
collide_with_objects = false,
|
|
collisionbox = SLIME_BOX,
|
|
selectionbox = SLIME_BOX,
|
|
visual = "upright_sprite",
|
|
visual_size = {x = 1, y = 0.75},
|
|
textures = {"strata_slime.png", "strata_slime.png"},
|
|
static_save = false,
|
|
use_texture_alpha = true,
|
|
damage_texture_modifier = strata.FLASH_MOD,
|
|
},
|
|
|
|
on_activate = function(self)
|
|
self.object:set_acceleration({x = 0, y = -GRAVITY, z = 0})
|
|
self.object:set_armor_groups({fleshy = 100})
|
|
self._hop_timer = math.random() * 2 + 1
|
|
self._check = 0
|
|
end,
|
|
|
|
on_step = function(self, dtime, moveresult)
|
|
local obj = self.object
|
|
local pos = obj:get_pos()
|
|
local vel = obj:get_velocity()
|
|
local on_platform = strata.platform_step(self, pos, vel, SLIME_BOX)
|
|
local on_ground = (moveresult and moveresult.touching_ground) or on_platform
|
|
|
|
self._check = self._check + dtime
|
|
if self._check > 2 then
|
|
self._check = 0
|
|
if not nearest_avatar(pos, DESPAWN_DIST) then
|
|
obj:remove()
|
|
return
|
|
end
|
|
end
|
|
|
|
if on_ground then
|
|
vel.x = vel.x * 0.5
|
|
self._hop_timer = self._hop_timer - dtime
|
|
if self._hop_timer <= 0 then
|
|
local target = nearest_avatar(pos, 24)
|
|
local dir
|
|
if target then
|
|
dir = target.x >= pos.x and 1 or -1
|
|
else
|
|
dir = math.random(0, 1) * 2 - 1
|
|
end
|
|
vel.x = dir * (2.5 + math.random() * 1.5)
|
|
vel.y = 6 + math.random() * 3
|
|
self._hop_timer = 1.2 + math.random() * 1.5
|
|
end
|
|
end
|
|
|
|
-- contact damage
|
|
for name, aobj in pairs(strata.avatars) do
|
|
local apos = aobj:get_pos()
|
|
if apos and math.abs(apos.x - pos.x) < 0.8 and math.abs(apos.y - pos.y) < 1.3 then
|
|
hurt_player(name, CONTACT_DAMAGE, obj)
|
|
end
|
|
end
|
|
|
|
strata.platform_support(self, on_platform and vel.y <= 0, GRAVITY)
|
|
|
|
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)
|
|
end,
|
|
|
|
on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
|
|
if puncher and puncher:is_player() then
|
|
local apos = strata.get_avatar_pos(puncher:get_player_name())
|
|
if not apos or vector.distance(apos, self.object:get_pos()) > strata.REACH + 1 then
|
|
return true -- out of reach: ignore the hit
|
|
end
|
|
local kb = apos.x <= self.object:get_pos().x and 1 or -1
|
|
self.object:add_velocity({x = kb * 5, y = 3, z = 0})
|
|
end
|
|
-- the death squelch is played in on_death
|
|
if damage > 0 and self.object:get_hp() > damage then
|
|
core.sound_play("strata_slime_hit", {object = self.object,
|
|
max_hear_distance = strata.SOUND_HEAR_DISTANCE}, true)
|
|
end
|
|
end,
|
|
|
|
on_death = function(self, killer)
|
|
core.sound_play("strata_slime_die", {pos = self.object:get_pos(),
|
|
max_hear_distance = strata.SOUND_HEAR_DISTANCE}, true)
|
|
local stack = ItemStack("strata_core:gel " .. math.random(1, 2))
|
|
local inv = killer and killer:is_player() and killer:get_inventory()
|
|
local left = inv and inv:add_item("main", stack) or stack
|
|
if not left:is_empty() then
|
|
core.add_item(self.object:get_pos(), left)
|
|
end
|
|
end,
|
|
})
|
|
|
|
-- Find standing room on the plane in column x, scanning down from y_top.
|
|
local function find_ground(x, y_top, y_bottom)
|
|
for y = y_top, y_bottom, -1 do
|
|
local below = core.get_node({x = x, y = y - 1, z = PLANE_Z}).name
|
|
local here = core.get_node({x = x, y = y, z = PLANE_Z}).name
|
|
local def = core.registered_nodes[below]
|
|
if below ~= "ignore" and def and def.walkable and here == "air" then
|
|
return y
|
|
end
|
|
end
|
|
end
|
|
|
|
local function count_slimes(pos, radius)
|
|
local n = 0
|
|
for _, obj in ipairs(core.get_objects_inside_radius(pos, radius)) do
|
|
local ent = obj:get_luaentity()
|
|
if ent and ent.name == "strata_mobs:slime" then
|
|
n = n + 1
|
|
end
|
|
end
|
|
return n
|
|
end
|
|
|
|
--- Creature entity names that may spawn at row y at time of day tod
|
|
-- (0..1). If x is given, the biome of column x decides on the surface
|
|
-- layer and the cavern biome of (x, y) in the cavern layer, when it has a
|
|
-- table; elsewhere the spawn table of the layer that owns the row.
|
|
function strata.spawn_candidates(y, tod, x)
|
|
local mg = strata.mapgen
|
|
local layer = mg.layer_at(y)
|
|
local spawns = layer.spawns
|
|
if x and layer.index == 1 then
|
|
spawns = mg.biome_at(x).spawns
|
|
elseif x and layer.index == mg.CAVERN_LAYER then
|
|
spawns = mg.cavern_biome_at(x, y).spawns or spawns
|
|
end
|
|
local night = tod < DAY_START or tod > DAY_END
|
|
return night and spawns.night or spawns.day
|
|
end
|
|
|
|
local timer = 0
|
|
core.register_globalstep(function(dtime)
|
|
timer = timer + dtime
|
|
if timer < 4 then
|
|
return
|
|
end
|
|
timer = 0
|
|
local tod = core.get_timeofday()
|
|
for _, obj in pairs(strata.avatars) do
|
|
local apos = obj:get_pos()
|
|
if apos and count_slimes(apos, 40) < MAX_PER_PLAYER then
|
|
local side = math.random(0, 1) * 2 - 1
|
|
local x = math.floor(apos.x + side * math.random(SPAWN_MIN, SPAWN_MAX))
|
|
local y = strata.mapgen.in_world(x) and find_ground(x, math.floor(apos.y) + 16, math.floor(apos.y) - 16)
|
|
local candidates = y and strata.spawn_candidates(y, tod, x) or {}
|
|
-- every creature so far is a daylight one; the mob framework
|
|
-- (v0.4) moves conditions like this into the creature's definition
|
|
if #candidates > 0 and core.get_node_light({x = x, y = y, z = PLANE_Z}, 0.5) >= 12 then
|
|
core.add_entity({x = x, y = y + 0.45, z = PLANE_Z}, candidates[math.random(#candidates)])
|
|
end
|
|
end
|
|
end
|
|
end)
|