init.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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_block.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_block.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.env: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.env:get_timeofday())
  81. if experimental.day then
  82. minetest.log("forcing day->night")
  83. experimental.day = false
  84. minetest.env:set_timeofday(0.0)
  85. else
  86. minetest.log("forcing night->day")
  87. experimental.day = true
  88. minetest.env:set_timeofday(0.5)
  89. end
  90. minetest.log("time of day is "..minetest.env: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.env:remove_node(p)
  123. minetest.env: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. if not minetest.get_player_privs(name)["give"] then
  230. minetest.chat_send_player(name, "you don't have permission to spawn (give)")
  231. return true -- Handled chat message
  232. end
  233. if not minetest.get_player_privs(name)["interact"] then
  234. minetest.chat_send_player(name, "you don't have permission to interact")
  235. return true -- Handled chat message
  236. end
  237. local player = minetest.env:get_player_by_name(name)
  238. if player == nil then
  239. print("Unable to spawn entity, player is nil")
  240. return true -- Handled chat message
  241. end
  242. local entityname = "experimental:dummyball"
  243. local p = player:getpos()
  244. p.y = p.y + 1
  245. minetest.env:add_entity(p, entityname)
  246. minetest.chat_send_player(name, '"'..entityname
  247. ..'" spawned.');
  248. return true -- Handled chat message
  249. end
  250. end)
  251. --
  252. -- A test entity for testing animated and yaw-modulated sprites
  253. --
  254. minetest.register_entity("experimental:testentity", {
  255. -- Static definition
  256. physical = true, -- Collides with things
  257. -- weight = 5,
  258. collisionbox = {-0.7,-1.35,-0.7, 0.7,1.0,0.7},
  259. --collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
  260. visual = "sprite",
  261. visual_size = {x=2, y=3},
  262. textures = {"dungeon_master.png^[makealpha:128,0,0^[makealpha:128,128,0"},
  263. spritediv = {x=6, y=5},
  264. initial_sprite_basepos = {x=0, y=0},
  265. on_activate = function(self, staticdata)
  266. print("testentity.on_activate")
  267. self.object:setsprite({x=0,y=0}, 1, 0, true)
  268. --self.object:setsprite({x=0,y=0}, 4, 0.3, true)
  269. -- Set gravity
  270. self.object:setacceleration({x=0, y=-10, z=0})
  271. -- Jump a bit upwards
  272. self.object:setvelocity({x=0, y=10, z=0})
  273. end,
  274. on_punch = function(self, hitter)
  275. self.object:remove()
  276. hitter:add_to_inventory('craft testobject1 1')
  277. end,
  278. })
  279. --
  280. -- More random stuff
  281. --
  282. minetest.register_on_respawnplayer(function(player)
  283. print("on_respawnplayer")
  284. -- player:setpos({x=0, y=30, z=0})
  285. -- return true
  286. end)
  287. minetest.register_on_generated(function(minp, maxp)
  288. --print("on_generated: minp="..dump(minp).." maxp="..dump(maxp))
  289. --cp = {x=(minp.x+maxp.x)/2, y=(minp.y+maxp.y)/2, z=(minp.z+maxp.z)/2}
  290. --minetest.env:add_node(cp, {name="sand"})
  291. end)
  292. -- Example setting get
  293. --print("setting max_users = " .. dump(minetest.setting_get("max_users")))
  294. --print("setting asdf = " .. dump(minetest.setting_get("asdf")))
  295. minetest.register_on_chat_message(function(name, message)
  296. --[[print("on_chat_message: name="..dump(name).." message="..dump(message))
  297. local cmd = "/testcommand"
  298. if message:sub(0, #cmd) == cmd then
  299. print(cmd.." invoked")
  300. return true
  301. end
  302. local cmd = "/help"
  303. if message:sub(0, #cmd) == cmd then
  304. print("script-overridden help command")
  305. minetest.chat_send_all("script-overridden help command")
  306. return true
  307. end]]
  308. end)
  309. -- Grow papyrus on TNT every 10 seconds
  310. --[[minetest.register_abm({
  311. nodenames = {"TNT"},
  312. interval = 10.0,
  313. chance = 1,
  314. action = function(pos, node, active_object_count, active_object_count_wider)
  315. print("TNT ABM action")
  316. pos.y = pos.y + 1
  317. minetest.env:add_node(pos, {name="papyrus"})
  318. end,
  319. })]]
  320. -- Replace texts of alls signs with "foo" every 10 seconds
  321. --[[minetest.register_abm({
  322. nodenames = {"sign_wall"},
  323. interval = 10.0,
  324. chance = 1,
  325. action = function(pos, node, active_object_count, active_object_count_wider)
  326. print("ABM: Sign text changed")
  327. local meta = minetest.env:get_meta(pos)
  328. meta:set_text("foo")
  329. end,
  330. })]]
  331. --[[local ncpos = nil
  332. local ncq = 1
  333. local ncstuff = {
  334. {2, 1, 0, 3}, {3, 0, 1, 2}, {4, -1, 0, 1}, {5, -1, 0, 1}, {6, 0, -1, 0},
  335. {7, 0, -1, 0}, {8, 1, 0, 3}, {9, 1, 0, 3}, {10, 1, 0, 3}, {11, 0, 1, 2},
  336. {12, 0, 1, 2}, {13, 0, 1, 2}, {14, -1, 0, 1}, {15, -1, 0, 1}, {16, -1, 0, 1},
  337. {17, -1, 0, 1}, {18, 0, -1, 0}, {19, 0, -1, 0}, {20, 0, -1, 0}, {21, 0, -1, 0},
  338. {22, 1, 0, 3}, {23, 1, 0, 3}, {24, 1, 0, 3}, {25, 1, 0, 3}, {10, 0, 1, 2}
  339. }
  340. local ncold = {}
  341. local nctime = nil
  342. minetest.register_abm({
  343. nodenames = {"dirt_with_grass"},
  344. interval = 100000.0,
  345. chance = 1,
  346. action = function(pos, node, active_object_count, active_object_count_wider)
  347. if ncpos ~= nil then
  348. return
  349. end
  350. if pos.x % 16 ~= 8 or pos.z % 16 ~= 8 then
  351. return
  352. end
  353. pos.y = pos.y + 1
  354. n = minetest.env:get_node(pos)
  355. print(dump(n))
  356. if n.name ~= "air" then
  357. return
  358. end
  359. pos.y = pos.y + 2
  360. ncpos = pos
  361. nctime = os.clock()
  362. minetest.env:add_node(ncpos, {name="nyancat"})
  363. end
  364. })
  365. minetest.register_abm({
  366. nodenames = {"nyancat"},
  367. interval = 1.0,
  368. chance = 1,
  369. action = function(pos, node, active_object_count, active_object_count_wider)
  370. if ncpos == nil then
  371. return
  372. end
  373. if pos.x == ncpos.x and pos.y == ncpos.y and pos.z == ncpos.z then
  374. clock = os.clock()
  375. if clock - nctime < 0.1 then
  376. return
  377. end
  378. nctime = clock
  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. minetest.register_node("experimental:tester_node_1", {
  398. description = "Tester Node 1",
  399. tile_images = {"wieldhand.png"},
  400. groups = {oddly_breakable_by_hand=2},
  401. sounds = default.node_sound_wood_defaults(),
  402. on_construct = function(pos)
  403. experimental.print_to_everything("experimental:tester_node_1:on_construct("..minetest.pos_to_string(pos)..")")
  404. local meta = minetest.env:get_meta(pos)
  405. meta:set_string("mine", "test")
  406. end,
  407. after_place_node = function(pos, placer)
  408. experimental.print_to_everything("experimental:tester_node_1:after_place_node("..minetest.pos_to_string(pos)..")")
  409. local meta = minetest.env:get_meta(pos)
  410. if meta:get_string("mine") == "test" then
  411. experimental.print_to_everything("correct metadata found")
  412. else
  413. experimental.print_to_everything("incorrect metadata found")
  414. end
  415. end,
  416. on_destruct = function(pos)
  417. experimental.print_to_everything("experimental:tester_node_1:on_destruct("..minetest.pos_to_string(pos)..")")
  418. end,
  419. after_destruct = function(pos)
  420. experimental.print_to_everything("experimental:tester_node_1:after_destruct("..minetest.pos_to_string(pos)..")")
  421. end,
  422. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  423. experimental.print_to_everything("experimental:tester_node_1:after_dig_node("..minetest.pos_to_string(pos)..")")
  424. end,
  425. })
  426. minetest.register_craftitem("experimental:tester_tool_1", {
  427. description = "Tester Tool 1",
  428. inventory_image = "experimental_tester_tool_1.png",
  429. on_use = function(itemstack, user, pointed_thing)
  430. --print(dump(pointed_thing))
  431. if pointed_thing.type == "node" then
  432. if minetest.env:get_node(pointed_thing.under).name == "experimental:tester_node_1" then
  433. local p = pointed_thing.under
  434. minetest.log("action", "Tester tool used at "..minetest.pos_to_string(p))
  435. minetest.env:dig_node(p)
  436. else
  437. local p = pointed_thing.above
  438. minetest.log("action", "Tester tool used at "..minetest.pos_to_string(p))
  439. minetest.env:place_node(p, {name="experimental:tester_node_1"})
  440. end
  441. end
  442. end,
  443. })
  444. minetest.register_craft({
  445. output = 'experimental:tester_tool_1',
  446. recipe = {
  447. {'group:crumbly'},
  448. {'group:crumbly'},
  449. }
  450. })
  451. minetest.log("experimental modname="..dump(minetest.get_current_modname()))
  452. minetest.log("experimental modpath="..dump(minetest.get_modpath("experimental")))
  453. minetest.log("experimental worldpath="..dump(minetest.get_worldpath()))
  454. -- END