Blame view

src/adv/Adv.lua 29 KB
46fac6f1   zhouahaihai   酱料
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
  local Passive = require "adv.AdvPassive"
  
  -- 工具函数
  local function getIdByCr(c, r)
  	local crId = math.abs(r) + math.abs(c) * 100
  	if c < 0 then
  		crId = crId + 10000
  	end
  	if r < 0 then
  		crId = crId + 20000
  	end
  	return crId
  end
  
  local function getCrById(crId)
  	local c = math.floor(crId % 10000 / 100)
  	local r = crId % 100
  	local last = math.floor(crId / 10000)
  	if last == 3 then
  		c, r = -c, -r
  	elseif last == 1 then
  		c = -c
  	elseif last == 2 then
  		r = -r
  	end
  	return c, r
  end
  
  -----------------------------随机地图-----------------------------
  --检查 是否满足层数限制条件
  local function checkIsIn(checkValue, checkType, checkRange)
  	if not checkValue then return end
  	if checkType == 1 then
  		local limits = checkRange:toNumMap()
  		for min, max in pairs(limits) do
  			if checkValue >= min and checkValue <= max then
  				return true
  			end
  		end
  	else
  		local limit = checkRange:toArray(true, "=")
  		for _, _l in ipairs(limit) do
  			if _l == checkValue then
  				return true
  			end
  		end
  	end
  end
  
  --关卡事件库
  local function getEventLib(chapterId, level, needEventType)
  	local chapter = math.floor(chapterId / 100) % 100
  
  	local libsToType = {
  		["event_monsterCsv"] = {AdvEventType.Monster, AdvEventType.BOSS},
  		["event_chooseCsv"] = AdvEventType.Choose,
  		["event_dropCsv"] = AdvEventType.Drop,
  		["event_buildingCsv"] = AdvEventType.Build,
  		["event_traderCsv"] = AdvEventType.Trader,
  
  	}
  	local eventLib = {}
  	for lib, eventType in pairs(libsToType) do
  		if type(eventType) == "table" then
  			for _, temp in ipairs(eventType) do
  				eventLib[temp] = {}
  			end
  		else
  			eventLib[eventType] = {}
  		end
  		if not needEventType or eventLib[needEventType] then 
  			for id, data in pairs(csvdb[lib]) do
  				if data.levelchapter == chapter then
  					if checkIsIn(level, data.leveltype, data.levellimit) then
  						if type(eventType) == "table" then
  							eventLib[eventType[data.type]][id] = data
  						else
  							eventLib[eventType][id] = data
  						end
  					end
  				end
  			end
  			if needEventType then
  				break
  			end
  		end
  	end
  	return eventLib
  end
  
  -- 生成地图   是否可以生成地图上层判断
  local function randomAdvMap(role, chapterId, level, notNotify)
  	local chapterData = csvdb["adv_chapterCsv"][chapterId]
  	if not chapterData then return end
  	if level > chapterData.limitlevel then return end
  	--随出地图
  	local raw_pool = chapterData.mapid:toArray(true, "=")
  	local advInfo = role:getProperty("advInfo")
  	local lastMapId = advInfo.mapId  --非同一层不连续随出同一张类似的地图
  	local lastChapterId = advInfo.chapter
ec87b4a5   zhouahaihai   冒险 完善
101
  	local power = advInfo.power or 100 --体力
