AdvBattle.lua 12.6 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
local Player, Enemy, Build = table.unpack(require "adv.AdvPlayer")
local Buff = require "adv.AdvBuff"
local Passive = require "adv.AdvPassive"
local Battle = class("Battle")
function Battle:ctor(adv)
	self.adv = adv
	self.player = nil --玩家
	self.isNewPlayer = false
	self.enemys = {} --怪
	self.builds = {} -- 建筑
	self.cachePassiveEvent = {}
	self:initPlayer()
	self:initEnemys()
	self:initAfter()
	if self.isNewPlayer then
		self.player:triggerPassive(Passive.BORN_ONCE)
	end
end

function Battle:initAfter(mapIdx)
	if not mapIdx then
		self.player:initAfter(self.adv.owner:getProperty("advTeam").player)
	end
	for idx, mapEnemys in pairs(self.enemys) do
		if not mapIdx or idx == mapIdx then
			for _, enemy in ipairs(mapEnemys) do
				enemy:initAfter(self.adv:getBlock(enemy.roomId, enemy.blockId, idx).event.enemy)
			end
		end
	end	
	for idx, mapBuilds in pairs(self.builds) do
		if not mapIdx or idx == mapIdx then
			for _, build in ipairs(mapBuilds) do
				build:initAfter(self.adv:getBlock(build.roomId, build.blockId, idx).event.build)
			end
		end
	end
end
--[[
队伍总属性 = 基础属性 + 等级 × 成长率 	
	基础属性:	固定属性,与章节、队伍够成、养成无关,由策划给出	
	等级:	根据进入时的关卡,获得一个【初始等级】;消灭怪物获得经验后,累计N点会提升等级	
	羁绊加成:	加成条件与“战斗”相同,加成目标改为【冒险队】	
			
	成长率:	由【队伍总生存力】决定	
			成长率 =(总生存力 / 80)^0.52 + INT(总角色等级 / 50)* 1
--]]

local function getAdvLvAttrUp(upAttrs, attrName, baseAttr)
	-- 1=冒险队属性;1=点数/百分比=属性枚举=参数;属性枚举(1=生命上限/2=魔法上限/3=攻击/4=防御);点数/百分比(0=点数/1=百分比)
	local Enem = {
		hp = 1,
		sp = 2,
		atk = 3,
		def = 4,
	}
	if not Enem[attrName] then return baseAttr end
	return baseAttr + (upAttrs[Enem[attrName]] or 0)
end

function Battle:initPlayer()
	local advTeam = self.adv.owner:getProperty("advTeam")
	if not advTeam.heros or not next(advTeam.heros) then return end
	local leaderPassive = {}
	local player = advTeam.player
	if not player then
		local advAddAttrs = self.adv.owner:getAdvLvAddAttrs()

		player = {}
		player.level = 1
		if self.adv.level ~= 1 then
			local relayData = self.adv:isHaveRelay()
			if relayData then
				player.level = relayData.level
			end
		end
		player.exp = 0
		player.sp = getAdvLvAttrUp(advAddAttrs, "sp", 100)
		player.growth = {}
		player.passives = {}

		for slot, heroId in pairs(advTeam.heros) do
			if heroId == advTeam.leader or heroId == advTeam.leader2 then
				local hero = self.adv.owner.heros[heroId]
				if hero then
					local unit = csvdb["unitCsv"][self.adv.owner.heros[heroId]:getProperty("type")]
					if unit.advCaptain ~= 0 then
						table.insert(player.passives, {id = unit.advCaptain, level = 1})
					end
				end
			end
		end

		local attrs = self.adv.owner:getTeamBattleInfo(advTeam).heros


		for attrName, _ in pairs(AdvAttsEnum) do
			for _, hero in pairs(attrs) do
				player[attrName] = (player[attrName] or 0) + hero[attrName]
			end
			player[attrName] = getAdvLvAttrUp(advAddAttrs, attrName, player[attrName]) * (globalCsv.adv_battle_attr_ratio[attrName] or 1)
			player.growth[attrName] = player[attrName] * (globalCsv.adv_battle_attr_growth_ratio[attrName] or 1)
			player[attrName] = player[attrName] + player.growth[attrName] * (player.level - 1)
		end

		player.hpMax = player.hp or 0
		self.isNewPlayer = true
		advTeam.player = player
	end
	self.player = Player.new(self, player)
