init.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. function city_block:save()
  13. local datastring = minetest.serialize(self.blocks)
  14. if not datastring then
  15. return
  16. end
  17. local file, err = io.open(self.filename, "w")
  18. if err then
  19. return
  20. end
  21. file:write(datastring)
  22. file:close()
  23. end
  24. function city_block:load()
  25. local file, err = io.open(self.filename, "r")
  26. if err then
  27. self.blocks = {}
  28. return
  29. end
  30. self.blocks = minetest.deserialize(file:read("*all"))
  31. if type(self.blocks) ~= "table" then
  32. self.blocks = {}
  33. end
  34. file:close()
  35. end
  36. function city_block:in_city(pos)
  37. -- Covers a 45x45x45 area.
  38. local r = 22
  39. for k, v in ipairs(self.blocks) do
  40. if pos.x > (v.pos.x - r) and pos.x < (v.pos.x + r) and
  41. pos.z > (v.pos.z - r) and pos.z < (v.pos.z + r) and
  42. pos.y > (v.pos.y - r) and pos.y < (v.pos.y + r) then
  43. return true
  44. end
  45. end
  46. return false
  47. end
  48. function city_block:in_safebed_zone(pos)
  49. -- Covers a 111x111x111 area.
  50. local r = 55
  51. for k, v in ipairs(self.blocks) do
  52. if pos.x > (v.pos.x - r) and pos.x < (v.pos.x + r) and
  53. pos.z > (v.pos.z - r) and pos.z < (v.pos.z + r) and
  54. pos.y > (v.pos.y - r) and pos.y < (v.pos.y + r) then
  55. return true
  56. end
  57. end
  58. return false
  59. end
  60. function city_block:in_no_tnt_zone(pos)
  61. local r = 50
  62. for k, v in ipairs(self.blocks) do
  63. if pos.x > (v.pos.x - r) and pos.x < (v.pos.x + r) and
  64. pos.z > (v.pos.z - r) and pos.z < (v.pos.z + r) and
  65. pos.y > (v.pos.y - r) and pos.y < (v.pos.y + r) then
  66. return true
  67. end
  68. end
  69. return false
  70. end
  71. function city_block:in_no_leecher_zone(pos)
  72. local r = 100
  73. for k, v in ipairs(self.blocks) do
  74. if pos.x > (v.pos.x - r) and pos.x < (v.pos.x + r) and
  75. pos.z > (v.pos.z - r) and pos.z < (v.pos.z + r) and
  76. pos.y > (v.pos.y - r) and pos.y < (v.pos.y + r) then
  77. return true
  78. end
  79. end
  80. return false
  81. end
  82. if not city_block.run_once then
  83. city_block:load()
  84. minetest.register_node("city_block:cityblock", {
  85. 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.",
  86. tiles = {"moreblocks_circle_stone_bricks.png^default_tool_mesepick.png"},
  87. is_ground_content = false,
  88. groups = utility.dig_groups("obsidian", {
  89. immovable=1,
  90. }),
  91. is_ground_content = false,
  92. sounds = default.node_sound_stone_defaults(),
  93. after_place_node = function(pos, placer)
  94. if placer and placer:is_player() then
  95. local pname = placer:get_player_name()
  96. local meta = minetest.get_meta(pos)
  97. local dname = rename.gpn(pname)
  98. meta:set_string("rename", dname)
  99. meta:set_string("owner", pname)
  100. meta:set_string("infotext", "City Marker (Placed by <" .. dname .. ">!)")
  101. table.insert(city_block.blocks, {pos=vector.round(pos), owner=pname})
  102. city_block:save()
  103. end
  104. end,
  105. -- We don't need an `on_blast` func because TNT calls `on_destruct` properly!
  106. on_destruct = function(pos)
  107. -- The cityblock may not exist in the list if the node was created by falling,
  108. -- and was later dug.
  109. for i, EachBlock in ipairs(city_block.blocks) do
  110. if vector.equals(EachBlock.pos, pos) then
  111. table.remove(city_block.blocks, i)
  112. city_block:save()
  113. end
  114. end
  115. end,
  116. -- Called by rename LBM.
  117. _on_rename_check = function(pos)
  118. local meta = minetest.get_meta(pos)
  119. local owner = meta:get_string("owner")
  120. -- Nobody placed this block.
  121. if owner == "" then
  122. return
  123. end
  124. local dname = rename.gpn(owner)
  125. meta:set_string("rename", dname)
  126. meta:set_string("infotext", "City Marker (Placed by <" .. dname .. ">!)")
  127. end,
  128. })
  129. minetest.register_craft({
  130. output = 'city_block:cityblock',
  131. recipe = {
  132. {'default:pick_mese', 'farming:hoe_mese', 'default:sword_diamond'},
  133. {'chests:chest_locked', 'default:goldblock', 'default:sandstone'},
  134. {'default:obsidianbrick', 'default:mese', 'cobble_furnace:inactive'},
  135. }
  136. })
  137. minetest.register_privilege("disable_pvp", "Players cannot damage players with this priv by punching.")
  138. minetest.register_on_punchplayer(function(...)
  139. return city_block.on_punchplayer(...)
  140. end)
  141. local c = "city_block:core"
  142. local f = city_block.modpath .. "/init.lua"
  143. reload.register_file(c, f, false)
  144. city_block.run_once = true
  145. end
  146. function city_block:get_adjective()
  147. local adjectives = {
  148. "murdering",
  149. "slaying",
  150. "killing",
  151. "whacking",
  152. "trashing",
  153. "fatally attacking",
  154. "fatally harming",
  155. "doing away with",
  156. "giving the Chicago treatment to",
  157. "fatally thrashing",
  158. "fatally stabbing",
  159. }
  160. return adjectives[math.random(1, #adjectives)]
  161. end
  162. local murder_messages = {
  163. "<v> collapsed from <k>'s brutal attack.",
  164. "<k>'s <w> apparently wasn't such an unusual weapon after all, as <v> found out.",
  165. "<k> killed <v> with great prejudice.",
  166. "<v> died from <k>'s horrid slaying.",
  167. "<v> fell prey to <k>'s deadly <w>.",
  168. "<k> went out of <k_his> way to slay <v> with <k_his> <w>.",
  169. "<v> danced <v_himself> to death under <k>'s craftily wielded <w>.",
  170. "<k> used <k_his> <w> to kill <v> with prejudice.",
  171. "<k> made a splortching sound with <v>'s head.",
  172. "<v> got flattened by <k>'s skillfully handled <w>.",
  173. "<v> became prey for <k>.",
  174. "<v> didn't get out of <k>'s way in time.",
  175. "<v> SAW <k> coming with <k_his> <w>. Didn't get away in time.",
  176. "<v> made no real attempt to get out of <k>'s way.",
  177. "<k> barreled through <v> as if <v_he> wasn't there.",
  178. "<k> sent <v> to that place where kindling wood isn't needed.",
  179. "<v> didn't suspect that <k> meant <v_him> any harm.",
  180. "<v> fought <k> to the death and lost painfully.",
  181. "<v> knew <k> was wielding <k_his> <w> but didn't guess what <k> meant to do with it.",
  182. "<k> clonked <v> over the head using <k_his> <w> with silent skill.",
  183. "<k> made sure <v> didn't see that coming!",
  184. "<k> has decided <k_his> favorite weapon is <k_his> <w>.",
  185. "<v> did the mad hatter dance just before being killed with <k>'s <w>.",
  186. "<v> played the victim to <k>'s bully behavior!",
  187. "<k> used <v> for weapons practice with <k_his> <w>.",
  188. "<v> failed to avoid <k>'s oncoming weapon.",
  189. "<k> successfully got <v> to complain of a headache.",
  190. "<v> got <v_himself> some serious hurt from <k>'s <w>.",
  191. "Trying to talk peace to <k> didn't win any for <v>.",
  192. "<v> was brutally slain by <k>'s <w>.",
  193. "<v> jumped the mad-hatter dance under <k>'s <w>.",
  194. "<v> got <v_himself> a fatal mauling by <k>'s <w>.",
  195. "<k> just assassinated <v> with <k_his> <w>.",
  196. "<k> split <v>'s wig.",
  197. "<k> took revenge on <v>.",
  198. "<k> flattened <v>.",
  199. "<v> played dead. Permanently.",
  200. "<v> never saw what hit <v_him>.",
  201. "<k> took <v> by surprise.",
  202. "<v> was assassinated.",
  203. "<k> didn't take any prisoners from <v>.",
  204. "<k> pinned <v> to the wall with <k_his> <w>.",
  205. "<v> failed <v_his> weapon checks.",
  206. }
  207. function city_block.murder_message(killer, victim, sendto)
  208. local msg = murder_messages[math.random(1, #murder_messages)]
  209. msg = string.gsub(msg, "<v>", "<" .. rename.gpn(victim) .. ">")
  210. msg = string.gsub(msg, "<k>", "<" .. rename.gpn(killer) .. ">")
  211. local ksex = skins.get_gender_strings(killer)
  212. local vsex = skins.get_gender_strings(victim)
  213. msg = string.gsub(msg, "<k_himself>", ksex.himself)
  214. msg = string.gsub(msg, "<k_his>", ksex.his)
  215. msg = string.gsub(msg, "<v_himself>", vsex.himself)
  216. msg = string.gsub(msg, "<v_his>", vsex.his)
  217. msg = string.gsub(msg, "<v_him>", vsex.him)
  218. msg = string.gsub(msg, "<v_he>", vsex.he)
  219. if string.find(msg, "<w>") then
  220. local hitter = minetest.get_player_by_name(killer)
  221. if hitter then
  222. local wield = hitter:get_wielded_item()
  223. local def = minetest.registered_items[wield:get_name()]
  224. local meta = wield:get_meta()
  225. local description = meta:get_string("description")
  226. if description ~= "" then
  227. msg = string.gsub(msg, "<w>", "'" .. utility.get_short_desc(description):trim() .. "'")
  228. elseif def and def.description then
  229. local str = utility.get_short_desc(def.description)
  230. if str == "" then
  231. str = "Potato Fist"
  232. end
  233. msg = string.gsub(msg, "<w>", str)
  234. end
  235. end
  236. end
  237. if type(sendto) == "string" then
  238. minetest.chat_send_player(sendto, "# Server: " .. msg)
  239. else
  240. minetest.chat_send_all("# Server: " .. msg)
  241. end
  242. end
  243. local in_jail = function(pos)
  244. -- Surface jail.
  245. if pos.y <= -48 and pos.y >= -52 then
  246. if pos.x >= -11 and pos.x <= 11 and pos.z >= -11 and pos.z <= 11 then
  247. return true
  248. end
  249. end
  250. -- Nether jail.
  251. if pos.y <= -30760 and pos.y >= -30770 then
  252. if pos.x >= -11 and pos.x <= 11 and pos.z >= -11 and pos.z <= 11 then
  253. return true
  254. end
  255. end
  256. end
  257. function city_block.hit_possible(p1pos, p2pos)
  258. -- Range limit, stops hackers with long reach.
  259. if vector.distance(p1pos, p2pos) > 5 then
  260. return false
  261. end
  262. -- Cannot attack through walls.
  263. -- But if node wouldn't stop an arrow, keep testing the line.
  264. --local raycast = minetest.raycast(p1pos, p2pos, false, false)
  265. -- This seems to cause random freezes and 100% CPU.
  266. --[[
  267. local los, pstop = minetest.line_of_sight(p1pos, p2pos)
  268. while not los do
  269. if throwing.node_blocks_arrow(minetest.get_node(vector.round(pstop)).name) then
  270. return false
  271. end
  272. local dir = vector.direction(pstop, p2pos)
  273. local ns = vector.add(pstop, dir)
  274. los, pstop = minetest.line_of_sight(ns, p2pos)
  275. end
  276. --]]
  277. return true
  278. end
  279. city_block.attacker = city_block.attacker or {}
  280. city_block.attack = city_block.attack or {}
  281. -- Return `true' to prevent the default damage mechanism.
  282. function city_block.on_punchplayer(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
  283. if not player:is_player() or not hitter:is_player() then
  284. return
  285. end
  286. -- Random accidents happen to punished players during PvP.
  287. do
  288. local attacker_pname = hitter:get_player_name()
  289. if sheriff.is_cheater(attacker_pname) then
  290. if sheriff.punish_probability(attacker_pname) then
  291. sheriff.punish_player(attacker_pname)
  292. end
  293. end
  294. end
  295. local p1pos = utility.get_head_pos(hitter:get_pos())
  296. local p2pos = utility.get_head_pos(player:get_pos())
  297. -- Check if hit is physically possible (range, blockage, etc).
  298. if not city_block.hit_possible(p1pos, p2pos) then
  299. return true
  300. end
  301. -- PvP is disabled for players in jail. This fixes a possible way to exploit jail.
  302. if in_jail(p1pos) or in_jail(p2pos) then
  303. minetest.chat_send_player(hitter:get_player_name(), "# Server: Brawling is not allowed in jail. This is colony law.")
  304. return true
  305. end
  306. -- Admins cannot be punched.
  307. if minetest.check_player_privs(player:get_player_name(), {disable_pvp=true}) then
  308. return true
  309. end
  310. local victim_pname = player:get_player_name();
  311. local attacker_pname = hitter:get_player_name();
  312. local t = minetest.get_gametime() or 0;
  313. city_block.attacker[victim_pname] = attacker_pname;
  314. city_block.attack[victim_pname] = t;
  315. local hp = player:get_hp();
  316. if damage > 0 then
  317. ambiance.sound_play("player_damage", p2pos, 2.0, 30)
  318. end
  319. if hp > 0 and (hp - damage) <= 0 then -- player will die because of this hit
  320. default.detach_player_if_attached(player)
  321. city_block.murder_message(attacker_pname, victim_pname)
  322. if city_block:in_city(p2pos) then
  323. local t0 = city_block.attack[attacker_pname] or t;
  324. t0 = t - t0;
  325. if not city_block.attacker[attacker_pname] then
  326. city_block.attacker[attacker_pname] = ""
  327. end
  328. local landowner = protector.get_node_owner(p2pos) or ""
  329. -- Justified killing 10 seconds after provocation, but not if the victim owns the land.
  330. if city_block.attacker[attacker_pname] == victim_pname and t0 < 10 and victim_pname ~= landowner then
  331. return
  332. else -- go to jail
  333. -- Killers don't go to jail if the victim is a registered cheater.
  334. if not sheriff.is_cheater(victim_pname) then
  335. jail.go_to_jail(hitter, nil)
  336. minetest.chat_send_all(
  337. "# Server: Criminal <" .. rename.gpn(attacker_pname) .. "> was sent to gaol for " ..
  338. city_block:get_adjective() .. " <" .. rename.gpn(victim_pname) .. "> within city limits.")
  339. end
  340. end
  341. else
  342. -- Bed position is only lost if player died outside city.
  343. if not city_block:in_safebed_zone(p2pos) then
  344. -- Victim doesn't lose their bed respawn if they were killed by a cheater.
  345. if not sheriff.is_cheater(attacker_pname) then
  346. minetest.chat_send_player(victim_pname, "# Server: Your bed is lost! You were assassinated outside of any town, city, or municipality.")
  347. beds.clear_player_spawn(victim_pname)
  348. end
  349. end
  350. end
  351. end
  352. end