Add a few v6-exclusive trades to villagers
[MineClone/MineClone2.git] / mods / ITEMS / mcl_end / eye_of_ender.lua
blob131db7bbc6c5cfbeab4e02758eadedf182342df0
1 -- Eye of Ender
3 minetest.register_entity("mcl_end:ender_eye", {
4 physical = false,
5 textures = {"mcl_end_ender_eye.png"},
6 visual_size = {x=1.5, y=1.5},
7 collisionbox = {0,0,0,0,0,0},
9 -- Save and restore age
10 get_staticdata = function(self)
11 return tostring(self._age)
12 end,
13 on_activate = function(self, staticdata, dtime_s)
14 local age = tonumber(staticdata)
15 if type(age) == "number" then
16 self._age = age
17 if self._age >= 2 then
18 self._phase = 1
19 else
20 self._phase = 0
21 end
22 end
23 end,
25 on_step = function(self, dtime)
26 self._age = self._age + dtime
27 if self._age >= 3 then
28 -- End of life
29 local r = math.random(1,5)
30 if r == 1 or minetest.settings:get_bool("creative_mode") then
31 -- 20% chance to get destroyed completely.
32 -- 100% if in Creative Mode
33 self.object:remove()
34 return
35 else
36 -- 80% to drop as an item
37 local pos = self.object:get_pos()
38 local v = self.object:getvelocity()
39 self.object:remove()
40 local item = minetest.add_item(pos, "mcl_end:ender_eye")
41 item:setvelocity(v)
42 return
43 end
44 elseif self._age >= 2 then
45 if self._phase == 0 then
46 self._phase = 1
47 -- Stop the eye and wait for another second.
48 -- The vertical speed changes are just eye candy.
49 self.object:setacceleration({x=0, y=-3, z=0})
50 self.object:setvelocity({x=0, y=self.object:getvelocity().y*0.2, z=0})
51 end
52 else
53 -- Fly normally and generate particles
54 local pos = self.object:get_pos()
55 pos.x = pos.x + math.random(-1, 1)*0.5
56 pos.y = pos.y + math.random(-1, 0)*0.5
57 pos.z = pos.z + math.random(-1, 1)*0.5
58 minetest.add_particle({
59 pos = pos,
60 texture = "mcl_particles_teleport.png",
61 expirationtime = 1,
62 velocity = {x=math.random(-1, 1)*0.1, y=math.random(-30, 0)*0.1, z=math.random(-1, 1)*0.1},
63 acceleration = {x=0, y=0, z=0},
64 size = 2.5,
66 end
67 end,
69 _age = 0, -- age in seconds
70 _phase = 0, -- phase 0: flying. phase 1: idling in mid air, about to drop or shatter
73 minetest.register_craftitem("mcl_end:ender_eye", {
74 description = "Eye of Ender",
75 _doc_items_longdesc = "This item is used to locate End portal shrines in the Overworld and to activate End portals." .. "\n" .. "NOTE: The End dimension is currently incomplete and boring.",
76 _doc_items_usagehelp = "Use the attack key to release the eye of ender. It will rise and fly in the horizontal direction of the closest end portal shrine. If you're very close, the eye of ender will take the direct path to the End portal shrine instead. After a few seconds, it stops. It may drop as an item, but there's a 20% chance it shatters." .. "\n" .. "To activate an End portal, eyes of ender need to be placed into each block of an intact End portal frame.",
77 wield_image = "mcl_end_ender_eye.png",
78 inventory_image = "mcl_end_ender_eye.png",
79 stack_max = 64,
80 -- Throw eye of ender to make it fly to the closest stronghold
81 on_use = function(itemstack, user, pointed_thing)
82 if user == nil then
83 return
84 end
85 local origin = user:get_pos()
86 origin.y = origin.y + 1.5
87 local strongholds = mcl_structures.get_registered_structures("stronghold")
88 local dim = mcl_worlds.pos_to_dimension(origin)
89 local is_creative = minetest.settings:get_bool("creative_mode")
91 -- Just drop the eye of ender if there are no strongholds
92 if #strongholds <= 0 or dim ~= "overworld" then
93 if not is_creative then
94 minetest.item_drop(ItemStack("mcl_end:ender_eye"), user, user:get_pos())
95 itemstack:take_item()
96 end
97 return itemstack
98 end
100 -- Find closest stronghold.
101 -- Note: Only the horizontal axes are taken into account.
102 local closest_stronghold
103 local lowest_dist
104 for s=1, #strongholds do
105 local h_pos = table.copy(strongholds[s].pos)
106 local h_origin = table.copy(origin)
107 h_pos.y = 0
108 h_origin.y = 0
109 local dist = vector.distance(h_origin, h_pos)
110 if not closest_stronghold then
111 closest_stronghold = strongholds[s]
112 lowest_dist = dist
113 else
114 if dist < lowest_dist then
115 closest_stronghold = strongholds[s]
116 lowest_dist = dist
121 -- Throw it!
122 local obj = minetest.add_entity(origin, "mcl_end:ender_eye")
123 local dir
125 if lowest_dist <= 25 then
126 local velocity = 4
127 -- Stronghold is close: Fly directly to stronghold and take Y into account.
128 dir = vector.normalize(vector.direction(origin, closest_stronghold.pos))
129 obj:setvelocity({x=dir.x*velocity, y=dir.y*velocity, z=dir.z*velocity})
130 else
131 local velocity = 12
132 -- Don't care about Y if stronghold is still far away.
133 -- Fly to direction of X/Z, and always upwards so it can be seen easily.
134 local o = {x=origin.x, y=0, z=origin.z}
135 local s = {x=closest_stronghold.pos.x, y=0, z=closest_stronghold.pos.z}
136 dir = vector.normalize(vector.direction(o, s))
137 obj:setacceleration({x=dir.x*-3, y=4, z=dir.z*-3})
138 obj:setvelocity({x=dir.x*velocity, y=3, z=dir.z*velocity})
142 if not is_creative then
143 itemstack:take_item()
145 return itemstack
146 end,
149 minetest.register_craft({
150 type = "shapeless",
151 output = "mcl_end:ender_eye",
152 recipe = {"mcl_mobitems:blaze_powder", "mcl_throwing:ender_pearl"},