end

function Battle:initEnemys()
	for idx, map in pairs(self.adv.maps) do
		self:initMapEnemys(idx)
	end
end

-- after 是否是 后创建的夹层
function Battle:initMapEnemys(mapIdx, after)
	self.enemys[mapIdx] = {}
	self.builds[mapIdx] = {}
	local map = self.adv.maps[mapIdx]
	if map then
		for _, room in pairs(map.rooms) do
			for _, block in pairs(room.blocks) do
				self:addEnemy(room, block, mapIdx, true)	
			end
		end
	end
	if after then
		self:initAfter(mapIdx)
	end
	if self.cachePassiveEvent[mapIdx] then
		for _, passiveC in ipairs(self.cachePassiveEvent or {}) do
			for _, enemy in ipairs(self.enemys[mapIdx]) do
				enemy:triggerPassive(passiveC[1], passiveC[2])
			end

			for _, build in ipairs(self.builds[mapIdx]) do
				build:triggerPassive(passiveC[1], passiveC[2])
			end
		end
	end
	self.cachePassiveEvent[mapIdx] = nil
end

function Battle:addEnemy(room, block, mapIdx, init)
	mapIdx = mapIdx or self.adv:getCurMapIdx()

	local isNew, player, data
	if block:isMonster() then
		if not block.event.enemy then
			local enemyCsv = csvdb["event_monsterCsv"][block.event.id]
			local enemy = {}
			local curFloorData = self.adv:getCurFloorData() or {}
			for attrName, _ in pairs(AdvAttsEnum) do
				enemy[attrName] = enemyCsv[attrName] * curFloorData[attrName]
			end
			enemy.passives = {}
			for _, id in ipairs(enemyCsv.mapPassive:toArray(true, "=")) do
				table.insert(enemy.passives, {id = id})
			end
			block.event.enemy = enemy
			isNew = true
		end
		if not block.event.mId then
			block.event.mId = self.adv.lastEnemyId
			self.adv.lastEnemyId = self.adv.lastEnemyId + 1
		end
		data = block.event.enemy
		player = Enemy.new(self, block.event.mId, block.event.id, room.roomId, block.blockId, not block.isOpen, block.event.enemy, mapIdx)
		table.insert(self.enemys[mapIdx], player)
	elseif block:isBuild() then
		if not block.event.build then
			local buildCsv = csvdb["event_buildingCsv"][block.event.id]
			local build = {}
			build.passives = {}
			for _, id in ipairs(buildCsv.passive:toArray(true, "=")) do
				table.insert(build.passives, {id = id})
			end
			block.event.build = build
			isNew = true
		end
		data = block.event.build
		player = Build.new(self, block.event.id, room.roomId, block.blockId, not block.isOpen, block.event.build, mapIdx)
		table.insert(self.builds[mapIdx], player)
	end 
	if not init then
		player:initAfter(data)

		-- 游戏中新创建的 加上地图 floor 效果
		if isNew then
			local passives, buffs = self:getMapEffect(2, true, mapIdx)
			for _, passive in ipairs(passives) do
				player:addPassive({id = passive})
			end

			for _, buff in ipairs(buffs) do
				enemy:addBuff(buff)
			end
		end
	end
	return player
end

function Battle:getEnemy(roomId, blockId, mapIdx)
	mapIdx = mapIdx or self.adv:getCurMapIdx()
	for _, enemy in ipairs(self.enemys[mapIdx] or {}) do
		if enemy.roomId == roomId and enemy.blockId == blockId then
			return enemy
		end
	end
