-
Notifications
You must be signed in to change notification settings - Fork 6
/
aoi.lua
345 lines (313 loc) · 8.95 KB
/
aoi.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
local skynet = require "skynet"
local queue = require "skynet.queue"
require "skynet.manager"
local enumtype =
{
CHAR_TYPE_PLAYER = 1,
CHAR_TYPE_MONSTER = 2,
}
local CMD = {}
local OBJ = {}
local playerview = {}
local monsterview = {}
local aoi
local update_thread
local need_update
local map_name = ...
local mapagent
local luaqueue = queue()
local AOI_RADIS = 200
local AOI_RADIS2 = AOI_RADIS * AOI_RADIS
local LEAVE_AOI_RADIS2 = AOI_RADIS2 * 4
local function DIST2(p1,p2)
return ((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) + (p1.z - p2.z) * (p1.z - p2.z))
end
skynet.register_protocol {
name = "text",
id = skynet.PTYPE_TEXT,
pack = function(text) return text end,
unpack = function(buf, sz) return skynet.tostring(buf,sz) end,
}
--怪物移动的时候通知玩家信息
local function updateviewmonster(monstertempid)
if monsterview[monstertempid] == nil then return end
local myobj = OBJ[monstertempid]
local mypos = myobj.movement.pos
--离开他人视野
local leavelist = {}
--进入他人视野
local enterlist = {}
--通知他人自己移动
local movelist = {}
local othertempid
local otherpos
local otheragent
local otherobj
for k,v in pairs(monsterview[monstertempid]) do
othertempid = OBJ[k].tempid
otherpos = OBJ[k].movement.pos
otheragent = OBJ[k].agent
otherobj = {
tempid = othertempid,
agent = OBJ[k].agent,
}
local distance = DIST2(mypos,otherpos)
if distance <= AOI_RADIS2 then
if not v then
monsterview[monstertempid][k] = true
playerview[k][monstertempid] = true
table.insert(enterlist,OBJ[k])
else
table.insert(movelist,otheragent)
end
elseif distance > AOI_RADIS2 and distance <= LEAVE_AOI_RADIS2 then
if v then
monsterview[monstertempid][k] = false
playerview[k][monstertempid] = false
table.insert(leavelist,otherobj)
end
else
if v then
table.insert(leavelist,otherobj)
end
monsterview[monstertempid][k] = nil
playerview[k][monstertempid] = nil
end
end
--离开他人视野
for _,v in pairs(leavelist) do
skynet.send(v.agent,"lua","delaoiobj",myobj.tempid)
end
--重新进入视野
for _,v in pairs(enterlist) do
skynet.send(v.agent,"lua","addaoiobj",myobj)
end
--视野范围内移动
for _,v in pairs(movelist) do
skynet.send(v,"lua","updateaoiobj",myobj)
end
skynet.send(myobj.agent,"lua","updateaoilist",myobj.tempid,enterlist,leavelist)
end
--根据对象类型插入table
local function inserttotablebytype(t,v,type)
if type ~= enumtype.CHAR_TYPE_PLAYER then
table.insert(t.monsterlist,v)
else
table.insert(t.playerlist,v)
end
end
--观看者坐标更新的时候
--根据距离情况通知他人自己的信息
local function updateviewplayer(viewertempid)
if playerview[viewertempid] == nil then return end
local myobj = OBJ[viewertempid]
local mypos = myobj.movement.pos
--离开他人视野
local leavelist = {
playerlist = {},
monsterlist = {},
}
--进入他人视野
local enterlist = {
playerlist = {},
monsterlist = {},
}
--通知他人自己移动
local movelist = {
playerlist = {},
monsterlist = {},
}
local othertempid
local otherpos
local othertype
local otherobj
for k,v in pairs(playerview[viewertempid]) do
othertempid = OBJ[k].tempid
otherpos = OBJ[k].movement.pos
othertype = OBJ[k].type
otherobj = {
tempid = othertempid,
agent = OBJ[k].agent,
}
local distance = DIST2(mypos,otherpos)
if distance <= AOI_RADIS2 then
if not v then
playerview[viewertempid][k] = true
if othertype ~= enumtype.CHAR_TYPE_PLAYER then
monsterview[k][viewertempid] = true
table.insert(enterlist.monsterlist,OBJ[k])
else
playerview[k][viewertempid] = true
table.insert(enterlist.playerlist,OBJ[k])
end
else
inserttotablebytype(movelist,otherobj,othertype)
end
elseif distance > AOI_RADIS2 and distance <= LEAVE_AOI_RADIS2 then
if v then
playerview[viewertempid][k] = false
if othertype ~= enumtype.CHAR_TYPE_PLAYER then
monsterview[k][viewertempid] = false
table.insert(leavelist.monsterlist,otherobj)
else
playerview[k][viewertempid] = false
table.insert(leavelist.playerlist,otherobj)
end
end
else
if v then
inserttotablebytype(leavelist,otherobj,othertype)
end
playerview[viewertempid][k] = nil
if othertype ~= enumtype.CHAR_TYPE_PLAYER then
monsterview[k][viewertempid] = nil
else
playerview[k][viewertempid] = nil
end
end
end
--离开他人视野
for _,v in pairs(leavelist.playerlist) do
skynet.send(v.agent,"lua","delaoiobj",viewertempid)
end
--重新进入视野
for _,v in pairs(enterlist.playerlist) do
skynet.send(v.agent,"lua","addaoiobj",myobj)
end
--视野范围内移动
for _,v in pairs(movelist.playerlist) do
skynet.send(v.agent,"lua","updateaoiobj",myobj)
end
--怪物的更新合并一起发送
if not table.empty(leavelist.monsterlist) or
not table.empty(enterlist.monsterlist) or
not table.empty(movelist.monsterlist) then
local monsterenterlist = {
obj = myobj,
monsterlist = enterlist.monsterlist,
}
local monsterleavelist = {
tempid = viewertempid,
monsterlist = leavelist.monsterlist,
}
local monstermovelist = {
obj = myobj,
monsterlist = movelist.monsterlist,
}
skynet.send(mapagent,"lua","updateaoiinfo",monsterenterlist,monsterleavelist,monstermovelist)
end
--通知自己
skynet.send(myobj.agent,"lua","updateaoilist",enterlist,leavelist)
end
--aoi回调
function CMD.aoicallback(w,m)
assert(OBJ[w],w)
assert(OBJ[m],m)
if playerview[OBJ[w].tempid] == nil then
playerview[OBJ[w].tempid] = {}
end
playerview[OBJ[w].tempid][OBJ[m].tempid] = true
--怪物视野内的玩家
if OBJ[m].type ~= enumtype.CHAR_TYPE_PLAYER then
if monsterview[OBJ[m].tempid] == nil then
monsterview[OBJ[m].tempid] = {}
end
monsterview[OBJ[m].tempid][OBJ[w].tempid] = true
end
--通知agent
skynet.send(OBJ[w].agent,"lua","addaoiobj",OBJ[m])
if OBJ[m].type ~= enumtype.CHAR_TYPE_PLAYER then
skynet.send(OBJ[m].agent,"lua","addaoiobj",OBJ[m].tempid,OBJ[w])
end
end
--添加到aoi
function CMD.characterenter(obj)
assert(obj)
assert(obj.agent)
assert(obj.movement)
assert(obj.movement.mode)
assert(obj.movement.pos.x)
assert(obj.movement.pos.y)
assert(obj.movement.pos.z)
--log.debug("AOI ENTER %d %s %d %d %d",obj.tempid,obj.movement.mode,obj.movement.pos.x,obj.movement.pos.y,obj.movement.pos.z)
OBJ[obj.tempid] = obj
if obj.type ~= enumtype.CHAR_TYPE_PLAYER then
updateviewmonster(obj.tempid)
else
updateviewplayer(obj.tempid)
end
assert(pcall(skynet.send,aoi, "text", "update "..obj.tempid.." "..obj.movement.mode.." "..obj.movement.pos.x.." "..obj.movement.pos.y.." "..obj.movement.pos.z))
need_update = true
end
--从aoi中移除
--TODO 怪物的离开
function CMD.characterleave(obj)
assert(obj)
skynet.error(obj.tempid, " leave aoi")
assert(pcall(skynet.send, aoi, "text", "update "..obj.tempid.." d "..obj.movement.pos.x.." "..obj.movement.pos.y.." "..obj.movement.pos.z))
OBJ[obj.tempid] = nil
if playerview[obj.tempid] then
local monsterleavelist = {
tempid = obj.tempid,
monsterlist = {},
}
for k,_ in pairs(playerview[obj.tempid]) do
if playerview[k] then
if playerview[k][obj.tempid] then
--视野内需要通知
skynet.send(OBJ[k].agent,"lua","delaoiobj",obj.tempid)
end
playerview[k][obj.tempid] = nil
elseif monsterview[k] then
if monsterview[k][obj.tempid] then
--视野内需要通知
table.insert(monsterleavelist.monsterlist,{tempid = k})
end
monsterview[k][obj.tempid] = nil
end
end
if not table.empty(monsterleavelist.monsterlist) then
skynet.send(mapagent,"lua","updateaoiinfo",{monsterlist = {}},monsterleavelist,{monsterlist = {}})
end
playerview[obj.tempid] = nil
end
need_update = true
end
--0.1秒更新一次
local function message_update ()
if need_update then
need_update = false
assert(pcall(skynet.send,aoi, "text", "message "))
end
update_thread = skynet.timeout(10, message_update)
end
function CMD.open()
aoi = assert(skynet.launch("caoi", map_name))
assert(aoi == (skynet.self() + 1))
mapagent = skynet.self() - 1
message_update()
end
function CMD.close(name)
skynet.error("close aoi(%s)...",name)
update_thread()
end
skynet.start(function()
skynet.dispatch("text", function (_, _, cmd)
local t = cmd:split(" ")
local f = CMD[t[1]]
if f then
luaqueue(f,tonumber(t[2]),tonumber(t[3]))
else
skynet.error("Unknown command : [%s]", cmd)
end
end)
skynet.dispatch("lua", function (_,_, cmd, ...)
local f = CMD[cmd]
if f then
skynet.ret(skynet.pack(luaqueue(f, ...)))
else
skynet.error("Unknown command : [%s]", cmd)
skynet.response()(false)
end
end)
end)