-
Notifications
You must be signed in to change notification settings - Fork 2
/
finders.lua
124 lines (110 loc) · 2.75 KB
/
finders.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
--[[
Copyright (c) 2016-2019 - Auke Kok <[email protected]>
* entity_ai is licensed as follows:
- All code is: LGPL-2.1
- All artwork is: CC-BY-SA-4.0
--]]
entity_ai.register_finder("find_habitat", function(self)
local pos = self.object:getpos()
local minp, maxp = vector.sort({
x = math.random(pos.x - 10, pos.x + 10),
y = pos.y - 5,
z = math.random(pos.z - 10, pos.z + 10)
}, {
x = math.random(pos.x - 10, pos.x + 10),
y = pos.y + 5,
z = math.random(pos.z - 10, pos.z + 10)
})
local nodes = minetest.find_nodes_in_area_under_air(minp, maxp, self.driver:get_property("habitatnodes"))
if #nodes == 0 then
return nil
end
local pick = nodes[math.random(1, #nodes)]
-- find top walkable node
while true do
local node = minetest.get_node(pick)
if not minetest.registered_nodes[node.name].walkable then
pick.y = pick.y - 1
else
-- one up at the end
pick.y = pick.y + 1
break
end
end
-- move to the top surface of pick
if not pick then
return nil
end
--[[ minetest.add_particle({
pos = {x = pick.x, y = pick.y - 0.1, z = pick.z},
velocity = vector.new(),
acceleration = vector.new(),
expirationtime = 3,
size = 6,
collisiondetection = false,
vertical = false,
texture = "wool_red.png",
playername = nil
})
--]]
return pick
end)
entity_ai.register_finder("flee_attacker", function(self)
local state = self.entity_ai_state
local from = state.attacked_at
if state.attacker and state.attacker ~= "" then
local player = minetest.get_player_by_name(state.attacker)
if player then
from = player:getpos()
end
end
if not from then
from = self.object:getpos()
state.attacked_at = from
end
from = vector.round(from)
local pos = self.object:getpos()
local dir = vector.subtract(pos, from)
dir = vector.normalize(dir)
dir = vector.multiply(dir, 10)
local to = vector.add(pos, dir)
local nodes = minetest.find_nodes_in_area_under_air(
vector.subtract(to, 4),
vector.add(to, 4),
{"group:crumbly", "group:cracky", "group:stone"})
if #nodes == 0 then
-- failed to get a target, just run away from attacker?!
print("No target found, stopped")
return
end
-- find top walkable node
local pick = nodes[math.random(1, #nodes)]
while true do
local node = minetest.get_node(pick)
if not minetest.registered_nodes[node.name].walkable then
pick.y = pick.y - 1
else
-- one up at the end
pick.y = pick.y + 1
break
end
end
-- move to the top surface of pick
if not pick then
return false
end
--[[
minetest.add_particle({
pos = {x = pick.x, y = pick.y - 0.1, z = pick.z},
velocity = vector.new(),
acceleration = vector.new(),
expirationtime = 3,
size = 6,
collisiondetection = false,
vertical = false,
texture = "wool_red.png",
playername = nil
})
--]]
return pick
end)