Adv.lua 28.3 KB
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 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 378 379 380 381 382 383 384 385 386 387 388 389 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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 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 498 499 500 501 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 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 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 595 596 597 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 623 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 654 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 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 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 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 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 871 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 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
local Passive = require "adv.AdvPassive"

local AdvCommon = require "adv.AdvCommon"
local AdvMap = require "adv.AdvMap"
local Buff = require "adv.AdvBuff"

local Adv = class("Adv")
local AdvTask = import(".AdvTask")  --任务相关数据搞出去
AdvTask.bind(Adv)

function Adv:ctor(owner)
	assert(owner, "Adv instance must have owner(role)")
	self.owner = owner
	self.maps = {}
	self.battle = nil
	self.backEvents = {} --发给客户端的事件组

	self.advTask = self.owner:getProperty("advTask")
	self.advMTask = self.owner:getProperty("advMTask")
	self.advTaskChange = false -- 任务改变才更新

	self:initByInfo(self.owner:getProperty("advInfo"))
end

--初始化adv 信息
function Adv:initByInfo(advInfo)
	if not next(advInfo) then return end --还没有 开始新地图

	self.chapterId = advInfo.chapterId
	self.level = advInfo.level or 1
	self.round = advInfo.round or 0
	self.score = advInfo.score or {}
	self.lastEnemyId = advInfo.lastEId or 1
	self.mapStack = advInfo.mstack or {}
	self.lchoose = advInfo.lch or {}
	self.maps = {}
	for id, map in ipairs(advInfo.maps or {}) do
		self.maps[id] = AdvMap.new(self, id, map)
	end

	self:initBattle()
end

-- 随机新的地图
function Adv:initByChapter(chapterId, level, isToNext, notNotify)
	if not self.chapterId then -- 开始新的章节
		self.chapterId = chapterId
		self:checkAchievement(Adv.AchievType.StartBattle, 1)
	elseif chapterId ~= self.chapterId then  -- 正常不会出现
		return 
	end
	
	self.level = level or 1
	self.round = 0
	self.score = self.score or {}
	self.lastEnemyId = 1
	self.mapStack = {1} -- 最后一个为当前的地图
	self.lchoose = self.lchoose or {}
	self.owner._advWheelSurfCount = nil -- 抽奖进行次数

	-- 随机出地图
	local mapId = self:randomMapId(chapterId, level)
	self.maps = {}
	self.maps[1] = AdvMap.new(self, 1, mapId)

	if isToNext then
		self:scoreChange(AdvScoreType.Level) --增加层级加分
	end

	self:initBattle()

	self:initLayerTask()

	self:checkTask(Adv.TaskType.Arrive)
	if not notNotify then
		self:saveDB(notNotify)
	end
end

function Adv:clear()
	self.chapterId = nil
	self.level = nil
	self.score = {}
	self.round = 0
	self.lastEnemyId = 1
	self.mapStack = {}
	self.lchoose = {}
	self.maps = {}
	self.battle = nil
end

function Adv:saveDB(notNotify)
	local advInfo,  advTeam = {}, self.owner:getProperty("advTeam")
	if self.chapterId then

		advInfo.chapterId = self.chapterId
		advInfo.level = self.level
		advInfo.round = self.round
		advInfo.score = self.score
		advInfo.lastEId = self.lastEnemyId
		advInfo.mstack = self.mapStack
		advInfo.lch = self.lchoose
		advInfo.maps = {}

		self.battle:saveDB()

		for id , map in ipairs(self.maps) do
			advInfo.maps[id] = map:getDB()
		end

		advTeam.player = self.battle.player:getDB()

	else
		advTeam.player = nil
	end

	self:updateTask(notNotify)
	self:updateAchievement(notNotify)
	self.owner:updateProperties({advInfo = advInfo, advTeam = advTeam}, notNotify)
end

function Adv:initBattle()
	self.battle = require("adv.AdvBattle").new(self)
	for _, passiveC in ipairs(self.cachePassiveEvent or {}) do
		self.battle:triggerPassive(passiveC[1], passiveC[2])
	end
	self.cachePassiveEvent = {}