46fac6f1   zhouahaihai   酱料
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
  	local pool = {}
  	for _, mapId in ipairs(raw_pool) do
  		local temp = csvdb["mapCsv"][mapId]
  		if temp and (lastChapterId == chapterId or lastMapId ~= mapId) then --非同一层不连续随出同一张类似的地图
  			if checkIsIn(level, temp.leveltype, temp.levellimit) then
  				table.insert(pool, mapId)
  			end
  		end
  	end
  	if not next(pool) then return end
  	local mapId = pool[math.randomInt(1, #pool)]
  	--随出事件
  	local mapData = csvdb["map_" .. csvdb["mapCsv"][mapId]["path"] .. "Csv"]
  	if not mapData then return end
  
  
  	table.clear(advInfo)
  	advInfo.chapter = chapterId
  	advInfo.level = level
  	advInfo.mapId = mapId
  	advInfo.power = power
  	advInfo.enemyId = 1 --怪递增的索引
  	advInfo.rooms = {} -- {[roomId] = {event = {}, open = {}},} -- event 事件信息(具体信息查看randomEvent), open 是否解锁
  
  	--事件随机
  	local eventLib = getEventLib(chapterId, level)
  	local monsterEvents = {}  --处理钥匙掉落
  	local haveBoss = false
  
  	local function randomEvent(roomId, blockId, eventType)
  		if advInfo.rooms[roomId]["event"][blockId] then return end --已经有事件了 不覆盖
  		local event = {etype = eventType}
  		local randomFunc = {}
  		--入口
  		randomFunc[AdvEventType.In] = function()end
  		--出口
  		randomFunc[AdvEventType.Out] = function() end
  
  		--boss
  		randomFunc[AdvEventType.BOSS] = function()
  			if not next(eventLib[eventType]) or haveBoss then return false end
  			haveBoss = true
  			event.id = math.randWeight(eventLib[eventType], "showup")
  		end
  		--怪物
  		randomFunc[AdvEventType.Monster] = function()
  			if not next(eventLib[eventType]) then return false end
  			event.id = math.randWeight(eventLib[eventType], "showup")
  			table.insert(monsterEvents, event)
  		end
  		--选择点
  		randomFunc[AdvEventType.Choose] = function()
  			if not next(eventLib[eventType]) then return false end
  			event.id = math.randWeight(eventLib[eventType], "showup")
  		end
  		--掉落点
  		randomFunc[AdvEventType.Drop] = randomFunc[AdvEventType.Choose]
  		--交易所
  		randomFunc[AdvEventType.Trader] = randomFunc[AdvEventType.Choose]
  		--建筑
  		randomFunc[AdvEventType.Build] = randomFunc[AdvEventType.Choose]
  
  		if randomFunc[eventType] then
  			if randomFunc[eventType]() ~= false then
  				advInfo.rooms[roomId]["event"][blockId] = event
  			end
  		end
  	end
  
  	stagePool = {["global"] = {}}
  	for roomId, roomName in pairs(mapData["rooms"]) do
  		stagePool[roomId] = {}
  		advInfo.rooms[roomId] = {event = {}, open = {}}  -- 事件, open  open == 1 房间内地块全部开放
  		local roomData
  		if roomName == "path" then
  			roomData = mapData["path"]
  		else
  			roomName = roomName:gsub("/", "_")
  			roomData = csvdb["room_" .. roomName .. "Csv"]
  		end
  		for blockId, stageType in pairs(roomData["blocks"]) do
  			if AdvSpecialStage[stageType] then
  				eventType = AdvEventType[AdvSpecialStage[stageType]]
  				randomEvent(roomId, blockId, eventType)
  			else
  				stagePool["global"][stageType] = stagePool["global"][stageType] or {}
  				stagePool[roomId][stageType] = stagePool[roomId][stageType] or {}
  				table.insert(stagePool["global"][stageType], {room = roomId, block = blockId})
  				stagePool[roomId][stageType][blockId] = 1
  			end
  		end
  	end
  	-- 全地图事件 优先级高
  	for stageType, events in pairs(mapData["events"]) do
  		for _, event in ipairs(events) do
  			local lastCount = #stagePool["global"][stageType]
  			if lastCount <= 0 then break end
  			if math.randomFloat(0, 1) <= (event["rate"] or 1) then
  				local count = math.randomInt(math.min(lastCount, event["minc"]), math.min(lastCount, event["maxc"]))
  				for i = 1, count do
  					local idx = math.randomInt(1, lastCount)
  					local cur = stagePool["global"][stageType][idx]
  					randomEvent(cur["room"], cur["block"], event["event"])
  					table.remove(stagePool["global"][stageType], idx)
  					lastCount = lastCount - 1
  					stagePool[cur["room"]][stageType][cur["block"]] = nil
  				end
  			end
  		end
  	end
  	-- 随机单个房间的事件	
  	for roomId, roomName in pairs(mapData["rooms"]) do
  		local roomData
  		if roomName == "path" then
  			roomData = mapData["path"]
  		else
  			roomName = roomName:gsub("/", "_")
  			roomData = csvdb["room_" .. roomName .. "Csv"]
  		end
  		for stageType, events in pairs(roomData["events"]) do
  			local bpool = {}
  			if stagePool[roomId][stageType] then
  				for block, _ in pairs(stagePool[roomId][stageType]) do
  					table.insert(bpool, block)
  				end
  			end
  			for _, event in ipairs(events) do
  				if #bpool <= 0 then break end
  				if math.randomFloat(0, 1) <= (event["rate"] or 1) then
  					local count = math.randomInt(math.min(#bpool, event["minc"]), math.min(#bpool, event["maxc"]))
  					for i = 1, count do
  						local idx = math.randomInt(1, #bpool)
  						randomEvent(roomId, bpool[idx], event["event"])
  						table.remove(bpool, idx)
  					end
  				end
  			end
  		end
  	end
  	if not haveBoss then
  		if not next(monsterEvents) then
  			print("这个地图没有钥匙!!! mapId : " .. mapId)
  		else
  			local event = monsterEvents[math.randomInt(1, #monsterEvents)]
36c30c5c   zhouahaihai   冒险
246
  			event.item = {ItemId.AdvKey, 1}  --掉落钥匙 --todo
46fac6f1   zhouahaihai   酱料
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
  		end
  	end
  end
  
  --块类型
  local Block = class("Block")
  function Block:ctor(blockId, event, isOpen)
  	self.blockId = blockId
  	self.col, self.row = getCrById(self.blockId)
  	self.isOpen = isOpen and true or false
  	self.event = event  -- 拿到的是引用可以直接更新
  end
  function Block:isBoss()
  	if not self.event then return end
  	return self.event["etype"] == AdvEventType.BOSS
  end
  
  --事件有需要额外处理的部分
  function Block:open(adv, room)
  	--如果翻开有数据处理在这里处理
  	local randomFunc = {}
  	--怪
  	randomFunc[AdvEventType.Monster] = function()
  		self.event.mId = adv.advInfo.enemyId  --给怪一个有序id 回合逻辑时使用
  		adv.advInfo.enemyId = adv.advInfo.enemyId + 1
36c30c5c   zhouahaihai   冒险
272
  		local enemy = adv.battle:getEnemy(room.roomId, self.blockId)
46fac6f1   zhouahaihai   酱料
273
274
275
  		if enemy then
  			enemy:unlock(self.event.mId)
  		else
02c4de8d   zhouahaihai   增加 固有技
276
  			enemy = adv.battle:addEnemy(room, self)
46fac6f1   zhouahaihai   酱料
277
  		end
02c4de8d   zhouahaihai   增加 固有技
278
  		enemy:triggerPassive(Passive.BORN_ONCE)
46fac6f1   zhouahaihai   酱料
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  	end
  	randomFunc[AdvEventType.BOSS] = randomFunc[AdvEventType.Monster]
  	--掉落
  	randomFunc[AdvEventType.Drop] = function()
  		self.event.item = csvdb["event_dropCsv"][self.event.id]["range"]:randWeight(true)
  	end
  	--交易
  	randomFunc[AdvEventType.Trader] = function()
  		local data = csvdb["event_traderCsv"][self.event.id]
  		self.event.shop = {}
  		self.event.status = "" --购买次数状态  1 就是购买过了  -- 购买id就是shop索引
  		for i = 1, 10 do
  			local numS, rangeS = "num" .. i, "range" .. i
  			if data[numS] and data[rangeS] then
  				for j = 1, data[numS] do
  					table.insert(self.event.shop, data[rangeS]:randWeight(true))
  				end
  			else
  				break
  			end
  		end
  	end
  	--建筑
36c30c5c   zhouahaihai   冒险
302
  	randomFunc[AdvEventType.Build] = function()
46fac6f1   zhouahaihai   酱料
303
304
305
306
307
308
309
310
311
  		local data = csvdb["event_buildingCsv"][self.event.id]
  		self.event.effect = data["range"]:randWeight(true) --随出建筑效果
  		if self.event.effect[1] == 1 then --获得某道具
  			local reward = csvdb["event_dropCsv"][self.event.effect[2]]["range"]:randWeight(true)
  			self.event.effect[2] = reward[1]
  			self.event.effect[3] = reward[2]
  		end
  	end
  	--抉择点
36c30c5c   zhouahaihai   冒险
312
  	randomFunc[AdvEventType.Choose] = function()
46fac6f1   zhouahaihai   酱料
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  		local data = csvdb["event_chooseCsv"][self.event.id]
  		self.event.effect = {}
  		for i = 1, 2 do
  			self.event.effect[i] = data["button".. i .."effect"]:toArray(true, "=")
  			if self.event.effect[i][1] == 1 then --获得某道具
  				local reward = csvdb["event_dropCsv"][self.event.effect[i][2]]["range"]:randWeight(true)
  				self.event.effect[i][2] = reward[1]
  				self.event.effect[i][3] = reward[2]
  			end 
  		end
  	end
  	if self.event then -- 随机出具体的事件
  		if randomFunc[self.event.etype] then
  			randomFunc[self.event.etype]()
  		end
  	end
36c30c5c   zhouahaihai   冒险
329
  	self.isOpen = true
46fac6f1   zhouahaihai   酱料
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  end
  
  local Room = class("Room")
  function Room:ctor(adv, roomId, csvData, info, isPath)
  	self.roomId = roomId
  	self.col, self.row = getCrById(self.roomId)
  	self.isPath = isPath
  	self.isBossRoom = false -- boss房间   --击败boss 以后重置为false
  	self.info = info -- 拿到引用 方便更新advInfo
  	self.isShow = false
  	self.blocks = {}
  
  	for blockId, _ in pairs(csvData["blocks"]) do
  		self.blocks[blockId] = Block.new(blockId, info.event[blockId], info.open == 1 or info.open[blockId])
  		if not self.isPath and self.blocks[blockId]:isBoss() then
  			self.isBossRoom = true 
  		end
  		if self.blocks[blockId].isOpen then
  			self.isShow = true
  		else
  			if self.blocks[blockId].event and self.blocks[blockId].event.etype == AdvEventType.In then -- 开放
  				self.isShow = true
  				self.blocks[blockId].isOpen = true
  				self.info.open[blockId] = 1
  
  				--入口房间只会在这里首次展示开放 --触发固有技
  				adv:triggerPassive(Passive.ROOM_SHOW, {roomId = self.roomId})
  			end
  		end
  	end
  end
  
  function Room:tranGToL(c, r)
  	return c - self.col, r - self.row
  end
  
  function Room:tranLtoG(c, r)
  	return c + self.col, r + self.row
  end
  
  function Room:getBByGPos(c, r)
  	local c, r = self:tranGToL(c, r)
  	return self.blocks[getIdByCr(c, r)]
  end
  
  function Room:openBlock(block, adv)
  	if self.blocks[block.blockId] ~= block then return end
  	if block.isOpen == true then return end
36c30c5c   zhouahaihai   冒险
378
  	if self.isBossRoom then
46fac6f1   zhouahaihai   酱料
379
  		for _, _block in pairs(self.blocks) do
36c30c5c   zhouahaihai   冒险
380
381
382
383
384
385
386
387
388
389
  			_block:open(adv, self)
  		end
  	else
  		block:open(adv, self)
  	end
  	local allOpen = true
  	for _, _block in pairs(self.blocks) do
  		if not _block.isOpen then
  			allOpen = false
  			break
46fac6f1   zhouahaihai   酱料
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
  		end
  	end
  
  	if allOpen then
  		self.info.open = 1
  	else
  		self.info.open[block.blockId] = 1
  	end
  
  	if not self.isShow then
  		self.isShow = true
  		--首次展示房间
  		adv:triggerPassive(Passive.ROOM_SHOW, {roomId = self.roomId})
  	end 
  end
  
  function Room:clearBEvent(block)
  	if self.blocks[block.blockId] ~= block then return end
  	block.event = nil
  	self.info.event[block.blockId] = nil
  end
  
  
  
  local Adv = class("Adv")
  function Adv:ctor(owner)
  	assert(owner, "Adv instance must have owner(role)")
  	self.owner = owner
  	self.advInfo = self.owner:getProperty("advInfo")  --这个变量置空使用 table.clear  
  	self.advTeam = self.owner:getProperty("advTeam")	--这个变量置空使用 table.clear  
  	self:clear()
36c30c5c   zhouahaihai   冒险
421
  	self.backEvents = {} --发给客户端的事件组
46fac6f1   zhouahaihai   酱料
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
  end
  
  -- 清空自己组织的数据
  function Adv:clear()
  	self.rooms = {}
  	self.cachePassiveEvent = {} -- 在battle 没有创建成功时 触发的被动技信息
  	self.battle = nil -- 战斗逻辑
  end
  
  --关卡通关,非层 score < 0 失败
  function Adv:over(success)
  	local score = -1
  	if success then
  		-- todo success 计算分数
  		score = 1
  		self.owner:updateProperty({field = "advPass", self.owner:getProperty("advPass"):setv(self.advInfo.chapter, score)})
  	end
  	table.clear(self.advInfo) --清空advInfo
  	self.advTeam.player = nil --重置玩家的数据
  	self:clear()
  	self:backEnd(score)
  end
  
ec87b4a5   zhouahaihai   冒险 完善
445
  function Adv:exit()
4b7c7c96   zhouahaihai   增加 清空 挂机 冒险gm 角色经验
446
  	self:over(false)
ec87b4a5   zhouahaihai   冒险 完善
447
448
449
  	self:saveDB()
  end
  
46fac6f1   zhouahaihai   酱料
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
  function Adv:getMapInfo()
  	if not next(self.advInfo) then return end
  	return csvdb["mapCsv"][self.advInfo.mapId]
  end
  
  function Adv:getMapData()
  	local mapInfo = self:getMapInfo()
  	if not mapInfo then return end
  	return csvdb["map_" .. self:getMapInfo()["path"] .. "Csv"]
  end
  
  function Adv:initByInfo()
  	self:clear()
  	if not next(self.advInfo) then return end --未初始化的 advInfo
  
  	local mapData = self:getMapData()
  	if not mapData then return end
  
  	for roomId, roomName in pairs(mapData["rooms"]) do
  		if roomName == "path" then
  			self.rooms[roomId] = Room.new(self, roomId, mapData["path"], self.advInfo.rooms[roomId], true)
  		else
  			roomName = roomName:gsub("/", "_")
  			self.rooms[roomId] = Room.new(self, roomId, csvdb["room_" .. roomName .. "Csv"], self.advInfo.rooms[roomId], false)
  		end
  	end
  	self:initBattle()
  	return true
  end
  
  function Adv:triggerPassive(condType, params)
  	if not self.battle then
  		table.insert(self.cachePassiveEvent, {condType, params})
  	else
  		self.battle:triggerPassive(condType, params)
  	end
  end
  
  function Adv:initBattle()
  	self.battle = require("adv.AdvBattle").new(self)
  	for _, passiveC in ipairs(self.cachePassiveEvent) do
  		self.battle:triggerPassive(passiveC[1], passiveC[2])
  	end
  	self.cachePassiveEvent = {}
  end
  
  -- 随机地图
  function Adv:initByChapter(chapterId, level, notNotify)
36c30c5c   zhouahaihai   冒险
498
  	level = level or 1
46fac6f1   zhouahaihai   酱料
499
500
  	randomAdvMap(self.owner, chapterId, level, notNotify)
  	self:initByInfo() --初始化
36c30c5c   zhouahaihai   冒险
501
  	self.owner:updateProperties({advInfo = self.advInfo, advTeam = self.advTeam}, notNotify)
46fac6f1   zhouahaihai   酱料
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  end
  
  --获取,某个位置上的 room 和 block
  function Adv:getRBByPos(c, r)
  	for roomId, room in pairs(self.rooms) do
  		local block = room:getBByGPos(c, r)
  		if block then
  			return room, block
  		end
  	end
  end
  
  function Adv:getAroundBlocks(room, block)
  	local blocks = {}
  	local range = {1, -1}
  	local col, row = room:tranLtoG(block.col, block.row)
  	for _, add in ipairs(range) do
  		local rroom, rblock = self:getRBByPos(col + add, row)
  		if rroom then
  			table.insert(blocks, {rroom, rblock})
  		end
  	end
  	for _, add in ipairs(range) do
  		local rroom, rblock = self:getRBByPos(col, row + add)
  		if rroom then
  			table.insert(blocks, {rroom, rblock})
  		end
  	end
  	return blocks
  end
  
  --随机一个空的位置生成怪, 如果没有就没有	
02c4de8d   zhouahaihai   增加 固有技
534
535
536
537
538
539
540
541
542
543
544
  function Adv:addNewMonsterRand(monsterId, where)
  	local room, block
  	if where then
  		room, block = where[1], where[2]
  	else
  		local pool = {}
  		for _, room_ in pairs(self.rooms) do
  			for _, block_ in pairs(room_.blocks) do
  				if block_.isOpen and not block_.event then
  					table.insert(pool, {room_, block_})
  				end
46fac6f1   zhouahaihai   酱料
545
546
  			end
  		end
02c4de8d   zhouahaihai   增加 固有技
547
548
549
  		if not next(pool) then return end
  		local idx = math.randomInt(1, #pool)
  		room, block = pool[idx][1], pool[idx][2]
46fac6f1   zhouahaihai   酱料
550
  	end
02c4de8d   zhouahaihai   增加 固有技
551
  	
9cffb42b   zhouahaihai   抉择点 建筑
552
553
554
555
556
  	if not monsterId then
  		local eventLib = getEventLib(self.advInfo.chapter, self.advInfo.level, AdvEventType.Monster)
  		if not next(eventLib[AdvEventType.Monster]) then return false end
  		monsterId = math.randWeight(eventLib[AdvEventType.Monster], "showup")
  	end
02c4de8d   zhouahaihai   增加 固有技
557
558
559
  
  	local event = {etype = AdvEventType.Monster, mId = self.advInfo.enemyId}
  	self.advInfo.enemyId = self.advInfo.enemyId + 1
9cffb42b   zhouahaihai   抉择点 建筑
560
  	event.id = monsterId
46fac6f1   zhouahaihai   酱料
561
562
  	block.event = event
  	room.info.event[block.blockId] = event
02c4de8d   zhouahaihai   增加 固有技
563
564
  	self.battle:addEnemy(room, block):triggerPassive(Passive.BORN_ONCE)
  
46fac6f1   zhouahaihai   酱料
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  	return room, block
  end
  
  -- 随机翻开 num 个 以开放的房间的 地块
  function Adv:openBlockRand(num)
  	local pool = {}
  	for _, room in pairs(self.rooms) do
  		if room.isShow and not room.isPath then
  			for _, block in pairs(room.blocks) do
  				if not block.isOpen then
  					table.insert(pool, {room.roomId, block.blockId})
  				end
  			end
  		end
  	end
  	if #pool <= num then
  		for _, temp in ipairs(pool) do
  			self:openBlock(temp[1], temp[2])
  		end
  	else
  		for i = 1, num do
  			local idx = math.randomInt(1, #pool)
  			self:openBlock(pool[idx][1], pool[idx][2])
  			table.remove(pool, idx)
  		end
  	end
  end
  -- 打开一个地块
  function Adv:openBlock(roomId, blockId)
  	local room = self.rooms[roomId]
02c4de8d   zhouahaihai   增加 固有技
595
  	if not room then return end
46fac6f1   zhouahaihai   酱料
596
  	local block = room.blocks[blockId]
02c4de8d   zhouahaihai   增加 固有技
597
  	if not block then return end
46fac6f1   zhouahaihai   酱料
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  	room:openBlock(block, self)
  	self:backBlockChange(roomId, blockId)
  end
  
  -- 在冒险中获得的物品都发放在冒险背包内
  function Adv:award(gift, params)
  	local tgift = {}
  	if type(gift) == "string" then
  		for _, one in pairs(gift:toTableArray(true)) do
  			tgift[one[1]] = (tgift[one[1]] or 0) + one[2]
  		end
  	else
  		tgift = gift
  	end
  	local items = self.owner:getProperty("advItems")
  	for itemId, count in pairs(tgift) do
  		local origin = items:getv(itemId, 0)
  		local nums = origin + count
  		if nums <= 0 then
  			items = items:delk(itemId)
  			nums = 0
  		else
  			items = items:incrv(itemId, count)
  		end
  	end
9cffb42b   zhouahaihai   抉择点 建筑
623
  
46fac6f1   zhouahaihai   酱料
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
  	self.owner:updateProperty({field = "advItems", value = items, notNotify = params.notNotify})
  	return tgift
  end
  
  -- 消耗物品 优先冒险背包  --check 只是检查够不够
  function Adv:cost(item, params, check)
  	local items = self.owner:getProperty("advItems")
  	local less = {}
  	local advCost = {}
  	for itemId, count in pairs(item) do
  		advCost[itemId] = - math.min(items:getv(itemId, 0), count)
  		if advCost[itemId] == 0 then
  			advCost[itemId] = nil
  		end
  
  		local last = items:getv(itemId, 0) - count
  		if last < 0 then
  			less[itemId] = -last
  		end
  
  	end
  	if next(less) and not self.owner:checkItemEnough(less) then return end --不够
  	if check then return true end
  	self:award(advCost, params)
  	self.owner:costItems(less, params)
  	return true
  end
  
  --事件点击处理
  local function clickOut(self, room, block, params)
36c30c5c   zhouahaihai   冒险
654
  	if self:cost({[ItemId.AdvKey] = 1}, {}) then
46fac6f1   zhouahaihai   酱料
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
  		if self.advInfo.level >= csvdb["adv_chapterCsv"][self.advInfo.chapter].limitlevel then --关卡结束
  			self:over(true)
  		else
  			self:initByChapter(self.advInfo.chapter, self.advInfo.level + 1, true)
  			self:backNext() --下一关
  		end
  		return true
  	end
  end
  
  --战斗 普通攻击
  local function clickMonster(self, room, block, params)
  	self.battle:playerAtk(room.roomId, block.blockId)
  	return true
  end
  
  local function clickChoose(self, room, block, params)
  	local choose = params.choose
  	local chooseData = csvdb["event_chooseCsv"][block.event.id]
  	if not chooseData or not chooseData["button".. choose .."cond"] then return end
  
  	local cond = chooseData["button".. choose .."cond"]:toArray(true, "=")
  	local checkCond = {
  		-- 拥有道具
  		[1] = function()
  			if self:cost({[cond[2]] = cond[3]}, {}, true) then
  				return true
  			end
  		end,
  		-- xx角色(todo 队长)
  		[2] = function()
  			for slot, heroId in pairs(self.advTeam.heros) do
  				if self.owner.heros[heroId] then
  					if self.owner.heros[heroId]:getProperty("type") == cond[2] then
  						return true
  					end
  				end
  			end
  		end,
  		--消灭所有怪
  		[3] = function()
  			for _, room in pairs(self.rooms) do
4ae223df   zhouahaihai   Buff bug
697
  				for _, block in pairs(room.blocks) do
46fac6f1   zhouahaihai   酱料
698
699
700
701
702
703
704
705
706
707
708
709
710
711
  					if block.event and (block.event.etype == AdvEventType.BOSS or block.event.etype == AdvEventType.Monster) then
  						return
  					end
  				end
  			end
  			return true
  		end,
  		--制定属性 > 
  		[4] = function()
  			if (self.battle.player[AttsEnumEx[cond[2]]] or 0) > cond[3] then
  				return true
  			end
  		end,
  	}
9cffb42b   zhouahaihai   抉择点 建筑
712
713
  	assert(not cond[1] or checkCond[cond[1]], "error cond, event_chooseCsv id :" .. block.event.id)
  	if cond[1] and not checkCond[cond[1]]() then return end
02c4de8d   zhouahaihai   增加 固有技
714
  	local clearBlock = true
46fac6f1   zhouahaihai   酱料
715
716
717
718
719
720
721
722
723
  	local effect = block.event.effect[choose]
  	local doEffect = {
  		[1] = function() -- 获得某道具N个
  			self:backReward(self:award({[effect[2]] = effect[3]}, {}))
  		end,
  		[2] = function() --获得冒险buff
  			self.battle.player:addBuff(effect[2])
  		end,
  		[3] = function() --发现怪物
02c4de8d   zhouahaihai   增加 固有技
724
725
  			self:addNewMonsterRand(effect[2], {room, block})
  			clearBlock = false
46fac6f1   zhouahaihai   酱料
726
727
728
729
730
731
  		end,
  		[4] = function() --无事发生
  		end
  	}
  	assert(doEffect[effect[1]], "error effect, event_chooseCsv id :" .. block.event.id)
  	doEffect[effect[1]]()
02c4de8d   zhouahaihai   增加 固有技
732
733
734
  	if clearBlock then
  		room:clearBEvent(block)
  	end
46fac6f1   zhouahaihai   酱料
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
  	return true
  end
  
  local function clickDrop(self, room, block, params)
  	local reward = {}
  	if not block.event.item then return end
  	local reward = self:award({[block.event.item[1]] = block.event.item[2]}, {})
  	room:clearBEvent(block)
  	self:backReward(reward)
  	return true
  end
  
  local function clickTrader(self, room, block, params)
  	local buyId = params.id
  	local traderData = csvdb["event_traderCsv"][block.event.id]
  	if not traderData then return end -- 偷偷改表了
  
  	if not block.event.shop or not block.event.shop[buyId] then return end
  	if (block.event.status or ""):getv(buyId, 0) == 1 then return end -- 买过了
  
  	if not self:cost(traderData.type:toNumMap(), {}) then return end --不够
  
  	local reward = self:award({[block.event.shop[buyId][1]] = block.event.shop[buyId][2]}, {})
  	block.event.status = block.event.status:setv(buyId, 1)
  	self:backReward(reward)
  	return true
  end
  
  local function clickBuild(self, room, block, params)
  	local buildData = csvdb["event_buildingCsv"][block.event.id]
  	if not buildData then return end-- 偷偷改表了
  	if not block.event.effect then return end -- 没有效果 气人不
02c4de8d   zhouahaihai   增加 固有技
767
  	local clearBlock = true
46fac6f1   zhouahaihai   酱料
768
769
770
771
772
773
774
775
776
777
  	local effect = block.event.effect
  	--todo 效果生效
  	local doEffect = {
  		[1] = function() -- 获得某道具N个
  			self:backReward(self:award({[effect[2]] = effect[3]}, {}))
  		end,
  		[2] = function() --获得冒险buff
  			self.battle.player:addBuff(effect[2])
  		end,
  		[3] = function() --发现怪物
02c4de8d   zhouahaihai   增加 固有技
778
779
  			self:addNewMonsterRand(effect[2], {room, block})
  			clearBlock = false
46fac6f1   zhouahaihai   酱料
780
781
782
783
784
785
786
  		end,
  		[4] = function() --无事发生
  		end
  	}
  	assert(doEffect[effect[1]], "error effect, event_buildingCsv id :" .. block.event.id)
  	if not self:cost({[buildData.type] = 1}, {}) then return end
  	doEffect[effect[1]]()
02c4de8d   zhouahaihai   增加 固有技
787
788
789
  	if clearBlock then
  		room:clearBEvent(block)
  	end
46fac6f1   zhouahaihai   酱料
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  	return true
  end
  
  local eventCallFunc = {
  	[AdvEventType.Out] = clickOut,
  	[AdvEventType.BOSS] = clickMonster,
  	[AdvEventType.Monster] = clickMonster,
  	[AdvEventType.Choose] = clickChoose,
  	[AdvEventType.Drop] = clickDrop,
  	[AdvEventType.Trader] = clickTrader,
  	[AdvEventType.Build] = clickBuild,
  }
  
  --点击处理 roomId, blockId 
  --params 某些事件需要的客户端传递的参数
  function Adv:clickBlock(roomId, blockId, params)
  	local room = self.rooms[roomId]
  	if not room then return end
  	local block = room.blocks[blockId]
  	if not block then return end
  
46fac6f1   zhouahaihai   酱料
811
  	local status = false
36c30c5c   zhouahaihai   冒险
812
  	local clickEvent = false
46fac6f1   zhouahaihai   酱料
813
  	if not block.isOpen then
36c30c5c   zhouahaihai   冒险
814
815
816
817
818
819
820
821
822
  		local canOpen = false  --如果未开放是否可以开放
  		local hadMonster = false -- 周围是否有解锁的怪未击败
  		for _, one in ipairs(self:getAroundBlocks(room, block)) do
  			local _room, _block = one[1], one[2]
  			if _block.isOpen then canOpen = true end
  			if _block.isOpen and _block.event and (_block.event.etype == AdvEventType.BOSS or _block.event.etype == AdvEventType.Monster) then 
  				hadMonster = true 
  			end
  		end
46fac6f1   zhouahaihai   酱料
823
824
825
826
827
  		if canOpen and not hadMonster then --开放
  			room:openBlock(block, self)
  			status = true
  		end
  	else
36c30c5c   zhouahaihai   冒险
828
  		clickEvent = true
46fac6f1   zhouahaihai   酱料
829
830
831
832
833
  		--点了空地
  		if not block.event then
  			return 
  		end
  		--可点击的事件
36c30c5c   zhouahaihai   冒险
834
  		if not room.isBossRoom or block.event.etype == AdvEventType.BOSS then
46fac6f1   zhouahaihai   酱料
835
836
837
838
839
  			if eventCallFunc[block.event.etype] then
  				status = eventCallFunc[block.event.etype](self, room, block, params)
  			end
  		end
  	end
36c30c5c   zhouahaihai   冒险
840
  	if status and (not clickEvent or (not block.event or block.event.etype ~= AdvEventType.Out)) then  --出去了就不计算回合了
46fac6f1   zhouahaihai   酱料
841
842
843
  		self:backBlockChange(roomId, blockId)
  		self:afterRound()
  	end
36c30c5c   zhouahaihai   冒险
844
  	self:saveDB()
46fac6f1   zhouahaihai   酱料
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
  	return status
  end
  
  --使用道具产生效果
  function Adv:useItem(itemId, count, target)
  	count = count or 1
  	local itemData = csvdb["adv_itemCsv"][itemId] 
  	if not itemData then return end
  	--重置数量 
  	if itemData["function"] == 0 or itemData["function"] == 2 then count = 1 end
  	if not self:cost({[itemId] = count}, {}, true) then return true end
  	--消耗
  	if itemData["function"] == 0 or itemData["function"] == 1 then 
  		self:cost({[itemId] = count}, {})
  	end
  	--生效
  	if itemData.type == 1 or itemData.type == 0 then --技能
  		self.battle.player:releaseSkill(itemData.effect, 1, target)
  	elseif itemData.type == 2 then	--掉落
  		local item = csvdb["event_dropCsv"][itemData.effect]["range"]:randWeight(true)
  		self:backReward(self:award({[item[1]] = item[2]}, {}))
  	else
  		return
  	end
  
  	self:afterRound()
36c30c5c   zhouahaihai   冒险
871
  	self:saveDB()
46fac6f1   zhouahaihai   酱料
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  	return true
  end
  
  --使用技能
  function Adv:useSkill(skillId, target)
  	local skillLevel = nil
  	if skillId > 1000 then return end
  	for slot, heroId in pairs(self.advTeam.heros) do
  		if self.owner.heros[heroId] then
  			if csvdb["unitCsv"][self.owner.heros[heroId]:getSkinId()]["adv"] == skillId then
  				skillLevel = self.owner.heros[heroId]:getSkillLevel(4)
  				break
  			end
  		end
  	end
  	if not skillLevel then return end
  	local skillData = csvdb["adv_skillCsv"][skillId]
  	if self.advInfo.power < skillData.cost then return end
  	self.advInfo.power = self.advInfo.power - skillData.cost
  	if skillData.target:toArray(true, "=")[1] == 0 then
  		local enemy = self.battle:getEnemy(target.roomId, target.blockId)
  		if not enemy then return end
  		self.battle.player:releaseSkill(skillId, skillLevel, enemy)
  	else
  		self.battle.player:releaseSkill(skillId, skillLevel)
  	end
  	
  	self:afterRound()
36c30c5c   zhouahaihai   冒险
900
  	self:saveDB()
46fac6f1   zhouahaihai   酱料
901
902
903
904
  	return true
  end
  
  --敌人死亡
02c4de8d   zhouahaihai   增加 固有技
905
  function Adv:enemyDead(roomId, blockId, escape)
36c30c5c   zhouahaihai   冒险
906
907
  	local room = self.rooms[roomId]
  	local block = room.blocks[blockId]
46fac6f1   zhouahaihai   酱料
908
909
  	--死了以后掉东西
  	if block.event and (block.event.etype == AdvEventType.BOSS or block.event.etype == AdvEventType.Monster) then --处理死亡
36c30c5c   zhouahaihai   冒险
910
911
912
  		if block.event.etype == AdvEventType.BOSS then
  			room.isBossRoom = false
  		end
02c4de8d   zhouahaihai   增加 固有技
913
914
915
916
917
918
919
920
921
922
923
924
  		if escape then
  			room:clearBEvent(block)
  		else
  			local item = block.event.item
  			if not item then
  				if block.event.etype == AdvEventType.BOSS then
  					item = {ItemId.AdvKey, 1} 
  				else
  					local monsterData = csvdb["event_monsterCsv"][block.event.id]
  					local dropData = csvdb["event_dropCsv"][monsterData.dropid]
  					item = dropData["range"]:randWeight(true)
  				end
46fac6f1   zhouahaihai   酱料
925
  			end
4b7c7c96   zhouahaihai   增加 清空 挂机 冒险gm 角色经验
926
927
928
929
930
931
932
  			if item[1] == 0 then
  				room:clearBEvent(block)
  			else
  				table.clear(block.event)
  				block.event.etype = AdvEventType.Drop
  				block.event.item = item
  			end
46fac6f1   zhouahaihai   酱料
933
  		end
46fac6f1   zhouahaihai   酱料
934
935
936
937
938
939
940
941
942
943
944
945
946
  	end
  	self:backBlockChange(roomId, blockId)
  end
  
  --cType 0 or nil  值  1 百分比
  function Adv:changePower(value, cType)
  	cType = cType or 0
  	if cType == 0 then
  		self.advInfo.power = self.advInfo.power + value
  	elseif cType == 1 then
  		self.advInfo.power = self.advInfo.power + self.advInfo.power * value
  	end
  	self.advInfo.power = math.floor(math.max(0, self.advInfo.power))
ec87b4a5   zhouahaihai   冒险 完善
947
  	self:pushBackEvent(AdvBackEventType.PowerChange)
46fac6f1   zhouahaihai   酱料
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
  end
  
  function Adv:pushBackEvent(btype, params)
  	table.insert(self.backEvents, {btype = btype, params = params})
  end
  
  function Adv:backReward(items)
  	self:pushBackEvent(AdvBackEventType.Reward, {items = items})
  end
  -- if is player enemyId is nil 
  --isMax 是否是改变血量上限
  function Adv:backHpChange(enemyId, change, isMax) 
  	self:pushBackEvent(AdvBackEventType.HpChange, {enemyId = enemyId, change = change, isMax = isMax})
  end
  -- if is player enemyId is nil 
  function Adv:backAtkChange(enemyId, change)
  	self:pushBackEvent(AdvBackEventType.AtkChange, {enemyId = enemyId, change = change})
  end
  -- if is player enemyId is nil 
  function Adv:backBuff(enemyId, buffId, isDel)
  	self:pushBackEvent(AdvBackEventType.Buff, {enemyId = enemyId, buffId = buffId, isDel = isDel})
  end
  -- if is player enemyId is nil 
  function Adv:backSkill(enemyId, skillId, receiver)
36c30c5c   zhouahaihai   冒险
972
  	self:pushBackEvent(AdvBackEventType.Skill, {enemyId = enemyId, skillId = skillId, receiver = receiver})
46fac6f1   zhouahaihai   酱料
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
  end
  
  function Adv:backNext()
  	self:pushBackEvent(AdvBackEventType.Next, {})
  end
  
  function Adv:backEnd(score)
  	self:pushBackEvent(AdvBackEventType.End, {score = score})
  end
  
  function Adv:backBlockChange(roomId, blockId)
  	self:pushBackEvent(AdvBackEventType.BlockChange, {roomId = roomId, blockId = blockId})
  end
  
  function Adv:backAtk(enemyId, receiver)
  	self:pushBackEvent(AdvBackEventType.Atk, {enemyId = enemyId, receiver = receiver})
  end
  
ec87b4a5   zhouahaihai   冒险 完善
991
  
46fac6f1   zhouahaihai   酱料
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  function Adv:popBackEvents()
  	local events = self.backEvents
  	self.backEvents = {}
  	return events
  end
  
  --回合事件处理
  function Adv:afterRound()
  	if self.battle then
  		self.battle:afterRound()
  	end
  end
  
36c30c5c   zhouahaihai   冒险
1005
1006
1007
1008
1009
1010
1011
  function Adv:saveDB()
  	if self.battle then
  		self.battle:getDB()
  	end
  	self.owner:updateProperties({advInfo = self.advInfo, advTeam = self.advTeam})
  end
  
46fac6f1   zhouahaihai   酱料
1012
  return Adv