Misc bugfixes and improvements

This commit is contained in:
stujones11 2015-07-29 20:40:54 +01:00
parent f5df4ebf8a
commit 8a359d62d7
10 changed files with 64 additions and 11315 deletions

View File

@ -184,6 +184,8 @@ Special Properties
------------------ ------------------
Properties used internally by the framework. Properties used internally by the framework.
initialized = false,
activated = false,
properties = {textures=textures}, properties = {textures=textures},
npcf_id = id, npcf_id = id,
owner = owner, owner = owner,
@ -198,31 +200,27 @@ where it may be desireable update the statically saved position.
Callbacks Callbacks
--------- ---------
Additional callbacks provided by the framework. Additional callbacks provided by the framework.
### on_construct = function(self)
This is called before the slightly delayed inbuilt on_activate callback.
Please note that the self.npc_id, self.owner and self.origin properties
may not be available or nil at the time of construction.
### on_destruct = function(self, hitter)
Called when an NPC is destroyed by punching. Can be used to unload the NPC when defeated by a player.
See the Guard NPC for an example.
### on_update = function(npc)
Called every time an NPC object is updated (NPCF_UPDATE_TIME) even when the LuaEntitySAO is not loaded.
Note that 'npc' is a NPC object reference, not a LuaEntitySAO reference. The latter can be obtained from Note that 'npc' is a NPC object reference, not a LuaEntitySAO reference. The latter can be obtained from
npc.object but only when the LuaEntitySAO is loaded. npc.object but only when the LuaEntitySAO is loaded.
## on_tell = function(npc, sender, message) ### on_construct = function(npc)
Called when the 'tell' chat command is issued. Once again here, 'npc' is a NPC object reference rather Called when the NPC object is first created.
than a LuaEntitySAO reference. A LuaEntitySAO reference is available when loaded.
This Behavior diverges from version 0.1.0, however, it does now allow for interaction even when the ### on_update = function(npc)
associated LuaEntitySAO not loaded. Called every time an NPC object is updated (NPCF_UPDATE_TIME) even when the LuaEntitySAO is not loaded.
### on_tell = function(npc, sender, message)
Called when the 'tell' chat command is issued. Note that this Behavior diverges from version 0.1.0,
however, it does now allow for interaction even when the associated LuaEntitySAO not loaded.
### on_receive_fields = function(self, fields, sender) ### on_receive_fields = function(self, fields, sender)
Called when a button is pressed in the NPC's formspec. text fields, dropdown, Called when a button is pressed in the NPC's formspec. text fields, dropdown,
list and checkbox selections are automatically stored in the metadata table. list and checkbox selections are automatically stored in the metadata table.
### on_destruct = function(self, hitter)
Called when an NPC is destroyed by punching. Can be used to unload the NPC when defeated by a player.
See the Guard NPC for an example.
npcf npcf
---- ----
The global NPC framework namespace. The global NPC framework namespace.
@ -241,17 +239,17 @@ The global NPC framework namespace.
All of the above can be overridden by including a npcf.conf file in the npcf directory. All of the above can be overridden by including a npcf.conf file in the npcf directory.
See: npcf.conf.example See: npcf.conf.example
## npcf.index ### npcf.index
Ownership table of all spawned NPCs (loaded or unloaded) Ownership table of all spawned NPCs (loaded or unloaded)
npcf.index[id] = owner -- owner's name npcf.index[id] = owner -- owner's name
## npcf.npcs ### npcf.npcs
Table of loaded NPC object references. Table of loaded NPC object references.
## npcf.npc ### npcf.npc
NPC object prototype. NPC object prototype.
@ -259,7 +257,7 @@ NPC object prototype.
timer = 0, timer = 0,
object = nil -- LuaEntitySAO added as required object = nil -- LuaEntitySAO added as required
## npcf.npc:new(ref) ### npcf.npc:new(ref)
Create a new NPC object instance. Create a new NPC object instance.
@ -282,7 +280,7 @@ Create a new NPC object instance.
If used directly then it is the caller's resposibilty to store the reference and update the index. If used directly then it is the caller's resposibilty to store the reference and update the index.
Use: npcf:add_npc(ref) instead to have this done automatically by the framework. Use: npcf:add_npc(ref) instead to have this done automatically by the framework.
## npc:update() ### npc:update()
Update the NPC object. Adds a LuaEntitySAO when in range of players (NPCF_RELOAD_DISTANCE) Update the NPC object. Adds a LuaEntitySAO when in range of players (NPCF_RELOAD_DISTANCE)
Called automatically on global step (NPCF_UPDATE_TIME) for all loaded NPC objects. Called automatically on global step (NPCF_UPDATE_TIME) for all loaded NPC objects.

