12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100 |
- -- Minetest mod "City block"
- -- City block disables use of water/lava buckets and also sends aggressive players to jail
- -- 2016.02 - improvements suggested by rnd. removed spawn_jailer support. some small fixes and improvements.
- -- This library is free software; you can redistribute it and/or
- -- modify it under the terms of the GNU Lesser General Public
- -- License as published by the Free Software Foundation; either
- -- version 2.1 of the License, or (at your option) any later version.
- if not minetest.global_exists("city_block") then city_block = {} end
- city_block.blocks = city_block.blocks or {}
- city_block.filename = minetest.get_worldpath() .. "/city_blocks.txt"
- city_block.modpath = minetest.get_modpath("city_block")
- city_block.formspecs = city_block.formspecs or {}
- -- Localize for performance.
- local vector_distance = vector.distance
- local vector_round = vector.round
- local vector_add = vector.add
- local vector_equals = vector.equals
- local math_random = math.random
- -- Cityblocks take 6 hours to become "active".
- -- This prevents certain classes of exploits (such as using them offensively
- -- during PvP). This also strongly discourages constantly moving them around
- -- for trivial reasons.
- local CITYBLOCK_DELAY_TIME = 60*60*6
- local function time_active(t1, t2)
- return (math.abs(t2 - t1) > CITYBLOCK_DELAY_TIME)
- end
- function city_block.delete_blocks_from_area(minp, maxp)
- local i = 1
- local blocks = city_block.blocks
- ::do_next::
- if i > #blocks then
- return
- end
- local p = blocks[i].pos
- if p.x >= minp.x and p.x <= maxp.x and
- p.y >= minp.y and p.y <= maxp.y and
- p.z >= minp.z and p.z <= maxp.z then
- -- Don't need to worry about relative ordering.
- -- This is your standard swap'n'pop.
- blocks[i] = blocks[#blocks]
- blocks[#blocks] = nil
- goto do_next
- end
- i = i + 1
- goto do_next
- end
- function city_block.on_punch(pos, node, puncher, pt)
- if not pos or not node or not puncher or not pt then
- return
- end
- local pname = puncher:get_player_name()
- if minetest.test_protection(pos, pname) then
- return
- end
- local wielded = puncher:get_wielded_item()
- if wielded:get_name() == "rosestone:head" and wielded:get_count() >= 8 then
- for i, v in ipairs(city_block.blocks) do
- if vector_equals(v.pos, pos) then
- if not v.is_jail then
- local p1 = vector_add(pos, {x=-1, y=0, z=-1})
- local p2 = vector_add(pos, {x=1, y=0, z=1})
- local positions, counts = minetest.find_nodes_in_area(p1, p2, "griefer:grieferstone")
- if counts["griefer:grieferstone"] == 8 then
- v.is_jail = true
- local meta = minetest.get_meta(pos)
- local infotext = meta:get_string("infotext")
- infotext = infotext .. "\nJail Marker"
- meta:set_string("infotext", infotext)
- city_block:save()
- wielded:take_item(8)
- puncher:set_wielded_item(wielded)
- minetest.chat_send_player(pname, "# Server: Jail position marked!")
- return
- end
- end
- end
- end
- end
- end
- -- Returns a table of the N-nearest city-blocks to a given position.
- -- The return value format is: {{pos, owner}, {pos, owner}, ...}
- -- Note: only returns blocks in the same realm! See RC mod.
- -- The 'rangelim' parameter is optional, if specified, blocks farther than this
- -- are ignored entirely.
- function city_block:nearest_blocks_to_position(pos, num, rangelim)
- local get_rn = rc.current_realm_at_pos
- local realm = get_rn(pos)
- -- Copy the master table's indices so we don't modify it.
- -- We do not need to copy the inner table data itself. Just the indices.
- -- Only copy over blocks in the same realm, too.
- local blocks = {}
- local sblocks = self.blocks
- local t2 = os.time()
- for i=1, #sblocks, 1 do
- local p = sblocks[i].pos
- local t1 = sblocks[i].time or 0
- if time_active(t1, t2) then
- if rangelim then
- if vector_distance(p, pos) < rangelim then
- if get_rn(p) == realm then
- blocks[#blocks+1] = sblocks[i]
- end
- end
- else
- if get_rn(p) == realm then
- blocks[#blocks+1] = sblocks[i]
- end
- end
- end
- end
- -- Sort blocks, nearest blocks first.
- table.sort(blocks,
- function(a, b)
- local d1 = vector_distance(a.pos, pos)
- local d2 = vector_distance(b.pos, pos)
- return d1 < d2
- end)
- -- Return N-nearest blocks (should be at the front of the sorted table).
- local ret = {}
- for i = 1, num, 1 do
- if i <= #blocks then
- ret[#ret + 1] = blocks[i]
- else
- break
- end
- end
- return ret
- end
- function city_block:nearest_jails_to_position(pos, num, rangelim)
- local get_rn = rc.current_realm_at_pos
- local realm = get_rn(pos)
- -- Copy the master table's indices so we don't modify it.
- -- We do not need to copy the inner table data itself. Just the indices.
- -- Only copy over blocks in the same realm, too.
- local blocks = {}
- local sblocks = self.blocks
- local t2 = os.time()
- for i=1, #sblocks, 1 do
- local v = sblocks[i]
- local p = v.pos
- local t1 = v.time or 0
- if v.is_jail and time_active(t1, t2) then
- if rangelim then
- if vector_distance(p, pos) < rangelim then
- if get_rn(p) == realm then
- blocks[#blocks+1] = v
- end
- end
- else
- if get_rn(p) == realm then
- blocks[#blocks+1] = v
- end
- end
- end
- end
- -- Sort blocks, nearest blocks first.
- table.sort(blocks,
- function(a, b)
- local d1 = vector_distance(a.pos, pos)
- local d2 = vector_distance(b.pos, pos)
- return d1 < d2
- end)
- -- Return N-nearest blocks (should be at the front of the sorted table).
- local ret = {}
- for i=1, num, 1 do
- if i <= #blocks then
- ret[#ret+1] = blocks[i]
- else
- break
- end
- end
- return ret
- end
- -- Get nearest named cityblock to position which is owned by 'owner'. If 'owner'
- -- is nil or an empty string, returns any nearest named cityblock.
- function city_block:nearest_named_region(pos, owner)
- local get_rn = rc.current_realm_at_pos
- local realm = get_rn(pos)
- -- Copy the master table's indices so we don't modify it.
- -- We do not need to copy the inner table data itself. Just the indices.
- -- Only copy over blocks in the same realm, too.
- local blocks = {}
- local sblocks = self.blocks
- local t2 = os.time()
- for i=1, #sblocks, 1 do
- local b = sblocks[i]
- local p = b.pos
- local t1 = b.time or 0
- if time_active(t1, t2) then
- if b.area_name and vector_distance(p, pos) < 100 and
- (not owner or owner == "" or b.owner == owner) then
- if get_rn(p) == realm then
- blocks[#blocks+1] = sblocks[i]
- end
- end
- end
- end
- -- Sort blocks, nearest blocks first.
- table.sort(blocks,
- function(a, b)
- local d1 = vector_distance(a.pos, pos)
- local d2 = vector_distance(b.pos, pos)
- return d1 < d2
- end)
- -- Return N-nearest blocks (should be at the front of the sorted table).
- if #blocks > 0 then
- return {blocks[1]}
- end
- return {}
- end
- function city_block.erase_jail(pos)
- pos = vector_round(pos)
- local b = city_block.blocks
- for k, v in ipairs(b) do
- if vector_equals(pos, v.pos) then
- local meta = minetest.get_meta(pos)
- local pname = meta:get_string("owner")
- local dname = rename.gpn(pname)
- meta:set_string("infotext", "City Marker (Placed by <" .. dname .. ">!)")
- v.is_jail = nil
- city_block:save()
- return
- end
- end
- end
- -- Get city information for the given position.
- function city_block.city_info(pos)
- pos = vector_round(pos)
- local marker = city_block:nearest_blocks_to_position(pos, 1, 100)
- if marker and marker[1] then
- -- Covers a 45x45x45 area.
- local r = 22
- local vpos = marker[1].pos
- if pos.x >= (vpos.x - r) and pos.x <= (vpos.x + r) and
- pos.z >= (vpos.z - r) and pos.z <= (vpos.z + r) and
- pos.y >= (vpos.y - r) and pos.y <= (vpos.y + r) then
- return marker[1]
- end
- end
- end
- function city_block:save()
- local datastring = minetest.serialize(self.blocks)
- if not datastring then
- return
- end
- minetest.safe_file_write(self.filename, datastring)
- --[[
- local file, err = io.open(self.filename, "w")
- if err then
- return
- end
- file:write(datastring)
- file:close()
- --]]
- end
- function city_block:load()
- local file, err = io.open(self.filename, "r")
- if err then
- self.blocks = {}
- return
- end
- self.blocks = minetest.deserialize(file:read("*all"))
- if type(self.blocks) ~= "table" then
- self.blocks = {}
- end
- file:close()
- end
- function city_block:in_city(pos)
- pos = vector_round(pos)
- -- Covers a 45x45x45 area.
- local r = 22
- local blocks = self.blocks
- local t2 = os.time()
- for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
- local v = blocks[i]
- local vpos = v.pos
- local t1 = v.time or 0
- if time_active(t1, t2) then
- if pos.x >= (vpos.x - r) and pos.x <= (vpos.x + r) and
- pos.z >= (vpos.z - r) and pos.z <= (vpos.z + r) and
- pos.y >= (vpos.y - r) and pos.y <= (vpos.y + r) then
- return true
- end
- end
- end
- return false
- end
- -- Pass the player doing the liquid dig/place action.
- function city_block:in_disallow_liquid_zone(pos, player)
- -- Never in city zone, if not a player doing this.
- if not player or not player:is_player() then
- return false
- end
- local pname = player:get_player_name()
- pos = vector_round(pos)
- -- Copy the master table's indices so we don't modify it.
- -- We do not need to copy the inner table data itself. Just the indices.
- local blocks = {}
- local sblocks = self.blocks
- local t2 = os.time()
- -- Covers a 45x45x45 area.
- local r = 22
- for i=1, #sblocks, 1 do
- local vpos = sblocks[i].pos
- local t1 = sblocks[i].time or 0
- -- Only include active blocks.
- if time_active(t1, t2) then
- -- This is a cubic distance check.
- if pos.x >= (vpos.x - r) and pos.x <= (vpos.x + r) and
- pos.z >= (vpos.z - r) and pos.z <= (vpos.z + r) and
- pos.y >= (vpos.y - r) and pos.y <= (vpos.y + r) then
- -- Add this block to list.
- blocks[#blocks+1] = sblocks[i]
- end
- end
- end
- -- Sort blocks, nearest blocks first.
- table.sort(blocks,
- function(a, b)
- local d1 = vector_distance(a.pos, pos)
- local d2 = vector_distance(b.pos, pos)
- return d1 < d2
- end)
- -- No intersecting blocks at all?
- if #blocks == 0 then
- return false
- end
- -- Check only the first, nearest block. Assumed active.
- local bcheck = blocks[1]
- if bcheck.owner == pname then
- return false
- end
- -- Nearest block NOT owned by player.
- -- This means this position is "in city" for purposes of placing/digging liquid.
- return true
- end
- function city_block:in_city_suburbs(pos)
- pos = vector_round(pos)
- local r = 44
- local blocks = self.blocks
- local t2 = os.time()
- for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
- local v = blocks[i]
- local vpos = v.pos
- local t1 = v.time or 0
- if time_active(t1, t2) then
- if pos.x >= (vpos.x - r) and pos.x <= (vpos.x + r) and
- pos.z >= (vpos.z - r) and pos.z <= (vpos.z + r) and
- pos.y >= (vpos.y - r) and pos.y <= (vpos.y + r) then
- return true
- end
- end
- end
- return false
- end
- function city_block:in_safebed_zone(pos)
- -- Covers a 111x111x111 area.
- pos = vector_round(pos)
- local r = 55
- local blocks = self.blocks
- local t2 = os.time()
- for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
- local v = blocks[i]
- local vpos = v.pos
- local t1 = v.time or 0
- if time_active(t1, t2) then
- if pos.x >= (vpos.x - r) and pos.x <= (vpos.x + r) and
- pos.z >= (vpos.z - r) and pos.z <= (vpos.z + r) and
- pos.y >= (vpos.y - r) and pos.y <= (vpos.y + r) then
- return true
- end
- end
- end
- return false
- end
- function city_block:in_no_tnt_zone(pos)
- pos = vector_round(pos)
- local r = 50
- local blocks = self.blocks
- local t2 = os.time()
- for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
- local v = blocks[i]
- local vpos = v.pos
- local t1 = v.time or 0
- if time_active(t1, t2) then
- if pos.x >= (vpos.x - r) and pos.x <= (vpos.x + r) and
- pos.z >= (vpos.z - r) and pos.z <= (vpos.z + r) and
- pos.y >= (vpos.y - r) and pos.y <= (vpos.y + r) then
- return true
- end
- end
- end
- return false
- end
- function city_block:in_no_leecher_zone(pos)
- pos = vector_round(pos)
- local r = 100
- local blocks = self.blocks
- local t2 = os.time()
- for i=1, #blocks, 1 do -- Convenience of ipairs() does not justify its overhead.
- local v = blocks[i]
- local vpos = v.pos
- local t1 = v.time or 0
- if time_active(t1, t2) then
- if pos.x >= (vpos.x - r) and pos.x <= (vpos.x + r) and
- pos.z >= (vpos.z - r) and pos.z <= (vpos.z + r) and
- pos.y >= (vpos.y - r) and pos.y <= (vpos.y + r) then
- return true
- end
- end
- end
- return false
- end
- function city_block.on_rightclick(pos, node, clicker, itemstack)
- if not clicker or not clicker:is_player() then
- return
- end
- local pname = clicker:get_player_name()
- local meta = minetest.get_meta(pos)
- -- Player must be owner of city block.
- if meta:get_string("owner") ~= pname then
- return
- end
- -- Create formspec context.
- city_block.formspecs[pname] = pos
- local formspec = city_block.create_formspec(pos, pname)
- minetest.show_formspec(pname, "city_block:main", formspec)
- end
- function city_block.create_formspec(pos, pname)
- local formspec = "size[4.1,2.0]" ..
- default.gui_bg ..
- default.gui_bg_img ..
- default.gui_slots ..
- "label[0,0;Enter city/area region name:]" ..
- "field[0.30,0.75;4,1;CITYNAME;;]" ..
- "button_exit[0,1.30;2,1;OK;Confirm]" ..
- "button_exit[2,1.30;2,1;CANCEL;Abort]" ..
- "field_close_on_enter[CITYNAME;true]"
- return formspec
- end
- function check_cityname(cityname)
- return not string.match(cityname, "[^%a%s]")
- end
- function city_block.on_receive_fields(player, formname, fields)
- if formname ~= "city_block:main" then
- return
- end
- if not player or not player:is_player() then
- return
- end
- local pname = player:get_player_name()
- local pos = city_block.formspecs[pname]
- -- Context should have been created in 'on_rightclick'. CSM protection.
- if not pos then
- return true
- end
- local meta = minetest.get_meta(pos)
- local owner = meta:get_string("owner")
- -- Form sender must be owner.
- if pname ~= owner then
- return true
- end
- if fields.key_enter_field == "CITYNAME" or fields.OK then
- local area_name = (fields.CITYNAME or ""):trim()
- area_name = area_name:gsub("%s+", " ")
- -- Ensure city name is valid.
- local is_valid = true
- if #area_name == 0 then
- is_valid = false
- end
- if #area_name > 20 then
- is_valid = false
- end
- if not check_cityname(area_name) then
- is_valid = false
- end
- if anticurse.check(pname, area_name, "foul") then
- is_valid = false
- elseif anticurse.check(pname, area_name, "curse") then
- is_valid = false
- end
- if not is_valid then
- minetest.chat_send_player(pname, "# Server: Region name not valid.")
- return
- end
- local allblocks = city_block.blocks
- local numblocks = #(city_block.blocks)
- local block
- for i = 1, numblocks do
- local entry = allblocks[i]
- if vector_equals(entry.pos, pos) then
- block = entry
- break
- end
- end
- -- Ensure we got the city block data.
- if not block then
- return
- end
- -- Write out.
- meta:set_string("cityname", area_name)
- meta:set_string("infotext", city_block.get_infotext(pos))
- block.area_name = area_name
- city_block:save()
- end
- return true
- end
- function city_block.get_infotext(pos)
- local meta = minetest.get_meta(pos)
- local pname = meta:get_string("owner")
- local cityname = meta:get_string("cityname")
- local dname = rename.gpn(pname)
- local text = "City Marker (Placed by <" .. dname .. ">!)"
- if cityname ~= "" then
- text = text .. "\nRegion Designate: \"" .. cityname .. "\""
- end
- return text
- end
- if not city_block.run_once then
- city_block:load()
- minetest.register_on_player_receive_fields(function(...)
- return city_block.on_receive_fields(...) end)
- minetest.register_node("city_block:cityblock", {
- 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 jail if caught in a city.\nPrevents the use of ore leeching equipment within 100 meters radius.\nPrevents mining with TNT nearby.",
- tiles = {"moreblocks_circle_stone_bricks.png^default_tool_mesepick.png"},
- is_ground_content = false,
- groups = utility.dig_groups("obsidian", {
- immovable=1,
- }),
- is_ground_content = false,
- sounds = default.node_sound_stone_defaults(),
- on_rightclick = function(...)
- return city_block.on_rightclick(...)
- end,
- after_place_node = function(pos, placer)
- if placer and placer:is_player() then
- local pname = placer:get_player_name()
- local meta = minetest.get_meta(pos)
- local dname = rename.gpn(pname)
- meta:set_string("rename", dname)
- meta:set_string("owner", pname)
- meta:set_string("infotext", city_block.get_infotext(pos))
- table.insert(city_block.blocks, {
- pos = vector_round(pos),
- owner = pname,
- time = os.time(),
- })
- city_block:save()
- end
- end,
- -- We don't need an `on_blast` func because TNT calls `on_destruct` properly!
- on_destruct = function(pos)
- -- The cityblock may not exist in the list if the node was created by falling,
- -- and was later dug.
- for i, EachBlock in ipairs(city_block.blocks) do
- if vector_equals(EachBlock.pos, pos) then
- table.remove(city_block.blocks, i)
- city_block:save()
- end
- end
- end,
- -- Called by rename LBM.
- _on_update_infotext = function(pos)
- local meta = minetest.get_meta(pos)
- local owner = meta:get_string("owner")
- -- Nobody placed this block.
- if owner == "" then
- return
- end
- local dname = rename.gpn(owner)
- meta:set_string("rename", dname)
- meta:set_string("infotext", city_block.get_infotext(pos))
- end,
- on_punch = function(...)
- return city_block.on_punch(...)
- end,
- })
- minetest.register_craft({
- output = 'city_block:cityblock',
- recipe = {
- {'default:pick_mese', 'farming:hoe_mese', 'default:sword_diamond'},
- {'chests:chest_locked', 'default:goldblock', 'default:sandstone'},
- {'default:obsidianbrick', 'default:mese', 'cobble_furnace:inactive'},
- }
- })
- minetest.register_privilege("disable_pvp", "Players cannot damage players with this priv by punching.")
- minetest.register_on_punchplayer(function(...)
- return city_block.on_punchplayer(...)
- end)
- local c = "city_block:core"
- local f = city_block.modpath .. "/init.lua"
- reload.register_file(c, f, false)
- city_block.run_once = true
- end
- function city_block:get_adjective()
- local adjectives = {
- "murdering",
- "slaying",
- "killing",
- "whacking",
- "trashing",
- "fatally attacking",
- "fatally harming",
- "doing away with",
- "giving the Chicago treatment to",
- "fatally thrashing",
- "fatally stabbing",
- }
- return adjectives[math_random(1, #adjectives)]
- end
- local murder_messages = {
- "<v> collapsed from <k>'s brutal attack.",
- "<k>'s <w> apparently wasn't such an unusual weapon after all, as <v> found out.",
- "<k> killed <v> with great prejudice.",
- "<v> died from <k>'s horrid slaying.",
- "<v> fell prey to <k>'s deadly <w>.",
- "<k> went out of <k_his> way to slay <v> with <k_his> <w>.",
- "<v> danced <v_himself> to death under <k>'s craftily wielded <w>.",
- "<k> used <k_his> <w> to kill <v> with prejudice.",
- "<k> made a splortching sound with <v>'s head.",
- "<v> got flattened by <k>'s skillfully handled <w>.",
- "<v> became prey for <k>.",
- "<v> didn't get out of <k>'s way in time.",
- "<v> SAW <k> coming with <k_his> <w>. Didn't get away in time.",
- "<v> made no real attempt to get out of <k>'s way.",
- "<k> barreled through <v> as if <v_he> wasn't there.",
- "<k> sent <v> to that place where kindling wood isn't needed.",
- "<v> didn't suspect that <k> meant <v_him> any harm.",
- "<v> fought <k> to the death and lost painfully.",
- "<v> knew <k> was wielding <k_his> <w> but didn't guess what <k> meant to do with it.",
- "<k> clonked <v> over the head using <k_his> <w> with silent skill.",
- "<k> made sure <v> didn't see that coming!",
- "<k> has decided <k_his> favorite weapon is <k_his> <w>.",
- "<v> did the mad hatter dance just before being killed with <k>'s <w>.",
- "<v> played the victim to <k>'s bully behavior!",
- "<k> used <v> for weapons practice with <k_his> <w>.",
- "<v> failed to avoid <k>'s oncoming weapon.",
- "<k> successfully got <v> to complain of a headache.",
- "<v> got <v_himself> some serious hurt from <k>'s <w>.",
- "Trying to talk peace to <k> didn't win any for <v>.",
- "<v> was brutally slain by <k>'s <w>.",
- "<v> jumped the mad-hatter dance under <k>'s <w>.",
- "<v> got <v_himself> a fatal mauling by <k>'s <w>.",
- "<k> just assassinated <v> with <k_his> <w>.",
- "<k> split <v>'s wig.",
- "<k> took revenge on <v>.",
- "<k> flattened <v>.",
- "<v> played dead. Permanently.",
- "<v> never saw what hit <v_him>.",
- "<k> took <v> by surprise.",
- "<v> was assassinated.",
- "<k> didn't take any prisoners from <v>.",
- "<k> pinned <v> to the wall with <k_his> <w>.",
- "<v> failed <v_his> weapon checks.",
- }
- function city_block.murder_message(killer, victim, sendto)
- if spam.test_key("kill" .. victim .. "15662") then
- return
- end
- spam.mark_key("kill" .. victim .. "15662", 30)
- local msg = murder_messages[math_random(1, #murder_messages)]
- msg = string.gsub(msg, "<v>", "<" .. rename.gpn(victim) .. ">")
- msg = string.gsub(msg, "<k>", "<" .. rename.gpn(killer) .. ">")
- local ksex = skins.get_gender_strings(killer)
- local vsex = skins.get_gender_strings(victim)
- msg = string.gsub(msg, "<k_himself>", ksex.himself)
- msg = string.gsub(msg, "<k_his>", ksex.his)
- msg = string.gsub(msg, "<v_himself>", vsex.himself)
- msg = string.gsub(msg, "<v_his>", vsex.his)
- msg = string.gsub(msg, "<v_him>", vsex.him)
- msg = string.gsub(msg, "<v_he>", vsex.he)
- if string.find(msg, "<w>") then
- local hitter = minetest.get_player_by_name(killer)
- if hitter then
- local wield = hitter:get_wielded_item()
- local def = minetest.registered_items[wield:get_name()]
- local meta = wield:get_meta()
- local description = meta:get_string("description")
- if description ~= "" then
- msg = string.gsub(msg, "<w>", "'" .. utility.get_short_desc(description):trim() .. "'")
- elseif def and def.description then
- local str = utility.get_short_desc(def.description)
- if str == "" then
- str = "Potato Fist"
- end
- msg = string.gsub(msg, "<w>", str)
- end
- end
- end
- if type(sendto) == "string" then
- minetest.chat_send_player(sendto, "# Server: " .. msg)
- else
- minetest.chat_send_all("# Server: " .. msg)
- end
- end
- function city_block.hit_possible(p1pos, p2pos)
- -- Range limit, stops hackers with long reach.
- if vector_distance(p1pos, p2pos) > 6 then
- return false
- end
- -- Cannot attack through walls.
- -- But if node wouldn't stop an arrow, keep testing the line.
- --local raycast = minetest.raycast(p1pos, p2pos, false, false)
- -- This seems to cause random freezes and 100% CPU.
- --[[
- local los, pstop = minetest.line_of_sight(p1pos, p2pos)
- while not los do
- if throwing.node_blocks_arrow(minetest.get_node(vector_round(pstop)).name) then
- return false
- end
- local dir = vector.direction(pstop, p2pos)
- local ns = vector.add(pstop, dir)
- los, pstop = minetest.line_of_sight(ns, p2pos)
- end
- --]]
- return true
- end
- function city_block.send_to_jail(victim_pname, attack_pname)
- -- Killers don't go to jail if the victim is a registered cheater.
- if not sheriff.is_cheater(victim_pname) then
- local hitter = minetest.get_player_by_name(attack_pname)
- if hitter and jail.go_to_jail(hitter, nil) then
- minetest.chat_send_all(
- "# Server: Criminal <" .. rename.gpn(attack_pname) .. "> was sent to gaol for " ..
- city_block:get_adjective() .. " <" .. rename.gpn(victim_pname) .. "> within city limits.")
- end
- end
- end
- function city_block.handle_assassination(p2pos, victim_pname, attack_pname, melee)
- -- Bed position is only lost if player died outside city to a melee weapon.
- if not city_block:in_safebed_zone(p2pos) and melee then
- -- Victim doesn't lose their bed respawn if they were killed by a cheater.
- if not sheriff.is_cheater(attack_pname) then
- minetest.chat_send_player(victim_pname, "# Server: Your bed is lost! You were assassinated in the wilds.")
- beds.clear_player_spawn(victim_pname)
- end
- end
- end
- -- Note: this is called on the next server step after the punch, otherwise we
- -- cannot know if the player died as a result.
- function city_block.handle_consequences(player, hitter, melee, stomp)
- --minetest.log('handle_consequences')
- local victim_pname = player:get_player_name()
- local attack_pname = hitter:get_player_name()
- local time = os.time()
- local hp = player:get_hp()
- local p2pos = utility.get_head_pos(player:get_pos())
- local vpos = vector_round(p2pos)
- city_block.attackers[victim_pname] = attack_pname
- city_block.victims[victim_pname] = time
- -- Victim didn't die yet.
- if hp > 0 then
- return
- end
- default.detach_player_if_attached(player)
- -- Stomp messages are handled elsewhere.
- if not stomp then
- city_block.murder_message(attack_pname, victim_pname)
- end
- if city_block:in_city(p2pos) then
- local t0 = city_block.victims[attack_pname] or time
- local tdiff = (time - t0)
- if not city_block.attackers[attack_pname] then
- city_block.attackers[attack_pname] = ""
- end
- --[[
- Behavior Table (obtained through testing):
- In city-block area, no protection:
- A kills B, B did not retaliate -> A goes to jail
- A kills B, B had retaliated -> Nobody jailed
- (The table is the same if A and B are inverted)
- In city-block area, protected by A (with nearby jail available):
- A kills B, B did not retaliate -> A goes to jail
- A kills B, B had retaliated -> Nobody jailed
- B kills A, A did not retaliate -> B goes to jail
- B kills A, A had retaliated -> B goes to jail
- (The table is the same if A and B are inverted, and protection is B's)
- Notes:
- A hit from A or B is considered retaliation if it happens very soon
- after the other player hit. Thus, if both A and B are hitting, then both
- are considered to be retaliating -- in that case, land ownership is used
- to resolve who should go to jail.
- It does not matter who hits first in a fight -- only who kills the other
- player first.
- If there is no jail available for a crook to be sent to, then nothing
- happens in any case, regardless of who wins the fight or owns the land.
- --]]
- -- Victim is "landowner" if area is protected, but they have access.
- local landowner = (minetest.test_protection(vpos, "") and
- not minetest.test_protection(vpos, victim_pname))
- -- Killing justified after provocation, but not if victim owns the land.
- if city_block.attackers[attack_pname] == victim_pname and
- tdiff < 30 and not landowner then
- return
- else
- -- Go to jail! Do not pass Go. Do not collect $200.
- city_block.send_to_jail(victim_pname, attack_pname)
- end
- else
- -- Player killed outside town.
- -- This only does something if the attack was with a melee weapon!
- city_block.handle_assassination(p2pos, victim_pname, attack_pname, melee)
- end
- end
- city_block.attackers = city_block.attackers or {}
- city_block.victims = city_block.victims or {}
- -- Return `true' to prevent the default damage mechanism.
- -- Note: player is sometimes the hitter (player punches self). This is sometimes
- -- necessary when a mod needs to punch a player, but has no entity that can do
- -- the actual punch.
- function city_block.on_punchplayer(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
- if not player:is_player() then
- return
- end
- -- Callback is called even if player is dead. Shortcut.
- if player:get_hp() <= 0 or hitter:get_hp() <= 0 then
- return
- end
- --minetest.log('on_punchplayer')
- local melee_hit = true
- local stomp_hit = false
- local from_env = false
- if tool_capabilities.damage_groups.from_stomp then
- stomp_hit = true
- end
- if tool_capabilities.damage_groups.from_env then
- from_env = true
- end
- if tool_capabilities.damage_groups.from_arrow then
- -- Someone launched this weapon. The hitter is most likely the nearest
- -- player that isn't the player going to be hit.
- melee_hit = false
- -- We don't have enough information to know exactly who fired this weapon,
- -- but it's probably a safe bet that it was the nearest player who is NOT
- -- the player being hit. But if we were explicitly provided a player object
- -- that is NOT self, then we don't need to do this.
- if hitter == player or not hitter:is_player() then
- -- If initial hitter is the player, or the hitter isn't a player, then
- -- get the nearest other player to this position (who is not the initial
- -- player) and use that player as the hitter.
- local pos = player:get_pos()
- local culprit = hb4.nearest_player_not(pos, player)
- if culprit then
- local cpos = culprit:get_pos()
- -- Only if culprit is nearby.
- if vector.distance(cpos, pos) < 50 then
- hitter = culprit
- end
- end
- end
- end
-
- if not hitter:is_player() then
- return
- end
- -- Random accidents happen to punished players during PvP.
- do
- local attacker = hitter:get_player_name()
- if sheriff.is_cheater(attacker) then
- if sheriff.punish_probability(attacker) then
- sheriff.punish_player(attacker)
- end
- end
- end
- local p1pos = utility.get_head_pos(hitter:get_pos())
- local p2pos = utility.get_head_pos(player:get_pos())
- -- Check if hit is physically possible (range, blockage, etc).
- if melee_hit and not city_block.hit_possible(p1pos, p2pos) then
- return true
- end
- -- PvP is disabled for players in jail. This fixes a possible way to exploit jail.
- if not from_env and (jail.is_player_in_jail(hitter) or jail.is_player_in_jail(player)) then
- minetest.chat_send_player(hitter:get_player_name(), "# Server: Brawling is not allowed in jail.")
- return true
- end
- -- Admins cannot be punched.
- if gdac.player_is_admin(player) then
- return true
- end
- -- Let others hear sounds of nearby combat.
- if damage > 0 then
- ambiance.sound_play("player_damage", p2pos, 2.0, 30)
- end
- -- If hitter is self, punch was (most likely) due to game code.
- if player == hitter then
- return
- end
- -- Stuff that happens when one player kills another.
- -- Must be executed on the next server step, so we can determine if victim
- -- really died! (This is because damage will often be modified.)
- local pname = player:get_player_name()
- local hname = hitter:get_player_name()
- minetest.after(0, function()
- local pref = minetest.get_player_by_name(pname)
- local href = minetest.get_player_by_name(hname)
- if pref and href then
- city_block.handle_consequences(pref, href, melee_hit, stomp_hit)
- end
- end)
- end
|