Rename to "cmer"

master
Jordan Irwin 2021-05-05 10:50:42 -07:00
parent df7f73c2d7
commit 9a4a2a95fa
6 changed files with 59 additions and 48 deletions

18
API.txt
View File

@ -1,25 +1,25 @@
Creatures MOB-Engine API Creatures MOB-Engine API
------------------------ ------------------------
creatures.register_mob(#Creature definition) cmer.register_mob(#Creature definition)
-registers a mob at MOB-Engine; returns true when sucessfull -registers a mob at MOB-Engine; returns true when sucessfull
creatures.register_alias(old_mob, new_mob) -- returns true if sucessfull cmer.register_alias(old_mob, new_mob) -- returns true if sucessfull
-registers an alias for other mob, e.g. from other mods or removed ones. -registers an alias for other mob, e.g. from other mods or removed ones.
existent entities (MOBs), Spawners and Spawn Eggs are converted; existent entities (MOBs), Spawners and Spawn Eggs are converted;
returns true when sucessfull returns true when sucessfull
^ old_mob: name as string, e.g. "creatures:oerrki" ^ old_mob: name as string, e.g. "creatures:oerrki"
^ new_mob: name as string, e.g. "creatures:oerkki" ^ new_mob: name as string, e.g. "creatures:oerkki"
^ example: creatures.register_alias("creatures:oerrki", "creatures:oerkki") ^ example: cmer.register_alias("creatures:oerrki", "creatures:oerkki")
creatures.rnd(chance_table) cmer.rnd(chance_table)
-returns a weighted random table element; chance_sum of table must be 1 -returns a weighted random table element; chance_sum of table must be 1
^ example: creatures.rnd({elem1 = {chance = 0.7}, {elem2 = {chance = 0.3}}) ^ example: cmer.rnd({elem1 = {chance = 0.7}, {elem2 = {chance = 0.3}})
creatures.compare_pos(pos1, pos2) cmer.compare_pos(pos1, pos2)
-returns true if pos1 == pos2 -returns true if pos1 == pos2
creatures.findTarget(search_obj, pos, radius, search_type, mob_name, xray, no_count) cmer.findTarget(search_obj, pos, radius, search_type, mob_name, xray, no_count)
-returns table of found objects (as ObjectRef) and boolean player_near -returns table of found objects (as ObjectRef) and boolean player_near
^ search_obj is searching object; can be nil ^ search_obj is searching object; can be nil
^ pos is starting position for search radius ^ pos is starting position for search radius
@ -37,7 +37,7 @@ creatures.findTarget(search_obj, pos, radius, search_type, mob_name, xray, no_co
^ no_count skips collecting loop and returns just the boolean player_near ^ no_count skips collecting loop and returns just the boolean player_near
^ table is empty ^ table is empty
creatures.dropItems(pos, drops) cmer.dropItems(pos, drops)
-drops items at position pos -drops items at position pos
^ pos where to drop Items ^ pos where to drop Items
^ drops table in #ItemDrops format ^ drops table in #ItemDrops format
@ -138,7 +138,7 @@ and with a chance of 100% 2 items of type "default:stone"
search_enemy = true, -- true to search enemies to attack search_enemy = true, -- true to search enemies to attack
search_timer = 2, -- time in seconds to search an enemy (only if none found yet) search_timer = 2, -- time in seconds to search an enemy (only if none found yet)
search_radius = 12, -- radius in blocks within enemies are searched search_radius = 12, -- radius in blocks within enemies are searched
search_type = "player", -- what enemy is being searched (see types at creatures.findTarget()) search_type = "player", -- what enemy is being searched (see types at cmer.findTarget())
} }
spawning = { -- defines spawning in world <optional> spawning = { -- defines spawning in world <optional>

View File

@ -25,7 +25,7 @@ nullVec = {x = 0, y = 0, z = 0}
DEGTORAD = math.pi / 180.0 DEGTORAD = math.pi / 180.0
-- common functions -- common functions
function creatures.sumChances(tab) function cmer.sumChances(tab)
local psum = 0 local psum = 0
for s,w in pairs(tab) do for s,w in pairs(tab) do
psum = psum + ((tonumber(w) or w.chance or 0)) psum = psum + ((tonumber(w) or w.chance or 0))
@ -33,7 +33,7 @@ function creatures.sumChances(tab)
return psum return psum
end end
function creatures.rnd(tab, errval) function cmer.rnd(tab, errval)
if not errval then if not errval then
errval = false errval = false
end end
@ -62,7 +62,7 @@ function throw_warning(msg)
core.log("warning", "#Creatures: WARNING: " .. msg) core.log("warning", "#Creatures: WARNING: " .. msg)
end end
function creatures.compare_pos(pos1, pos2) function cmer.compare_pos(pos1, pos2)
if not pos1 or not pos2 then if not pos1 or not pos2 then
return return
end end
@ -72,7 +72,7 @@ function creatures.compare_pos(pos1, pos2)
return true return true
end end
function creatures.findTarget(search_obj, pos, radius, search_type, ignore_mob, xray, no_count) function cmer.findTarget(search_obj, pos, radius, search_type, ignore_mob, xray, no_count)
local player_near = false local player_near = false
local mobs = {} local mobs = {}
for _,obj in ipairs(core.get_objects_inside_radius(pos, radius)) do for _,obj in ipairs(core.get_objects_inside_radius(pos, radius)) do
@ -117,7 +117,7 @@ function creatures.findTarget(search_obj, pos, radius, search_type, ignore_mob,
return mobs,player_near return mobs,player_near
end end
function creatures.dropItems(pos, drops) function cmer.dropItems(pos, drops)
if not pos or not drops then if not pos or not drops then
return return
end end
@ -134,7 +134,7 @@ function creatures.dropItems(pos, drops)
local ct = {} local ct = {}
ct[name] = chance ct[name] = chance
ct["_fake"] = 1 - chance ct["_fake"] = 1 - chance
local res = creatures.rnd(ct) local res = cmer.rnd(ct)
if res == "_fake" then if res == "_fake" then
name = nil name = nil
end end

View File

@ -57,7 +57,7 @@ local function on_hit(me)
end) end)
end end
local hasMoved = creatures.compare_pos local hasMoved = cmer.compare_pos
local function getDir(pos1, pos2) local function getDir(pos1, pos2)
local retval local retval
@ -77,7 +77,7 @@ local function getDistance(vec, fly_offset)
return math.sqrt((vec.x)^2 + (vec.y)^2 + (vec.z)^2) return math.sqrt((vec.x)^2 + (vec.y)^2 + (vec.z)^2)
end end
local findTarget = creatures.findTarget local findTarget = cmer.findTarget
local function update_animation(obj_ref, mode, anim_def) local function update_animation(obj_ref, mode, anim_def)
if anim_def and obj_ref then if anim_def and obj_ref then
@ -111,7 +111,7 @@ local function getYaw(dirOrYaw)
return yaw return yaw
end end
local dropItems = creatures.dropItems local dropItems = cmer.dropItems
local function killMob(me, def) local function killMob(me, def)
if not def then if not def then
@ -255,7 +255,7 @@ end
-- Default entity functions -- Default entity functions
-- -- -- --
creatures.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir) cmer.on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
if self.stunned == true then if self.stunned == true then
return return
end end
@ -286,10 +286,10 @@ creatures.on_punch = function(self, puncher, time_from_last_punch, tool_capabili
end end
end end
creatures.on_rightclick = function(self, clicker) cmer.on_rightclick = function(self, clicker)
end end
creatures.on_step = function(self, dtime) cmer.on_step = function(self, dtime)
-- first get the relevant specs; exit if we don't know anything (1-3ms) -- first get the relevant specs; exit if we don't know anything (1-3ms)
local def = core.registered_entities[self.mob_name] local def = core.registered_entities[self.mob_name]
if not def then if not def then
@ -518,7 +518,7 @@ creatures.on_step = function(self, dtime)
(current_mode == "" or self.modetimer > (modes[current_mode].duration or 4)) then (current_mode == "" or self.modetimer > (modes[current_mode].duration or 4)) then
self.modetimer = 0 self.modetimer = 0
local new_mode = creatures.rnd(modes) or "idle" local new_mode = cmer.rnd(modes) or "idle"
if new_mode == "eat" and self.in_water == true then if new_mode == "eat" and self.in_water == true then
new_mode = "idle" new_mode = "idle"
end end
@ -675,7 +675,7 @@ creatures.on_step = function(self, dtime)
end end
creatures.get_staticdata = function(self) cmer.get_staticdata = function(self)
return { return {
hp = self.object:get_hp(), hp = self.object:get_hp(),
mode = self.mode, mode = self.mode,

View File

@ -20,14 +20,23 @@
-- --
creatures = {} cmer = {}
local modpath = core.get_modpath("creatures") if not core.global_exists("creatures") then
-- backward compat
creatures = cmer
end
-- API and common functions cmer.modname = core.get_current_modname()
dofile(modpath .."/common.lua") cmer.modpath = core.get_modpath(cmer.modname)
dofile(modpath .."/functions.lua")
dofile(modpath .."/register.lua")
-- Common items local scripts = {
dofile(modpath .."/items.lua") "common",
"functions",
"register",
"items",
}
for _, s in ipairs(scripts) do
dofile(cmer.modpath .. "/" .. s .. ".lua")
end

View File

@ -1,3 +1,5 @@
name = creatures name = cmer
title = Creatures Revived
description = An API for creating mobs. description = An API for creating mobs.
depends = default, wool depends = default, wool
optional_depends = creatures

View File

@ -56,7 +56,7 @@ local function translate_def(def)
end end
-- Check if the modes have correct sum -- Check if the modes have correct sum
local mode_chance_sum = creatures.sumChances(new_def.modes) local mode_chance_sum = cmer.sumChances(new_def.modes)
if mode_chance_sum > 1 then if mode_chance_sum > 1 then
throw_warning("Chance of modes too high for MOB: " .. def.name .. throw_warning("Chance of modes too high for MOB: " .. def.name ..
". Mode chances will be incorrect.") ". Mode chances will be incorrect.")
@ -99,7 +99,7 @@ local function translate_def(def)
new_def.get_staticdata = function(self) new_def.get_staticdata = function(self)
local main_tab = creatures.get_staticdata(self) local main_tab = cmer.get_staticdata(self)
-- is own staticdata function defined? If so, merge results -- is own staticdata function defined? If so, merge results
if def.get_staticdata then if def.get_staticdata then
local data = def.get_staticdata(self) local data = def.get_staticdata(self)
@ -210,7 +210,7 @@ local function translate_def(def)
return return
end end
creatures.on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir) cmer.on_punch(self, puncher, time_from_last_punch, tool_capabilities, dir)
end end
new_def.on_rightclick = function(self, clicker) new_def.on_rightclick = function(self, clicker)
@ -218,7 +218,7 @@ local function translate_def(def)
return return
end end
creatures.on_rightclick(self, clicker) cmer.on_rightclick(self, clicker)
end end
new_def.on_step = function(self, dtime) new_def.on_step = function(self, dtime)
@ -226,13 +226,13 @@ local function translate_def(def)
return return
end end
creatures.on_step(self, dtime) cmer.on_step(self, dtime)
end end
return new_def return new_def
end end
function creatures.register_mob(def) -- returns true if sucessfull function cmer.register_mob(def) -- returns true if sucessfull
if not def or not def.name then if not def or not def.name then
throw_error("Can't register mob. No name or Definition given.") throw_error("Can't register mob. No name or Definition given.")
return false return false
@ -247,7 +247,7 @@ function creatures.register_mob(def) -- returns true if sucessfull
local spawn_def = def.spawning local spawn_def = def.spawning
spawn_def.mob_name = def.name spawn_def.mob_name = def.name
spawn_def.mob_size = def.model.collisionbox spawn_def.mob_size = def.model.collisionbox
if creatures.register_spawn(spawn_def) ~= true then if cmer.register_spawn(spawn_def) ~= true then
throw_error("Couldn't register spawning for '" .. def.name .. "'") throw_error("Couldn't register spawning for '" .. def.name .. "'")
end end
@ -255,7 +255,7 @@ function creatures.register_mob(def) -- returns true if sucessfull
local egg_def = def.spawning.spawn_egg local egg_def = def.spawning.spawn_egg
egg_def.mob_name = def.name egg_def.mob_name = def.name
egg_def.box = def.model.collisionbox egg_def.box = def.model.collisionbox
creatures.register_egg(egg_def) cmer.register_egg(egg_def)
end end
if spawn_def.spawner then if spawn_def.spawner then
@ -264,7 +264,7 @@ function creatures.register_mob(def) -- returns true if sucessfull
spawner_def.range = spawner_def.range or 4 spawner_def.range = spawner_def.range or 4
spawner_def.number = spawner_def.number or 6 spawner_def.number = spawner_def.number or 6
spawner_def.model = def.model spawner_def.model = def.model
creatures.register_spawner(spawner_def) cmer.register_spawner(spawner_def)
end end
end end
@ -330,7 +330,7 @@ local function groupSpawn(pos, mob, group, nodes, range, max_loops)
end end
end end
function creatures.register_spawn(spawn_def) function cmer.register_spawn(spawn_def)
if not spawn_def or not spawn_def.abm_nodes then if not spawn_def or not spawn_def.abm_nodes then
throw_error("No valid definition for given.") throw_error("No valid definition for given.")
return false return false
@ -380,7 +380,7 @@ function creatures.register_spawn(spawn_def)
-- creature count check -- creature count check
local max local max
if active_object_count_wider > (spawn_def.max_number or 1) then if active_object_count_wider > (spawn_def.max_number or 1) then
local mates_num = #creatures.findTarget(nil, pos, 16, "mate", spawn_def.mob_name, true) local mates_num = #cmer.findTarget(nil, pos, 16, "mate", spawn_def.mob_name, true)
if not spawn_def.max_number or (mates_num or 0) >= spawn_def.max_number then if not spawn_def.max_number or (mates_num or 0) >= spawn_def.max_number then
return return
else else
@ -438,7 +438,7 @@ local function eggSpawn(itemstack, placer, pointed_thing, egg_def)
end end
end end
function creatures.register_egg(egg_def) function cmer.register_egg(egg_def)
if not egg_def or not egg_def.mob_name or not egg_def.box then if not egg_def or not egg_def.mob_name or not egg_def.box then
throw_error("Can't register Spawn-Egg. Not enough parameters given.") throw_error("Can't register Spawn-Egg. Not enough parameters given.")
return false return false
@ -492,7 +492,7 @@ local function makeSpawnerEntiy(mob_name, model)
end end
local function spawnerSpawn(pos, spawner_def) local function spawnerSpawn(pos, spawner_def)
local mates = creatures.findTarget(nil, pos, spawner_def.range, "mate", spawner_def.mob_name, true) or {} local mates = cmer.findTarget(nil, pos, spawner_def.range, "mate", spawner_def.mob_name, true) or {}
if #mates >= spawner_def.number then if #mates >= spawner_def.number then
return false return false
end end
@ -528,7 +528,7 @@ end
local spawner_timers = {} local spawner_timers = {}
function creatures.register_spawner(spawner_def) function cmer.register_spawner(spawner_def)
if not spawner_def or not spawner_def.mob_name or not spawner_def.model then if not spawner_def or not spawner_def.mob_name or not spawner_def.model then
throw_error("Can't register Spawn-Egg. Not enough parameters given.") throw_error("Can't register Spawn-Egg. Not enough parameters given.")
return false return false
@ -574,7 +574,7 @@ function creatures.register_spawner(spawner_def)
spawner_timers[id] = os.time() spawner_timers[id] = os.time()
end end
local time_from_last_call = os.time() - spawner_timers[id] local time_from_last_call = os.time() - spawner_timers[id]
local mobs,player_near = creatures.findTarget(nil, pos, spawner_def.player_range, "player", nil, true, true) local mobs,player_near = cmer.findTarget(nil, pos, spawner_def.player_range, "player", nil, true, true)
if player_near == true and time_from_last_call > 10 and (math.random(1, 5) == 1 or (time_from_last_call ) > 27) then if player_near == true and time_from_last_call > 10 and (math.random(1, 5) == 1 or (time_from_last_call ) > 27) then
spawner_timers[id] = os.time() spawner_timers[id] = os.time()
@ -619,7 +619,7 @@ local function register_alias_entity(old_mob, new_mob)
end end
function creatures.register_alias(old_mob, new_mob) -- returns true if sucessfull function cmer.register_alias(old_mob, new_mob) -- returns true if sucessfull
local def = core.registered_entities[new_mob] local def = core.registered_entities[new_mob]
if not def then if not def then
throw_error("No valid definition for given.") throw_error("No valid definition for given.")