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