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.
initialized = false,
activated = false,
properties = {textures=textures},
npcf_id = id,
owner = owner,
@ -198,31 +200,27 @@ where it may be desireable update the statically saved position.
Callbacks
---------
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
npc.object but only when the LuaEntitySAO is loaded.
## on_tell = function(npc, sender, message)
Called when the 'tell' chat command is issued. Once again here, 'npc' is a NPC object reference rather
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
associated LuaEntitySAO not loaded.
### on_construct = function(npc)
Called when the NPC object is first created.
### on_update = function(npc)
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)
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.
### 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
----
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.
See: npcf.conf.example
## npcf.index
### npcf.index
Ownership table of all spawned NPCs (loaded or unloaded)
npcf.index[id] = owner -- owner's name
## npcf.npcs
### npcf.npcs
Table of loaded NPC object references.
## npcf.npc
### npcf.npc
NPC object prototype.
@ -259,7 +257,7 @@ NPC object prototype.
timer = 0,
object = nil -- LuaEntitySAO added as required
## npcf.npc:new(ref)
### npcf.npc:new(ref)
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.
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)
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)
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.owner = ref.owner
entity.origin = ref.origin
entity.initialized = true
return object
end
end
@ -158,21 +159,28 @@ function npcf:add_npc(ref)
ref.name = NPCF_ALIAS[ref.name] or ref.name
local def = deepcopy(minetest.registered_entities[ref.name])
if def then
for k, v in pairs(def.metadata) do
if ref.metadata[k] == nil then
ref.metadata[k] = v
ref.metadata = ref.metadata or {}
if type(def.metadata) == "table" then
for k, v in pairs(def.metadata) do
if ref.metadata[k] == nil then
ref.metadata[k] = v
end
end
end
ref.yaw = ref.yaw or {x=0, y=0, z=0}
ref.title = ref.title or def.title
ref.properties = {textures=ref.textures or def.textures}
ref.metadata = ref.metadata
ref.var = ref.var or def.var
ref.origin = {
pos = ref.pos,
yaw = ref.yaw,
}
if not ref.origin then
ref.origin = {
pos = ref.pos,
yaw = ref.yaw,
}
end
local npc = npcf.npc:new(ref)
if type(def.on_construct) == "function" then
def.on_construct(npc)
end
npcf.npcs[ref.id] = npc
npcf.index[ref.id] = ref.owner
return npc
@ -188,6 +196,8 @@ function npcf:register_npc(name, def)
ref[k] = v
end
end
ref.initialized = false
ref.activated = false
ref.on_activate = function(self, staticdata)
if staticdata == "expired" then
self.object:remove()
@ -196,14 +206,6 @@ function npcf:register_npc(name, def)
if self.object then
self.object:set_armor_groups(def.armor_groups)
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
ref.on_rightclick = function(self, clicker)
local id = self.npc_id
@ -234,9 +236,18 @@ function npcf:register_npc(name, def)
end
end
ref.on_step = function(self, dtime)
self.timer = self.timer + dtime
if type(def.on_step) == "function" then
def.on_step(self, dtime)
if self.initialized == true then
if self.activated == true then
if type(def.on_step) == "function" then
self.timer = self.timer + dtime
def.on_step(self, dtime)
end
else
if type(def.on_activate) == "function" then
def.on_activate(self)
end
self.activated = true
end
end
end
ref.get_staticdata = function(self)
@ -338,8 +349,8 @@ function npcf:load(id)
local ref = minetest.deserialize(input:read('*all'))
io.close(input)
ref.id = id
ref.pos = ref.origin.pos
ref.yaw = ref.origin.yaw
ref.pos = ref.pos or ref.origin.pos
ref.yaw = ref.yaw or ref.origin.yaw
return npcf:add_npc(ref)
end
minetest.log("error", "Failed to laod NPC: "..id)
@ -349,6 +360,8 @@ function npcf:save(id)
local npc = self.npcs[id]
if npc then
local ref = {
pos = npc.pos,
yaw = npc.yaw,
name = npc.name,
owner = npc.owner,
title = {

View File

@ -152,15 +152,13 @@ npcf:register_npc("npcf_builder:npc" ,{
},
stepheight = 1.1,
inventory_image = "npcf_builder_inv.png",
on_construct = function(self)
self.metadata.building = false
on_activate = function(self)
self.object:setvelocity({x=0, y=0, 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
load_schematic(self, self.metadata.schematic)
end
end,
on_activate = function(self)
local inv = minetest.create_detached_inventory("npcf_"..self.npc_id, {
on_put = function(inv, listname, index, stack, player)
local player_name = player:get_player_name()

View File

@ -67,12 +67,10 @@ npcf:register_npc("npcf_deco:npc" ,{
},
stepheight = 1.1,
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:setacceleration({x=0, y=-10, z=0})
npcf:set_animation(self, ANIMATION[self.metadata.anim_stop].state)
end,
on_activate = function(self)
if self.metadata.follow_players == "true" then
self.var.target = get_target_player(self)
end

View File

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

Binary file not shown.

Binary file not shown.

View File

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

File diff suppressed because it is too large Load Diff