]> git.lizzy.rs Git - dragonfireclient.git/blob - data/mods/experimental/init.lua
Players are more like objects + related stuff
[dragonfireclient.git] / data / mods / experimental / init.lua
1 --
2 -- Experimental things
3 --
4
5 -- An example furnace-thing implemented in Lua
6
7 minetest.register_node("luafurnace", {
8         tile_images = {"lava.png", "furnace_side.png", "furnace_side.png",
9                 "furnace_side.png", "furnace_side.png", "furnace_front.png"},
10         --inventory_image = "furnace_front.png",
11         inventory_image = inventorycube("furnace_front.png"),
12         paramtype = "facedir_simple",
13         metadata_name = "generic",
14         material = digprop_stonelike(3.0),
15 })
16
17 minetest.register_on_placenode(function(pos, newnode, placer)
18         if newnode.name == "luafurnace" then
19                 print("get_meta");
20                 local meta = minetest.env:get_meta(pos)
21                 print("inventory_set_list");
22                 meta:inventory_set_list("fuel", {""})
23                 print("inventory_set_list");
24                 meta:inventory_set_list("src", {""})
25                 print("inventory_set_list");
26                 meta:inventory_set_list("dst", {"","","",""})
27                 print("set_inventory_draw_spec");
28                 meta:set_inventory_draw_spec(
29                         "invsize[8,9;]"
30                         .."list[current_name;fuel;2,3;1,1;]"
31                         .."list[current_name;src;2,1;1,1;]"
32                         .."list[current_name;dst;5,1;2,2;]"
33                         .."list[current_player;main;0,5;8,4;]"
34                 )
35                 
36                 local total_cooked = 0;
37                 print("set_string")
38                 meta:set_string("total_cooked", total_cooked)
39                 print("set_infotext");
40                 meta:set_infotext("Lua Furnace: total cooked: "..total_cooked)
41         end
42 end)
43
44 minetest.register_abm({
45         nodenames = {"luafurnace"},
46         interval = 1.0,
47         chance = 1,
48         action = function(pos, node, active_object_count, active_object_count_wider)
49                 local meta = minetest.env:get_meta(pos)
50                 local fuellist = meta:inventory_get_list("fuel")
51                 local srclist = meta:inventory_get_list("src")
52                 local dstlist = meta:inventory_get_list("dst")
53                 if fuellist == nil or srclist == nil or dstlist == nil then
54                         return
55                 end
56                 _, srcitem = stackstring_take_item(srclist[1])
57                 _, fuelitem = stackstring_take_item(fuellist[1])
58                 if not srcitem or not fuelitem then return end
59                 if fuelitem.type == "NodeItem" then
60                         local prop = minetest.registered_nodes[fuelitem.name]
61                         if prop == nil then return end
62                         if prop.furnace_burntime < 0 then return end
63                 else
64                         return
65                 end
66                 local resultstack = nil
67                 if srcitem.type == "NodeItem" then
68                         local prop = minetest.registered_nodes[srcitem.name]
69                         if prop == nil then return end
70                         if prop.cookresult_item == "" then return end
71                         resultstack = prop.cookresult_item
72                 else
73                         return
74                 end
75
76                 if resultstack == nil then
77                         return
78                 end
79
80                 dstlist[1], success = stackstring_put_stackstring(dstlist[1], resultstack)
81                 if not success then
82                         return
83                 end
84
85                 fuellist[1], _ = stackstring_take_item(fuellist[1])
86                 srclist[1], _ = stackstring_take_item(srclist[1])
87
88                 meta:inventory_set_list("fuel", fuellist)
89                 meta:inventory_set_list("src", srclist)
90                 meta:inventory_set_list("dst", dstlist)
91
92                 local total_cooked = meta:get_string("total_cooked")
93                 total_cooked = tonumber(total_cooked) + 1
94                 meta:set_string("total_cooked", total_cooked)
95                 meta:set_infotext("Lua Furnace: total cooked: "..total_cooked)
96         end,
97 })
98
99 minetest.register_craft({
100         output = 'NodeItem "luafurnace" 1',
101         recipe = {
102                 {'NodeItem "cobble"', 'NodeItem "cobble"', 'NodeItem "cobble"'},
103                 {'NodeItem "cobble"', 'NodeItem "cobble"', 'NodeItem "cobble"'},
104                 {'NodeItem "cobble"', 'NodeItem "cobble"', 'NodeItem "cobble"'},
105         }
106 })
107
108 --
109 -- Random stuff
110 --
111
112 --[[
113 minetest.register_tool("horribletool", {
114         image = "lava.png",
115         basetime = 2.0
116         dt_weight = 0.2
117         dt_crackiness = 0.2
118         dt_crumbliness = 0.2
119         dt_cuttability = 0.2
120         basedurability = 50
121         dd_weight = -5
122         dd_crackiness = -5
123         dd_crumbliness = -5
124         dd_cuttability = -5
125 })
126 --]]
127
128 minetest.register_craft({
129         output = 'NodeItem "somenode" 4',
130         recipe = {
131                 {'CraftItem "Stick" 1'},
132         }
133 })
134
135 minetest.register_node("somenode", {
136         tile_images = {"lava.png", "mese.png", "stone.png", "grass.png", "cobble.png", "tree_top.png"},
137         inventory_image = "treeprop.png",
138         material = {
139                 diggability = "normal",
140                 weight = 0,
141                 crackiness = 0,
142                 crumbliness = 0,
143                 cuttability = 0,
144                 flammability = 0
145         },
146         metadata_name = "chest",
147 })
148
149 --
150 -- TNT (not functional)
151 --
152
153 minetest.register_craft({
154         output = 'NodeItem "TNT" 4',
155         recipe = {
156                 {'NodeItem "wood" 1'},
157                 {'CraftItem "lump_of_coal" 1'},
158                 {'NodeItem "wood" 1'}
159         }
160 })
161
162 minetest.register_node("TNT", {
163         tile_images = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png", "tnt_side.png", "tnt_side.png", "tnt_side.png"},
164         inventory_image = "tnt_side.png",
165         dug_item = '', -- Get nothing
166         material = {
167                 diggability = "not",
168         },
169 })
170
171 local TNT = {
172         -- Static definition
173         physical = true, -- Collides with things
174         -- weight = 5,
175         collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
176         visual = "cube",
177         textures = {"tnt_top.png","tnt_bottom.png","tnt_side.png","tnt_side.png","tnt_side.png","tnt_side.png"},
178         -- Initial value for our timer
179         timer = 0,
180         -- Number of punches required to defuse
181         health = 1,
182         blinktimer = 0,
183         blinkstatus = true,
184 }
185
186 -- Called when a TNT object is created
187 function TNT:on_activate(staticdata)
188         print("TNT:on_activate()")
189         self.object:setvelocity({x=0, y=4, z=0})
190         self.object:setacceleration({x=0, y=-10, z=0})
191         self.object:settexturemod("^[brighten")
192 end
193
194 -- Called periodically
195 function TNT:on_step(dtime)
196         --print("TNT:on_step()")
197         self.timer = self.timer + dtime
198         self.blinktimer = self.blinktimer + dtime
199         if self.blinktimer > 0.5 then
200                 self.blinktimer = self.blinktimer - 0.5
201                 if self.blinkstatus then
202                         self.object:settexturemod("")
203                 else
204                         self.object:settexturemod("^[brighten")
205                 end
206                 self.blinkstatus = not self.blinkstatus
207         end
208 end
209
210 -- Called when object is punched
211 function TNT:on_punch(hitter)
212         print("TNT:on_punch()")
213         self.health = self.health - 1
214         if self.health <= 0 then
215                 self.object:remove()
216                 hitter:add_to_inventory("NodeItem TNT 1")
217                 hitter:set_hp(hitter:get_hp() - 1)
218         end
219 end
220
221 -- Called when object is right-clicked
222 function TNT:on_rightclick(clicker)
223         --pos = self.object:getpos()
224         --pos = {x=pos.x, y=pos.y+0.1, z=pos.z}
225         --self.object:moveto(pos, false)
226 end
227
228 print("TNT dump: "..dump(TNT))
229
230 print("Registering TNT");
231 minetest.register_entity("TNT", TNT)
232
233 --
234 -- A test entity for testing animated and yaw-modulated sprites
235 --
236
237 minetest.register_entity("testentity", {
238         -- Static definition
239         physical = true, -- Collides with things
240         -- weight = 5,
241         collisionbox = {-0.7,-1.35,-0.7, 0.7,1.0,0.7},
242         --collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
243         visual = "sprite",
244         visual_size = {x=2, y=3},
245         textures = {"dungeon_master.png^[makealpha:128,0,0^[makealpha:128,128,0"},
246         spritediv = {x=6, y=5},
247         initial_sprite_basepos = {x=0, y=0},
248
249         on_activate = function(self, staticdata)
250                 print("testentity.on_activate")
251                 self.object:setsprite({x=0,y=0}, 1, 0, true)
252                 --self.object:setsprite({x=0,y=0}, 4, 0.3, true)
253
254                 -- Set gravity
255                 self.object:setacceleration({x=0, y=-10, z=0})
256                 -- Jump a bit upwards
257                 self.object:setvelocity({x=0, y=10, z=0})
258         end,
259
260         on_punch = function(self, hitter)
261                 self.object:remove()
262                 hitter:add_to_inventory('CraftItem testobject1 1')
263         end,
264 })
265
266 --
267 -- More random stuff
268 --
269
270 minetest.register_on_respawnplayer(function(player)
271         print("on_respawnplayer")
272         -- player:setpos({x=0, y=30, z=0})
273         -- return true
274 end)
275
276 minetest.register_on_generated(function(minp, maxp)
277         --print("on_generated: minp="..dump(minp).." maxp="..dump(maxp))
278         --cp = {x=(minp.x+maxp.x)/2, y=(minp.y+maxp.y)/2, z=(minp.z+maxp.z)/2}
279         --minetest.env:add_node(cp, {name="sand"})
280 end)
281
282 -- Example setting get
283 print("setting max_users = " .. dump(minetest.setting_get("max_users")))
284 print("setting asdf = " .. dump(minetest.setting_get("asdf")))
285
286 minetest.register_on_chat_message(function(name, message)
287         --[[print("on_chat_message: name="..dump(name).." message="..dump(message))
288         local cmd = "/testcommand"
289         if message:sub(0, #cmd) == cmd then
290                 print(cmd.." invoked")
291                 return true
292         end
293         local cmd = "/help"
294         if message:sub(0, #cmd) == cmd then
295                 print("script-overridden help command")
296                 minetest.chat_send_all("script-overridden help command")
297                 return true
298         end]]
299 end)
300
301 -- Grow papyrus on TNT every 10 seconds
302 --[[minetest.register_abm({
303         nodenames = {"TNT"},
304         interval = 10.0,
305         chance = 1,
306         action = function(pos, node, active_object_count, active_object_count_wider)
307                 print("TNT ABM action")
308                 pos.y = pos.y + 1
309                 minetest.env:add_node(pos, {name="papyrus"})
310         end,
311 })]]
312
313 -- Replace texts of alls signs with "foo" every 10 seconds
314 --[[minetest.register_abm({
315         nodenames = {"sign_wall"},
316         interval = 10.0,
317         chance = 1,
318         action = function(pos, node, active_object_count, active_object_count_wider)
319                 print("ABM: Sign text changed")
320                 local meta = minetest.env:get_meta(pos)
321                 meta:set_text("foo")
322         end,
323 })]]
324
325 --[[local ncpos = nil
326 local ncq = 1
327 local ncstuff = {
328     {2, 1, 0, 3}, {3, 0, 1, 2}, {4, -1, 0, 1}, {5, -1, 0, 1}, {6, 0, -1, 0},
329     {7, 0, -1, 0}, {8, 1, 0, 3}, {9, 1, 0, 3}, {10, 1, 0, 3}, {11, 0, 1, 2},
330     {12, 0, 1, 2}, {13, 0, 1, 2}, {14, -1, 0, 1}, {15, -1, 0, 1}, {16, -1, 0, 1},
331     {17, -1, 0, 1}, {18, 0, -1, 0}, {19, 0, -1, 0}, {20, 0, -1, 0}, {21, 0, -1, 0},
332     {22, 1, 0, 3}, {23, 1, 0, 3}, {24, 1, 0, 3}, {25, 1, 0, 3}, {10, 0, 1, 2}
333 }
334 local ncold = {}
335 local nctime = nil
336
337 minetest.register_abm({
338     nodenames = {"dirt_with_grass"},
339     interval = 100000.0,
340     chance = 1,
341     action = function(pos, node, active_object_count, active_object_count_wider)
342         if ncpos ~= nil then
343             return
344         end
345        
346         if pos.x % 16 ~= 8 or pos.z % 16 ~= 8 then
347             return
348         end
349        
350         pos.y = pos.y + 1
351         n = minetest.env:get_node(pos)
352         print(dump(n))
353         if n.name ~= "air" then
354             return
355         end
356
357         pos.y = pos.y + 2
358         ncpos = pos
359         nctime = os.clock()
360         minetest.env:add_node(ncpos, {name="nyancat"})
361     end
362 })
363
364 minetest.register_abm({
365     nodenames = {"nyancat"},
366     interval = 1.0,
367     chance = 1,
368     action = function(pos, node, active_object_count, active_object_count_wider)
369         if ncpos == nil then
370             return
371         end
372         if pos.x == ncpos.x and pos.y == ncpos.y and pos.z == ncpos.z then
373             clock = os.clock()
374             if clock - nctime < 0.1 then
375                 return
376             end
377             nctime = clock
378            
379             s0 = ncstuff[ncq]
380             ncq = s0[1]
381             s1 = ncstuff[ncq]
382             p0 = pos
383             p1 = {x = p0.x + s0[2], y = p0.y, z = p0.z + s0[3]}
384             p2 = {x = p1.x + s1[2], y = p1.y, z = p1.z + s1[3]}
385             table.insert(ncold, 1, p0)
386             while #ncold >= 10 do
387                 minetest.env:add_node(ncold[#ncold], {name="air"})
388                 table.remove(ncold, #ncold)
389             end
390             minetest.env:add_node(p0, {name="nyancat_rainbow"})
391             minetest.env:add_node(p1, {name="nyancat", param1=s0[4]})
392             minetest.env:add_node(p2, {name="air"})
393             ncpos = p1
394         end
395     end,
396 })--]]
397
398