end

function Adv:triggerPassive(condType, params)
	self.cachePassiveEvent = self.cachePassiveEvent or {}
	if not self.battle then
		table.insert(self.cachePassiveEvent, {condType, params})
	else
		self.battle:triggerPassive(condType, params)
	end
end

function Adv:getCurMap()
	return self.maps[self.mapStack[#self.mapStack]]
end

function Adv:getCurMapIdx()
	return self.mapStack[#self.mapStack]
end

function Adv:getRoom(roomId, mapIdx)
	mapIdx = mapIdx or self:getCurMapIdx()
	local map = self.maps[mapIdx]
	if map then
		return map.rooms[roomId]
	end
end

function Adv:getBlock(roomId, blockId, mapIdx)
	local room = self:getRoom(roomId, mapIdx)
	if room then
		return room.blocks[blockId]
	end
end



--关卡通关,非层 score < 0 失败
function Adv:over(success, isAllPass)
	local score = self:getScore()
	local scoreInfo = self.score
	local reward
	if success then
		reward = self.owner:award(self.owner:getProperty("advItems"):toNumMap())
		self.owner:checkTaskEnter("AdvPass", {id = self.chapterId, level = self.level, score = score})
		if isAllPass then
			self.owner:checkTaskEnter("AdvAllPass", {id = self.chapterId})
		end

		-- 冒险队等级升一下子
		local advL = self.owner:getProperty("advL")
		local oldL = advL[1]
		advL[2] = (advL[2] or 0) + 1
		for level = ((advL[1] or 0) + 1), #csvdb["adv_rankCsv"] do
			local ldata = csvdb["adv_rankCsv"][level]
			if ldata.unlock <= advL[2] then
				advL[1] = level
				self.owner:award(ldata.reward)
			end
		end
		if advL[1] > oldL then
			self.owner:checkTaskEnter("AdvLevel", {level = advL[1]})
		end
		self.owner:updateProperty("advL", advL)

		if self.owner:isFuncOpen(FuncOpenType.AdvWheelSurf) then
			self.owner._advWheelSurfCount = 0 -- 抽奖进行次数
		end
	end
	self:clear()
	
	self.owner:updateProperty({field = "advItems", value = ""})

	self:backEnd(success, score, scoreInfo, reward)
end

function Adv:exit()
	self:over(false)
	self:saveDB()
end

function Adv:randomMapId(chapterId, level)
	local chapterData = csvdb["adv_chapterCsv"][chapterId]
	if not chapterData then 
		error("chapterId " .. chapterId .. " dont exist!")
		return 
	end
	if AdvCommon.isEndless(chapterId) then
		level = AdvCommon.getEndlessDataLv(chapterId, level)
	else
		if level > chapterData.limitlevel then 
			error("level overflow!")
			return 
		end
	end
	
	--随出地图Id
	local raw_pool = chapterData.mapid:toArray(true, "=")

	local lastMapIds = {}
	for id, map in ipairs(self.maps or {}) do
		lastMapIds[map.mapId] = 1
	end
	
	local pool = {}
	for _, mapId in ipairs(raw_pool) do
		local temp = csvdb["mapCsv"][mapId]
		if temp and not lastMapIds[mapId] then
			if AdvCommon.checkIsIn(level, temp.leveltype, temp.levellimit) then
				table.insert(pool, mapId)
			end
		end
	end
	if not next(pool) then 
		error("mapIds is empty!")
		return 
	end
	return pool[math.randomInt(1, #pool)]
end


-- 在冒险中获得的物品都发放在冒险背包内
function Adv:award(gift, params)
	params = params or {}
	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
		if count > 0 then
			self:scoreChange(AdvScoreType.Item, {itemId, count})
			self:checkTask(Adv.TaskType.Item, count, itemId)
			self:checkAchievement(Adv.AchievType.GetItem, count, itemId)
		end
		local origin = items:getv(itemId, 0)
		local nums = origin + count
		if csvdb["adv_artifactCsv"][itemId] then
			nums = self:checkArtifact(itemId, origin, count, nums)
		end
		if nums <= 0 then
			items = items:delk(itemId)
			nums = 0
		else
			items = items:setv(itemId, nums)
		end
	end

	self.owner:updateProperty({field = "advItems", value = items, notNotify = params.notNotify})
	return tgift
end


function Adv:delArtifactEffect(effectType, effects)
	if effectType == 1 then
		for _, id in ipairs(effects:toArray(true, "=")) do
			self.battle.player:delPassiveById(id)
		end
	elseif effectType == 2 then
		for _, id in ipairs(effects:toArray(true, "=")) do
			self.battle.player:delBuffById(id)
		end
	end
end

function Adv:addArtifactEffect(effectType, effects)
	if effectType == 1 then
		for _, id in ipairs(effects:toArray(true, "=")) do
			self.battle.player:addPassive({id = id})
		end
	elseif effectType == 2 then
		for _, id in ipairs(effects:toArray(true, "=")) do
			self.battle.player:addBuff(id)
		end
	end
end
-- 检查神器
function Adv:checkArtifact(itemId, origin, count, nums)
	local artifactData = csvdb["adv_artifactCsv"][itemId]
	if count == 0 or not artifactData then return nums end
	local advItems = self.owner:getProperty("advItems")
	if count < 0 then --删除
		nums = 0
		local curData = artifactData[origin]
		if curData then
			-- 删除自己的效果
			self:delArtifactEffect(curData.type, curData.effect)

			--删除组合效果
			if curData.comboId ~= 0 then
				local comboLv = advItems:getv(curData.comboId, 0)
				if comboLv ~= 0 then
					--删除自己的组合效果
					if curData.comboType == 1 or curData.comboType == 2 then
						self:delArtifactEffect(curData.comboType, curData.comboEffect)
					elseif curData.comboType == 3 then
						self:delArtifactEffect(curData.type, curData.comboEffect)
					end

					-- 删除组合的组合效果
					local comboData = (csvdb["adv_artifactCsv"][curData.comboId] or {})[comboLv]
					if comboData then
						if comboData.comboType == 1 or comboData.comboType == 2 then
							self:delArtifactEffect(comboData.comboType, comboData.comboEffect)
						elseif comboData.comboType == 3 then
							self:delArtifactEffect(comboData.type, comboData.comboEffect)
							self:addArtifactEffect(comboData.type, comboData.effect)
						end
					end
				end
			end
		end
	else
		nums = math.max(0, math.min(nums, #artifactData))
		if nums == origin then return nums end
		if origin == 0 then --初始获得
			local curData = artifactData[nums]
			local addSelfEffect = true
			--查看是否有组合
			if curData.comboId ~= 0 then
				local comboLv = advItems:getv(curData.comboId, 0)
				if comboLv ~= 0 then
					--自己的组合效果
					if curData.comboType == 1 or curData.comboType == 2 then
						self:addArtifactEffect(curData.comboType, curData.comboEffect)
					elseif curData.comboType == 3 then
						self:addArtifactEffect(curData.type, curData.comboEffect)
						addSelfEffect = false
					end

					--对方的组合效果
					local comboData = (csvdb["adv_artifactCsv"][curData.comboId] or {})[comboLv]
					if comboData then
						if comboData.comboType == 1 or comboData.comboType == 2 then
							self:addArtifactEffect(comboData.comboType, comboData.comboEffect)
						elseif comboData.comboType == 3 then
							self:delArtifactEffect(comboData.type, comboData.effect)
							self:addArtifactEffect(comboData.type, comboData.comboEffect)
						end
					end
				end
			end
			if addSelfEffect then
				self:addArtifactEffect(curData.type, curData.effect)
			end
		else --升级
			local originData = artifactData[origin]
			local curData = artifactData[nums]

			if originData then
				self:delArtifactEffect(originData.type, originData.effect)
			end

			if curData then 
				self:addArtifactEffect(curData.type, curData.effect)
			end
		end
	end
	return nums
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)
	if self:getCurMap():checkOver() then --检查是否可以出去了
		if #self.mapStack > 1 then -- 处于夹层中
			table.remove(self.mapStack) --退出夹层
			self:backLayer()
		else --处于底层

			local advPass = self.owner:getProperty("advPass")

			if AdvCommon.isEndless(self.chapterId) then
				-- 刷新最高层
				if self.owner:getProperty("advElM") < self.level then
					self.owner:updateProperty({field = "advElM", value = self.level})
				end
			else
				if self.level > (advPass[self.chapterId] or 0) then
					self.owner:changeUpdates({{type = "advPass", field = self.chapterId, value = self.level}})
				end
			end
			if params.relay then
				if self.level % globalCsv.adv_can_out_layer_pre ~= 0  or not self.owner:isFuncOpen(FuncOpenType.AdvRelay) then return end
			end
			
			self:checkAchievement(Adv.AchievType.OverWin, 1, self.level)
			local levellimit = csvdb["adv_chapterCsv"][self.chapterId].limitlevel
			if params.relay or (not AdvCommon.isEndless(self.chapterId) and (self.level >= levellimit or not self.owner:advChapterIsOpen(self.chapterId, self.level + 1))) then --关卡结束
				self:over(true, not AdvCommon.isEndless(self.chapterId) and self.level >= levellimit)
			else
				self:initByChapter(self.chapterId, self.level + 1, true, true)
				self:backNext() --下一关
			end

		end
		
		return true
	end
end

--战斗 普通攻击
local function clickMonster(self, room, block, params)
	self.battle:battleBegin(room.roomId, block.blockId, params)
	return true
end

local function chooseCommon(self, room, block, chooseData, choose)
	if not chooseData or not chooseData["button".. choose .."cond"] then return end

	local cond = chooseData["button".. choose .."cond"]:toArray(true, "=")
	local checkCond = {
		-- 没有条件
		[0] = function()
			return true
		end,
		-- 拥有道具
		[1] = function()
			if self:cost({[cond[2]] = cond[3]}, {}, true) then
				return true
			end
		end,
		-- xx角色(todo 队长)
		[2] = function()
			local hero = self.owner.heros[self.owner:getProperty("advTeam").leader]
			if hero and hero:getProperty("type") == cond[2] then
				return true
			end
		end,
		--消灭所有怪
		[3] = function()
			for _, room in pairs(self:getCurMap().rooms) do
				for _, block in pairs(room.blocks) do
					if block:isMonster() 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,
		-- 提交一个物品
		[5] = function ()
			if self:cost({[cond[2]] = cond[3]}, {}) then
				return true
			end
		end,
		-- sp 到达指定值
		[6] = function()
			if self.battle.player.sp >= cond[2] then
				return true
			end
		end,
	}
	assert(not cond[1] or checkCond[cond[1]], "error cond, event_[link]chooseCsv id :" .. block.event.id)

	if cond[1] and not checkCond[cond[1]]() then return end
	local clearBlock = chooseData.keep ~= 1
	local effects = chooseData["button".. choose .."effect"]:toTableArray(true)
	for _, effect in ipairs(effects) do
		if effect[1] == 1 then
			local reward = csvdb["event_dropCsv"][effect[2]]["range"]:randWeight(true)
			effect[2] = reward[1]
			effect[3] = reward[2]
		end

		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() --发现怪物
				self:getCurMap():addNewMonsterRand(effect[2], {room, block})
				clearBlock = false
			end,
			[4] = function() --无事发生
			end
		}
		assert(doEffect[effect[1]], "error effect, event_[link]chooseCsv id :" .. block.event.id)
		doEffect[effect[1]]()
	end
	self:checkTask(Adv.TaskType.Choose, 1, block.event.id)
	self:checkAchievement(Adv.AchievType.Choose, 1, block.event.id)
	return true, clearBlock
end

local function clickChoose(self, room, block, params)
	local choose = params.choose
	local chooseData = csvdb["event_chooseCsv"][block.event.id]
	local status, clearBlock = chooseCommon(self, room, block, chooseData, choose)
	if not status then return end

	if clearBlock then
		block:clear()
	end
	return true
end

local function clickLinkChoose(self, room, block, params)
	local choose = params.choose
	local chooseData = csvdb["event_linkchooseCsv"][block.event.id]
	local status, clearBlock = chooseCommon(self, room, block, chooseData, choose)
	if not status then return end

	-- 完成统计次数
	local idx = block.event.id % 10
	if idx == 9 or not csvdb["event_linkchooseCsv"][block.event.id + 1] then --全部完成
		local startId = math.floor(block.event.id / 10) * 10 + 1
		self.lchoose[startId] = (self.lchoose[startId] or 0) + 1
		self:checkAchievement(Adv.AchievType.LinkChoose, 1, startId)
	else
		self.lchoose.ing = block.event.id + 1  --后面会出现后继事件
	end

	if clearBlock then
		block:clear()
	end

	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]})
	-- local reward = self:award({[5801] = 1})
	block:clear()
	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] = block.event.shop[buyId][3]}, {}) 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:checkTask(Adv.TaskType.Shop, 1, block.event.id)
	self:checkAchievement(Adv.AchievType.Shop, 1, block.event.id)
	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 -- 没有效果 气人不
	local clearBlock = true
	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() --发现怪物
			self:getCurMap():addNewMonsterRand(effect[2], {room, block})
			clearBlock = false
		end,
		[4] = function() --无事发生
		end
	}
	assert(doEffect[effect[1]], "error effect, event_buildingCsv id :" .. block.event.id)
	if not self:cost(buildData.required:toNumMap(), {}) then return end
	doEffect[effect[1]]()
	self:checkTask(Adv.TaskType.Build, 1, block.event.id)
	self:checkAchievement(Adv.AchievType.Build, 1, block.event.id)
	if clearBlock then
		block:clear()
	end
	return true
