init.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 25
  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. minetest.register_craft({
  317. output = "protector:protect",
  318. recipe = {
  319. {"default:stone", "default:stone", "default:stone"},
  320. {"default:stone", "default:gold_ingot", "default:stone"},
  321. {"default:stone", "default:stone", "default:stone"},
  322. }
  323. })
  324. -- protection logo
  325. minetest.register_node("protector:protect2", {
  326. description = S("Protection Logo") .. " (" .. S("USE for area check") .. ")",
  327. tiles = {"protector_logo.png"},
  328. wield_image = "protector_logo.png",
  329. inventory_image = "protector_logo.png",
  330. sounds = default.node_sound_stone_defaults(),
  331. groups = {dig_immediate = 2, unbreakable = 1},
  332. paramtype = "light",
  333. paramtype2 = "wallmounted",
  334. legacy_wallmounted = true,
  335. light_source = 4,
  336. drawtype = "nodebox",
  337. sunlight_propagates = true,
  338. walkable = true,
  339. node_box = {
  340. type = "wallmounted",
  341. wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
  342. wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
  343. wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
  344. },
  345. selection_box = {type = "wallmounted"},
  346. on_place = check_overlap,
  347. after_place_node = function(pos, placer)
  348. local meta = minetest.get_meta(pos)
  349. meta:set_string("owner", placer:get_player_name() or "")
  350. meta:set_string("infotext", S("Protection (owned by @1)", meta:get_string("owner")))
  351. meta:set_string("members", "")
  352. end,
  353. on_use = function(itemstack, user, pointed_thing)
  354. if pointed_thing.type ~= "node" then
  355. return
  356. end
  357. protector.can_dig(protector_radius, pointed_thing.under, user:get_player_name(), false, 2)
  358. end,
  359. on_rightclick = function(pos, node, clicker, itemstack)
  360. local meta = minetest.get_meta(pos)
  361. local name = clicker:get_player_name()
  362. if meta
  363. and protector.can_dig(1, pos, name, true, 1) then
  364. player_pos[name] = pos
  365. minetest.show_formspec(name, "protector:node", protector_formspec(meta))
  366. end
  367. end,
  368. on_punch = function(pos, node, puncher)
  369. if minetest.is_protected(pos, puncher:get_player_name()) then
  370. return
  371. end
  372. minetest.add_entity(pos, "protector:display")
  373. end,
  374. can_dig = function(pos, player)
  375. return player and protector.can_dig(1, pos, player:get_player_name(), true, 1)
  376. end,
  377. on_blast = function() end,
  378. after_destruct = function(pos, oldnode)
  379. local objects = minetest.get_objects_inside_radius(pos, 0.5)
  380. for _, v in ipairs(objects) do
  381. v:remove()
  382. end
  383. end,
  384. })
  385. -- recipes to switch between protectors
  386. minetest.register_craft({
  387. type = "shapeless",
  388. output = "protector:protect",
  389. recipe = {"protector:protect2"}
  390. })
  391. minetest.register_craft({
  392. type = "shapeless",
  393. output = "protector:protect2",
  394. recipe = {"protector:protect"}
  395. })
  396. -- check formspec buttons or when name entered
  397. minetest.register_on_player_receive_fields(function(player, formname, fields)
  398. if formname ~= "protector:node" then
  399. return
  400. end
  401. local name = player:get_player_name()
  402. local pos = player_pos[name]
  403. if not name or not pos then
  404. return
  405. end
  406. local add_member_input = fields.protector_add_member
  407. -- reset formspec until close button pressed
  408. if (fields.close_me or fields.quit)
  409. and (not add_member_input or add_member_input == "") then
  410. player_pos[name] = nil
  411. return
  412. end
  413. -- only owner can add names
  414. if not protector.can_dig(1, pos, player:get_player_name(), true, 1) then
  415. return
  416. end
  417. -- are we adding member to a protection node ? (csm protection)
  418. local nod = minetest.get_node(pos).name
  419. if nod ~= "protector:protect"
  420. and nod ~= "protector:protect2" then
  421. player_pos[name] = nil
  422. return
  423. end
  424. local meta = minetest.get_meta(pos)
  425. if not meta then
  426. return
  427. end
  428. -- add member [+]
  429. if add_member_input then
  430. for _, i in pairs(add_member_input:split(" ")) do
  431. add_member(meta, i)
  432. end
  433. end
  434. -- remove member [x]
  435. for field, value in pairs(fields) do
  436. if string.sub(field, 0,
  437. string.len("protector_del_member_")) == "protector_del_member_" then
  438. del_member(meta,
  439. string.sub(field,string.len("protector_del_member_") + 1))
  440. end
  441. end
  442. minetest.show_formspec(name, formname, protector_formspec(meta))
  443. end)
  444. -- display entity shown when protector node is punched
  445. minetest.register_entity("protector:display", {
  446. physical = false,
  447. collisionbox = {0, 0, 0, 0, 0, 0},
  448. visual = "wielditem",
  449. -- wielditem seems to be scaled to 1.5 times original node size
  450. visual_size = {x = 0.67, y = 0.67},
  451. textures = {"protector:display_node"},
  452. timer = 0,
  453. glow = 10,
  454. on_step = function(self, dtime)
  455. self.timer = self.timer + dtime
  456. -- remove after set number of seconds
  457. if self.timer > protector_show then
  458. self.object:remove()
  459. end
  460. end,
  461. })
  462. -- Display-zone node, Do NOT place the display as a node,
  463. -- it is made to be used as an entity (see above)
  464. local x = protector_radius
  465. minetest.register_node("protector:display_node", {
  466. tiles = {"protector_display.png"},
  467. use_texture_alpha = 'clip',
  468. walkable = false,
  469. drawtype = "nodebox",
  470. node_box = {
  471. type = "fixed",
  472. fixed = {
  473. -- sides
  474. {-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)},
  475. {-(x+.55), -(x+.55), (x+.45), (x+.55), (x+.55), (x+.55)},
  476. {(x+.45), -(x+.55), -(x+.55), (x+.55), (x+.55), (x+.55)},
  477. {-(x+.55), -(x+.55), -(x+.55), (x+.55), (x+.55), -(x+.45)},
  478. -- top
  479. {-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)},
  480. -- bottom
  481. {-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)},
  482. -- middle (surround protector)
  483. {-.55,-.55,-.55, .55,.55,.55},
  484. },
  485. },
  486. selection_box = {
  487. type = "regular",
  488. },
  489. paramtype = "light",
  490. groups = {dig_immediate = 3, not_in_creative_inventory = 1},
  491. drop = "",
  492. })
  493. dofile(MP .. "/doors_chest.lua")
  494. dofile(MP .. "/pvp.lua")
  495. dofile(MP .. "/admin.lua")
  496. dofile(MP .. "/tool.lua")
  497. dofile(MP .. "/hud.lua")
  498. print (S("[MOD] Protector Redo loaded"))