2
0

init.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. --
  2. -- Experimental things
  3. --
  4. dofile(minetest.get_modpath("experimental").."/modchannels.lua")
  5. -- For testing random stuff
  6. experimental = {}
  7. function experimental.print_to_everything(msg)
  8. minetest.log("action", msg)
  9. minetest.chat_send_all(msg)
  10. end
  11. --[[
  12. experimental.player_visual_index = 0
  13. function switch_player_visual()
  14. for _, obj in pairs(minetest.get_connected_players()) do
  15. if experimental.player_visual_index == 0 then
  16. obj:set_properties({visual="upright_sprite"})
  17. else
  18. obj:set_properties({visual="cube"})
  19. end
  20. end
  21. experimental.player_visual_index = (experimental.player_visual_index + 1) % 2
  22. minetest.after(1.0, switch_player_visual)
  23. end
  24. minetest.after(1.0, switch_player_visual)
  25. ]]
  26. minetest.register_node("experimental:soundblock", {
  27. tiles = {"unknown_node.png", "default_tnt_bottom.png",
  28. "default_tnt_side.png", "default_tnt_side.png",
  29. "default_tnt_side.png", "default_tnt_side.png"},
  30. inventory_image = minetest.inventorycube("unknown_node.png",
  31. "default_tnt_side.png", "default_tnt_side.png"),
  32. groups = {dig_immediate=3},
  33. })
  34. minetest.register_alias("sb", "experimental:soundblock")
  35. minetest.register_abm({
  36. nodenames = {"experimental:soundblock"},
  37. interval = 1,
  38. chance = 1,
  39. action = function(p0, node, _, _)
  40. minetest.sound_play("default_grass_footstep", {pos=p0, gain=0.5})
  41. end,
  42. })
  43. --[[
  44. stepsound = -1
  45. function test_sound()
  46. print("test_sound")
  47. stepsound = minetest.sound_play("default_grass_footstep", {gain=1.0})
  48. minetest.after(2.0, test_sound)
  49. --minetest.after(0.1, test_sound_stop)
  50. end
  51. function test_sound_stop()
  52. print("test_sound_stop")
  53. minetest.sound_stop(stepsound)
  54. minetest.after(2.0, test_sound)
  55. end
  56. test_sound()
  57. --]]
  58. function on_step(dtime)
  59. -- print("experimental on_step")
  60. --[[
  61. objs = minetest.get_objects_inside_radius({x=0,y=0,z=0}, 10)
  62. for k, obj in pairs(objs) do
  63. name = obj:get_player_name()
  64. if name then
  65. print(name.." at "..dump(obj:getpos()))
  66. print(name.." dir: "..dump(obj:get_look_dir()))
  67. print(name.." pitch: "..dump(obj:get_look_pitch()))
  68. print(name.." yaw: "..dump(obj:get_look_yaw()))
  69. else
  70. print("Some object at "..dump(obj:getpos()))
  71. end
  72. end
  73. --]]
  74. --[[
  75. if experimental.t1 == nil then
  76. experimental.t1 = 0
  77. end
  78. experimental.t1 = experimental.t1 + dtime
  79. if experimental.t1 >= 2 then
  80. experimental.t1 = experimental.t1 - 2
  81. minetest.log("time of day is "..minetest.get_timeofday())
  82. if experimental.day then
  83. minetest.log("forcing day->night")
  84. experimental.day = false
  85. minetest.set_timeofday(0.0)
  86. else
  87. minetest.log("forcing night->day")
  88. experimental.day = true
  89. minetest.set_timeofday(0.5)
  90. end
  91. minetest.log("time of day is "..minetest.get_timeofday())
  92. end
  93. --]]
  94. end
  95. minetest.register_globalstep(on_step)
  96. --
  97. -- Random stuff
  98. --
  99. --
  100. -- TNT (not functional)
  101. --
  102. minetest.register_craft({
  103. output = 'experimental:tnt',
  104. recipe = {
  105. {'default:wood'},
  106. {'default:coal_lump'},
  107. {'default:wood'}
  108. }
  109. })
  110. minetest.register_node("experimental:tnt", {
  111. tiles = {"default_tnt_top.png", "default_tnt_bottom.png",
  112. "default_tnt_side.png", "default_tnt_side.png",
  113. "default_tnt_side.png", "default_tnt_side.png"},
  114. inventory_image = minetest.inventorycube("default_tnt_top.png",
  115. "default_tnt_side.png", "default_tnt_side.png"),
  116. drop = '', -- Get nothing
  117. material = {
  118. diggability = "not",
  119. },
  120. })
  121. minetest.register_on_punchnode(function(p, node)
  122. if node.name == "experimental:tnt" then
  123. minetest.remove_node(p)
  124. minetest.add_entity(p, "experimental:tnt")
  125. minetest.check_for_falling(p)
  126. end
  127. end)
  128. local TNT = {
  129. -- Static definition
  130. physical = true, -- Collides with things
  131. -- weight = 5,
  132. collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
  133. visual = "cube",
  134. textures = {"default_tnt_top.png", "default_tnt_bottom.png",
  135. "default_tnt_side.png", "default_tnt_side.png",
  136. "default_tnt_side.png", "default_tnt_side.png"},
  137. -- Initial value for our timer
  138. timer = 0,
  139. -- Number of punches required to defuse
  140. health = 1,
  141. blinktimer = 0,
  142. blinkstatus = true,
  143. }
  144. -- Called when a TNT object is created
  145. function TNT:on_activate(staticdata)
  146. print("TNT:on_activate()")
  147. self.object:setvelocity({x=0, y=4, z=0})
  148. self.object:setacceleration({x=0, y=-10, z=0})
  149. self.object:settexturemod("^[brighten")
  150. self.object:set_armor_groups({immortal=1})
  151. end
  152. -- Called periodically
  153. function TNT:on_step(dtime)
  154. --print("TNT:on_step()")
  155. self.timer = self.timer + dtime
  156. self.blinktimer = self.blinktimer + dtime
  157. if self.blinktimer > 0.5 then
  158. self.blinktimer = self.blinktimer - 0.5
  159. if self.blinkstatus then
  160. self.object:settexturemod("")
  161. else
  162. self.object:settexturemod("^[brighten")
  163. end
  164. self.blinkstatus = not self.blinkstatus
  165. end
  166. end
  167. -- Called when object is punched
  168. function TNT:on_punch(hitter)
  169. print("TNT:on_punch()")
  170. self.health = self.health - 1
  171. if self.health <= 0 then
  172. self.object:remove()
  173. hitter:get_inventory():add_item("main", "experimental:tnt")
  174. --hitter:set_hp(hitter:get_hp() - 1)
  175. end
  176. end
  177. -- Called when object is right-clicked
  178. function TNT:on_rightclick(clicker)
  179. --pos = self.object:getpos()
  180. --pos = {x=pos.x, y=pos.y+0.1, z=pos.z}
  181. --self.object:moveto(pos, false)
  182. end
  183. --print("TNT dump: "..dump(TNT))
  184. --print("Registering TNT");
  185. minetest.register_entity("experimental:tnt", TNT)
  186. -- Add TNT's old name also
  187. minetest.register_alias("TNT", "experimental:tnt")
  188. --
  189. -- The dummyball!
  190. --
  191. minetest.register_entity("experimental:dummyball", {
  192. initial_properties = {
  193. hp_max = 20,
  194. physical = false,
  195. collisionbox = {-0.4,-0.4,-0.4, 0.4,0.4,0.4},
  196. visual = "sprite",
  197. visual_size = {x=1, y=1},
  198. textures = {"experimental_dummyball.png"},
  199. spritediv = {x=1, y=3},
  200. initial_sprite_basepos = {x=0, y=0},
  201. },
  202. phase = 0,
  203. phasetimer = 0,
  204. on_activate = function(self, staticdata)
  205. minetest.log("Dummyball activated!")
  206. end,
  207. on_step = function(self, dtime)
  208. self.phasetimer = self.phasetimer + dtime
  209. if self.phasetimer > 2.0 then
  210. self.phasetimer = self.phasetimer - 2.0
  211. self.phase = self.phase + 1
  212. if self.phase >= 3 then
  213. self.phase = 0
  214. end
  215. self.object:setsprite({x=0, y=self.phase})
  216. phasearmor = {
  217. [0]={cracky=3},
  218. [1]={crumbly=3},
  219. [2]={fleshy=3}
  220. }
  221. self.object:set_armor_groups(phasearmor[self.phase])
  222. end
  223. end,
  224. on_punch = function(self, hitter)
  225. end,
  226. })
  227. minetest.register_on_chat_message(function(name, message)
  228. local cmd = "/dummyball"
  229. if message:sub(0, #cmd) == cmd then
  230. count = tonumber(message:sub(#cmd+1)) or 1
  231. if not minetest.get_player_privs(name)["give"] then
  232. minetest.chat_send_player(name, "you don't have permission to spawn (give)")
  233. return true -- Handled chat message
  234. end
  235. if not minetest.get_player_privs(name)["interact"] then
  236. minetest.chat_send_player(name, "you don't have permission to interact")
  237. return true -- Handled chat message
  238. end
  239. if count >= 2 and not minetest.get_player_privs(name)["server"] then
  240. minetest.chat_send_player(name, "you don't have " ..
  241. "permission to spawn multiple " ..
  242. "dummyballs (server)")
  243. return true -- Handled chat message
  244. end
  245. local player = minetest.get_player_by_name(name)
  246. if player == nil then
  247. print("Unable to spawn entity, player is nil")
  248. return true -- Handled chat message
  249. end
  250. local entityname = "experimental:dummyball"
  251. local p = player:getpos()
  252. p.y = p.y + 1
  253. for i = 1,count do
  254. minetest.add_entity(p, entityname)
  255. end
  256. minetest.chat_send_player(name, '"'..entityname
  257. ..'" spawned '..tostring(count)..' time(s).');
  258. return true -- Handled chat message
  259. end
  260. end)
  261. --
  262. -- A test entity for testing animated and yaw-modulated sprites
  263. --
  264. minetest.register_entity("experimental:testentity", {
  265. -- Static definition
  266. physical = true, -- Collides with things
  267. -- weight = 5,
  268. collisionbox = {-0.7,-1.35,-0.7, 0.7,1.0,0.7},
  269. --collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
  270. visual = "sprite",
  271. visual_size = {x=2, y=3},
  272. textures = {"dungeon_master.png^[makealpha:128,0,0^[makealpha:128,128,0"},
  273. spritediv = {x=6, y=5},
  274. initial_sprite_basepos = {x=0, y=0},
  275. on_activate = function(self, staticdata)
  276. print("testentity.on_activate")
  277. self.object:setsprite({x=0,y=0}, 1, 0, true)
  278. --self.object:setsprite({x=0,y=0}, 4, 0.3, true)
  279. -- Set gravity
  280. self.object:setacceleration({x=0, y=-10, z=0})
  281. -- Jump a bit upwards
  282. self.object:setvelocity({x=0, y=10, z=0})
  283. end,
  284. on_punch = function(self, hitter)
  285. self.object:remove()
  286. hitter:add_to_inventory('craft testobject1 1')
  287. end,
  288. on_death = function(self, killer)
  289. print("testentity.on_death")
  290. end
  291. })
  292. --
  293. -- More random stuff
  294. --
  295. minetest.register_on_respawnplayer(function(player)
  296. print("on_respawnplayer")
  297. -- player:setpos({x=0, y=30, z=0})
  298. -- return true
  299. end)
  300. minetest.register_on_generated(function(minp, maxp)
  301. --print("on_generated: minp="..dump(minp).." maxp="..dump(maxp))
  302. --cp = {x=(minp.x+maxp.x)/2, y=(minp.y+maxp.y)/2, z=(minp.z+maxp.z)/2}
  303. --minetest.add_node(cp, {name="sand"})
  304. end)
  305. -- Example setting get
  306. --print("setting max_users = " .. dump(minetest.setting_get("max_users")))
  307. --print("setting asdf = " .. dump(minetest.setting_get("asdf")))
  308. minetest.register_on_chat_message(function(name, message)
  309. --[[print("on_chat_message: name="..dump(name).." message="..dump(message))
  310. local cmd = "/testcommand"
  311. if message:sub(0, #cmd) == cmd then
  312. print(cmd.." invoked")
  313. return true
  314. end
  315. local cmd = "/help"
  316. if message:sub(0, #cmd) == cmd then
  317. print("script-overridden help command")
  318. minetest.chat_send_all("script-overridden help command")
  319. return true
  320. end]]
  321. end)
  322. -- Grow papyrus on TNT every 10 seconds
  323. --[[minetest.register_abm({
  324. nodenames = {"TNT"},
  325. interval = 10.0,
  326. chance = 1,
  327. action = function(pos, node, active_object_count, active_object_count_wider)
  328. print("TNT ABM action")
  329. pos.y = pos.y + 1
  330. minetest.add_node(pos, {name="papyrus"})
  331. end,
  332. })]]
  333. -- Replace texts of alls signs with "foo" every 10 seconds
  334. --[[minetest.register_abm({
  335. nodenames = {"sign_wall"},
  336. interval = 10.0,
  337. chance = 1,
  338. action = function(pos, node, active_object_count, active_object_count_wider)
  339. print("ABM: Sign text changed")
  340. local meta = minetest.get_meta(pos)
  341. meta:set_text("foo")
  342. end,
  343. })]]
  344. --[[local ncpos = nil
  345. local ncq = 1
  346. local ncstuff = {
  347. {2, 1, 0, 3}, {3, 0, 1, 2}, {4, -1, 0, 1}, {5, -1, 0, 1}, {6, 0, -1, 0},
  348. {7, 0, -1, 0}, {8, 1, 0, 3}, {9, 1, 0, 3}, {10, 1, 0, 3}, {11, 0, 1, 2},
  349. {12, 0, 1, 2}, {13, 0, 1, 2}, {14, -1, 0, 1}, {15, -1, 0, 1}, {16, -1, 0, 1},
  350. {17, -1, 0, 1}, {18, 0, -1, 0}, {19, 0, -1, 0}, {20, 0, -1, 0}, {21, 0, -1, 0},
  351. {22, 1, 0, 3}, {23, 1, 0, 3}, {24, 1, 0, 3}, {25, 1, 0, 3}, {10, 0, 1, 2}
  352. }
  353. local ncold = {}
  354. local nctime = nil
  355. minetest.register_abm({
  356. nodenames = {"dirt_with_grass"},
  357. interval = 100000.0,
  358. chance = 1,
  359. action = function(pos, node, active_object_count, active_object_count_wider)
  360. if ncpos ~= nil then
  361. return
  362. end
  363. if pos.x % 16 ~= 8 or pos.z % 16 ~= 8 then
  364. return
  365. end
  366. pos.y = pos.y + 1
  367. n = minetest.get_node(pos)
  368. print(dump(n))
  369. if n.name ~= "air" then
  370. return
  371. end
  372. pos.y = pos.y + 2
  373. ncpos = pos
  374. nctime = os.clock()
  375. minetest.add_node(ncpos, {name="nyancat"})
  376. end
  377. })
  378. minetest.register_abm({
  379. nodenames = {"nyancat"},
  380. interval = 1.0,
  381. chance = 1,
  382. action = function(pos, node, active_object_count, active_object_count_wider)
  383. if ncpos == nil then
  384. return
  385. end
  386. if pos.x == ncpos.x and pos.y == ncpos.y and pos.z == ncpos.z then
  387. clock = os.clock()
  388. if clock - nctime < 0.1 then
  389. return
  390. end
  391. nctime = clock
  392. s0 = ncstuff[ncq]
  393. ncq = s0[1]
  394. s1 = ncstuff[ncq]
  395. p0 = pos
  396. p1 = {x = p0.x + s0[2], y = p0.y, z = p0.z + s0[3]}
  397. p2 = {x = p1.x + s1[2], y = p1.y, z = p1.z + s1[3]}
  398. table.insert(ncold, 1, p0)
  399. while #ncold >= 10 do
  400. minetest.add_node(ncold[#ncold], {name="air"})
  401. table.remove(ncold, #ncold)
  402. end
  403. minetest.add_node(p0, {name="nyancat_rainbow"})
  404. minetest.add_node(p1, {name="nyancat", param1=s0[4]})
  405. minetest.add_node(p2, {name="air"})
  406. ncpos = p1
  407. end
  408. end,
  409. })--]]
  410. minetest.register_node("experimental:tester_node_1", {
  411. description = "Tester Node 1 (construct/destruct/timer)",
  412. tiles = {"wieldhand.png"},
  413. groups = {oddly_breakable_by_hand=2},
  414. sounds = default.node_sound_wood_defaults(),
  415. -- This was known to cause a bug in minetest.item_place_node() when used
  416. -- via minetest.place_node(), causing a placer with no position
  417. paramtype2 = "facedir",
  418. on_construct = function(pos)
  419. experimental.print_to_everything("experimental:tester_node_1:on_construct("..minetest.pos_to_string(pos)..")")
  420. local meta = minetest.get_meta(pos)
  421. meta:set_string("mine", "test")
  422. local timer = minetest.get_node_timer(pos)
  423. timer:start(4, 3)
  424. end,
  425. after_place_node = function(pos, placer)
  426. experimental.print_to_everything("experimental:tester_node_1:after_place_node("..minetest.pos_to_string(pos)..")")
  427. local meta = minetest.get_meta(pos)
  428. if meta:get_string("mine") == "test" then
  429. experimental.print_to_everything("correct metadata found")
  430. else
  431. experimental.print_to_everything("incorrect metadata found")
  432. end
  433. end,
  434. on_destruct = function(pos)
  435. experimental.print_to_everything("experimental:tester_node_1:on_destruct("..minetest.pos_to_string(pos)..")")
  436. end,
  437. after_destruct = function(pos)
  438. experimental.print_to_everything("experimental:tester_node_1:after_destruct("..minetest.pos_to_string(pos)..")")
  439. end,
  440. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  441. experimental.print_to_everything("experimental:tester_node_1:after_dig_node("..minetest.pos_to_string(pos)..")")
  442. end,
  443. on_timer = function(pos, elapsed)
  444. experimental.print_to_everything("on_timer(): elapsed="..dump(elapsed))
  445. return true
  446. end,
  447. })
  448. minetest.register_node("experimental:tiled", {
  449. description = "Tiled stone",
  450. tiles = {{
  451. name = "experimental_tiled.png",
  452. align_style = "world",
  453. scale = 8,
  454. }},
  455. groups = {cracky=2},
  456. })
  457. stairs.register_stair_and_slab("tiled_n", "experimental:tiled",
  458. {cracky=2},
  459. {{name="experimental_tiled.png", align_style="node", scale=8}},
  460. "Tiled stair (node-aligned)",
  461. "Tiled slab (node-aligned)")
  462. stairs.register_stair_and_slab("tiled", "experimantal:tiled",
  463. {cracky=2},
  464. {{name="experimental_tiled.png", align_style="world", scale=8}},
  465. "Tiled stair",
  466. "Tiled slab")
  467. minetest.register_craft({
  468. output = 'experimental:tiled 4',
  469. recipe = {
  470. {'default:cobble', '', 'default:cobble'},
  471. {'', '', ''},
  472. {'default:cobble', '', 'default:cobble'},
  473. }
  474. })
  475. minetest.register_craft({
  476. output = 'stairs:stair_tiled',
  477. recipe = {{'stairs:stair_tiled_n'}}
  478. })
  479. minetest.register_craft({
  480. output = 'stairs:stair_tiled_n',
  481. recipe = {{'stairs:stair_tiled'}}
  482. })
  483. minetest.register_craft({
  484. output = 'stairs:slab_tiled',
  485. recipe = {{'stairs:slab_tiled_n'}}
  486. })
  487. minetest.register_craft({
  488. output = 'stairs:slab_tiled_n',
  489. recipe = {{'stairs:slab_tiled'}}
  490. })
  491. minetest.register_craftitem("experimental:tester_tool_1", {
  492. description = "Tester Tool 1",
  493. inventory_image = "experimental_tester_tool_1.png",
  494. on_use = function(itemstack, user, pointed_thing)
  495. --print(dump(pointed_thing))
  496. if pointed_thing.type == "node" then
  497. local node = minetest.get_node(pointed_thing.under)
  498. if node.name == "experimental:tester_node_1" or node.name == "default:chest" then
  499. local p = pointed_thing.under
  500. minetest.log("action", "Tester tool used at "..minetest.pos_to_string(p))
  501. if node.name == "experimental:tester_node_1" then
  502. minetest.dig_node(p)
  503. else
  504. minetest.get_meta(p):mark_as_private({"infotext", "formspec"})
  505. minetest.chat_send_player(user:get_player_name(), "Verify that chest is unusable now.")
  506. end
  507. else
  508. local p = pointed_thing.above
  509. minetest.log("action", "Tester tool used at "..minetest.pos_to_string(p))
  510. minetest.place_node(p, {name="experimental:tester_node_1"})
  511. end
  512. end
  513. end,
  514. })
  515. minetest.register_craft({
  516. output = 'experimental:tester_tool_1',
  517. recipe = {
  518. {'group:crumbly'},
  519. {'group:crumbly'},
  520. }
  521. })
  522. minetest.register_craftitem("experimental:tester_tool_2", {
  523. description = "Tester Tool 2",
  524. inventory_image = "experimental_tester_tool_1.png^[invert:g",
  525. on_use = function(itemstack, user, pointed_thing)
  526. local pos = minetest.get_pointed_thing_position(pointed_thing, true)
  527. if pos == nil then return end
  528. pos = vector.add(pos, {x=0, y=0.5, z=0})
  529. local tex, anim
  530. if math.random(0, 1) == 0 then
  531. tex = "default_lava_source_animated.png"
  532. anim = {type="sheet_2d", frames_w=3, frames_h=2, frame_length=0.5}
  533. else
  534. tex = "default_lava_flowing_animated.png"
  535. anim = {type="vertical_frames", aspect_w=16, aspect_h=16, length=3.3}
  536. end
  537. minetest.add_particle({
  538. pos = pos,
  539. velocity = {x=0, y=0, z=0},
  540. acceleration = {x=0, y=0.04, z=0},
  541. expirationtime = 6,
  542. collisiondetection = true,
  543. texture = tex,
  544. animation = anim,
  545. size = 4,
  546. glow = math.random(0, 5),
  547. })
  548. end,
  549. })
  550. minetest.register_craft({
  551. output = 'experimental:tester_tool_2',
  552. recipe = {
  553. {'group:crumbly','group:crumbly'},
  554. }
  555. })
  556. --[[minetest.register_on_joinplayer(function(player)
  557. minetest.after(3, function()
  558. player:set_inventory_formspec("size[8,7.5]"..
  559. "image[1,0.6;1,2;player.png]"..
  560. "list[current_player;main;0,3.5;8,4;]"..
  561. "list[current_player;craft;3,0;3,3;]"..
  562. "list[current_player;craftpreview;7,1;1,1;]")
  563. end)
  564. end)]]
  565. -- Create a detached inventory
  566. local inv = minetest.create_detached_inventory("test_inventory", {
  567. allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  568. experimental.print_to_everything("allow move asked")
  569. return count -- Allow all
  570. end,
  571. allow_put = function(inv, listname, index, stack, player)
  572. experimental.print_to_everything("allow put asked")
  573. return 1 -- Allow only 1
  574. end,
  575. allow_take = function(inv, listname, index, stack, player)
  576. experimental.print_to_everything("allow take asked")
  577. return 4 -- Allow 4 at max
  578. end,
  579. on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  580. experimental.print_to_everything(player:get_player_name().." moved items")
  581. end,
  582. on_put = function(inv, listname, index, stack, player)
  583. experimental.print_to_everything(player:get_player_name().." put items")
  584. end,
  585. on_take = function(inv, listname, index, stack, player)
  586. experimental.print_to_everything(player:get_player_name().." took items")
  587. end,
  588. })
  589. inv:set_size("main", 4*6)
  590. inv:add_item("main", "experimental:tester_tool_1")
  591. inv:add_item("main", "experimental:tnt 5")
  592. minetest.register_chatcommand("test1", {
  593. params = "",
  594. description = "Test 1: Modify player's inventory view",
  595. func = function(name, param)
  596. local player = minetest.get_player_by_name(name)
  597. if not player then
  598. return
  599. end
  600. player:set_inventory_formspec(
  601. "size[13,7.5]"..
  602. "image[6,0.6;1,2;player.png]"..
  603. "list[current_player;main;5,3.5;8,4;]"..
  604. "list[current_player;craft;8,0;3,3;]"..
  605. "list[current_player;craftpreview;12,1;1,1;]"..
  606. "list[detached:test_inventory;main;0,0;4,6;0]"..
  607. "button[0.5,7;2,1;button1;Button 1]"..
  608. "button_exit[2.5,7;2,1;button2;Exit Button]"
  609. )
  610. minetest.chat_send_player(name, "Done.");
  611. end,
  612. })
  613. minetest.register_chatcommand("test_bulk_set_node", {
  614. params = "",
  615. description = "Test 2: bulk set a node",
  616. func = function(name, param)
  617. local player = minetest.get_player_by_name(name)
  618. if not player then
  619. return
  620. end
  621. local pos_list = {}
  622. local ppos = player:get_pos()
  623. local i = 1
  624. for x=2,10 do
  625. for y=2,10 do
  626. for z=2,10 do
  627. pos_list[i] = {x=ppos.x + x,y = ppos.y + y,z = ppos.z + z}
  628. i = i + 1
  629. end
  630. end
  631. end
  632. minetest.bulk_set_node(pos_list, {name = "default:stone"})
  633. minetest.chat_send_player(name, "Done.");
  634. end,
  635. })
  636. minetest.register_chatcommand("bench_bulk_set_node", {
  637. params = "",
  638. description = "Test 3: bulk set a node (bench)",
  639. func = function(name, param)
  640. local player = minetest.get_player_by_name(name)
  641. if not player then
  642. return
  643. end
  644. local pos_list = {}
  645. local ppos = player:get_pos()
  646. local i = 1
  647. for x=2,100 do
  648. for y=2,100 do
  649. for z=2,100 do
  650. pos_list[i] = {x=ppos.x + x,y = ppos.y + y,z = ppos.z + z}
  651. i = i + 1
  652. end
  653. end
  654. end
  655. minetest.chat_send_player(name, "Benching bulk set node. Warming up...");
  656. -- warm up with default:stone to prevent having different callbacks
  657. -- due to different node topology
  658. minetest.bulk_set_node(pos_list, {name = "default:stone"})
  659. minetest.chat_send_player(name, "Warming up finished, now benching...");
  660. local start_time = os.clock()
  661. for i=1,#pos_list do
  662. minetest.set_node(pos_list[i], {name = "default:stone"})
  663. end
  664. local middle_time = os.clock()
  665. minetest.bulk_set_node(pos_list, {name = "default:stone"})
  666. local end_time = os.clock()
  667. minetest.chat_send_player(name,
  668. string.format("Bench results: set_node loop[%.2fms], bulk_set_node[%.2fms]",
  669. (middle_time - start_time) * 1000,
  670. (end_time - middle_time) * 1000
  671. )
  672. );
  673. end,
  674. })
  675. minetest.register_on_player_receive_fields(function(player, formname, fields)
  676. experimental.print_to_everything("Inventory fields 1: player="..player:get_player_name()..", fields="..dump(fields))
  677. end)
  678. minetest.register_on_player_receive_fields(function(player, formname, fields)
  679. experimental.print_to_everything("Inventory fields 2: player="..player:get_player_name()..", fields="..dump(fields))
  680. return true -- Disable the first callback
  681. end)
  682. minetest.register_on_player_receive_fields(function(player, formname, fields)
  683. experimental.print_to_everything("Inventory fields 3: player="..player:get_player_name()..", fields="..dump(fields))
  684. end)
  685. minetest.log("experimental modname="..dump(minetest.get_current_modname()))
  686. minetest.log("experimental modpath="..dump(minetest.get_modpath("experimental")))
  687. minetest.log("experimental worldpath="..dump(minetest.get_worldpath()))
  688. -- END