end

local function clickClick(self, room, block, params)
	local clickData = csvdb["event_clickCsv"][block.event.id]
	if not clickData then return end
	local clearBlock = true
	local doEffect = {
		[1] = function() -- 技能
			for _, skillId in ipairs(clickData.effect:toArray(true, "=")) do
				self.battle.player:releaseSkill(skillId)
			end
		end,
		[2] = function() -- dropId
			local reward = {}
			for _, dropId in ipairs(clickData.effect:toArray(true, "=")) do
				local item = csvdb["event_dropCsv"][dropId]["range"]:randWeight(true)
				reward[item[1]] = (reward[item[1]] or 0) + reward[item[2]]
			end
			self:backReward(self:award(reward, {}))
		end,
	}
	if clearBlock then
		block:clear()
	end
	return true
end

local function clickLayer(self, room, block, params)
	if block.event.mapIdx then
		table.insert(self.mapStack, block.event.mapIdx) --进入夹层
	else
		--生成夹层
		local mapId = csvdb["event_layerCsv"][block.event.id].effect
		local mapIdx = #self.maps + 1
		block.event.mapIdx = mapIdx
		table.insert(self.mapStack, mapIdx)

		self.maps[mapIdx] = AdvMap.new(self, mapIdx, mapId)
		self.battle:initMapEnemys(mapIdx)
		self:checkAchievement(Adv.AchievType.EnterILayer, 1, mapId)
	end
	self:backLayer()
	return true
