init.lua 15 KB

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