-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrivers.lua
223 lines (202 loc) · 5.64 KB
/
drivers.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
--[[
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_driver("roam", {
start = function(self)
-- start with idle animation unless we get a path
self.driver:animation("idle")
local state = self.entity_ai_state
state.roam_ttl = math.random(3, 9)
self.path = Path(self)
if not self.path:find() then
--print("Unable to calculate path")
self.driver:switch("idle")
return
end
-- done, roaming mode good!
self.driver:animation("move")
end,
step = function(self, dtime)
-- handle movement stuff
local state = self.entity_ai_state
if state.roam_ttl and state.roam_ttl <= 0 then
self.driver:switch("idle")
return
end
state.roam_ttl = state.roam_ttl - dtime
-- do path movement
if not self.path or self.path:distance() < 0.7 or
not self.path:step(dtime) then
self.driver:switch("idle")
return
end
end,
stop = function(self)
local state = self.entity_ai_state
state.roam_ttl = nil
end,
})
entity_ai.register_driver("idle", {
start = function(self)
self.driver:animation("idle")
self.object:setvelocity(vector.new())
local state = self.entity_ai_state
state.idle_ttl = math.random(2, 20)
-- sanity checks
check_trapped_and_escape(self)
end,
step = function(self, dtime)
local state = self.entity_ai_state
state.idle_ttl = state.idle_ttl - dtime
if state.idle_ttl <= 0 then
self.driver:switch("roam")
return
end
end,
stop = function(self)
local state = self.entity_ai_state
state.idle_ttl = nil
end,
})
entity_ai.register_driver("startle", {
start = function(self, factordata)
-- startle animation
self.driver:animation("startle")
self.object:setvelocity(vector.new())
-- collect info we want to use in this driver
local state = self.entity_ai_state
if factordata and factordata["got_hit"] then
state.attacker = factordata["got_hit"][1]
state.attacked_at = factordata["got_hit"][5]
end
end,
step = function(self, dtime)
end,
stop = function(self)
-- play out remaining animations
end,
})
entity_ai.register_driver("eat", {
start = function(self, factordata)
self.driver:animation("eat")
self.object:setvelocity(vector.new())
-- collect info we want to use in this driver
local state = self.entity_ai_state
state.eat_ttl = math.random(30, 60)
if factordata and factordata.near_foodnode then
state.food = factordata.near_foodnode
end
end,
step = function(self, dtime)
local state = self.entity_ai_state
if state.eat_ttl > 0 then
state.eat_ttl = state.eat_ttl - dtime
return
end
state.ate_enough = math.random(200, 300)
self.driver:switch("eat_end")
end,
stop = function(self)
local state = self.entity_ai_state
state.eat_ttl = nil
-- increase HP
local hp = self.object:get_hp()
if hp < self.driver:get_property("hp_max") then
self.object:set_hp(hp + 1)
end
-- eat foodnode
local food = state.food
if not food then
return
end
local node = minetest.get_node(food)
minetest.sound_play(minetest.registered_nodes[node.name].sounds.dug, {pos = food, max_hear_distance = 18})
if node.name == "default:dirt_with_grass" or node.name == "default:dirt_with_dry_grass" then
minetest.set_node(food, {name = "default:dirt"})
--elseif node.name == "default:grass_1" or node.name == "default:dry_grass_1" then
-- minetest.remove_node(food)
elseif node.name == "default:grass_2" then
minetest.set_node(food, {name = "default:grass_1"})
elseif node.name == "default:grass_3" then
minetest.set_node(food, {name = "default:grass_2"})
elseif node.name == "default:grass_4" then
minetest.set_node(food, {name = "default:grass_3"})
elseif node.name == "default:grass_5" then
minetest.set_node(food, {name = "default:grass_4"})
elseif node.name == "default:dry_grass_2" then
minetest.set_node(food, {name = "default:dry_grass_1"})
elseif node.name == "default:dry_grass_3" then
minetest.set_node(food, {name = "default:dry_grass_2"})
elseif node.name == "default:dry_grass_4" then
minetest.set_node(food, {name = "default:dry_grass_3"})
elseif node.name == "default:dry_grass_5" then
minetest.set_node(food, {name = "default:dry_grass_4"})
end
state.food = nil
end,
})
entity_ai.register_driver("eat_end", {
start = function(self)
self.driver:animation("eat")
self.object:setvelocity(vector.new())
end,
step = function(self, dtime)
end,
stop = function(self)
end,
})
entity_ai.register_driver("flee", {
start = function(self)
self.driver:animation("move")
local state = self.entity_ai_state
state.flee_start = minetest.get_us_time()
end,
step = function(self, dtime)
-- check timer ourselves
local state = self.entity_ai_state
if (minetest.get_us_time() - state.flee_start) > (15 * 1000000) then
state.flee_start = nil
self.driver:switch("roam")
return
end
-- are we fleeing yet?
if self.path and self.path.distance then
-- stop fleeing if we're at a safe distance
-- execute flee path
if self.path:distance() < 2.0 then
-- get a new flee path
self.path = {}
else
-- follow path
if not self.path:step() then
self.path = {}
end
end
else
self.path = Path(self)
if not self.path:find() then
--print("Unable to calculate path")
return
end
-- done, flee path good!
self.driver:animation("move")
end
end,
stop = function(self)
-- play out remaining animations
end,
})
entity_ai.register_driver("death", {
start = function(self)
-- start with moving animation
self.driver:animation("idle")
end,
step = function(self, dtime)
end,
stop = function(self)
-- play out remaining animations
end,
})