end

local function clickTask(self, room, block, params)
	if self:addTask(block.event.id) then --增加任务
		block:clear()
		return true
	end
end

local eventCallFunc = {
	[AdvEventType.Out] = clickOut,
	[AdvEventType.BOSS] = clickMonster,
	[AdvEventType.Monster] = clickMonster,
	[AdvEventType.Choose] = clickChoose,
	[AdvEventType.LinkChoose] = clickLinkChoose,
	[AdvEventType.Drop] = clickDrop,
	[AdvEventType.Trader] = clickTrader,
	[AdvEventType.Build] = clickBuild,
	[AdvEventType.Click] = clickClick,
	[AdvEventType.Layer] = clickLayer,
	[AdvEventType.Task] = clickTask,
}

--点击处理 roomId, blockId 
--params 某些事件需要的客户端传递的参数
function Adv:clickBlock(roomId, blockId, params)
	local map = self:getCurMap()
	local room = self:getRoom(roomId)
	local block = self:getBlock(roomId, blockId)
	if not block then return end

	local status = false
	local clickEvent = false
	if not block.isOpen then
		local canOpen = false  --如果未开放是否可以开放
		local hadMonster = false -- 周围是否有解锁的怪未击败
		for _, one in ipairs(map:getAroundBlocks(room, block)) do
			local _room, _block = one[1], one[2]
			if _block.isOpen then canOpen = true end
			if _block.isOpen and _block:isMonster() then 
				hadMonster = true 
			end
		end
		if canOpen and not hadMonster then --开放
			room:openBlock(block)
			status = true
		end
	else
		clickEvent = true
		--点了空地
		if not block.event then
			return 
		end
		--可点击的事件
		if not room.isBossRoom or block:isBoss() then
			if eventCallFunc[block.event.etype] then
				status = eventCallFunc[block:getEventType()](self, room, block, params)
			end
		end
	end
	local needChange = true
	if clickEvent and block.event then
		if block:getEventType() == AdvEventType.Out then
			needChange = false
		end
	end
	if status and needChange then  --出去了就不计算回合了
		self:backBlockChange(roomId, blockId)
		self:afterRound()
	end
	self:saveDB()
	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, 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:checkAchievement(Adv.AchievType.UseItem, count, itemId)

	self:afterRound()
	self:saveDB()
	return true
