init.lua 16 KB

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