diff --git a/API.txt b/API.txt index 56cded0..d3cbfe6 100644 --- a/API.txt +++ b/API.txt @@ -1,25 +1,25 @@ Creatures MOB-Engine API ------------------------ -creatures.register_mob(#Creature definition) +cmer.register_mob(#Creature definition) -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. existent entities (MOBs), Spawners and Spawn Eggs are converted; returns true when sucessfull ^ old_mob: name as string, e.g. "creatures:oerrki" ^ 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 - ^ 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 -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 ^ search_obj is searching object; can be nil ^ 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 ^ table is empty -creatures.dropItems(pos, drops) +cmer.dropItems(pos, drops) -drops items at position pos ^ pos where to drop Items ^ 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_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_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 diff --git a/common.lua b/common.lua index ad8bf39..6d42c69 100644 --- a/common.lua +++ b/common.lua @@ -25,7 +25,7 @@ nullVec = {x = 0, y = 0, z = 0} DEGTORAD = math.pi / 180.0 -- common functions -function creatures.sumChances(tab) +function cmer.sumChances(tab) local psum = 0 for s,w in pairs(tab) do psum = psum + ((tonumber(w) or w.chance or 0)) @@ -33,7 +33,7 @@ function creatures.sumChances(tab) return psum end -function creatures.rnd(tab, errval) +function cmer.rnd(tab, errval) if not errval then errval = false end @@ -62,7 +62,7 @@ function throw_warning(msg) core.log("warning", "#Creatures: WARNING: " .. msg) end -function creatures.compare_pos(pos1, pos2) +function cmer.compare_pos(pos1, pos2) if not pos1 or not pos2 then return end @@ -72,7 +72,7 @@ function creatures.compare_pos(pos1, pos2) return true 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 mobs = {} 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 end -function creatures.dropItems(pos, drops) +function cmer.dropItems(pos, drops) if not pos or not drops then return end @@ -134,7 +134,7 @@ function creatures.dropItems(pos, drops) local ct = {} ct[name] = chance ct["_fake"] = 1 - chance - local res = creatures.rnd(ct) + local res = cmer.rnd(ct) if res == "_fake" then name = nil end diff --git a/functions.lua b/functions.lua index 3be228a..da4a0fa 100644 --- a/functions.lua +++ b/functions.lua @@ -57,7 +57,7 @@ local function on_hit(me) end) end -local hasMoved = creatures.compare_pos +local hasMoved = cmer.compare_pos local function getDir(pos1, pos2) local retval @@ -77,7 +77,7 @@ local function getDistance(vec, fly_offset) return math.sqrt((vec.x)^2 + (vec.y)^2 + (vec.z)^2) end -local findTarget = creatures.findTarget +local findTarget = cmer.findTarget local function update_animation(obj_ref, mode, anim_def) if anim_def and obj_ref then @@ -111,7 +111,7 @@ local function getYaw(dirOrYaw) return yaw end -local dropItems = creatures.dropItems +local dropItems = cmer.dropItems local function killMob(me, def) if not def then @@ -255,7 +255,7 @@ end -- 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 return end @@ -286,10 +286,10 @@ creatures.on_punch = function(self, puncher, time_from_last_punch, tool_capabili end end -creatures.on_rightclick = function(self, clicker) +cmer.on_rightclick = function(self, clicker) 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) local def = core.registered_entities[self.mob_name] 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 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 new_mode = "idle" end @@ -675,7 +675,7 @@ creatures.on_step = function(self, dtime) end -creatures.get_staticdata = function(self) +cmer.get_staticdata = function(self) return { hp = self.object:get_hp(), mode = self.mode, diff --git a/init.lua b/init.lua index 2a851ab..789f0ea 100644 --- a/init.lua +++ b/init.lua @@ -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 -dofile(modpath .."/common.lua") -dofile(modpath .."/functions.lua") -dofile(modpath .."/register.lua") +cmer.modname = core.get_current_modname() +cmer.modpath = core.get_modpath(cmer.modname) --- Common items -dofile(modpath .."/items.lua") +local scripts = { + "common", + "functions", + "register", + "items", +} + +for _, s in ipairs(scripts) do + dofile(cmer.modpath .. "/" .. s .. ".lua") +end diff --git a/mod.conf b/mod.conf index ee2d161..9e0a222 100644 --- a/mod.conf +++ b/mod.conf @@ -1,3 +1,5 @@ -name = creatures +name = cmer +title = Creatures Revived description = An API for creating mobs. depends = default, wool +optional_depends = creatures diff --git a/register.lua b/register.lua index 091d3e5..011fee1 100644 --- a/register.lua +++ b/register.lua @@ -56,7 +56,7 @@ local function translate_def(def) end -- 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 throw_warning("Chance of modes too high for MOB: " .. def.name .. ". Mode chances will be incorrect.") @@ -99,7 +99,7 @@ local function translate_def(def) 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 if def.get_staticdata then local data = def.get_staticdata(self) @@ -210,7 +210,7 @@ local function translate_def(def) return 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 new_def.on_rightclick = function(self, clicker) @@ -218,7 +218,7 @@ local function translate_def(def) return end - creatures.on_rightclick(self, clicker) + cmer.on_rightclick(self, clicker) end new_def.on_step = function(self, dtime) @@ -226,13 +226,13 @@ local function translate_def(def) return end - creatures.on_step(self, dtime) + cmer.on_step(self, dtime) end return new_def 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 throw_error("Can't register mob. No name or Definition given.") return false @@ -247,7 +247,7 @@ function creatures.register_mob(def) -- returns true if sucessfull local spawn_def = def.spawning spawn_def.mob_name = def.name 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 .. "'") end @@ -255,7 +255,7 @@ function creatures.register_mob(def) -- returns true if sucessfull local egg_def = def.spawning.spawn_egg egg_def.mob_name = def.name egg_def.box = def.model.collisionbox - creatures.register_egg(egg_def) + cmer.register_egg(egg_def) end 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.number = spawner_def.number or 6 spawner_def.model = def.model - creatures.register_spawner(spawner_def) + cmer.register_spawner(spawner_def) end end @@ -330,7 +330,7 @@ local function groupSpawn(pos, mob, group, nodes, range, max_loops) 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 throw_error("No valid definition for given.") return false @@ -380,7 +380,7 @@ function creatures.register_spawn(spawn_def) -- creature count check local max 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 return else @@ -438,7 +438,7 @@ local function eggSpawn(itemstack, placer, pointed_thing, egg_def) 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 throw_error("Can't register Spawn-Egg. Not enough parameters given.") return false @@ -492,7 +492,7 @@ local function makeSpawnerEntiy(mob_name, model) end 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 return false end @@ -528,7 +528,7 @@ end 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 throw_error("Can't register Spawn-Egg. Not enough parameters given.") return false @@ -574,7 +574,7 @@ function creatures.register_spawner(spawner_def) spawner_timers[id] = os.time() end 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 spawner_timers[id] = os.time() @@ -619,7 +619,7 @@ local function register_alias_entity(old_mob, new_mob) 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] if not def then throw_error("No valid definition for given.")