end

--使用技能
function Adv:usePotion(potionId, potionLevel, target)
	--  cost
	local potionData = csvdb["adv_potionCsv"][potionId][potionLevel]
	local enemy = self.battle:getEnemy(target.roomId, target.blockId)
	if not enemy then return end
	--生效
	if potionData.type == 1 or potionData.type == 0 then --技能
		self.battle.player:releaseSkill(potionData.effect, enemy)
	elseif potionData.type == 2 then	--掉落
		local item = csvdb["event_dropCsv"][potionData.effect]["range"]:randWeight(true)
		self:backReward(self:award({[item[1]] = item[2]}, {}))
	else
		return
	end
	self:afterRound()
	self:saveDB()
	self.owner:checkTaskEnter("AdvUsePotion")
	return true
end

-- 地图上物品变化
function Adv:mapItemChange(ctype)
	local clist = csvdb["transform_itemCsv"][ctype]
	if clist then
		for roomId, room in pairs(self:getCurMap().rooms) do
			for blockId, block in pairs(room.blocks) do
				if block:getEventType() == AdvEventType.Drop and block.event.item then
					local id = block.event.item[1]
					local changeTo = nil
					if clist[id] then
						changeTo = {clist[id].toId, clist[id].num}
					elseif clist[-1] then
						changeTo = {clist[-1].toId, clist[-1].num}
					end
					if changeTo and changeTo[1] ~= 0 and changeTo[2] ~= 0 then
						block.event.item = changeTo
						self:backBlockChange(roomId, blockId)
					end
				end
			end
		end
	end
