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