end

function Battle:getBuild(roomId, blockId, mapIdx)
	mapIdx = mapIdx or self.adv:getCurMapIdx()
	for _, build in ipairs(self.builds[mapIdx] or {}) do
		if build.roomId == roomId and build.blockId == blockId then
			return build
		end
	end
end

function Battle:getEnemyById(id)
	for idx, mapEnemys in pairs(self.enemys) do
		for _, enemy in ipairs(mapEnemys) do
			if enemy.id == id then
				return enemy
			end
		end
	end
end

function Battle:getRBByEnemyId(enemyId)
	local enemy = self:getEnemyById(enemyId)
	return enemy.roomId, enemy.blockId, enemy.mapIdx
end


--触发全员被动技能
function Battle:triggerPassive(condType, params, mapIdx)
	mapIdx = mapIdx or self.adv:getCurMapIdx()
	self.player:triggerPassive(condType, params)
	if not self.enemys[mapIdx] then
		-- 缓存一下
		self.cachePassiveEvent[mapIdx] = self.cachePassiveEvent[mapIdx] or {}
		table.insert(self.cachePassiveEvent[mapIdx], {condType, params})
	else
		for _, enemy in ipairs(self.enemys[mapIdx]) do
			enemy:triggerPassive(condType, params)
		end
		for _, build in ipairs(self.builds[mapIdx]) do
			build:triggerPassive(condType, params)
		end
	end
end

-- 只是从战斗中移除  从地图中移除 在外面操作
function Battle:removeEnemyById(id)
	local mapIdx = self.adv:getCurMapIdx()
	for i = #self.enemys[mapIdx], 1, -1 do
		if self.enemys[mapIdx][i].id == id then
			local enemy = table.remove(self.enemys[mapIdx], i)
			enemy:clear()
			break
		end
	end
end
-- 只是从战斗中移除  从地图中移除 在外面操作
function Battle:removeBuildByPos(roomId, blockId)
	local mapIdx = self.adv:getCurMapIdx()
	for i = #self.builds[mapIdx], 1, -1 do
		if self.builds[mapIdx][i].roomId == roomId and self.builds[mapIdx][i].blockId == blockId then
			local build = table.remove(self.builds[mapIdx], i)
			build:clear()
			break
		end
	end
end

--回合
function Battle:afterRound()
	local mapIdx = self.adv:getCurMapIdx()

	table.sort(self.enemys[mapIdx], function(e1, e2)
		return e1.id < e2.id
	end)


	self.player:afterRound("buffBefore")
	for _, enemy in ipairs(self.enemys[mapIdx]) do
		enemy:afterRound("buffBefore")
	end
	for _, build in ipairs(self.builds[mapIdx]) do
		build:afterRound("buffBefore")
	end

	self.player:afterRound("passive")
	for _, enemy in ipairs(self.enemys[mapIdx]) do
		enemy:afterRound("passive")
	end
	for _, build in ipairs(self.builds[mapIdx]) do
		build:afterRound("passive")
	end

	self.player:afterRound("buffAfter")
	for _, enemy in ipairs(self.enemys[mapIdx]) do
		enemy:afterRound("buffAfter")
	end
	for _, build in ipairs(self.builds[mapIdx]) do
		build:afterRound("buffAfter")
	end



	self.player:clearRound()
	for _, enemy in ipairs(self.enemys[mapIdx]) do
		enemy:clearRound()
	end
	for _, build in ipairs(self.builds[mapIdx]) do
		build:clearRound()
	end
	for i = #self.enemys[mapIdx], 1, -1 do
		if self.enemys[mapIdx][i].isDead then
			local enemy = table.remove(self.enemys[mapIdx], i)
			self.adv:enemyDead(enemy, enemy.isDead == 1)
			enemy:clear()
		end
	end
	for i = #self.builds[mapIdx], 1, -1 do
		if self.builds[mapIdx][i].isDead then
			local build = table.remove(self.builds[mapIdx], i)
			
			local room = self.adv:getCurMap().rooms[build.roomId]
			if room then
				local block = room.blocks[build.blockId]
				if block then
					block:clear()
					self.adv:backBlockChange(build.roomId, build.blockId)
				end
			end
			build:clear()
		end
	end

	self.player:triggerPassive(Passive.AFTER_ROUND)

	if self.player.isDead then
		self.adv:over(false, nil, -2)
	end
end


function Battle:battleBegin(roomId, blockId, params)
	local enemy = self:getEnemy(roomId, blockId)
	if not enemy then return end
	local player = params.player
	if not player then return end
	-- 玩家没死就是怪死了
	if player.hp > 0 then
		enemy:kill()
		self.player:effectBattleBuff()

		self.adv.owner:checkTaskEnter("AdvBattleWin", {id = self.adv.chapterId})
		if params.bySkill then
			self.player:triggerPassive(Passive.SKILL_KILL)
		end
	end
	if player.hp > self.player.hp then
		self.player:recover(player.hp - self.player.hp, player)
	else
		self.player:hurt(math.max(0, math.ceil(self.player.hp - player.hp)), enemy, {hurtType = 5}) --战斗血量只会变少
	end
	
	self.player:changeSp(math.floor(player.sp - self.player.sp) , 0) --战斗魔力只会变少
end


function Battle:getMapEffect(ctype, ilayer, mapIdx)
	mapIdx = mapIdx or self.adv:getCurMapIdx()
	local mapData = self.adv.maps[mapIdx]:getMapInfoCsv() or {}
	local floorData = self.adv:getCurFloorData() or {}

	local passives = {}
	local buffs = {}

	for _type, temp in ipairs({mapData, floorData}) do
		for _, one in ipairs((temp.passive or ""):toTableArray(true)) do
			passives[one[1]] = passives[one[1]] or {}
			if ilayer and one[1] == 1 and _type == 2 then
			else
				table.insert(passives[one[1]], one[2])
			end
		end
		for _, one in ipairs((temp.buff or ""):toTableArray(true)) do
			buffs[one[1]] = buffs[one[1]] or {}
			if ilayer and one[1] == 1 and _type == 2 then
			else
				table.insert(buffs[one[1]], one[2])
			end
		end
	end
	if ctype then
		passives = passives[ctype] or {}
		buffs = buffs[ctype] or {}
	end
	return passives, buffs
end
-- 夹层玩家 不重复加 floor
function Battle:initMapEffect(ilayer)
	local passives, buffs = self:getMapEffect(nil, ilayer)
	local mapIdx = self.adv:getCurMapIdx()

	for _, passive in ipairs(passives[1] or {}) do
		self.player:addPassive({id = passive})
	end

	for _, passive in ipairs(passives[2] or {}) do
		for _, enemy in ipairs(self.enemys[mapIdx]) do
			enemy:addPassive({id = passive})
		end
	end

	for _, buff in ipairs(buffs[1] or {}) do
		self.palyer:addBuff(buff)
	end

	for _, buff in ipairs(buffs[2] or {}) do
		for _, enemy in ipairs(self.enemys[mapIdx]) do
			enemy:addBuff(buff)
		end
	end
end


--写入数据
function Battle:saveDB()
	for idx, mapEnemys in pairs(self.enemys) do
		for _, enemy in ipairs(mapEnemys) do
			local block = self.adv:getBlock(enemy.roomId, enemy.blockId, idx)
			if block and block:isMonster() then
				block.event.enemy = enemy:getDB()
			end
		end
	end
	for idx, mapBuilds in pairs(self.builds) do
		for _, build in ipairs(mapBuilds) do
			local block = self.adv:getBlock(build.roomId, build.blockId, idx)
			if block and block:isBuild() then
				block.event.build = build:getDB()
			end
		end
	end
end

return Battle