init.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. -- Minetest mod "City block"
  2. -- City block disables use of water/lava buckets and also sends aggressive players to jail
  3. -- 2016.02 - improvements suggested by rnd. removed spawn_jailer support. some small fixes and improvements.
  4. -- This library is free software; you can redistribute it and/or
  5. -- modify it under the terms of the GNU Lesser General Public
  6. -- License as published by the Free Software Foundation; either
  7. -- version 2.1 of the License, or (at your option) any later version.
  8. city_block = city_block or {}
  9. city_block.blocks = city_block.blocks or {}
  10. city_block.filename = minetest.get_worldpath() .. "/city_blocks.txt"
  11. city_block.modpath = minetest.get_modpath("city_block")
  12. -- Localize for performance.
  13. local vector_distance = vector.distance
  14. local vector_round = vector.round
  15. local vector_add = vector.add
  16. local vector_equals = vector.equals
  17. local math_random = math.random
  18. function city_block.on_punch(pos, node, puncher, pt)
  19. if not pos or not node or not puncher or not pt then
  20. return
  21. end
  22. local pname = puncher:get_player_name()
  23. if minetest.test_protection(pos, pname) then
  24. return
  25. end
  26. local wielded = puncher:get_wielded_item()
  27. if wielded:get_name() == "rosestone:head" and wielded:get_count() >= 8 then
  28. for i, v in ipairs(city_block.blocks) do
  29. if vector_equals(v.pos, pos) then
  30. if not v.is_jail then
  31. local p1 = vector_add(pos, {x=-1, y=0, z=-1})
  32. local p2 = vector_add(pos, {x=1, y=0, z=1})
  33. local positions, counts = minetest.find_nodes_in_area(p1, p2, "griefer:grieferstone")
  34. if counts["griefer:grieferstone"] == 8 then
  35. v.is_jail = true
  36. local meta = minetest.get_meta(pos)
  37. local infotext = meta:get_string("infotext")
  38. infotext = infotext .. "\nJail Marker"
  39. meta:set_string("infotext", infotext)
  40. city_block:save()
  41. wielded:take_item(8)
  42. puncher:set_wielded_item(wielded)
  43. minetest.chat_send_player(pname, "# Server: Jail position marked!")
  44. return
  45. end
  46. end
  47. end
  48. end
  49. end
  50. end
  51. -- Returns a table of the N-nearest city-blocks to a given position.
  52. -- The return value format is: {{pos, owner}, {pos, owner}, ...}
  53. -- Note: only returns blocks in the same realm! See RC mod.
  54. -- The 'rangelim' parameter is optional, if specified, blocks farther than this
  55. -- are ignored entirely.
  56. function city_block:nearest_blocks_to_position(pos, num, rangelim)
  57. local get_rn = rc.current_realm_at_pos
  58. local realm = get_rn(pos)
  59. -- Copy the master table's indices so we don't modify it.
  60. -- We do not need to copy the inner table data itself. Just the indices.
  61. -- Only copy over blocks in the same realm, too.
  62. local blocks = {}
  63. local sblocks = self.blocks
  64. for i=1, #sblocks, 1 do
  65. local p = sblocks[i].pos
  66. if rangelim then
  67. if vector_distance(p, pos) < rangelim then
  68. if get_rn(p) == realm then
  69. blocks[#blocks+1] = sblocks[i]
  70. end
  71. end
  72. else
  73. if get_rn(p) == realm then
  74. blocks[#blocks+1] = sblocks[i]
  75. end
  76. end
  77. end
  78. -- Sort blocks, nearest blocks first.
  79. table.sort(blocks,
  80. function(a, b)
  81. local d1 = vector_distance(a.pos, pos)
  82. local d2 = vector_distance(b.pos, pos)
  83. return d1 < d2
  84. end)
  85. -- Return N-nearest blocks (should be at the front of the sorted table).
  86. local ret = {}
  87. for i=1, num, 1 do
  88. if i <= #blocks then
  89. ret[#ret+1] = blocks[i]
  90. else
  91. break
  92. end
  93. end
  94. return ret
  95. end
  96. function city_block:nearest_jails_to_position(pos, num, rangelim)
  97. local get_rn = rc.current_realm_at_pos
  98. local realm = get_rn(pos)
  99. -- Copy the master table's indices so we don't modify it.
  100. -- We do not need to copy the inner table data itself. Just the indices.
  101. -- Only copy over blocks in the same realm, too.
  102. local blocks = {}
  103. local sblocks = self.blocks
  104. for i=1, #sblocks, 1 do
  105. local v = sblocks[i]
  106. local p = v.pos
  107. if v.is_jail then
  108. if rangelim then
  109. if vector_distance(p, pos) < rangelim then
  110. if get_rn(p) == realm then
  111. blocks[#blocks+1] = v
  112. end
  113. end
  114. else
  115. if get_rn(p) == realm then
  116. blocks[#blocks+1] = v
  117. end
  118. end
  119. end
  120. end
  121. -- Sort blocks, nearest blocks first.
  122. table.sort(blocks,
  123. function(a, b)
  124. local d1 = vector_distance(a.pos, pos)
  125. local d2 = vector_distance(b.pos, pos)
  126. return d1 < d2
  127. end)
  128. -- Return N-nearest blocks (should be at the front of the sorted table).
  129. local ret = {}
  130. for i=1, num, 1 do
  131. if i <= #blocks then
  132. ret[#ret+1] = blocks[i]
  133. else
  134. break
  135. end
  136. end
  137. return ret
  138. end
  139. function city_block.erase_jail(pos)
  140. pos = vector_round(pos)
  141. local b = city_block.blocks
  142. for k, v in ipairs(b) do
  143. if vector_equals(pos, v.pos) then
  144. local meta = minetest.get_meta(pos)
  145. local pname = meta:get_string("owner")
  146. local dname = rename.gpn(pname)
  147. meta:set_string("infotext", "City Marker (Placed by <" .. dname .. ">!)")
  148. v.is_jail = nil
  149. city_block:save()
  150. return
  151. end
  152. end
  153. end
  154. function city_block:save()
  155. local datastring = minetest.serialize(self.blocks)
  156. if not datastring then
  157. return
  158. end
  159. local file, err = io.open(self.filename, "w")
  160. if err then
  161. return
  162. end
  163. file:write(datastring)
  164. file:close()
  165. end
  166. function city_block:load()
  167. local file, err = io.open(self.filename, "r")
  168. if err then
  169. self.blocks = {}
  170. return
  171. end
  172. self.blocks = minetest.deserialize(file:read("*all"))
  173. if type(self.blocks) ~= "table" then
  174. self.blocks = {}
  175. end
  176. file:close()
  177. end
  178. function city_block:in_city(pos)
  179. -- Covers a 45x45x45 area.
  180. local r = 22
  181. local blocks = self.blocks
  182. for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
  183. local v = blocks[i]
  184. local vpos = v.pos
  185. if pos.x > (vpos.x - r) and pos.x < (vpos.x + r) and
  186. pos.z > (vpos.z - r) and pos.z < (vpos.z + r) and
  187. pos.y > (vpos.y - r) and pos.y < (vpos.y + r) then
  188. return true
  189. end
  190. end
  191. return false
  192. end
  193. function city_block:in_city_suburbs(pos)
  194. local r = 44
  195. local blocks = self.blocks
  196. for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
  197. local v = blocks[i]
  198. local vpos = v.pos
  199. if pos.x > (vpos.x - r) and pos.x < (vpos.x + r) and
  200. pos.z > (vpos.z - r) and pos.z < (vpos.z + r) and
  201. pos.y > (vpos.y - r) and pos.y < (vpos.y + r) then
  202. return true
  203. end
  204. end
  205. return false
  206. end
  207. function city_block:in_safebed_zone(pos)
  208. -- Covers a 111x111x111 area.
  209. local r = 55
  210. local blocks = self.blocks
  211. for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
  212. local v = blocks[i]
  213. local vpos = v.pos
  214. if pos.x > (vpos.x - r) and pos.x < (vpos.x + r) and
  215. pos.z > (vpos.z - r) and pos.z < (vpos.z + r) and
  216. pos.y > (vpos.y - r) and pos.y < (vpos.y + r) then
  217. return true
  218. end
  219. end
  220. return false
  221. end
  222. function city_block:in_no_tnt_zone(pos)
  223. local r = 50
  224. local blocks = self.blocks
  225. for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
  226. local v = blocks[i]
  227. local vpos = v.pos
  228. if pos.x > (vpos.x - r) and pos.x < (vpos.x + r) and
  229. pos.z > (vpos.z - r) and pos.z < (vpos.z + r) and
  230. pos.y > (vpos.y - r) and pos.y < (vpos.y + r) then
  231. return true
  232. end
  233. end
  234. return false
  235. end
  236. function city_block:in_no_leecher_zone(pos)
  237. local r = 100
  238. local blocks = self.blocks
  239. for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
  240. local v = blocks[i]
  241. local vpos = v.pos
  242. if pos.x > (vpos.x - r) and pos.x < (vpos.x + r) and
  243. pos.z > (vpos.z - r) and pos.z < (vpos.z + r) and
  244. pos.y > (vpos.y - r) and pos.y < (vpos.y + r) then
  245. return true
  246. end
  247. end
  248. return false
  249. end
  250. if not city_block.run_once then
  251. city_block:load()
  252. minetest.register_node("city_block:cityblock", {
  253. description = "Lawful Zone Marker [Marks a 45x45x45 area as a city.]\n\nSaves your bed respawn position, if someone killed you within the city area.\nMurderers and trespassers will be sent to the Colony jail if caught in a city.\nPrevents the use of ore leeching equipment within 100 meters radius.\nPrevents mining with TNT nearby.",
  254. tiles = {"moreblocks_circle_stone_bricks.png^default_tool_mesepick.png"},
  255. is_ground_content = false,
  256. groups = utility.dig_groups("obsidian", {
  257. immovable=1,
  258. }),
  259. is_ground_content = false,
  260. sounds = default.node_sound_stone_defaults(),
  261. after_place_node = function(pos, placer)
  262. if placer and placer:is_player() then
  263. local pname = placer:get_player_name()
  264. local meta = minetest.get_meta(pos)
  265. local dname = rename.gpn(pname)
  266. meta:set_string("rename", dname)
  267. meta:set_string("owner", pname)
  268. meta:set_string("infotext", "City Marker (Placed by <" .. dname .. ">!)")
  269. table.insert(city_block.blocks, {pos=vector_round(pos), owner=pname})
  270. city_block:save()
  271. end
  272. end,
  273. -- We don't need an `on_blast` func because TNT calls `on_destruct` properly!
  274. on_destruct = function(pos)
  275. -- The cityblock may not exist in the list if the node was created by falling,
  276. -- and was later dug.
  277. for i, EachBlock in ipairs(city_block.blocks) do
  278. if vector_equals(EachBlock.pos, pos) then
  279. table.remove(city_block.blocks, i)
  280. city_block:save()
  281. end
  282. end
  283. end,
  284. -- Called by rename LBM.
  285. _on_rename_check = function(pos)
  286. local meta = minetest.get_meta(pos)
  287. local owner = meta:get_string("owner")
  288. -- Nobody placed this block.
  289. if owner == "" then
  290. return
  291. end
  292. local dname = rename.gpn(owner)
  293. meta:set_string("rename", dname)
  294. meta:set_string("infotext", "City Marker (Placed by <" .. dname .. ">!)")
  295. end,
  296. on_punch = function(...)
  297. return city_block.on_punch(...)
  298. end,
  299. })
  300. minetest.register_craft({
  301. output = 'city_block:cityblock',
  302. recipe = {
  303. {'default:pick_mese', 'farming:hoe_mese', 'default:sword_diamond'},
  304. {'chests:chest_locked', 'default:goldblock', 'default:sandstone'},
  305. {'default:obsidianbrick', 'default:mese', 'cobble_furnace:inactive'},
  306. }
  307. })
  308. minetest.register_privilege("disable_pvp", "Players cannot damage players with this priv by punching.")
  309. minetest.register_on_punchplayer(function(...)
  310. return city_block.on_punchplayer(...)
  311. end)
  312. local c = "city_block:core"
  313. local f = city_block.modpath .. "/init.lua"
  314. reload.register_file(c, f, false)
  315. city_block.run_once = true
  316. end
  317. function city_block:get_adjective()
  318. local adjectives = {
  319. "murdering",
  320. "slaying",
  321. "killing",
  322. "whacking",
  323. "trashing",
  324. "fatally attacking",
  325. "fatally harming",
  326. "doing away with",
  327. "giving the Chicago treatment to",
  328. "fatally thrashing",
  329. "fatally stabbing",
  330. }
  331. return adjectives[math_random(1, #adjectives)]
  332. end
  333. local murder_messages = {
  334. "<v> collapsed from <k>'s brutal attack.",
  335. "<k>'s <w> apparently wasn't such an unusual weapon after all, as <v> found out.",
  336. "<k> killed <v> with great prejudice.",
  337. "<v> died from <k>'s horrid slaying.",
  338. "<v> fell prey to <k>'s deadly <w>.",
  339. "<k> went out of <k_his> way to slay <v> with <k_his> <w>.",
  340. "<v> danced <v_himself> to death under <k>'s craftily wielded <w>.",
  341. "<k> used <k_his> <w> to kill <v> with prejudice.",
  342. "<k> made a splortching sound with <v>'s head.",
  343. "<v> got flattened by <k>'s skillfully handled <w>.",
  344. "<v> became prey for <k>.",
  345. "<v> didn't get out of <k>'s way in time.",
  346. "<v> SAW <k> coming with <k_his> <w>. Didn't get away in time.",
  347. "<v> made no real attempt to get out of <k>'s way.",
  348. "<k> barreled through <v> as if <v_he> wasn't there.",
  349. "<k> sent <v> to that place where kindling wood isn't needed.",
  350. "<v> didn't suspect that <k> meant <v_him> any harm.",
  351. "<v> fought <k> to the death and lost painfully.",
  352. "<v> knew <k> was wielding <k_his> <w> but didn't guess what <k> meant to do with it.",
  353. "<k> clonked <v> over the head using <k_his> <w> with silent skill.",
  354. "<k> made sure <v> didn't see that coming!",
  355. "<k> has decided <k_his> favorite weapon is <k_his> <w>.",
  356. "<v> did the mad hatter dance just before being killed with <k>'s <w>.",
  357. "<v> played the victim to <k>'s bully behavior!",
  358. "<k> used <v> for weapons practice with <k_his> <w>.",
  359. "<v> failed to avoid <k>'s oncoming weapon.",
  360. "<k> successfully got <v> to complain of a headache.",
  361. "<v> got <v_himself> some serious hurt from <k>'s <w>.",
  362. "Trying to talk peace to <k> didn't win any for <v>.",
  363. "<v> was brutally slain by <k>'s <w>.",
  364. "<v> jumped the mad-hatter dance under <k>'s <w>.",
  365. "<v> got <v_himself> a fatal mauling by <k>'s <w>.",
  366. "<k> just assassinated <v> with <k_his> <w>.",
  367. "<k> split <v>'s wig.",
  368. "<k> took revenge on <v>.",
  369. "<k> flattened <v>.",
  370. "<v> played dead. Permanently.",
  371. "<v> never saw what hit <v_him>.",
  372. "<k> took <v> by surprise.",
  373. "<v> was assassinated.",
  374. "<k> didn't take any prisoners from <v>.",
  375. "<k> pinned <v> to the wall with <k_his> <w>.",
  376. "<v> failed <v_his> weapon checks.",
  377. }
  378. function city_block.murder_message(killer, victim, sendto)
  379. local msg = murder_messages[math_random(1, #murder_messages)]
  380. msg = string.gsub(msg, "<v>", "<" .. rename.gpn(victim) .. ">")
  381. msg = string.gsub(msg, "<k>", "<" .. rename.gpn(killer) .. ">")
  382. local ksex = skins.get_gender_strings(killer)
  383. local vsex = skins.get_gender_strings(victim)
  384. msg = string.gsub(msg, "<k_himself>", ksex.himself)
  385. msg = string.gsub(msg, "<k_his>", ksex.his)
  386. msg = string.gsub(msg, "<v_himself>", vsex.himself)
  387. msg = string.gsub(msg, "<v_his>", vsex.his)
  388. msg = string.gsub(msg, "<v_him>", vsex.him)
  389. msg = string.gsub(msg, "<v_he>", vsex.he)
  390. if string.find(msg, "<w>") then
  391. local hitter = minetest.get_player_by_name(killer)
  392. if hitter then
  393. local wield = hitter:get_wielded_item()
  394. local def = minetest.registered_items[wield:get_name()]
  395. local meta = wield:get_meta()
  396. local description = meta:get_string("description")
  397. if description ~= "" then
  398. msg = string.gsub(msg, "<w>", "'" .. utility.get_short_desc(description):trim() .. "'")
  399. elseif def and def.description then
  400. local str = utility.get_short_desc(def.description)
  401. if str == "" then
  402. str = "Potato Fist"
  403. end
  404. msg = string.gsub(msg, "<w>", str)
  405. end
  406. end
  407. end
  408. if type(sendto) == "string" then
  409. minetest.chat_send_player(sendto, "# Server: " .. msg)
  410. else
  411. minetest.chat_send_all("# Server: " .. msg)
  412. end
  413. end
  414. function city_block.hit_possible(p1pos, p2pos)
  415. -- Range limit, stops hackers with long reach.
  416. if vector_distance(p1pos, p2pos) > 5 then
  417. return false
  418. end
  419. -- Cannot attack through walls.
  420. -- But if node wouldn't stop an arrow, keep testing the line.
  421. --local raycast = minetest.raycast(p1pos, p2pos, false, false)
  422. -- This seems to cause random freezes and 100% CPU.
  423. --[[
  424. local los, pstop = minetest.line_of_sight(p1pos, p2pos)
  425. while not los do
  426. if throwing.node_blocks_arrow(minetest.get_node(vector_round(pstop)).name) then
  427. return false
  428. end
  429. local dir = vector.direction(pstop, p2pos)
  430. local ns = vector.add(pstop, dir)
  431. los, pstop = minetest.line_of_sight(ns, p2pos)
  432. end
  433. --]]
  434. return true
  435. end
  436. city_block.attacker = city_block.attacker or {}
  437. city_block.attack = city_block.attack or {}
  438. -- Return `true' to prevent the default damage mechanism.
  439. function city_block.on_punchplayer(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
  440. if not player:is_player() or not hitter:is_player() then
  441. return
  442. end
  443. -- Random accidents happen to punished players during PvP.
  444. do
  445. local attacker_pname = hitter:get_player_name()
  446. if sheriff.is_cheater(attacker_pname) then
  447. if sheriff.punish_probability(attacker_pname) then
  448. sheriff.punish_player(attacker_pname)
  449. end
  450. end
  451. end
  452. local p1pos = utility.get_head_pos(hitter:get_pos())
  453. local p2pos = utility.get_head_pos(player:get_pos())
  454. -- Check if hit is physically possible (range, blockage, etc).
  455. if not city_block.hit_possible(p1pos, p2pos) then
  456. return true
  457. end
  458. -- PvP is disabled for players in jail. This fixes a possible way to exploit jail.
  459. if jail.is_player_in_jail(hitter) or jail.is_player_in_jail(player) then
  460. minetest.chat_send_player(hitter:get_player_name(), "# Server: Brawling is not allowed in jail. This is colony law.")
  461. return true
  462. end
  463. -- Admins cannot be punched.
  464. if minetest.check_player_privs(player:get_player_name(), {disable_pvp=true}) then
  465. return true
  466. end
  467. local victim_pname = player:get_player_name();
  468. local attacker_pname = hitter:get_player_name();
  469. local t = minetest.get_gametime() or 0;
  470. city_block.attacker[victim_pname] = attacker_pname;
  471. city_block.attack[victim_pname] = t;
  472. local hp = player:get_hp();
  473. if damage > 0 then
  474. ambiance.sound_play("player_damage", p2pos, 2.0, 30)
  475. end
  476. if hp > 0 and (hp - damage) <= 0 then -- player will die because of this hit
  477. default.detach_player_if_attached(player)
  478. city_block.murder_message(attacker_pname, victim_pname)
  479. if city_block:in_city(p2pos) then
  480. local t0 = city_block.attack[attacker_pname] or t;
  481. t0 = t - t0;
  482. if not city_block.attacker[attacker_pname] then
  483. city_block.attacker[attacker_pname] = ""
  484. end
  485. local landowner = protector.get_node_owner(p2pos) or ""
  486. -- Justified killing 10 seconds after provocation, but not if the victim owns the land.
  487. if city_block.attacker[attacker_pname] == victim_pname and t0 < 10 and victim_pname ~= landowner then
  488. return
  489. else -- go to jail
  490. -- Killers don't go to jail if the victim is a registered cheater.
  491. if not sheriff.is_cheater(victim_pname) then
  492. if jail.go_to_jail(hitter, nil) then
  493. minetest.chat_send_all(
  494. "# Server: Criminal <" .. rename.gpn(attacker_pname) .. "> was sent to gaol for " ..
  495. city_block:get_adjective() .. " <" .. rename.gpn(victim_pname) .. "> within city limits.")
  496. end
  497. end
  498. end
  499. else
  500. -- Bed position is only lost if player died outside city.
  501. if not city_block:in_safebed_zone(p2pos) then
  502. -- Victim doesn't lose their bed respawn if they were killed by a cheater.
  503. if not sheriff.is_cheater(attacker_pname) then
  504. minetest.chat_send_player(victim_pname, "# Server: Your bed is lost! You were assassinated outside of any town, city, or municipality.")
  505. beds.clear_player_spawn(victim_pname)
  506. end
  507. end
  508. end
  509. end
  510. end