init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. -- default support (for use with MineClone2 and other [games]
  2. default = default or {
  3. node_sound_stone_defaults = function(table) end,
  4. node_sound_wood_defaults = function(table) end,
  5. gui_bg = "",
  6. gui_bg_img = "",
  7. gui_slots = "",
  8. }
  9. -- Load support for intllib.
  10. local MP = minetest.get_modpath(minetest.get_current_modname())
  11. local S = dofile(MP .. "/intllib.lua")
  12. local F = minetest.formspec_escape
  13. protector = {
  14. mod = "redo",
  15. modpath = MP,
  16. intllib = S
  17. }
  18. local protector_max_share_count = 12
  19. -- get minetest.conf settings
  20. local protector_radius = tonumber(minetest.settings:get("protector_radius")) or 5
  21. local protector_flip = minetest.settings:get_bool("protector_flip") or false
  22. local protector_hurt = tonumber(minetest.settings:get("protector_hurt")) or 0
  23. local protector_spawn = tonumber(minetest.settings:get("protector_spawn")
  24. or minetest.settings:get("protector_pvp_spawn")) or 0
  25. local protector_show = tonumber(minetest.settings:get("protector_show_interval")) or 5
  26. -- get static spawn position
  27. local statspawn = minetest.string_to_pos(minetest.settings:get("static_spawnpoint"))
  28. or {x = 0, y = 2, z = 0}
  29. -- return list of members as a table
  30. local get_member_list = function(meta)
  31. return meta:get_string("members"):split(" ")
  32. end
  33. -- write member list table in protector meta as string
  34. local set_member_list = function(meta, list)
  35. meta:set_string("members", table.concat(list, " "))
  36. end
  37. -- check for owner name
  38. local is_owner = function(meta, name)
  39. return name == meta:get_string("owner")
  40. end
  41. -- check for member name
  42. local is_member = function (meta, name)
  43. for _, n in pairs(get_member_list(meta)) do
  44. if n == name then
  45. return true
  46. end
  47. end
  48. return false
  49. end
  50. -- add player name to table as member
  51. local add_member = function(meta, name)
  52. -- Constant (20) defined by player.h
  53. if name:len() > 25 then
  54. return
  55. end
  56. -- does name already exist?
  57. if is_owner(meta, name)
  58. or is_member(meta, name) then
  59. return
  60. end
  61. local list = get_member_list(meta)
  62. if #list >= protector_max_share_count then
  63. return
  64. end
  65. table.insert(list, name)
  66. set_member_list(meta, list)
  67. end
  68. -- remove player name from table
  69. local del_member = function(meta, name)
  70. local list = get_member_list(meta)
  71. for i, n in pairs(list) do
  72. if n == name then
  73. table.remove(list, i)
  74. break
  75. end
  76. end
  77. set_member_list(meta, list)
  78. end
  79. -- protector interface
  80. local protector_formspec = function(meta)
  81. local formspec = "size[8,7]"
  82. .. default.gui_bg
  83. .. default.gui_bg_img
  84. .. default.gui_slots
  85. .. "label[2.5,0;" .. F(S("-- Protector interface --")) .. "]"
  86. .. "label[0,1;" .. F(S("PUNCH node to show protected area")) .. "]"
  87. .. "label[0,2;" .. F(S("Members:")) .. "]"
  88. .. "button_exit[2.5,6.2;3,0.5;close_me;" .. F(S("Close")) .. "]"
  89. .. "field_close_on_enter[protector_add_member;false]"
  90. local members = get_member_list(meta)
  91. local npp = protector_max_share_count -- max users added to protector list
  92. local i = 0
  93. for n = 1, #members do
  94. if i < npp then
  95. -- show username
  96. formspec = formspec .. "button[" .. (i % 4 * 2)
  97. .. "," .. math.floor(i / 4 + 3)
  98. .. ";1.5,.5;protector_member;" .. F(members[n]) .. "]"
  99. -- username remove button
  100. .. "button[" .. (i % 4 * 2 + 1.25) .. ","
  101. .. math.floor(i / 4 + 3)
  102. .. ";.75,.5;protector_del_member_" .. F(members[n]) .. ";X]"
  103. end
  104. i = i + 1
  105. end
  106. if i < npp then
  107. -- user name entry field
  108. formspec = formspec .. "field[" .. (i % 4 * 2 + 1 / 3) .. ","
  109. .. (math.floor(i / 4 + 3) + 1 / 3)
  110. .. ";1.433,.5;protector_add_member;;]"
  111. -- username add button
  112. .."button[" .. (i % 4 * 2 + 1.25) .. ","
  113. .. math.floor(i / 4 + 3) .. ";.75,.5;protector_submit;+]"
  114. end
  115. return formspec
  116. end
  117. -- check if pos is inside a protected spawn area
  118. local inside_spawn = function(pos, radius)
  119. if protector_spawn <= 0 then
  120. return false
  121. end
  122. if pos.x < statspawn.x + radius
  123. and pos.x > statspawn.x - radius
  124. and pos.y < statspawn.y + radius
  125. and pos.y > statspawn.y - radius
  126. and pos.z < statspawn.z + radius
  127. and pos.z > statspawn.z - radius then
  128. return true
  129. end
  130. return false
  131. end
  132. -- Infolevel:
  133. -- 0 for no info
  134. -- 1 for "This area is owned by <owner> !" if you can't dig
  135. -- 2 for "This area is owned by <owner>.
  136. -- 3 for checking protector overlaps
  137. protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
  138. if not digger or not pos then
  139. return false
  140. end
  141. -- protector_bypass privileged users can override protection
  142. if infolevel == 1
  143. and minetest.check_player_privs(digger, {protection_bypass = true}) then
  144. return true
  145. end
  146. -- infolevel 3 is only used to bypass priv check, change to 1 now
  147. if infolevel == 3 then infolevel = 1 end
  148. -- is spawn area protected ?
  149. if inside_spawn(pos, protector_spawn) then
  150. minetest.chat_send_player(digger,
  151. S("Spawn @1 has been protected up to a @2 block radius.",
  152. minetest.pos_to_string(statspawn), protector_spawn))
  153. return false
  154. end
  155. -- find the protector nodes
  156. local pos = minetest.find_nodes_in_area(
  157. {x = pos.x - r, y = pos.y - r, z = pos.z - r},
  158. {x = pos.x + r, y = pos.y + r, z = pos.z + r},
  159. {"protector:protect", "protector:protect2"})
  160. local meta, owner, members
  161. for n = 1, #pos do
  162. meta = minetest.get_meta(pos[n])
  163. owner = meta:get_string("owner") or ""
  164. members = meta:get_string("members") or ""
  165. -- node change and digger isn't owner
  166. if infolevel == 1 and owner ~= digger then
  167. -- and you aren't on the member list
  168. if onlyowner or not is_member(meta, digger) then
  169. minetest.chat_send_player(digger,
  170. S("This area is owned by @1", owner) .. "!")
  171. return false
  172. end
  173. end
  174. -- when using protector as tool, show protector information
  175. if infolevel == 2 then
  176. minetest.chat_send_player(digger,
  177. S("This area is owned by @1", owner) .. ".")
  178. minetest.chat_send_player(digger,
  179. S("Protection located at: @1", minetest.pos_to_string(pos[n])))
  180. if members ~= "" then
  181. minetest.chat_send_player(digger, S("Members: @1.", members))
  182. end
  183. return false
  184. end
  185. end
  186. -- show when you can build on unprotected area
  187. if infolevel == 2 then
  188. if #pos < 1 then
  189. minetest.chat_send_player(digger, S("This area is not protected."))
  190. end
  191. minetest.chat_send_player(digger, S("You can build here."))
  192. end
  193. return true
  194. end
  195. local old_is_protected = minetest.is_protected
  196. -- check for protected area, return true if protected and digger isn't on list
  197. function minetest.is_protected(pos, digger)
  198. digger = digger or "" -- nil check
  199. -- is area protected against digger?
  200. if not protector.can_dig(protector_radius, pos, digger, false, 1) then
  201. local player = minetest.get_player_by_name(digger)
  202. if player and player:is_player() then
  203. -- hurt player if protection violated
  204. if protector_hurt > 0 and player:get_hp() > 0 then
  205. player:set_hp(player:get_hp() - protector_hurt)
  206. end
  207. -- flip player when protection violated
  208. if protector_flip then
  209. -- yaw + 180°
  210. local yaw = player:get_look_horizontal() + math.pi
  211. if yaw > 2 * math.pi then
  212. yaw = yaw - 2 * math.pi
  213. end
  214. player:set_look_horizontal(yaw)
  215. -- invert pitch
  216. player:set_look_vertical(-player:get_look_vertical())
  217. -- if digging below player, move up to avoid falling through hole
  218. local pla_pos = player:get_pos()
  219. if pos.y < pla_pos.y then
  220. player:set_pos({
  221. x = pla_pos.x,
  222. y = pla_pos.y + 0.8,
  223. z = pla_pos.z
  224. })
  225. end
  226. end
  227. end
  228. return true
  229. end
  230. -- otherwise can dig or place
  231. return old_is_protected(pos, digger)
  232. end
  233. -- make sure protection block doesn't overlap another protector's area
  234. local check_overlap = function(itemstack, placer, pointed_thing)
  235. if pointed_thing.type ~= "node" then
  236. return itemstack
  237. end
  238. local pos = pointed_thing.above
  239. local name = placer:get_player_name()
  240. -- make sure protector doesn't overlap onto protected spawn area
  241. if inside_spawn(pos, protector_spawn + protector_radius) then
  242. minetest.chat_send_player(name,
  243. S("Spawn @1 has been protected up to a @2 block radius.",
  244. minetest.pos_to_string(statspawn), protector_spawn))
  245. return itemstack
  246. end
  247. -- make sure protector doesn't overlap any other player's area
  248. if not protector.can_dig(protector_radius * 2, pos, name, true, 3) then
  249. minetest.chat_send_player(name,
  250. S("Overlaps into above players protected area"))
  251. return itemstack
  252. end
  253. return minetest.item_place(itemstack, placer, pointed_thing)
  254. end
  255. -- temporary pos store
  256. local player_pos = {}
  257. -- protection node
  258. minetest.register_node("protector:protect", {
  259. description = S("Protection Block") .. " (" .. S("USE for area check") .. ")",
  260. drawtype = "nodebox",
  261. tiles = {
  262. "default_stone.png^protector_overlay.png",
  263. "default_stone.png^protector_overlay.png",
  264. "default_stone.png^protector_overlay.png^protector_logo.png"
  265. },
  266. sounds = default.node_sound_stone_defaults(),
  267. groups = {dig_immediate = 2, unbreakable = 1},
  268. is_ground_content = false,
  269. paramtype = "light",
  270. light_source = 4,
  271. node_box = {
  272. type = "fixed",
  273. fixed = {
  274. {-0.5 ,-0.5, -0.5, 0.5, 0.5, 0.5},
  275. }
  276. },
  277. on_place = check_overlap,
  278. after_place_node = function(pos, placer)
  279. local meta = minetest.get_meta(pos)
  280. meta:set_string("owner", placer:get_player_name() or "")
  281. meta:set_string("infotext", S("Protection (owned by @1)", meta:get_string("owner")))
  282. meta:set_string("members", "")
  283. end,
  284. on_use = function(itemstack, user, pointed_thing)
  285. if pointed_thing.type ~= "node" then
  286. return
  287. end
  288. protector.can_dig(protector_radius, pointed_thing.under, user:get_player_name(), false, 2)
  289. end,
  290. on_rightclick = function(pos, node, clicker, itemstack)
  291. local meta = minetest.get_meta(pos)
  292. local name = clicker:get_player_name()
  293. if meta
  294. and protector.can_dig(1, pos, name, true, 1) then
  295. player_pos[name] = pos
  296. minetest.show_formspec(name, "protector:node", protector_formspec(meta))
  297. end
  298. end,
  299. on_punch = function(pos, node, puncher)
  300. if minetest.is_protected(pos, puncher:get_player_name()) then
  301. return
  302. end
  303. minetest.add_entity(pos, "protector:display")
  304. end,
  305. can_dig = function(pos, player)
  306. return player and protector.can_dig(1, pos, player:get_player_name(), true, 1)
  307. end,
  308. on_blast = function() end,
  309. after_destruct = function(pos, oldnode)
  310. local objects = minetest.get_objects_inside_radius(pos, 0.5)
  311. for _, v in ipairs(objects) do
  312. v:remove()
  313. end
  314. end,
  315. })
  316. -- default recipe and alternative for MineClone2
  317. if minetest.registered_items["default:stone"] then
  318. minetest.register_craft({
  319. output = "protector:protect",
  320. recipe = {
  321. {"default:stone", "default:stone", "default:stone"},
  322. {"default:stone", "default:gold_ingot", "default:stone"},
  323. {"default:stone", "default:stone", "default:stone"},
  324. }
  325. })
  326. else
  327. minetest.register_craft({
  328. output = "protector:protect",
  329. recipe = {
  330. {"mcl_core:stone", "mcl_core:stone", "mcl_core:stone"},
  331. {"mcl_core:stone", "mcl_core:gold_ingot", "mcl_core:stone"},
  332. {"mcl_core:stone", "mcl_core:stone", "mcl_core:stone"},
  333. }
  334. })
  335. end
  336. -- protection logo
  337. minetest.register_node("protector:protect2", {
  338. description = S("Protection Logo") .. " (" .. S("USE for area check") .. ")",
  339. tiles = {"protector_logo.png"},
  340. wield_image = "protector_logo.png",
  341. inventory_image = "protector_logo.png",
  342. sounds = default.node_sound_stone_defaults(),
  343. groups = {dig_immediate = 2, unbreakable = 1},
  344. paramtype = "light",
  345. paramtype2 = "wallmounted",
  346. legacy_wallmounted = true,
  347. light_source = 4,
  348. drawtype = "nodebox",
  349. sunlight_propagates = true,
  350. walkable = true,
  351. node_box = {
  352. type = "wallmounted",
  353. wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
  354. wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
  355. wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
  356. },
  357. selection_box = {type = "wallmounted"},
  358. on_place = check_overlap,
  359. after_place_node = function(pos, placer)
  360. local meta = minetest.get_meta(pos)
  361. meta:set_string("owner", placer:get_player_name() or "")
  362. meta:set_string("infotext", S("Protection (owned by @1)", meta:get_string("owner")))
  363. meta:set_string("members", "")
  364. end,
  365. on_use = function(itemstack, user, pointed_thing)
  366. if pointed_thing.type ~= "node" then
  367. return
  368. end
  369. protector.can_dig(protector_radius, pointed_thing.under, user:get_player_name(), false, 2)
  370. end,
  371. on_rightclick = function(pos, node, clicker, itemstack)
  372. local meta = minetest.get_meta(pos)
  373. local name = clicker:get_player_name()
  374. if meta
  375. and protector.can_dig(1, pos, name, true, 1) then
  376. player_pos[name] = pos
  377. minetest.show_formspec(name, "protector:node", protector_formspec(meta))
  378. end
  379. end,
  380. on_punch = function(pos, node, puncher)
  381. if minetest.is_protected(pos, puncher:get_player_name()) then
  382. return
  383. end
  384. minetest.add_entity(pos, "protector:display")
  385. end,
  386. can_dig = function(pos, player)
  387. return player and protector.can_dig(1, pos, player:get_player_name(), true, 1)
  388. end,
  389. on_blast = function() end,
  390. after_destruct = function(pos, oldnode)
  391. local objects = minetest.get_objects_inside_radius(pos, 0.5)
  392. for _, v in ipairs(objects) do
  393. v:remove()
  394. end
  395. end,
  396. })
  397. -- recipes to switch between protectors
  398. minetest.register_craft({
  399. type = "shapeless",
  400. output = "protector:protect",
  401. recipe = {"protector:protect2"}
  402. })
  403. minetest.register_craft({
  404. type = "shapeless",
  405. output = "protector:protect2",
  406. recipe = {"protector:protect"}
  407. })
  408. -- check formspec buttons or when name entered
  409. minetest.register_on_player_receive_fields(function(player, formname, fields)
  410. if formname ~= "protector:node" then
  411. return
  412. end
  413. local name = player:get_player_name()
  414. local pos = player_pos[name]
  415. if not name or not pos then
  416. return
  417. end
  418. local add_member_input = fields.protector_add_member
  419. -- reset formspec until close button pressed
  420. if (fields.close_me or fields.quit)
  421. and (not add_member_input or add_member_input == "") then
  422. player_pos[name] = nil
  423. return
  424. end
  425. -- only owner can add names
  426. if not protector.can_dig(1, pos, player:get_player_name(), true, 1) then
  427. return
  428. end
  429. -- are we adding member to a protection node ? (csm protection)
  430. local nod = minetest.get_node(pos).name
  431. if nod ~= "protector:protect"
  432. and nod ~= "protector:protect2" then
  433. player_pos[name] = nil
  434. return
  435. end
  436. local meta = minetest.get_meta(pos)
  437. if not meta then
  438. return
  439. end
  440. -- add member [+]
  441. if add_member_input then
  442. for _, i in pairs(add_member_input:split(" ")) do
  443. add_member(meta, i)
  444. end
  445. end
  446. -- remove member [x]
  447. for field, value in pairs(fields) do
  448. if string.sub(field, 0,
  449. string.len("protector_del_member_")) == "protector_del_member_" then
  450. del_member(meta,
  451. string.sub(field,string.len("protector_del_member_") + 1))
  452. end
  453. end
  454. minetest.show_formspec(name, formname, protector_formspec(meta))
  455. end)
  456. -- display entity shown when protector node is punched
  457. minetest.register_entity("protector:display", {
  458. physical = false,
  459. collisionbox = {0, 0, 0, 0, 0, 0},
  460. visual = "wielditem",
  461. -- wielditem seems to be scaled to 1.5 times original node size
  462. visual_size = {x = 0.67, y = 0.67},
  463. textures = {"protector:display_node"},
  464. timer = 0,
  465. glow = 10,
  466. on_step = function(self, dtime)
  467. self.timer = self.timer + dtime
  468. -- remove after set number of seconds
  469. if self.timer > protector_show then
  470. self.object:remove()
  471. end
  472. end,
  473. })
  474. -- Display-zone node, Do NOT place the display as a node,
  475. -- it is made to be used as an entity (see above)
  476. local x = protector_radius
  477. minetest.register_node("protector:display_node", {
  478. tiles = {"protector_display.png"},
  479. use_texture_alpha = true,
  480. walkable = false,
  481. drawtype = "nodebox",
  482. node_box = {
  483. type = "fixed",
  484. fixed = {
  485. -- sides
  486. {-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)},
  487. {-(x+.55), -(x+.55), (x+.45), (x+.55), (x+.55), (x+.55)},
  488. {(x+.45), -(x+.55), -(x+.55), (x+.55), (x+.55), (x+.55)},
  489. {-(x+.55), -(x+.55), -(x+.55), (x+.55), (x+.55), -(x+.45)},
  490. -- top
  491. {-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)},
  492. -- bottom
  493. {-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)},
  494. -- middle (surround protector)
  495. {-.55,-.55,-.55, .55,.55,.55},
  496. },
  497. },
  498. selection_box = {
  499. type = "regular",
  500. },
  501. paramtype = "light",
  502. groups = {dig_immediate = 3, not_in_creative_inventory = 1},
  503. drop = "",
  504. })
  505. dofile(MP .. "/doors_chest.lua")
  506. dofile(MP .. "/pvp.lua")
  507. dofile(MP .. "/admin.lua")
  508. dofile(MP .. "/tool.lua")
  509. dofile(MP .. "/hud.lua")
  510. dofile(MP .. "/lucky_block.lua")
  511. -- stop mesecon pistons from pushing protectors
  512. if minetest.get_modpath("mesecons_mvps") then
  513. mesecon.register_mvps_stopper("protector:protect")
  514. mesecon.register_mvps_stopper("protector:protect2")
  515. mesecon.register_mvps_stopper("protector:chest")
  516. end
  517. print (S("[MOD] Protector Redo loaded"))