reasonably working basic step function
parent
7e50b30d80
commit
eb8bbfab12
374
api_fast.lua
374
api_fast.lua
|
@ -14,6 +14,7 @@ function mobehavior:register_mob_fast(name, def)
|
|||
rotate = 0,
|
||||
physical = true,
|
||||
weight = 5,
|
||||
jump_height = 6,
|
||||
collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
|
||||
visual = "mesh",
|
||||
visual_size = {x=1, y=1},
|
||||
|
@ -31,6 +32,8 @@ function mobehavior:register_mob_fast(name, def)
|
|||
bt = nil,
|
||||
btData = nil,
|
||||
|
||||
jump_timer = 0,
|
||||
walk_timer = 0,
|
||||
|
||||
on_step = function(self, dtime)
|
||||
local btdata = self.btData
|
||||
|
@ -64,7 +67,6 @@ function mobehavior:register_mob_fast(name, def)
|
|||
self.bt_timer = 0
|
||||
end
|
||||
|
||||
btdata.lastpos = pos
|
||||
|
||||
|
||||
-- handle movement
|
||||
|
@ -75,7 +77,7 @@ function mobehavior:register_mob_fast(name, def)
|
|||
|
||||
-- going up then apply gravity
|
||||
-- if v.y > 0.1 then
|
||||
|
||||
|
||||
self.object:setacceleration({
|
||||
x = 0,
|
||||
y = self.fall_speed,
|
||||
|
@ -85,11 +87,16 @@ function mobehavior:register_mob_fast(name, def)
|
|||
|
||||
-- TODO: fall damage
|
||||
|
||||
self.jump_timer = self.jump_timer + dtime
|
||||
|
||||
|
||||
if self.destination ~= nil then
|
||||
|
||||
|
||||
self.walk_timer = self.walk_timer + dtime
|
||||
|
||||
--print("destination ")
|
||||
|
||||
local tdist = distance(pos, btdata.lastpos)
|
||||
local dist = distance(pos, self.destination)
|
||||
-- print("walk dist ".. dist)
|
||||
local s = self.destination
|
||||
|
@ -99,225 +106,218 @@ function mobehavior:register_mob_fast(name, def)
|
|||
z = pos.z - s.z
|
||||
}
|
||||
|
||||
if vec.x ~= 0 or vec.z ~= 0 then
|
||||
|
||||
yaw = (math.atan(vec.z / vec.x) + math.pi / 2) - self.rotate
|
||||
if tdist < self.walk_velocity * dtime * .9 and self.walk_timer > 1 then
|
||||
|
||||
if s.x > pos.x then
|
||||
yaw = yaw + math.pi
|
||||
if self.jump_timer > 4 then
|
||||
local v = self.object:getvelocity()
|
||||
|
||||
v.y = self.jump_height + 1
|
||||
v.x = v.x * 2.2
|
||||
v.z = v.z * 2.2
|
||||
|
||||
self.object:setvelocity(v)
|
||||
|
||||
self.jump_timer = 0
|
||||
end
|
||||
|
||||
-- print("yaw " .. yaw)
|
||||
|
||||
self.object:setyaw(yaw)
|
||||
end
|
||||
|
||||
|
||||
yaw = (math.atan2(vec.z, vec.x) + math.pi / 2) - self.rotate
|
||||
self.object:setyaw(yaw)
|
||||
|
||||
|
||||
if dist > (self.approachDistance or .1) then
|
||||
--[[
|
||||
if (self.jump
|
||||
and get_velocity(self) <= 0.5
|
||||
and self.object:getvelocity().y == 0)
|
||||
or (self.object:getvelocity().y == 0
|
||||
and self.jump_chance > 0) then
|
||||
do_jump(self)
|
||||
end]]
|
||||
|
||||
|
||||
|
||||
set_velocity(self, self.walk_velocity)
|
||||
set_animation(self, "walk")
|
||||
else
|
||||
-- we have arrived
|
||||
self.destination = nil
|
||||
self.walk_timer = 0
|
||||
|
||||
-- TODO: bump bttimer to get new directions
|
||||
-- bump bttimer to get new directions
|
||||
self.bt_timer = 99
|
||||
|
||||
set_velocity(self, 0)
|
||||
set_animation(self, "stand")
|
||||
end
|
||||
end
|
||||
|
||||
btdata.lastpos = pos
|
||||
|
||||
end,
|
||||
|
||||
|
||||
on_activate = function(self, staticdata, dtime_s)
|
||||
self.btData = {
|
||||
groupID = "default",
|
||||
|
||||
waypoints= {},
|
||||
paths= {},
|
||||
counters={},
|
||||
|
||||
history={},
|
||||
history_queue={},
|
||||
history_depth=20,
|
||||
on_activate = function(self, staticdata, dtime_s)
|
||||
self.btData = {
|
||||
groupID = "default",
|
||||
|
||||
waypoints= {},
|
||||
paths= {},
|
||||
counters={},
|
||||
|
||||
history={},
|
||||
history_queue={},
|
||||
history_depth=20,
|
||||
|
||||
posStack={},
|
||||
}
|
||||
|
||||
posStack={},
|
||||
}
|
||||
local btdata = self.btData
|
||||
|
||||
self.inv_id= name..":"..math.random(1, 2000000000)
|
||||
--print(btdata.id)
|
||||
|
||||
btdata.lastpos = self.object:getpos()
|
||||
|
||||
local btdata = self.btData
|
||||
if type(def.pre_activate) == "function" then
|
||||
def.pre_activate(self, static_data, dtime_s)
|
||||
end
|
||||
|
||||
self.inv_id= name..":"..math.random(1, 2000000000)
|
||||
--print(btdata.id)
|
||||
|
||||
btdata.lastpos = self.object:getpos()
|
||||
|
||||
if type(def.pre_activate) == "function" then
|
||||
def.pre_activate(self, static_data, dtime_s)
|
||||
end
|
||||
|
||||
-- load entity variables
|
||||
if staticdata then
|
||||
-- load entity variables
|
||||
if staticdata then
|
||||
|
||||
local tmp = minetest.deserialize(staticdata)
|
||||
local tmp = minetest.deserialize(staticdata)
|
||||
|
||||
if tmp then
|
||||
if tmp then
|
||||
|
||||
for _,stat in pairs(tmp) do
|
||||
self[_] = stat
|
||||
for _,stat in pairs(tmp) do
|
||||
self[_] = stat
|
||||
end
|
||||
end
|
||||
else
|
||||
self.object:remove()
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
local inventory = minetest.create_detached_inventory(self.inv_id, {})
|
||||
inventory:set_size("main", 9)
|
||||
|
||||
|
||||
-- select random texture, set model and size
|
||||
if not self.base_texture then
|
||||
|
||||
self.base_texture = def.textures[math.random(1, #def.textures)]
|
||||
self.base_mesh = def.mesh
|
||||
self.base_size = self.visual_size
|
||||
self.base_colbox = self.collisionbox
|
||||
end
|
||||
|
||||
-- set texture, model and size
|
||||
local textures = self.base_texture
|
||||
local mesh = self.base_mesh
|
||||
local vis_size = self.base_size
|
||||
local colbox = self.base_colbox
|
||||
|
||||
-- specific texture if gotten
|
||||
if self.gotten == true
|
||||
and def.gotten_texture then
|
||||
textures = def.gotten_texture
|
||||
end
|
||||
|
||||
-- specific mesh if gotten
|
||||
if self.gotten == true
|
||||
and def.gotten_mesh then
|
||||
mesh = def.gotten_mesh
|
||||
end
|
||||
|
||||
-- set child objects to half size
|
||||
if self.child == true then
|
||||
|
||||
vis_size = {
|
||||
x = self.base_size.x / 2,
|
||||
y = self.base_size.y / 2
|
||||
}
|
||||
|
||||
if def.child_texture then
|
||||
textures = def.child_texture[1]
|
||||
end
|
||||
|
||||
colbox = {
|
||||
self.base_colbox[1] / 2,
|
||||
self.base_colbox[2] / 2,
|
||||
self.base_colbox[3] / 2,
|
||||
self.base_colbox[4] / 2,
|
||||
self.base_colbox[5] / 2,
|
||||
self.base_colbox[6] / 2
|
||||
}
|
||||
end
|
||||
|
||||
if self.health == 0 then
|
||||
self.health = math.random (self.hp_min, self.hp_max)
|
||||
end
|
||||
|
||||
self.object:set_hp(self.health)
|
||||
self.object:set_armor_groups({fleshy = self.armor})
|
||||
self.old_y = self.object:getpos().y
|
||||
self.object:setyaw(math.random(1, 360) / 180 * math.pi)
|
||||
-- self.sounds.distance = (self.sounds.distance or 10)
|
||||
self.textures = textures
|
||||
self.mesh = mesh
|
||||
self.collisionbox = colbox
|
||||
self.visual_size = vis_size
|
||||
|
||||
-- set anything changed above
|
||||
self.object:set_properties(self)
|
||||
update_tag(self)
|
||||
|
||||
if type(def.post_activate) == "function" then
|
||||
def.post_activate(self, static_data, dtime_s)
|
||||
end
|
||||
end,
|
||||
|
||||
get_staticdata = function(self)
|
||||
|
||||
-- remove mob when out of range unless tamed
|
||||
if mobs.remove
|
||||
and self.remove_ok
|
||||
and not self.tamed then
|
||||
|
||||
--print ("REMOVED", self.remove_ok, self.name)
|
||||
|
||||
self.object:remove()
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
self.remove_ok = true
|
||||
self.attack = nil
|
||||
self.following = nil
|
||||
self.state = "stand"
|
||||
|
||||
if self.btData ~= nil then
|
||||
self.btData.inv = nil -- just in case
|
||||
self.btData.mob = nil -- just in case
|
||||
end
|
||||
|
||||
|
||||
local tmp = {}
|
||||
|
||||
for _,stat in pairs(self) do
|
||||
|
||||
local t = type(stat)
|
||||
|
||||
if t ~= 'function'
|
||||
and t ~= 'nil'
|
||||
and t ~= 'userdata' then
|
||||
tmp[_] = self[_]
|
||||
end
|
||||
end
|
||||
else
|
||||
self.object:remove()
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
local inventory = minetest.create_detached_inventory(self.inv_id, {})
|
||||
inventory:set_size("main", 9)
|
||||
|
||||
|
||||
-- select random texture, set model and size
|
||||
if not self.base_texture then
|
||||
|
||||
self.base_texture = def.textures[math.random(1, #def.textures)]
|
||||
self.base_mesh = def.mesh
|
||||
self.base_size = self.visual_size
|
||||
self.base_colbox = self.collisionbox
|
||||
end
|
||||
|
||||
-- set texture, model and size
|
||||
local textures = self.base_texture
|
||||
local mesh = self.base_mesh
|
||||
local vis_size = self.base_size
|
||||
local colbox = self.base_colbox
|
||||
|
||||
-- specific texture if gotten
|
||||
if self.gotten == true
|
||||
and def.gotten_texture then
|
||||
textures = def.gotten_texture
|
||||
end
|
||||
|
||||
-- specific mesh if gotten
|
||||
if self.gotten == true
|
||||
and def.gotten_mesh then
|
||||
mesh = def.gotten_mesh
|
||||
end
|
||||
|
||||
-- set child objects to half size
|
||||
if self.child == true then
|
||||
|
||||
vis_size = {
|
||||
x = self.base_size.x / 2,
|
||||
y = self.base_size.y / 2
|
||||
}
|
||||
|
||||
if def.child_texture then
|
||||
textures = def.child_texture[1]
|
||||
end
|
||||
|
||||
colbox = {
|
||||
self.base_colbox[1] / 2,
|
||||
self.base_colbox[2] / 2,
|
||||
self.base_colbox[3] / 2,
|
||||
self.base_colbox[4] / 2,
|
||||
self.base_colbox[5] / 2,
|
||||
self.base_colbox[6] / 2
|
||||
}
|
||||
end
|
||||
|
||||
if self.health == 0 then
|
||||
self.health = math.random (self.hp_min, self.hp_max)
|
||||
end
|
||||
|
||||
self.object:set_hp(self.health)
|
||||
self.object:set_armor_groups({fleshy = self.armor})
|
||||
self.old_y = self.object:getpos().y
|
||||
self.object:setyaw(math.random(1, 360) / 180 * math.pi)
|
||||
-- self.sounds.distance = (self.sounds.distance or 10)
|
||||
self.textures = textures
|
||||
self.mesh = mesh
|
||||
self.collisionbox = colbox
|
||||
self.visual_size = vis_size
|
||||
|
||||
-- set anything changed above
|
||||
self.object:set_properties(self)
|
||||
update_tag(self)
|
||||
|
||||
if type(def.post_activate) == "function" then
|
||||
def.post_activate(self, static_data, dtime_s)
|
||||
end
|
||||
end,
|
||||
|
||||
get_staticdata = function(self)
|
||||
|
||||
-- remove mob when out of range unless tamed
|
||||
if mobs.remove
|
||||
and self.remove_ok
|
||||
and not self.tamed then
|
||||
|
||||
--print ("REMOVED", self.remove_ok, self.name)
|
||||
|
||||
self.object:remove()
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
self.remove_ok = true
|
||||
self.attack = nil
|
||||
self.following = nil
|
||||
self.state = "stand"
|
||||
|
||||
if self.btData ~= nil then
|
||||
self.btData.inv = nil -- just in case
|
||||
self.btData.mob = nil -- just in case
|
||||
end
|
||||
|
||||
-- used to rotate older mobs
|
||||
if self.drawtype
|
||||
and self.drawtype == "side" then
|
||||
self.rotate = math.rad(90)
|
||||
end
|
||||
|
||||
local tmp = {}
|
||||
|
||||
for _,stat in pairs(self) do
|
||||
|
||||
local t = type(stat)
|
||||
|
||||
if t ~= 'function'
|
||||
and t ~= 'nil'
|
||||
and t ~= 'userdata' then
|
||||
tmp[_] = self[_]
|
||||
end
|
||||
end
|
||||
|
||||
-- print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
|
||||
return minetest.serialize(tmp)
|
||||
end,
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-- print('===== '..self.name..'\n'.. dump(tmp)..'\n=====\n')
|
||||
return minetest.serialize(tmp)
|
||||
end,
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
for k,v in pairs(def) do
|
||||
mdef[k] = v
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
minetest.register_entity(name, mdef)
|
||||
end
|
||||
|
||||
|
|
12
entities.lua
12
entities.lua
|
@ -1,7 +1,7 @@
|
|||
|
||||
function make_bunny(name, behavior_fn)
|
||||
|
||||
mobs:register_simple_mob(mn..":"..name, {
|
||||
mobehavior:register_mob_fast(mn..":"..name, {
|
||||
type = "animal",
|
||||
passive = true,
|
||||
reach = 1,
|
||||
|
@ -62,7 +62,7 @@ end
|
|||
|
||||
function make_wolf(name, behavior_fn)
|
||||
|
||||
mobs:register_simple_mob(mn..":"..name, {
|
||||
mobehavior:register_mob_fast(mn..":"..name, {
|
||||
|
||||
type = "animal",
|
||||
passive = false,
|
||||
|
@ -74,7 +74,7 @@ function make_wolf(name, behavior_fn)
|
|||
visual = "mesh",
|
||||
mesh = "animal_wolf.b3d",
|
||||
drawtype = "front",
|
||||
rotate = 270,
|
||||
rotate = math.pi / -2,
|
||||
textures = {
|
||||
{"animal_wolf_mesh.png"},
|
||||
{"animal_wolf_tamed_mesh.png"},
|
||||
|
@ -124,7 +124,7 @@ end
|
|||
|
||||
function make_bear(name, behavior_fn)
|
||||
|
||||
mobs:register_simple_mob(mn..":"..name, {
|
||||
mobehavior:register_mob_fast(mn..":"..name, {
|
||||
|
||||
type = "animal",
|
||||
passive = false,
|
||||
|
@ -137,7 +137,7 @@ function make_bear(name, behavior_fn)
|
|||
mesh = "mob_bear.b3d",
|
||||
drawtype = "front",
|
||||
visual_size= {x=3,y=3,z=3},
|
||||
rotate = 270,
|
||||
rotate = math.pi / -2,
|
||||
textures = {
|
||||
{"mob_bear_bear_mesh.png"},
|
||||
-- {"mob_bear_bear_tamed_mesh.png"}, -- has a harness
|
||||
|
@ -187,7 +187,7 @@ end
|
|||
|
||||
function make_rat(name, behavior_fn)
|
||||
|
||||
mobs:register_simple_mob(mn..":"..name, {
|
||||
mobehavior:register_mob_fast(mn..":"..name, {
|
||||
type = "animal",
|
||||
passive = true,
|
||||
reach = 1,
|
||||
|
|
60
giant.lua
60
giant.lua
|
@ -112,6 +112,59 @@ local lumberjack = function()
|
|||
})
|
||||
end
|
||||
|
||||
local bare_lumberjack = function()
|
||||
return bt.Sequence("", {
|
||||
|
||||
-- build a chest and remember where it is
|
||||
bt.FindSpotOnGround(),
|
||||
bt.Approach(2),
|
||||
bt.SetNode({name="default:chest"}),
|
||||
-- bt.GetGroupWaypoint("lumber_chest"),
|
||||
bt.SetWaypoint("chest"),
|
||||
|
||||
bt.UntilFailed(bt.Sequence("logs some trees", {
|
||||
|
||||
-- find a tree
|
||||
bt.Selector("find a tree", {
|
||||
bt.Sequence("find a tree near the last one", {
|
||||
bt.GetWaypoint("tree"),
|
||||
bt.FindNodeNear({"group:tree"}, 15),
|
||||
}),
|
||||
bt.FindNodeNear({"group:tree"}, 50),
|
||||
}),
|
||||
bt.Approach(2),
|
||||
|
||||
-- chop it down
|
||||
bt.Invert(bt.UntilFailed(bt.Sequence("chop tree", {
|
||||
bt.Wield("default:axe_steel"),
|
||||
bt.Animate("punch"),
|
||||
bt.FindNodeNear({"group:tree"}, 3), -- gets stuck on aspen and jungle
|
||||
bt.DigNode(),
|
||||
bt.WaitTicks(1),
|
||||
}))),
|
||||
bt.SetWaypointHere("tree"),
|
||||
|
||||
bt.Wield(""),
|
||||
|
||||
|
||||
bt.Succeed(bt.Sequence("pick up saplings", {
|
||||
--bt.FindItemNear("group:sapling", 20),
|
||||
bt.PickUpNearbyItems("group:sapling", 5),
|
||||
})),
|
||||
|
||||
|
||||
-- put wood in chest
|
||||
bt.GetWaypoint("chest"),
|
||||
bt.Approach(2),
|
||||
bt.PutInChest(nil),
|
||||
|
||||
|
||||
bt.WaitTicks(1),
|
||||
--bt.Print("end of loop \n"),
|
||||
}))
|
||||
})
|
||||
end
|
||||
|
||||
local fence_region = function(item)
|
||||
return bt.Sequence("", {
|
||||
|
||||
|
@ -483,7 +536,7 @@ end
|
|||
|
||||
|
||||
|
||||
--[[
|
||||
|
||||
make_wolf("wolf", function()
|
||||
return wander_around(6)
|
||||
end)
|
||||
|
@ -498,9 +551,10 @@ end)
|
|||
make_bear("bear", function()
|
||||
return wander_around(6)
|
||||
end)
|
||||
]]
|
||||
|
||||
|
||||
make_NPC("npc", function()
|
||||
return wander_around(6)
|
||||
-- return wander_around(6)
|
||||
return bare_lumberjack()
|
||||
end)
|
||||
|
||||
|
|
|
@ -119,7 +119,7 @@ minetest.register_abm({
|
|||
|
||||
-- only if light levels are within range
|
||||
if lig and lig >= mlig and lig <= xlig then
|
||||
minetest.add_entity(pos2, mob)
|
||||
-- minetest.add_entity(pos2, mob)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ minetest.register_abm({
|
|||
--print("----------spawning rat")
|
||||
local p = minetest.find_node_near(pos, 3, "air")
|
||||
if p then
|
||||
local mob = minetest.add_entity(p, "mobehavior:rat")
|
||||
-- local mob = minetest.add_entity(p, "mobehavior:rat")
|
||||
end
|
||||
end
|
||||
})
|
||||
|
@ -108,7 +108,7 @@ minetest.register_abm({
|
|||
--print("----------spawning rat")
|
||||
local p = minetest.find_node_near(pos, 3, "air")
|
||||
if p then
|
||||
local mob = minetest.add_entity(p, "mobehavior:bunny")
|
||||
-- local mob = minetest.add_entity(p, "mobehavior:bunny")
|
||||
end
|
||||
end
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue