init.lua 17 KB

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