View File

@ -104,3 +104,9 @@ minetest.register_globalstep(function(dtime)
end end
end) end)
minetest.register_on_shutdown(function()
for id, npc in pairs(npcf.npcs) do
npcf:save(id)
end
end)

View File

@ -148,6 +148,7 @@ function npcf:add_entity(ref)
entity.var = ref.var entity.var = ref.var
entity.owner = ref.owner entity.owner = ref.owner
entity.origin = ref.origin entity.origin = ref.origin
entity.initialized = true
return object return object
end end
end end
@ -158,21 +159,28 @@ function npcf:add_npc(ref)
ref.name = NPCF_ALIAS[ref.name] or ref.name ref.name = NPCF_ALIAS[ref.name] or ref.name
local def = deepcopy(minetest.registered_entities[ref.name]) local def = deepcopy(minetest.registered_entities[ref.name])
if def then if def then
ref.metadata = ref.metadata or {}
if type(def.metadata) == "table" then
for k, v in pairs(def.metadata) do for k, v in pairs(def.metadata) do
if ref.metadata[k] == nil then if ref.metadata[k] == nil then
ref.metadata[k] = v ref.metadata[k] = v
end end
end end
end
ref.yaw = ref.yaw or {x=0, y=0, z=0} ref.yaw = ref.yaw or {x=0, y=0, z=0}
ref.title = ref.title or def.title ref.title = ref.title or def.title
ref.properties = {textures=ref.textures or def.textures} ref.properties = {textures=ref.textures or def.textures}
ref.metadata = ref.metadata
ref.var = ref.var or def.var ref.var = ref.var or def.var
if not ref.origin then
ref.origin = { ref.origin = {
pos = ref.pos, pos = ref.pos,
yaw = ref.yaw, yaw = ref.yaw,
} }
end
local npc = npcf.npc:new(ref) local npc = npcf.npc:new(ref)
if type(def.on_construct) == "function" then
def.on_construct(npc)
end
npcf.npcs[ref.id] = npc npcf.npcs[ref.id] = npc
npcf.index[ref.id] = ref.owner npcf.index[ref.id] = ref.owner
return npc return npc
@ -188,6 +196,8 @@ function npcf:register_npc(name, def)
ref[k] = v ref[k] = v
end end
end end
ref.initialized = false
ref.activated = false
ref.on_activate = function(self, staticdata) ref.on_activate = function(self, staticdata)
if staticdata == "expired" then if staticdata == "expired" then
self.object:remove() self.object:remove()
@ -196,14 +206,6 @@ function npcf:register_npc(name, def)
if self.object then if self.object then
self.object:set_armor_groups(def.armor_groups) self.object:set_armor_groups(def.armor_groups)
end end
if type(ref.on_construct) == "function" then
ref.on_construct(self)
end
if type(def.on_activate) == "function" then
minetest.after(0.5, function()
def.on_activate(self)
end)
end
end end
ref.on_rightclick = function(self, clicker) ref.on_rightclick = function(self, clicker)
local id = self.npc_id local id = self.npc_id
@ -234,10 +236,19 @@ function npcf:register_npc(name, def)
end end
end end
ref.on_step = function(self, dtime) ref.on_step = function(self, dtime)
self.timer = self.timer + dtime if self.initialized == true then
if self.activated == true then
if type(def.on_step) == "function" then if type(def.on_step) == "function" then
self.timer = self.timer + dtime
def.on_step(self, dtime) def.on_step(self, dtime)
end end
else
if type(def.on_activate) == "function" then
def.on_activate(self)
end
self.activated = true
end
end
end end
ref.get_staticdata = function(self) ref.get_staticdata = function(self)
return "expired" return "expired"
@ -338,8 +349,8 @@ function npcf:load(id)
local ref = minetest.deserialize(input:read('*all')) local ref = minetest.deserialize(input:read('*all'))
io.close(input) io.close(input)
ref.id = id ref.id = id
ref.pos = ref.origin.pos ref.pos = ref.pos or ref.origin.pos
ref.yaw = ref.origin.yaw ref.yaw = ref.yaw or ref.origin.yaw
return npcf:add_npc(ref) return npcf:add_npc(ref)
end end
minetest.log("error", "Failed to laod NPC: "..id) minetest.log("error", "Failed to laod NPC: "..id)
@ -349,6 +360,8 @@ function npcf:save(id)
local npc = self.npcs[id] local npc = self.npcs[id]
if npc then if npc then
local ref = { local ref = {
pos = npc.pos,
yaw = npc.yaw,
name = npc.name, name = npc.name,
owner = npc.owner, owner = npc.owner,
title = { title = {

View File

@ -152,15 +152,13 @@ npcf:register_npc("npcf_builder:npc" ,{
}, },
stepheight = 1.1, stepheight = 1.1,
inventory_image = "npcf_builder_inv.png", inventory_image = "npcf_builder_inv.png",
on_construct = function(self) on_activate = function(self)
self.metadata.building = false
self.object:setvelocity({x=0, y=0, z=0}) self.object:setvelocity({x=0, y=0, z=0})
self.object:setacceleration({x=0, y=-10, z=0}) self.object:setacceleration({x=0, y=-10, z=0})
self.metadata.building = false
if self.metadata.schematic and self.metadata.build_pos then if self.metadata.schematic and self.metadata.build_pos then
load_schematic(self, self.metadata.schematic) load_schematic(self, self.metadata.schematic)
end end
end,
on_activate = function(self)
local inv = minetest.create_detached_inventory("npcf_"..self.npc_id, { local inv = minetest.create_detached_inventory("npcf_"..self.npc_id, {
on_put = function(inv, listname, index, stack, player) on_put = function(inv, listname, index, stack, player)
local player_name = player:get_player_name() local player_name = player:get_player_name()

View File

@ -67,12 +67,10 @@ npcf:register_npc("npcf_deco:npc" ,{
}, },
stepheight = 1.1, stepheight = 1.1,
inventory_image = "npcf_deco_inv.png", inventory_image = "npcf_deco_inv.png",
on_construct = function(self) on_activate = function(self)
self.object:setvelocity({x=0, y=0, z=0}) self.object:setvelocity({x=0, y=0, z=0})
self.object:setacceleration({x=0, y=-10, z=0}) self.object:setacceleration({x=0, y=-10, z=0})
npcf:set_animation(self, ANIMATION[self.metadata.anim_stop].state) npcf:set_animation(self, ANIMATION[self.metadata.anim_stop].state)
end,
on_activate = function(self)
if self.metadata.follow_players == "true" then if self.metadata.follow_players == "true" then
self.var.target = get_target_player(self) self.var.target = get_target_player(self)
end end

View File

@ -71,7 +71,6 @@ end
npcf:register_npc("npcf_mob:npc", { npcf:register_npc("npcf_mob:npc", {
description = "Mob NPC", description = "Mob NPC",
mesh = "npcf_mob.b3d",
textures = {"npcf_mob_skin.png"}, textures = {"npcf_mob_skin.png"},
collisionbox = {-0.35,-1.0,-0.35, 0.35,0.5,0.35}, collisionbox = {-0.35,-1.0,-0.35, 0.35,0.5,0.35},
animation_speed = 25, animation_speed = 25,
@ -97,7 +96,7 @@ npcf:register_npc("npcf_mob:npc", {
end end
end end
end, end,
on_construct = function(self) on_activate = function(self)
self.object:setvelocity({x=0, y=0, z=0}) self.object:setvelocity({x=0, y=0, z=0})
self.object:setacceleration({x=0, y=-10, z=0}) self.object:setacceleration({x=0, y=-10, z=0})
end, end,

Binary file not shown.

Binary file not shown.

View File

@ -91,7 +91,6 @@ end
npcf:register_npc("npcf_trader:npc" ,{ npcf:register_npc("npcf_trader:npc" ,{
description = "Trader NPC", description = "Trader NPC",
mesh = "npcf_trader.x",
textures = {"npcf_trader_skin.png"}, textures = {"npcf_trader_skin.png"},
metadata = { metadata = {
trades = {}, trades = {},

File diff suppressed because it is too large Load Diff