]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/api/data_handling.lua
Overhaul fire
[Crafter.git] / mods / mob / api / data_handling.lua
1 --
2 mobs.create_data_handling_functions = function(def,mob_register)
3         mob_register.get_staticdata = function(self)
4                 if self.deactivating == false then
5                         global_mob_amount = global_mob_amount + 1
6                         --print("Mob Spawned. Current Mobs: "..global_mob_amount)
7                 elseif self.deactivating == true then
8                         minetest.after(0, function()
9                                 if not self.object:get_luaentity() then
10                                         global_mob_amount = global_mob_amount - 1
11                                         --print("Mob Deactivated. Current Mobs: "..global_mob_amount)
12                                 end
13                         end)
14                 end
15
16                 return minetest.serialize({
17                         --range = self.range,
18                         hp = self.hp,
19                         hunger = self.hunger,
20                         hostile = self.hostile,
21                         hostile_timer = self.hostile_timer,
22                         death_animation_timer = self.death_animation_timer,
23                         dead = self.dead,
24                         tnt_timer = self.tnt_timer,
25                         tnt_tick_timer = self.tnt_tick_timer,
26                         tnt_mod_state = self.tnt_mod_state,
27                         punch_timer = self.punch_timer,
28                         projectile_timer = self.projectile_timer,
29                         scared = self.scared,
30                         scared_timer = self.scared_timer,
31                         c_mob_data = self.c_mob_data,
32                         on_fire = self.on_fire,
33                 })
34         end
35
36
37         mob_register.on_activate = function(self, staticdata, dtime_s)
38                 self.object:set_armor_groups({immortal = 1})
39                 --self.object:set_velocity({x = math.random(-5,5), y = 5, z = math.random(-5,5)})
40                 self.object:set_acceleration(def.gravity)
41                 if string.sub(staticdata, 1, string.len("return")) == "return" then
42                         local data = minetest.deserialize(staticdata)
43                         if data and type(data) == "table" then
44                                 --self.range = data.range
45                                 self.hp = data.hp
46                                 self.hunger = data.hunger
47                                 self.hostile = data.hostile
48                                 self.hostile_timer = data.hostile_timer
49                                 self.death_animation_timer = data.death_animation_timer
50                                 self.dead = data.dead
51                                 self.tnt_timer = data.tnt_timer
52                                 self.tnt_tick_timer = data.tnt_tick_timer
53                                 self.tnt_mod_state = data.tnt_mod_state
54                                 self.punch_timer = data.punch_timer
55                                 self.projectile_timer = data.projectile_timer
56                                 self.scared = data.scared
57                                 self.scared_timer = data.scared_timer                           
58                                 self.c_mob_data = data.c_mob_data
59                                 self.on_fire = data.on_fire
60                         end
61                 end
62                 
63                 --set up mob
64                 self.object:set_animation(def.standing_frame, 0, 0, true)
65                 self.current_animation = 0
66                 self.object:set_hp(self.hp)
67                 self.direction = vector.new(math.random()*math.random(-1,1),0,math.random()*math.random(-1,1))
68                 
69                 
70                 --set the head up
71                 if self.head_bone then
72                         self.object:set_bone_position(self.head_bone, self.head_position_correction, vector.new(0,0,0))
73                 end
74                 self.is_mob = true
75                 self.object:set_armor_groups({immortal = 1})
76
77                 if self.custom_on_activate then
78                         self.custom_on_activate(self)
79                 end
80
81                 if self.on_fire == true then
82                         start_fire(self.object)
83                 end
84
85                 --use this to handle the global mob table
86                 minetest.after(0,function()
87                         self.deactivating = true
88                 end)
89         end
90
91         --this is the info on the mob
92         mob_register.debug_nametag = function(self,dtime)
93                 --we're doing this to the child because the nametage breaks the
94                 --animation on the mob's body
95                 
96                 --we add in items we want to see in this list
97                 local debug_items = {"hostile","hostile_timer"}
98                 local text = ""
99                 for _,item in pairs(debug_items) do
100                         if self[item] ~= nil then
101                                 text = text..item..": "..tostring(self[item]).."\n"
102                         end
103                 end
104                 self.object:set_nametag_attributes({
105                 color = "white",
106                 text = text
107                 })
108         end
109         return(mob_register)
110 end