end

--敌人死亡
function Adv:enemyDead(enemy, escape)
	local roomId, blockId = enemy.roomId, enemy.blockId
	local map = self:getCurMap()
	local room = self:getRoom(roomId)
	local block = self:getBlock(roomId, blockId)
	if not block then return end
	--死了以后掉东西
	if block:isMonster() then --处理死亡
		if block:isBoss() then
			room.isBossRoom = false
		end
		if escape then
			block:clear()
		else
			local enemyId = block.event.id
			local monsterData = csvdb["event_monsterCsv"][enemyId]
			self:scoreChange(AdvScoreType.Kill, monsterData.type)
			self.battle.player:addExp(monsterData.exp)
			local item = block.event.item
			if not item then
				local buff = enemy:hadBuff(Buff.CHANGE_DROP) 
				if buff then
					item = table.pack(buff:effect())
				else
					local dropData = csvdb["event_dropCsv"][monsterData.dropid]
					item = dropData["range"]:randWeight(true)
				end
				
			end

			if item[1] == 0 then
				block:clear()
			else
				block:updateEvent({
					etype = AdvEventType.Drop,
					item = item
				})
			end
			self:checkTask(Adv.TaskType.Kill, 1, enemyId)
			self:checkTask(Adv.TaskType.KillAll)
			self:checkAchievement(Adv.AchievType.Kill, 1, enemyId)
		end
	end
	self:backBlockChange(roomId, blockId)
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

