Spawner: Large code refactor to remove dependency on plotmarkers.

Most spawner functions can now be called without giving a plotmarker.
Move scanning functions to places.lua.
Places: Cleanup and add more area-scanning functions.
Schedules: Bugfix where schedules weren't being executed due to wrong
"end" order in the do_custom() function.
Data: Moved random data to "data" folder.
Textures: Add 14 male textures and 10 female textures.
Occupations: Small tweaks to "default_basic" occupation.
This commit is contained in:
Hector Franqui 2017-08-25 09:31:45 -04:00
parent 698d247aba
commit 4c102a70a4
28 changed files with 816 additions and 741 deletions

View File

@ -155,6 +155,12 @@ function npc.places.get_by_type(self, place_type)
return result
end
---------------------------------------------------------------------------------------
-- Utility functions
---------------------------------------------------------------------------------------
-- The following are utility functions that are used to operate on nodes for
-- specific conditions
-- This function searches on a squared are of the given radius
-- for nodes of the given type. The type should be npc.places.nodes
function npc.places.find_node_nearby(pos, type, radius)
@ -188,11 +194,64 @@ function npc.places.find_node_orthogonally(pos, nodes, y_adjustment)
return result
end
-- Wrapper around minetest.find_nodes_in_area()
-- TODO: Verify if this wrapper is actually needed
function npc.places.find_node_in_area(start_pos, end_pos, type)
local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type)
return nodes
end
-- Function used to filter all nodes in the first floor of a building
-- If floor height isn't given, it will assume 2
-- Notice that nodes is an array of entries {node_pos={}, type={}}
function npc.places.filter_first_floor_nodes(nodes, ground_pos, floor_height)
local height = floor_height or 2
local result = {}
for _,node in pairs(nodes) do
if node.node_pos.y <= ground_pos.y + height then
table.insert(result, node)
end
end
return result
end
-- Creates an array of {pos=<node_pos>, owner=''} for managing
-- which NPC owns what
function npc.places.get_nodes_by_type(start_pos, end_pos, type)
local result = {}
local nodes = npc.places.find_node_in_area(start_pos, end_pos, type)
--minetest.log("Found "..dump(#nodes).." nodes of type: "..dump(type))
for _,node_pos in pairs(nodes) do
local entry = {}
entry["node_pos"] = node_pos
entry["owner"] = ''
table.insert(result, entry)
end
return result
end
-- Scans an area for the supported nodes: beds, benches,
-- furnaces, storage (e.g. chests) and openable (e.g. doors).
-- Returns a table with these classifications
function npc.places.scan_area_for_usable_nodes(pos1, pos2)
local result = {
bed_type = {},
sittable_type = {},
furnace_type = {},
storage_type = {},
openable_type = {}
}
local start_pos, end_pos = vector.sort(pos1, pos2)
result.bed_type = npc.places.get_nodes_by_type(start_pos, end_pos, npc.places.nodes.BED_TYPE)
result.sittable_type = npc.places.get_nodes_by_type(start_pos, end_pos, npc.places.nodes.SITTABLE_TYPE)
result.furnace_type = npc.places.get_nodes_by_type(start_pos, end_pos, npc.places.nodes.FURNACE_TYPE)
result.storage_type = npc.places.get_nodes_by_type(start_pos, end_pos, npc.places.nodes.STORAGE_TYPE)
result.openable_type = npc.places.get_nodes_by_type(start_pos, end_pos, npc.places.nodes.OPENABLE_TYPE)
return result
end
-- Specialized function to find doors that are an entrance to a building.
-- The definition of an entrance is:
-- The openable node with the shortest path to the plotmarker node
@ -231,18 +290,8 @@ function npc.places.find_entrance_from_openable_nodes(all_openable_nodes, marker
local start_pos = {x=open_pos.x, y=open_pos.y, z=open_pos.z}
local end_pos = {x=marker_pos.x, y=marker_pos.y, z=marker_pos.z}
-- Check if there's any difference in vertical position
-- minetest.log("Openable node pos: "..minetest.pos_to_string(open_pos))
-- minetest.log("Plotmarker node pos: "..minetest.pos_to_string(marker_pos))
-- NOTE: Commented out while testing MarkBu's pathfinder
--if start_pos.y ~= end_pos.y then
-- Adjust to make pathfinder find nodes one node above
-- end_pos.y = start_pos.y
--end
-- This adjustment allows the map to be created correctly
--start_pos.y = start_pos.y + 1
--end_pos.y = end_pos.y + 1
-- Find path from the openable node to the plotmarker
--local path = pathfinder.find_path(start_pos, end_pos, 20, {})

View File

@ -37,9 +37,9 @@ dofile(path .. "/actions/node_registry.lua")
dofile(path .. "/occupations/occupations.lua")
-- Load random data definitions
dofile(path .. "/random_data.lua")
dofile(path .. "/random_data/dialogues_data.lua")
dofile(path .. "/random_data/gift_items_data.lua")
dofile(path .. "/random_data/names_data.lua")
dofile(path .. "/random_data/occupations_data.lua")
dofile(path .. "/data/dialogues_data.lua")
dofile(path .. "/data/gift_items_data.lua")
dofile(path .. "/data/names_data.lua")
dofile(path .. "/data/occupations_data.lua")
print (S("[Mod] Advanced NPC loaded"))

225
npc.lua
View File

@ -474,7 +474,8 @@ function npc.initialize(entity, pos, is_lua_entity, npc_stats, occupation_name)
-- If occupation name given, override properties with
-- occupation values and initialize schedules
minetest.log("Entity age: "..dump(ent.age)..", afult? "..dump(ent.age==npc.age.adult))
--minetest.log("Entity age: "..dump(ent.age)..", afult? "..dump(ent.age==npc.age.adult))
npc.log("INFO", "Overriding NPC values with occupation: "..dump(occupation_name))
if occupation_name and occupation_name ~= "" and ent.age == npc.age.adult then
npc.occupations.initialize_occupation_values(ent, occupation_name)
end
@ -508,7 +509,7 @@ function npc.initialize(entity, pos, is_lua_entity, npc_stats, occupation_name)
-- Set initialized flag on
ent.initialized = true
npc.log("WARNING", "Spawned entity: "..dump(ent))
--npc.log("WARNING", "Spawned entity: "..dump(ent))
npc.log("INFO", "Successfully initialized NPC with name "..dump(ent.npc_name)
..", sex: "..ent.sex..", is child: "..dump(ent.is_child)
..", texture: "..dump(ent.textures))
@ -1156,7 +1157,26 @@ mobs:register_mob("advanced_npc:npc", {
{"npc_male4.png"},
{"npc_male5.png"},
{"npc_male6.png"},
{"npc_male7.png"},
{"npc_male8.png"},
{"npc_male9.png"},
{"npc_male10.png"},
{"npc_male11.png"},
{"npc_male12.png"},
{"npc_male13.png"},
{"npc_male14.png"},
{"npc_male15.png"},
{"npc_female1.png"}, -- female by nuttmeg20
{"npc_female2.png"},
{"npc_female3.png"},
{"npc_female4.png"},
{"npc_female5.png"},
{"npc_female6.png"},
{"npc_female7.png"},
{"npc_female8.png"},
{"npc_female9.png"},
{"npc_female10.png"},
{"npc_female11.png"},
},
child_texture = {
{"npc_child_male1.png"},
@ -1206,7 +1226,7 @@ mobs:register_mob("advanced_npc:npc", {
local item = clicker:get_wielded_item()
local name = clicker:get_player_name()
npc.log("DEBUG", "Right-clicked NPC: "..dump(self))
npc.log("INFO", "Right-clicked NPC: "..dump(self))
-- Receive gift or start chat. If player has no item in hand
-- then it is going to start chat directly
@ -1232,11 +1252,10 @@ mobs:register_mob("advanced_npc:npc", {
end,
name
)
else
npc.start_dialogue(self, clicker, true)
end
end,
else
npc.start_dialogue(self, clicker, true)
end
end,
do_custom = function(self, dtime)
if self.initialized == nil then
-- Initialize NPC if spawned using the spawn egg built in from
@ -1266,7 +1285,8 @@ mobs:register_mob("advanced_npc:npc", {
-- Set interval to large interval so this code isn't called frequently
npc.texture_check.interval = 60
end
end
end
end
-- Timer function for casual traders to reset their trade offers
self.trader_data.change_offers_timer = self.trader_data.change_offers_timer + dtime
@ -1335,103 +1355,102 @@ mobs:register_mob("advanced_npc:npc", {
end
end
-- Schedule timer
-- Check if schedules are enabled
if self.schedules.enabled == true then
-- Get time of day
local time = get_time_in_hours()
-- Check if time is an hour
if time % 1 < 0.1 and self.schedules.lock == false then
-- Activate lock to avoid more than one entry to this code
self.schedules.lock = true
-- Get integer part of time
time = (time) - (time % 1)
-- Check if there is a schedule entry for this time
-- Note: Currently only one schedule is supported, for day 0
--minetest.log("Time: "..dump(time))
local schedule = self.schedules.generic[0]
if schedule ~= nil then
-- Check if schedule for this time exists
--minetest.log("Found default schedule")
if schedule[time] ~= nil then
npc.log("WARNING", "Found schedule for time "..dump(time)..": "..dump(schedule[time]))
npc.log("DEBUG", "Adding actions to action queue")
-- Add to action queue all actions on schedule
for i = 1, #schedule[time] do
-- Check if schedule has a check function
if not schedule[time][i].check then
-- Add parameters for check function and run for first time
npc.log("INFO", "NPC "..dump(self.npc_name).." is starting check on "..minetest.pos_to_string(self.object:getpos()))
local check_params = schedule[time][i]
-- Calculates how many times check will be executed
local execution_times = check_params.count
if check_params.random_execution_times then
execution_times = math.random(check_params.min_count, check_params.max_count)
end
-- Set current parameters
self.schedules.current_check_params = {
range = check_params.range,
nodes = check_params.nodes,
actions = check_params.actions,
none_actions = check_params.none_actions,
execution_count = 0,
execution_times = execution_times
}
-- Execute check for the first time
npc.schedule_check(self)
else
-- Run usual schedule entry
-- Check chance
local execution_chance = math.random(1, 100)
if not schedule[time][i].chance or
(schedule[time][i].chance and execution_chance <= schedule[time][i].chance) then
-- Check if entry has dependency on other entry
local dependencies_met = nil
if schedule[time][i].depends then
dependencies_met = npc.utils.array_is_subset_of_array(
self.schedules.temp_executed_queue,
schedule[time][i].depends)
end
-- Schedule timer
-- Check if schedules are enabled
if self.schedules.enabled == true then
-- Get time of day
local time = get_time_in_hours()
-- Check if time is an hour
if ((time % 1) < dtime) and self.schedules.lock == false then
-- Activate lock to avoid more than one entry to this code
self.schedules.lock = true
-- Get integer part of time
time = (time) - (time % 1)
-- Check if there is a schedule entry for this time
-- Note: Currently only one schedule is supported, for day 0
minetest.log("Time: "..dump(time))
local schedule = self.schedules.generic[0]
if schedule ~= nil then
-- Check if schedule for this time exists
if schedule[time] ~= nil then
npc.log("WARNING", "Found schedule for time "..dump(time)..": "..dump(schedule[time]))
npc.log("DEBUG", "Adding actions to action queue")
-- Add to action queue all actions on schedule
for i = 1, #schedule[time] do
-- Check if schedule has a check function
if schedule[time][i].check then
-- Add parameters for check function and run for first time
npc.log("INFO", "NPC "..dump(self.npc_name).." is starting check on "..minetest.pos_to_string(self.object:getpos()))
local check_params = schedule[time][i]
-- Calculates how many times check will be executed
local execution_times = check_params.count
if check_params.random_execution_times then
execution_times = math.random(check_params.min_count, check_params.max_count)
end
-- Set current parameters
self.schedules.current_check_params = {
range = check_params.range,
nodes = check_params.nodes,
actions = check_params.actions,
none_actions = check_params.none_actions,
execution_count = 0,
execution_times = execution_times
}
-- Execute check for the first time
npc.schedule_check(self)
else
npc.log("INFO", "Executing schedule entry: "..dump(schedule[time][i]))
-- Run usual schedule entry
-- Check chance
local execution_chance = math.random(1, 100)
if not schedule[time][i].chance or
(schedule[time][i].chance and execution_chance <= schedule[time][i].chance) then
-- Check if entry has dependency on other entry
local dependencies_met = nil
if schedule[time][i].depends then
dependencies_met = npc.utils.array_is_subset_of_array(
self.schedules.temp_executed_queue,
schedule[time][i].depends)
end
-- Check for dependencies being met
if dependencies_met == nil or dependencies_met == true then
-- Add tasks
if schedule[time][i].task ~= nil then
-- Add task
npc.add_task(self, schedule[time][i].task, schedule[time][i].args)
elseif schedule[time][i].action ~= nil then
-- Add action
npc.add_action(self, schedule[time][i].action, schedule[time][i].args)
elseif schedule[time][i].property ~= nil then
-- Change NPC property
npc.schedule_change_property(self, schedule[time][i].property, schedule[time][i].args)
end
-- Backward compatibility check
if self.schedules.temp_executed_queue then
-- Add into execution queue to meet dependency
table.insert(self.schedules.temp_executed_queue, i)
end
-- Check for dependencies being met
if dependencies_met == nil or dependencies_met == true then
-- Add tasks
if schedule[time][i].task ~= nil then
-- Add task
npc.add_task(self, schedule[time][i].task, schedule[time][i].args)
elseif schedule[time][i].action ~= nil then
-- Add action
npc.add_action(self, schedule[time][i].action, schedule[time][i].args)
elseif schedule[time][i].property ~= nil then
-- Change NPC property
npc.schedule_change_property(self, schedule[time][i].property, schedule[time][i].args)
end
-- Backward compatibility check
if self.schedules.temp_executed_queue then
-- Add into execution queue to meet dependency
table.insert(self.schedules.temp_executed_queue, i)
end
else
-- TODO: Change to debug
npc.log("WARNING", "Skipping schedule entry for time "..dump(time)..": "..dump(schedule[time][i]))
end
end
end
-- Clear execution queue
self.schedules.temp_executed_queue = {}
npc.log("WARNING", "New action queue: "..dump(self.actions))
end
end
end
else
-- Check if lock can be released
if time % 1 > 0.1 then
-- Release lock
self.schedules.lock = false
end
end
end
else
-- TODO: Change to debug
npc.log("WARNING", "Skipping schedule entry for time "..dump(time)..": "..dump(schedule[time][i]))
end
end
end
-- Clear execution queue
self.schedules.temp_executed_queue = {}
npc.log("WARNING", "New action queue: "..dump(self.actions))
end
end
else
-- Check if lock can be released
if (time % 1) > dtime then
-- Release lock
self.schedules.lock = false
end
end
end
return self.freeze
end

View File

@ -95,10 +95,7 @@ npc.occupations.registered_occupations = {}
-- don't have a specific occupation. It serves as an example.
npc.occupations.basic_def = {
-- Use random textures
textures = {
{"npc_male1.png"},
{"npc_child_male1.png"}
},
textures = {},
-- Use random dialogues
dialogues = {},
-- Initialize inventory with random items
@ -240,6 +237,7 @@ npc.occupations.basic_def = {
-- This function registers an occupation
function npc.occupations.register_occupation(name, def)
npc.occupations.registered_occupations[name] = def
npc.log("INFO", "Successfully registered occupation with name: "..dump(name))
end
-- This function scans all registered occupations and filter them by

File diff suppressed because it is too large Load Diff

3
spawner_marker.lua Normal file
View File

@ -0,0 +1,3 @@
-- Spawner markers
-- Specialized functionality to allow players do NPC spawning
-- on their own custom buildings.

BIN
textures/npc_female10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
textures/npc_female11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
textures/npc_female2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
textures/npc_female3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
textures/npc_female4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
textures/npc_female5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
textures/npc_female6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

BIN
textures/npc_female7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
textures/npc_female8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
textures/npc_female9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
textures/npc_male10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
textures/npc_male11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
textures/npc_male12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
textures/npc_male13.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
textures/npc_male14.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
textures/npc_male15.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
textures/npc_male8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
textures/npc_male9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB