AdvTask.lua 14 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
local AdvTask = {}
local AdvCommon = require "adv.AdvCommon"

local advTaskChange = {}
function AdvTask.bind(Adv)
	Adv.TaskType = {
		Arrive 		= 1, --到达N层
		Kill		= 2, --消灭指定怪物N个
		Item		= 3, --获得指定道具N个
		Choose		= 4, --完成指定抉择点N次
		Shop		= 5, --和指定商人交易N次
		Build		= 6, --和指定建筑交互N次
		KillAll		= 7, --消灭本层所有怪物
		KillElite	= 8, --击杀精英怪
		KillBoss	= 9, --击杀Boss
		RelayOpen = 10, -- 解锁中继营地
		FinishStoryId = 11, -- 完成故事
		Click = 12, -- 点击点击事件
	}

	-- 检查任务状态 在新领取任务的时候回进行检查
	local function checkTaskStatus(self, taskData, status, count, cond)
		count = count or 0
		if status == -1 or not taskData then
			return
		end
		local function checkCondValue()
			if taskData.value2 ~= "" then
				local conds = taskData.value2:toArray(true, "=")
				for _, one in pairs(conds) do
					if one == cond then
						return true
					end
				end
				return false
			end
			return true
		end	

		local checkTaskFunc = {}
		checkTaskFunc[Adv.TaskType.Arrive] = function()
			if self.level > status then
				if self.level >= taskData.value1 then
					return -1
				else
					return self.level
				end
			end
		end
		checkTaskFunc[Adv.TaskType.KillAll] = function()
			if  #self.battle.player:getTeam(2, nil, nil, true) == 0 then
				return -1
			end
		end

		checkTaskFunc[Adv.TaskType.RelayOpen] = function()
			local limit = tonum(taskData.value2, 0)
			if self.level > limit or (self.level == limit and self.isRelay) then
				return -1
			end
		end

		if checkTaskFunc[taskData.condition] then
			status = checkTaskFunc[taskData.condition]()
		else
			if count == 0 or not checkCondValue() then return end --没有变化
			status = status + count
			if status >= taskData.value1 then
				status = -1
			end
		end
		return status
	end 

	function Adv:initLayerTask()
		self.advTask = {}
		advTaskChange.t = true
		advTaskChange.m = true
		if not next(self.advMTask) then
			self.advMTask = {
				id = 1,
				status = 0,
				lock = 1,
			}
		end
		self.advMTask.id = self.advMTask.id or 1
		local mainTaskData = csvdb["adv_questCsv"][self.advMTask.id]
		if mainTaskData and self.advMTask.lock then 
			if self.chapterId == mainTaskData.chapter and self.level >= mainTaskData.level then --到达指定关卡解锁当前任务
				self.advMTask.lock = nil
				self.advMTask.status = 0
				local ts = checkTaskStatus(self, mainTaskData, self.advMTask.status, 0)
				if ts then
					self.advMTask.status = ts
				end
			end
		end
		self:addTask()
		self:checkTask(Adv.TaskType.RelayOpen) --每一层都检查一下
	end

	function Adv:checkTask(taskType, count, cond)
		local chapter = self.chapterId
		for taskId, status in pairs(self.advTask) do
			local taskData = csvdb["event_questCsv"][taskId]
			if taskData and taskData.levelchapter == chapter and taskType == taskData.condition then
				local ts = checkTaskStatus(self, taskData, status, count, cond)
				if ts then
					self.advTask[taskId] = ts
					advTaskChange.t = true
				end
			end
		end

		local mainTaskData = csvdb["adv_questCsv"][self.advMTask.id]
		if not self.advMTask.lock and mainTaskData and mainTaskData.chapter == chapter and taskType == mainTaskData.condition then
			local ts = checkTaskStatus(self, mainTaskData, self.advMTask.status, count, cond)
			if ts then
				self.advMTask.status = ts
				advTaskChange.m = true
			end
		end
	end

	-- 点击任务地块领取任务
	function Adv:addTask()
		if self.isRelay or self.haveBoss then return end
		
		local chapterId, level = self.chapterId, self.level
		if self:isEndless() then
			level = AdvCommon.getEndlessDataLv(chapterId, level)
		end
		local advEventOpenStatus = self.owner:advEventOpenStatus()
		local pool = {}

		local mapData = self:getCurMap():getMapInfoCsv()
		for _, id in ipairs(mapData.quest:toArray(true, "=")) do
			local data = csvdb["event_questCsv"][taskId]
			if data then
				pool[id] = {showup = data.showup}
			end
		end

		if not next(pool) then
			for id, data in pairs(csvdb["event_questCsv"]) do
				if data.levelchapter == chapterId and (data.unlockType == 0 or (advEventOpenStatus[etype] or {})[data.unlockType]) then
					if AdvCommon.checkIsIn(level, data.leveltype, data.levellimit) then
						pool[id] = {showup = data.showup}
					end
				end
			end
		end

		if not next(pool) then return end

		local taskId = math.randWeight(pool, "showup")
		local taskData = csvdb["event_questCsv"][taskId]
		if taskData then
			self.advTask[taskId] = 0
			local ts = checkTaskStatus(self, taskData, self.advTask[taskId], 0)
			if ts then
				self.advTask[taskId] = ts
			end

			advTaskChange.t = true
			self:pushBackEvent(AdvBackEventType.Task, {id = taskId})
			return true
		end
	end

	--完成层任务
	function Adv:finishTask(taskId)
		local ok, reward
		if self.advTask[taskId] and self.advTask[taskId] == -1 then
			local taskData = csvdb["event_questCsv"][taskId]
			if not taskData then return end
			local curFloorData = self:getCurFloorData()
			if not curFloorData then return end

			reward = {[ItemId.AdvPoint] = curFloorData.questAward}

			local item = csvdb["event_dropCsv"][taskData.drop]["range"]:randWeight(true)
			reward[item[1]] = (reward[item[1]] or 0) + item[2]
			reward = self:award(reward, {log = {desc = "finishTask", int1 = taskId}})
			
			self:scoreChange(AdvScoreType.Task, taskData.advScore) --增加加分
			
			self.advTask[taskId] = nil
			ok = true
			advTaskChange.t = true
		end
		return ok, reward
	end

	-- 完成主线任务
	function Adv:finishMTask()
		local ok, reward, change
		if self.advMTask.status == -1 then  --已完成带领取
			local mainTaskData = csvdb["adv_questCsv"][self.advMTask.id]
			if not mainTaskData then return end
			if mainTaskData.reward == 1 then
				reward, change = self.owner:award(mainTaskData.rewardValue, {log = {desc = "advMainTask", int1 = self.advMTask.id}})
			end
			self.advMTask.id = self.advMTask.id + 1
			self.advMTask.status = 0
			local nextTaskData = csvdb["adv_questCsv"][self.advMTask.id]
			if not nextTaskData or self.chapterId ~= nextTaskData.chapter or self.level < nextTaskData.level then
				self.advMTask.lock = true
			else
				local ts = checkTaskStatus(self, nextTaskData, self.advMTask.status, 0)
				if ts then
					self.advMTask.status = ts
				end
			end
			ok = true
			advTaskChange.m = true
		end
		return ok, reward, change
	end

	function Adv:updateTask(notNotify)
		local properties = {}
		if advTaskChange.t then
			properties.advTask = self.advTask
		end
		if advTaskChange.m then
			properties.advMTask = self.advMTask
		end
		if next(properties) then
			self.owner:updateProperties(properties, notNotify)
		end
		advTaskChange = {}
	end



	------ 冒险成就 ------------
	Adv.AchievType = {
		StartBattle 		= 1, --累计挑战N次
		OverWin				= 2, --通关N次M层
		TaskLayer			= 3, --完成每层任务N次
		UseItem				= 4, --使用道具N次
		GetItem				= 5, --获得道具N个
		GetMWeapon			= 6, --获得神器N个
		Build				= 7, --完成建筑N个
		Choose				= 8, --完成事件N个
		Shop				= 9, --完成商店N次
		LinkChoose			= 10, --完成连锁事件N次
		Trap				= 11, --触发陷阱N次
		Kill				= 12, --消灭怪物N个
		EnterILayer			= 13, --进入夹层N次
		KillByBuff			= 14, --使用BUFF消灭敌人N个
		KillBoss			= 15, --击杀首领N次
		FinishStoryId		= 16, -- 完成故事Id
		MWeaponLv			= 17, -- 指定神器等级达到
		GetBuff				= 18, -- 获得指定buff
		KillHadBuff			= 19, -- 击败拥有指定buff的敌人
		ChooseBySelect		= 20, -- 指定事件的指定选项N次
		BuildBySelect		= 21, -- 指定建筑的指定选项N次
		KillWithBuff		= 22, -- 携带指定层数的buff 击败怪物
		KillBossWithBuff	= 23, -- 携带指定层数的buff 击败boss
		KillNoBuff			= 24, -- 不携带指定的buff 击败boss
		KillBossNoBuff		= 25, -- 不携带指定的buff 击败boss
		StorryDone			= 26, -- 故事完成个数
		KillWithMWeapon		= 27, -- 携带指定神器击败怪物N次
		KillBossWithMWeapon	= 28, -- 携带指定神器击败BossN次
		KillWithAMWeapon	= 29, -- 激活神器套装 击败怪物N次
		KillBossWithAMWeapon= 30, -- 激活神器套装 击败BossN次
	}



	local advAchievChange = {}

	local function insertChange(self, chapterId, taskId, value, pts)
		local achievField = AdvCommon.isEndless(chapterId) and "advEAchiev" or "advAchiev"
		local dbData = self.owner:getProperty(achievField)
		if pts then
			advAchievChange[achievField] = advAchievChange[achievField] or {}
			advAchievChange[achievField][chapterId] = advAchievChange[achievField][chapterId] or {}
			advAchievChange[achievField][chapterId]["pts"] = advAchievChange[achievField][chapterId]["pts"] or {}
			advAchievChange[achievField][chapterId]["pts"][taskId] = value

			dbData[chapterId] = dbData[chapterId] or {}
			dbData[chapterId]["pts"] = dbData[chapterId]["pts"] or {}
			dbData[chapterId]["pts"][taskId] = value
		else
			advAchievChange[achievField] = advAchievChange[achievField] or {}
			advAchievChange[achievField][chapterId] = advAchievChange[achievField][chapterId] or {}
			advAchievChange[achievField][chapterId][taskId] = value

			dbData[chapterId] = dbData[chapterId] or {}
			dbData[chapterId][taskId] = value
		end
	end

	function Adv:checkAchievement(taskType, count, cond, cond2)
		local achievField = self:isEndless() and "advEAchiev" or "advAchiev"
		local dbData = self.owner:getProperty(achievField)
		dbData[self.chapterId] = dbData[self.chapterId] or {}
		local advAchiev = dbData[self.chapterId]
		for taskId , data in pairs(csvdb["adv_achievementCsv"][self.chapterId] or {}) do
			local oldStatus = advAchiev[taskId] or 0
			if oldStatus ~= -1 and data.type == taskType then
				local status
				local checkTaskFunc = {}
				checkTaskFunc[Adv.AchievType.KillHadBuff] = function()
					if cond:hadBuffById(data.value2) then
						return (oldStatus + count)
					end
				end

				checkTaskFunc[Adv.AchievType.KillWithBuff] = function()
					local buff = self.battle.player:hadBuffById(data.value2)
					if buff and buff.layer == data.value3 then
						return (oldStatus + count)
					end
				end
				checkTaskFunc[Adv.AchievType.KillBossWithBuff] = checkTaskFunc[Adv.AchievType.KillWithBuff]

				checkTaskFunc[Adv.AchievType.KillNoBuff] = function()
					local buff = self.battle.player:hadBuffById(data.value2)
					if not buff then
						return (oldStatus + count)
					end
				end
				checkTaskFunc[Adv.AchievType.KillBossNoBuff] = checkTaskFunc[Adv.AchievType.KillNoBuff]

				checkTaskFunc[Adv.AchievType.KillWithMWeapon] = function()
					if self:isWearAF(data.value2) then
						return (oldStatus + count)
					end
				end
				checkTaskFunc[Adv.AchievType.KillBossWithMWeapon] = checkTaskFunc[Adv.AchievType.KillWithMWeapon]

				checkTaskFunc[Adv.AchievType.KillWithAMWeapon] = function()
					if self:haveComboAF(data.value2) then
						return (oldStatus + count)
					end
				end
				checkTaskFunc[Adv.AchievType.KillBossWithAMWeapon] = checkTaskFunc[Adv.AchievType.KillWithAMWeapon]

				checkTaskFunc[Adv.AchievType.StorryDone] = function()
					if data.value2 == 0 or data.value2 == cond then
						local advStoryB = self.owner:getProperty("advStoryB")
						local newCount = 0
						for storyId, _ in pairs(advStoryB) do
							if data.value2 == 0 then
								newCount = newCount + 1
							else
								local storyData = csvdb["event_linkchoose_storyCsv"][storyId]
								if storyData[1].chapter == data.value2 then
									newCount = newCount + 1
								end
							end
						end
						if newCount > oldStatus then
							return newCount
						end
					end
				end


				if checkTaskFunc[taskType] then
					status = checkTaskFunc[taskType]()
				else
					if count ~= 0 and (data.value2 == 0 or data.value2 == cond) and (data.value3 == 0 or data.value3 == cond2) then 
						status = oldStatus + count
					end
				end

				if self:isEndless() then
					if status and status ~= oldStatus then
						insertChange(self, self.chapterId, taskId, status)
					end
				else
					if (status or -1) >= data.value1 then
						status = -1
						self.owner:mylog("adv_action", {desc = "finishAchiev", short1 = 1, int1 = self.chapterId, int2 = taskId})
					end
					if status and status ~= oldStatus then
						insertChange(self, self.chapterId, taskId, status)
						if status == -1 then
							local ptcount = advAchiev[-1] or 0
							ptcount = ptcount + data.pt
							insertChange(self, self.chapterId, -1, ptcount)
						end
					end
				end
			end
		end
	end

	-- 说不用领取 注释掉
	-- 又说要领取 打开 修改
	function Adv:finishAchievement(chapterId, taskId)
		if not AdvCommon.isEndless(chapterId) then return end -- 暂时只有无尽可以领奖
		local achievField = AdvCommon.isEndless(chapterId) and "advEAchiev" or "advAchiev"
		local achievData = (csvdb["adv_achievementCsv"][chapterId] or {})[taskId]
		local status = (self.owner:getProperty(achievField)[chapterId] or {})[taskId] or -1

		local reward, change = {}
		if status >= achievData.value1 then
			insertChange(self, chapterId, taskId, -1)
			local count = (self.owner:getProperty(achievField)[chapterId] or {})[-1] or 0
			count = count + achievData.pt
			insertChange(self, chapterId, -1, count)

			-- 发放奖励
			reward, change = self.owner:award(achievData.reward, {log = {desc = "advAchiev", int1 = chapterId, int2 = taskId}})
			return true, reward, change
		end
	end

	function Adv:getAchievementReward(chapterId, taskId)
		local achievField = AdvCommon.isEndless(chapterId) and "advEAchiev" or "advAchiev"
		local count = (self.owner:getProperty(achievField)[chapterId] or {})[-1] or 0
		local achievData = (csvdb["adv_achievement_rewardCsv"][chapterId] or {})[taskId]
		local status = ((self.owner:getProperty(achievField)[chapterId] or {})["pts"] or {})[taskId] or 0
		if status == -1 or count < achievData.pt then return end

		local reward, change = self.owner:award(achievData.reward, {log = {desc = "advAchievReward", int1 = chapterId, int2 = taskId}})
		insertChange(self, chapterId, taskId, -1, true)
		return true, reward, change
	end

	function Adv:updateAchievement(notNotify)
		if not next(advAchievChange) then return end
		self.owner:changeMapUpdates(advAchievChange, notNotify)
		advAchievChange = {}
	end

end


return AdvTask