summaryrefslogtreecommitdiff
path: root/init.lua
blob: f59cbe31e5bc4e0e803c443271502cc1094cb4a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
-- pvp_areas
-- Original   : Copyright 2016 James Stevenson (everamzah)
-- Additional : Copyright Tai Kedzierski (DuCake)
-- LGPL v2.1+

local pvp_areas_worlddir = minetest.get_worldpath()
local pvp_areas_modname = minetest.get_current_modname()

local hasareasmod = minetest.get_modpath("areas")

-- The settings object
local settings = {}
if minetest.settings then
	settings = minetest.settings
else
	--- Function to retrieve a setting value.
	--
	-- @function settings:get
	-- @param setting **string** setting name
	-- @return String value of setting
	function settings:get(setting)
		return minetest.setting_get(setting)
	end

	--- Function to retrieve a boolean setting value.
	--
	-- @function settings:get_bool
	-- @param setting **string** setting name
	-- @return **bool**: True if setting is enabled
	function settings:get_bool(setting)
		return minetest.setting_getbool(setting)
	end
end

local safemode = settings:get_bool("pvp_areas.safemode") or false
local area_label = settings:get("pvp_areas.label") or "Defined area."
-- if false Mob does Damage
local mobsDoNoDamage = false

local pvp_areas_store = AreaStore()
pvp_areas_store:from_file(pvp_areas_worlddir .. "/pvp_areas_store.dat")

local pvp_default = minetest.is_yes(settings:get_bool("pvp_areas.enable_pvp"))
minetest.log("action", "[" .. pvp_areas_modname .. "] PvP by Default: " .. tostring(pvp_default))

local pvp_areas_players = {}
local pvp_areas = {}

local function update_pvp_areas()
	local counter = 0
	pvp_areas = {}
	while pvp_areas_store:get_area(counter) do
		table.insert(pvp_areas, pvp_areas_store:get_area(counter))
		counter = counter + 1
	end
end
update_pvp_areas()

local function save_pvp_areas()
	pvp_areas_store:to_file(pvp_areas_worlddir .. "/pvp_areas_store.dat")
end

local function areas_entity(pos,num)
	if hasareasmod then
		local obj = minetest.add_entity(pos, "areas:pos"..tostring(num))
		local ent = obj:get_luaentity()
		ent.active = true
	end
end

-- Register privilege and chat command.
minetest.register_privilege("pvp_areas_admin", "Can set and remove PvP areas.")

minetest.register_chatcommand("pvp_areas", {
	description = "Mark and set areas for PvP.",
	params = "<pos1> <pos2> <set> <remove>",
	privs = "pvp_areas_admin",
	func = function(name, param)
		local pos = vector.round(minetest.get_player_by_name(name):getpos())
		if param == "pos1" then
			if not pvp_areas_players[name] then
				pvp_areas_players[name] = {pos1 = pos}
			else
				pvp_areas_players[name].pos1 = pos
			end
			minetest.chat_send_player(name, "Position 1: " .. minetest.pos_to_string(pos))
		elseif param == "pos2" then
			if not pvp_areas_players[name] then
				pvp_areas_players[name] = {pos2 = pos}
			else
				pvp_areas_players[name].pos2 = pos
			end
			minetest.chat_send_player(name, "Position 2: " .. minetest.pos_to_string(pos))
		elseif param == "set" then
			if not pvp_areas_players[name] or not pvp_areas_players[name].pos1 then
				minetest.chat_send_player(name, "Position 1 missing, use \"/pvp_areas pos1\" to set.")
			elseif not pvp_areas_players[name].pos2 then
				minetest.chat_send_player(name, "Position 2 missing, use \"/pvp_areas pos2\" to set.")
			else
				pvp_areas_store:insert_area(pvp_areas_players[name].pos1, pvp_areas_players[name].pos2, "pvp_areas", #pvp_areas)
				table.insert(pvp_areas, pvp_areas_store:get_area(#pvp_areas))
				update_pvp_areas()
				save_pvp_areas()
				pvp_areas_players[name] = nil
				minetest.chat_send_player(name, "Area set.")
			end
		elseif param:sub(1, 6) == "remove" then
			local n = tonumber(param:sub(8, -1))
			if n and pvp_areas_store:get_area(n) then
				pvp_areas_store:remove_area(n)
				if pvp_areas_store:get_area(n + 1) then
					-- Insert last entry in new empty (removed) slot.
					local a = pvp_areas_store:get_area(#pvp_areas - 1)
					pvp_areas_store:remove_area(#pvp_areas - 1)
					pvp_areas_store:insert_area(a.min, a.max, "pvp_areas", n)
				end
				update_pvp_areas()
				save_pvp_areas()
				minetest.chat_send_player(name, "Removed " .. tostring(n))
			else
				minetest.chat_send_player(name, "Invalid argument.  You must enter a valid area identifier.")
			end
		elseif param ~= "" then
			minetest.chat_send_player(name, "Invalid usage.  Type \"/help pvp_areas\" for more information.")
		else
			for k, v in pairs(pvp_areas) do
				minetest.chat_send_player(name, k - 1 .. ": " ..
						minetest.pos_to_string(v.min) .. " " ..
						minetest.pos_to_string(v.max))
			end
		end
	end
})

local KILL_NO = true
local KILL_OK = false

local AREA_ACTIVATE = KILL_OK
local AREA_NOACTIVATE = KILL_NO

local savemodeToString = "FALSCH" -- only for debugging
if safemode then
	AREA_ACTIVATE = KILL_NO
	AREA_NOACTIVATE = KILL_OK
	savemodeToString = "WAHR"
end

-- Register punchplayer callback.
minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
	local isPlayer = hitter:is_player()
	
	if hitter:is_player() == false then
		return mobsDoNoDamage -- if this is a MOB then give Damage
	end
	--[[
	local IsPlayerToString = "PLAYER"	-- only for debugging
	if isplayer then 
		IsPlayerToString = "PLAYER"			-- only for debugging
	else
		IsPlayerToString = "MOB"				-- only for debugging
		return mobsDoNoDamage -- if this is a MOB then give Damage
	end
	]]--
	
	local playername = player:get_player_name() 
	for k, v in pairs(pvp_areas_store:get_areas_for_pos(player:getpos())) do
		if k then
			--minetest.chat_send_player(playername, "in loop - safemode"..savemodeToString.." isPlayer "..IsPlayerToString)
			return AREA_ACTIVATE --KILL_NO
		end
	end
	--minetest.chat_send_player(playername, "after - safemode"..savemodeToString.." isPlayer "..IsPlayerToString)
	return AREA_NOACTIVATE --KILL_OK
end)

if hasareasmod then
	if areas.registerHudHandler then

		local function advertise_nokillzone(pos, list)
			for k, v in pairs(pvp_areas_store:get_areas_for_pos(pos)) do
				if k then
					table.insert(list, {
						id = "PvP Control Area "..tostring(k),
						name = area_label,
					} )
					return
				end
			end
		end

		areas:registerHudHandler(advertise_nokillzone)
	else
		minetest.log("info","Your version of `areas` does not support registering hud handlers.")
	end
end