]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/api/data_handling.lua
9e973b0a4045f899e6e6ca404909f42b5b4472a7
[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         return minetest.serialize({
5             --range = self.range,
6             hp = self.hp,
7             hunger = self.hunger,
8             hostile = self.hostile,
9             hostile_timer = self.hostile_timer,
10             death_animation_timer = self.death_animation_timer,
11             dead = self.dead
12         })
13     end
14
15
16     mob_register.on_activate = function(self, staticdata, dtime_s)
17         self.object:set_armor_groups({immortal = 1})
18         --self.object:set_velocity({x = math.random(-5,5), y = 5, z = math.random(-5,5)})
19         self.object:set_acceleration(def.gravity)
20         if string.sub(staticdata, 1, string.len("return")) == "return" then
21             local data = minetest.deserialize(staticdata)
22             if data and type(data) == "table" then
23                 --self.range = data.range
24                 self.hp = data.hp
25                 self.hunger = data.hunger
26                 self.hostile = data.hostile
27                 self.hostile_timer = data.hostile_timer
28                 self.death_animation_timer = data.death_animation_timer
29                 self.dead = data.dead
30             end
31         end
32         
33         --set up mob
34         self.object:set_animation({x=0,y=0}, 1, 0, true)
35         self.object:set_hp(self.hp)
36         self.direction = vector.new(math.random()*math.random(-1,1),0,math.random()*math.random(-1,1))
37         
38         
39         --set the head up
40         if def.has_head == true then
41             local head = minetest.add_entity(self.object:get_pos(), "mob:head"..def.mobname)
42             if head then
43                 self.child = head
44                 self.child:get_luaentity().parent = self.object
45                 self.child:set_attach(self.object, "", self.head_mount, vector.new(0,0,0))
46                 self.head_rotation = vector.new(0,0,0)
47                 self.child:set_animation({x=90,y=90}, 15, 0, true)
48             end
49         end
50         self.is_mob = true
51         self.object:set_armor_groups({immortal = 1})
52         --self.object:set_yaw(math.pi*math.random(-1,1)*math.random())
53     end
54
55     --this is the info on the mob
56     mob_register.debug_nametag = function(self,dtime)
57         --we're doing this to the child because the nametage breaks the
58         --animation on the mob's body
59         if self.child then
60             --we add in items we want to see in this list
61             local debug_items = {"hostile_timer","hostile"}
62             local text = ""
63             for _,item in pairs(debug_items) do
64                 if self[item] ~= nil then
65                     text = text..item..": "..tostring(self[item]).."\n"
66                 end
67             end
68             self.child:set_nametag_attributes({
69             color = "white",
70             text = text
71             })
72         end
73     end
74     return(mob_register)
75 end