function Adv:backMiss(enemyId) 
	self:pushBackEvent(AdvBackEventType.Miss, {enemyId = enemyId})
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:backDefChange(enemyId, change)
	self:pushBackEvent(AdvBackEventType.DefChange, {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)
	self:pushBackEvent(AdvBackEventType.Skill, {enemyId = enemyId, skillId = skillId, receiver = receiver})
end

-- if is player enemyId is nil 
function Adv:backPassive(enemyId, passiveId)
	self:pushBackEvent(AdvBackEventType.Passive, {enemyId = enemyId, passiveId = passiveId})
end

function Adv:backNext()
	self:pushBackEvent(AdvBackEventType.Next, {})
end

function Adv:backEnd(success, score, scoreInfo, reward)
	self:pushBackEvent(AdvBackEventType.End, {success = success, score = score, scoreInfo = scoreInfo, reward = reward})
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

function Adv:backDead(enemyId)
	self:pushBackEvent(AdvBackEventType.Dead, {enemyId = enemyId})
end

function Adv:backTrap()
	self:pushBackEvent(AdvBackEventType.Trap, {})
end

function Adv:backLayer()
	self:pushBackEvent(AdvBackEventType.Layer, {})
end

function Adv:scoreChange(scoreType, pms)
	local cutTypes = {}
	local score = 0
	cutTypes[AdvScoreType.Level] = function()
		score = globalCsv.adv_score_floor
	end
	cutTypes[AdvScoreType.Kill] = function()
		local chapterData = csvdb["adv_chapterCsv"][self.chapterId]
		score = globalCsv.adv_score_monster[pms] * chapterData["reward"]
	end
	cutTypes[AdvScoreType.Item] = function()
		score = csvdb["itemCsv"][pms[1]].adv_score_item * pms[2]
	end
	cutTypes[AdvScoreType.Hurt] = function()
		score = globalCsv.adv_score_hurt * pms
	end
	cutTypes[AdvScoreType.Block] = function()
		score = globalCsv.adv_score_block
	end
	if cutTypes[scoreType] then
		cutTypes[scoreType]()
	else
		return
	end
	self.score[scoreType] = self.score[scoreType] or 0
	self.score[scoreType] = self.score[scoreType] + score
end

function Adv:getScore()
	self.score[AdvScoreType.Level] = math.floor(self.score[AdvScoreType.Level] or 0)
	self.score[AdvScoreType.Block] = math.floor(self.score[AdvScoreType.Block] or 0)
	self.score[AdvScoreType.Hurt] = math.max(math.floor(self.score[AdvScoreType.Hurt] or 0),  - (self.score[AdvScoreType.Level] + self.score[AdvScoreType.Block]))
	self.score[AdvScoreType.Kill] = math.floor(self.score[AdvScoreType.Kill] or 0)
	self.score[AdvScoreType.Item] = math.floor(self.score[AdvScoreType.Item] or 0)

	return self.score[AdvScoreType.Level] + self.score[AdvScoreType.Block] + self.score[AdvScoreType.Hurt]
		+ self.score[AdvScoreType.Kill] + self.score[AdvScoreType.Item]
end

function Adv:popBackEvents()
	local events = self.backEvents
	self.backEvents = {}
	return events
end

--回合事件处理
function Adv:afterRound()
	self.round = self.round + 1
	if self.battle then
		self.battle:afterRound()
	end
end

return Adv