init.lua 17 KB

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