]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/api/api_hook.lua
Optimize a ton of globals to locals
[Crafter.git] / mods / mob / api / api_hook.lua
1 local minetest = minetest
2 --class 
3 mobs = {}
4
5 local path = minetest.get_modpath(minetest.get_current_modname()).."/api/"
6 dofile(path.."movement.lua")
7 dofile(path.."interaction.lua")
8 dofile(path.."data_handling.lua")
9 dofile(path.."head_code.lua")
10 dofile(path.."animation.lua")
11 dofile(path.."timers.lua")
12
13 mobs.register_mob = function(def)
14
15
16
17 local mob_register = {}
18
19 ------------------------------------------------
20 mob_register.initial_properties = {
21         physical = def.physical,
22         collide_with_objects = def.collide_with_objects,
23         collisionbox = def.collisionbox,
24         visual = def.visual,
25         visual_size = def.visual_size,
26         mesh = def.mesh,
27         textures = def.textures,
28         is_visible = def.is_visible,
29         pointable = def.pointable,
30         automatic_face_movement_dir = def.automatic_face_movement_dir,
31         automatic_face_movement_max_rotation_per_sec = def.automatic_face_movement_max_rotation_per_sec,
32         makes_footstep_sound = def.makes_footstep_sound,
33 }
34
35
36 mob_register.hp = def.hp
37 mob_register.max_speed = def.max_speed
38 mob_register.jump_timer = 0
39
40
41 if def.head_bone then
42         mob_register.head_bone = def.head_bone
43         mobs.create_head_functions(def,mob_register)
44         mob_register.debug_head_pos = def.debug_head_pos
45         mob_register.head_directional_offset = def.head_directional_offset
46         mob_register.head_height_offset = def.head_height_offset
47         mob_register.head_rotation_offset = def.head_rotation_offset
48         mob_register.head_position_correction = def.head_position_correction
49         mob_register.head_coord = def.head_coord
50         mob_register.flip_pitch = def.flip_pitch
51 else
52         --print("create some other functions to turn mob " .. def.mobname)
53 end
54
55 mob_register.hurt_inside_timer = 0
56 mob_register.death_animation_timer = 0
57 mob_register.dead = false
58
59 mob_register.mob = true
60 mob_register.mobname = def.mobname
61
62 mob_register.hostile = def.hostile
63 if def.friendly_in_daylight == true then
64         mob_register.friendly_in_daylight = def.friendly_in_daylight
65         mob_register.friendly_in_daylight_timer = 0
66 end
67
68 mob_register.hostile_cooldown = def.hostile_cooldown
69
70 mob_register.hostile_timer = 0
71 mob_register.timer = 0
72
73 mob_register.state = def.state
74
75 mob_register.hunger = 200
76
77 mob_register.view_distance = def.view_distance
78
79 mob_register.punch_timer = 0
80 mob_register.punched_timer = 0
81 mob_register.group_attack = def.group_attack
82
83 mob_register.death_rotation = def.death_rotation
84
85 mob_register.head_mount = def.head_mount
86 mob_register.rotational_correction = def.rotational_correction or 0
87
88 mob_register.hurt_sound = def.hurt_sound
89 mob_register.die_sound = def.die_sound
90
91 mob_register.attack_type = def.attack_type
92 if def.attack_type == "explode" then
93         mob_register.tnt_tick_timer = 0
94         mob_register.explosion_type = def.explosion_type
95 end
96 mob_register.explosion_radius = def.explosion_radius
97 mob_register.explosion_power = def.explosion_power
98 mob_register.tnt_timer = nil
99 mob_register.explosion_time = def.explosion_time
100 mob_register.explosion_blink_color = def.explosion_blink_color or "white"
101 mob_register.explosion_blink_timer = def.explosion_blink_timer or 0.2
102
103 mob_register.custom_function_begin = def.custom_function_begin
104 mob_register.custom_function = def.custom_function
105 mob_register.custom_function_end = def.custom_function_end
106
107 mob_register.projectile_timer_cooldown = def.projectile_timer_cooldown
108 mob_register.attacked_hostile = def.attacked_hostile
109 if not def.hostile and not def.attacked_hostile then
110         mob_register.scared = false
111         mob_register.scared_timer = 0
112 end
113 mob_register.attack_damage = def.attack_damage
114
115 mob_register.projectile_timer = 0
116 mob_register.projectile_type = def.projectile_type
117
118 mob_register.takes_fall_damage = def.takes_fall_damage or true
119 mob_register.make_jump_noise = def.make_jump_noise
120 mob_register.jump_animation = def.jump_animation
121 mob_register.jumping_frame = def.jumping_frame
122
123 mob_register.item_drop = def.item_drop
124 mob_register.item_minimum = def.item_minimum or 1
125 mob_register.item_max = def.item_max
126
127 mob_register.die_in_light = def.die_in_light
128 mob_register.die_in_light_level = def.die_in_light_level
129
130 mob_register.current_animation = 0
131 mob_register.hurt_color_timer = 0
132 mob_register.damage_color = def.damage_color or "red"
133 mob_register.custom_on_death = def.custom_on_death
134
135 mob_register.custom_on_activate = def.custom_on_activate
136
137 mob_register.custom_on_punch = def.custom_on_punch
138
139 mob_register.c_mob_data = def.c_mob_data
140
141 mob_register.deactivating = false
142
143 mob_register.on_fire = false
144
145 mob_register.fire_table = def.fire_table
146
147 if def.pathfinds then
148         --mob_register.path = {}
149         mob_register.pathfinding_timer = 0
150 end
151
152 if def.custom_timer then
153         mob_register.c_timer = 0
154         mob_register.custom_timer = def.custom_timer
155         mob_register.custom_timer_function = def.custom_timer_function
156 end
157
158 mobs.create_movement_functions(def,mob_register)
159 mobs.create_interaction_functions(def,mob_register)
160 mobs.create_data_handling_functions(def,mob_register)
161 mobs.create_animation_functions(def,mob_register)
162 mobs.create_timer_functions(def,mob_register)
163
164
165 mob_register.on_step = function(self, dtime,moveresult)
166         if self.custom_function_begin then
167                 self.custom_function_begin(self,dtime)
168         end
169         
170         self.collision_detection(self)
171         if self.fall_damage then
172                 self.fall_damage(self)
173         end
174         
175         if self.dead == false and self.death_animation_timer == 0 then
176                 if self.do_custom_timer then
177                         self.do_custom_timer(self,dtime)
178                 end
179
180                 if self.custom_function then
181                         self.custom_function(self,dtime,moveresult)
182                 end
183
184                 self.move(self,dtime,moveresult)
185                 
186                 --self.debug_nametag(self,dtime)
187
188                 self.manage_hurt_color_timer(self,dtime)
189
190                 if self.manage_scared_timer then
191                         self.manage_scared_timer(self,dtime)
192                 end
193
194                 if self.set_animation then
195                         self.set_animation(self)
196                 end
197                 
198                 if self.look_around then
199                         self.look_around(self,dtime)
200                 end
201                 
202                 if self.pathfinding then
203                         self.pathfinding(self,dtime)
204                 end
205
206                 if self.handle_friendly_in_daylight_timer then
207                         self.handle_friendly_in_daylight_timer(self,dtime)
208                 end
209
210                 self.manage_punch_timer(self,dtime)
211         else
212                 self.manage_death_animation(self,dtime)
213                 if self.move_head then
214                         self.move_head(self,nil,dtime)
215                 end
216         end
217
218         --fix zombie state again
219         if self.dead == true and self.death_animation_timer <= 0 then
220                 self.on_death(self)
221         end
222         
223         if self.tnt_timer then
224                 self.manage_explode_timer(self,dtime)
225         end
226         
227         if self.projectile_timer then
228                 self.manage_projectile_timer(self,dtime)
229         end
230         
231         if self.custom_function_end then
232                 self.custom_function_end(self,dtime)
233         end
234 end
235
236 minetest.register_entity("mob:"..def.mobname, mob_register)
237 ------------------------------------------------
238
239 end