forked from minetest-mods/privilegeareas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
273 lines (224 loc) · 7.24 KB
/
init.lua
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
privilegeareas = {
players = nil,
userdata = {},
areas = {
},
createPlayerTable = function(player)
if not privilegeareas.players then
load_data()
end
if not player or not player:get_player_name() then
print("[PrivilegeAreas] Player does not exist!")
return;
end
local name = player:get_player_name()
print("[PrivilegeAreas] writing player table for "..name)
if (name=="") then
return;
end
privilegeareas.players[name] = {}
privilegeareas.userdata[name] = player
if not privilegeareas.players[name].areas then
privilegeareas.players[name].areas = {}
end
end,
calculate_current_areas = function(player)
local name = player:get_player_name()
if (name=="") then
return;
end
if (not privilegeareas.players[name]) then
createPlayerTable(player)
end
for i=1,# privilegeareas.areas do
if privilegeareas.areas[i].type == "radius" then
if distance(player:getpos(),privilegeareas.areas[i].location) < privilegeareas.areas[i].location.radius then
if (not privilegeareas.players[name].areas or not privilegeareas.players[name].areas[i] or privilegeareas.players[name].areas[i]==false) then
privilegeareas.enter_area(player,i)
end
else
if (privilegeareas.players[name].areas[i]==true) then
privilegeareas.leave_area(player,i)
end
end
elseif privilegeareas.areas[i].type == "box" then
if (vector_is_in(privilegeareas.areas[i].location,privilegeareas.areas[i].location2,player:getpos())) then
if (not privilegeareas.players[name].areas or not privilegeareas.players[name].areas[i] or privilegeareas.players[name].areas[i]==false) then
privilegeareas.enter_area(player,i)
end
else
if (privilegeareas.players[name].areas[i]==true) then
privilegeareas.leave_area(player,i)
end
end
end
end
end,
enter_area = function(player,i)
local name = player:get_player_name()
privilegeareas.players[name].areas[i]=true
minetest.chat_send_player(name, "You have entered area "..i)
print ("[PrivilegeAreas] "..name.." has entered area "..i)
-- Get privs
local privs = minetest.get_player_privs(name)
if not privs then
print("[PrivilegeAreas] player does not exist error!")
end
-- loop grants
local tmpv = false
if privilegeareas.areas[i].actions.on_enter.grant then
for a=1,# privilegeareas.areas[i].actions.on_enter.grant do
if tmpv == false then
tmpv = true
minetest.chat_send_player(name, "You have been given the following privs:")
print("[PrivilegeAreas] "..name.." has been given the following privs:")
end
privs[privilegeareas.areas[i].actions.on_enter.grant[a]]=true;
minetest.chat_send_player(name, "-- "..privilegeareas.areas[i].actions.on_enter.grant[a])
print("[PrivilegeAreas] -- "..privilegeareas.areas[i].actions.on_enter.grant[a])
end
end
-- Loop though takes
tmpv = false
if privilegeareas.areas[i].actions.on_enter.take then
for a=1,# privilegeareas.areas[i].actions.on_enter.take do
if tmpv == false then
tmpv = true
minetest.chat_send_player(name, "You have lost the following privs:")
print("[PrivilegeAreas] "..name.." has lost the following privs:")
end
privs[privilegeareas.areas[i].actions.on_enter.take[a]]=nil;
minetest.chat_send_player(name, "-- "..privilegeareas.areas[i].actions.on_enter.take[a])
print("[PrivilegeAreas] -- "..privilegeareas.areas[i].actions.on_enter.take[a])
end
end
-- Set privs
minetest.set_player_privs(name, privs)
-- save data
save_data()
end,
leave_area = function(player,i)
local name = player:get_player_name()
privilegeareas.players[name].areas[i]=false
minetest.chat_send_player(name, "You have left area "..i)
print ("[PrivilegeAreas] "..name.." has left area "..i)
-- Get privs
local privs = minetest.get_player_privs(name)
if not privs then
print("[PrivilegeAreas] player does not exist error!")
end
-- loop grants
local tmp = false
if privilegeareas.areas[i].actions.on_leave.grant then
for a=1,# privilegeareas.areas[i].actions.on_leave.grant do
if tmp == false then
tmp = true
minetest.chat_send_player(name, "You have been given the following privs:")
print("[PrivilegeAreas] "..name.." has been given the following privs:")
end
privs[privilegeareas.areas[i].actions.on_leave.grant[a]]=true;
minetest.chat_send_player(name, "-- "..privilegeareas.areas[i].actions.on_leave.grant[a])
print("[PrivilegeAreas] -- "..privilegeareas.areas[i].actions.on_leave.grant[a])
end
end
-- Loop though takes
tmp = false
if privilegeareas.areas[i].actions.on_leave.take then
for a=1,# privilegeareas.areas[i].actions.on_leave.take do
if tmp == false then
tmp = true
minetest.chat_send_player(name, "You have lost the following privs:")
print("[PrivilegeAreas] "..name.." has lost the following privs:")
end
privs[privilegeareas.areas[i].actions.on_leave.take[a]]=nil;
minetest.chat_send_player(name, "-- "..privilegeareas.areas[i].actions.on_leave.take[a])
print("[PrivilegeAreas] -- "..privilegeareas.areas[i].actions.on_leave.take[a])
end
end
-- Set privs
minetest.set_player_privs(name, privs)
-- save data
save_data()
end,
}
-- Table Save Load Functions
function save_data()
if privilegeareas.players == nil then
return
end
print("[PrivilegeAreas] Saving data")
local file = io.open(minetest.get_worldpath().."/privareas.txt", "w")
if file then
file:write(minetest.serialize(privilegeareas.players))
file:close()
end
local file = io.open(minetest.get_worldpath().."/privareas_areas.txt", "w")
if file then
file:write(minetest.serialize(privilegeareas.areas))
file:close()
end
end
function _load_data()
local file = io.open(minetest.get_worldpath().."/privareas_areas.txt", "r")
if file then
local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then
privilegeareas.areas = table
return
end
end
end
function load_data()
print("[PrivilegeAreas] Loading data")
_load_data()
if privilegeareas.players == nil then
local file = io.open(minetest.get_worldpath().."/privareas.txt", "r")
if file then
local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then
privilegeareas.players = table
return
end
end
end
privilegeareas.players = {}
end
load_data()
minetest.register_on_shutdown(function()
-- save data
save_data()
end)
function vector_is_in(hay,box,needle)
if (needle.x > hay.x and needle.x < box.x) then
if (needle.y > hay.y and needle.y < box.y) then
if (needle.z > hay.z and needle.z <box.z) then
return true
end
end
end
return false
end
function distance(v, w)
return math.sqrt(
math.pow(v.x - w.x, 2) +
math.pow(v.y - w.y, 2) +
math.pow(v.z - w.z, 2)
)
end
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime;
if timer >= 1 then
timer=0
for _, plr in pairs(privilegeareas.userdata) do
privilegeareas.calculate_current_areas(plr)
end
end
end)
minetest.register_on_joinplayer(function(player)
privilegeareas.createPlayerTable(player)
end)
minetest.register_on_leaveplayer(function(player)
privilegeareas.userdata[player:get_player_name()]=nil
end)
dofile(minetest.get_modpath("privilegeareas").."/gui.lua")