entity_ai/factors.lua
Auke Kok 8a62d34438 Major refactor.
All step code is now part of the driver engine:
- sound steps
- factor accounting
- factor-based switching
- animation step handling.

Moreover, there is no more local factor state subtable,
and factors need to return or pass factordata to the
driver method. Factordata is only passed to the .start()
method as well, but that can then store the needed data
into the entity_ai_state as needed by the driver itself.

This still has an issue with entity animation sequence
ends, as far as I can see, in the sheep eat driver. This
will need further work.

finders, drivers and factors are now all in separate files.

The sheep script has been partially cleaned up to account
for the differences

Many asserts were added. We need to keep these until things
become more stable, and possibly add a ton more of them to
make sure we're not passing garbage.

A luacheck file is added to keep things sane going forward.
2017-04-05 00:28:22 -07:00

61 lines
1.4 KiB
Lua

--[[
Copyright (c) 2016 - Auke Kok <sofar@foo-projects.org>
* entity_ai is licensed as follows:
- All code is: GNU Affero General Public License, Version 3.0 (AGPL-3.0)
- All artwork is: CC-BY-ND-4.0
A Contributor License Agreement exists, please read:
- https://github.com/sofar/entity_ai/readme.md.
--]]
entity_ai.register_factor("near_foodnode", function(self, dtime)
local state = self.entity_ai_state
-- still fed?
if state.ate_enough and state.ate_enough > 0 then
state.ate_enough = state.ate_enough - dtime
return
end
state.ate_enough = nil
-- don't check too often
if state.near_foodnode_ttl and state.near_foodnode_ttl > 0 then
state.near_foodnode_ttl = state.near_foodnode_ttl - dtime
return
end
state.near_foodnode_ttl = 2.0
local pos = vector.round(self.object:getpos())
local yaw = self.object:getyaw()
self.yaw = yaw
local offset = minetest.yaw_to_dir(yaw)
local maxp = vector.add(pos, offset)
local minp = vector.subtract(maxp, {x = 0, y = 1, z = 0 })
local nodes = minetest.find_nodes_in_area(minp, maxp, self.driver:get_property("foodnodes"))
if #nodes == 0 then
return
end
--[[ minetest.add_particle({
pos = maxp,
velocity = vector.new(),
acceleration = vector.new(),
expirationtime = 3,
size = 6,
collisiondetection = false,
vertical = false,
texture = "wool_pink.png",
playername = nil
})
--]]
-- store grass node in our factor result - take topmost in list
return nodes[#nodes]
end)