init.lua 17 KB

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