init.lua 16 KB

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