From 6b174501d871ee7b4030ba783072db198885719c Mon Sep 17 00:00:00 2001 From: AUTOMATIC1111 <16777216c@gmail.com> Date: Tue, 13 Mar 2018 20:47:30 +0300 Subject: [PATCH] 2.1 --- game/scripts/mod_loader/altered.lua | 101 +++++++++++++++++- game/scripts/mod_loader/mod_loader.lua | 12 +-- game/scripts/mod_loader/modapi.lua | 137 ++++++++++++++++++++++++- sdl-utils.cpp | 2 +- 4 files changed, 235 insertions(+), 17 deletions(-) diff --git a/game/scripts/mod_loader/altered.lua b/game/scripts/mod_loader/altered.lua index 8c411fd..ec7d467 100644 --- a/game/scripts/mod_loader/altered.lua +++ b/game/scripts/mod_loader/altered.lua @@ -1,8 +1,10 @@ ----Add API functionality that changes existing content here---- +oldGetPopulationTexts = GetPopulationTexts local oldGetStartingSquad = getStartingSquad local oldBaseNextTurn = Mission.BaseNextTurn local oldBaseUpdate = Mission.BaseUpdate +local oldBaseStart = Mission.BaseStart local oldGetText = GetText local oldStartNewGame = startNewGame local oldLoadGame = LoadGame @@ -45,6 +47,37 @@ function Mission:BaseUpdate() end end +function Mission:MissionEnd() + local ret = SkillEffect() + + CurrentMission = self + EndingMission = true + self.delayToAdd = 4 + for i, hook in ipairs(modApi.MissionEndHooks) do + hook(self,ret) + end + ret:AddDelay(self:GetEndDelay()) + EndingMission = false + + Board:AddEffect(ret) +end + +function Mission:BaseStart() + for i, hook in ipairs(modApi.preMissionStartHooks) do + hook(self) + end + + oldBaseStart(self) + + for i, hook in ipairs(modApi.missionStartHooks) do + hook(self) + end +end + +function Mission:GetEndDelay() + return math.max(0,self.delayToAdd) +end + function GetText(id) if modApi.textOverrides and modApi.textOverrides[id] then return modApi.textOverrides[id] @@ -53,8 +86,68 @@ function GetText(id) return oldGetText(id) end +function GetPopulationTexts(event, count) + LOG(event) + + local nullReturn = count == 1 and "" or {} + + if modApi.PopEvents[event] == nil then + return nullReturn + end + + if Game == nil then + return nullReturn + end + + local list = copy_table(modApi.PopEvents[event]) + local ret = {} + for i = 1, count do + if #list == 0 then + break + end + + ret[#ret+1] = random_removal(list) + + + if modApi.PopEvents[event].Odds ~= nil and random_int(100) > modApi.PopEvents[event].Odds then + ret[#ret] = nil + end + end + + if #ret == 0 then + return nullReturn + end + + local corp_name = Game:GetCorp().bark_name + local squad_name = Game:GetSquad() + for i,v in ipairs(ret) do + ret[i] = string.gsub(ret[i], "#squad", squad_name) + ret[i] = string.gsub(ret[i], "#corp", corp_name) + for j, fn in ipairs(modApi.onGetPopEvent) do + ret[i] = fn(ret[i],ret,i,event,count) + end + end + + if count == 1 then + return ret[1] + end + + return ret +end + function startNewGame() + --This should be replaced if/when we get the option to configure mod options from within the game + --(either from a mods menu under the main menu or on a separate screen before the squad select screen like in Invisible, Inc.) + local modOptions = mod_loader:getModContentDefaults() + local savedOrder = {}--This should be replaced if/when we get the option to re-order mods + + --We have already loaded mods because we needed to do that before we entered the squad selection screen in order to show it correctly + --mod_loader:loadModContent(modOptions,savedOrder) + oldStartNewGame() + + GAME.modOptions = modOptions + GAME.modLoadOrder = savedOrder GAME.squadTitles = {} for i, key in ipairs(modApi.squadKeys) do @@ -63,10 +156,16 @@ function startNewGame() end function LoadGame() - oldLoadGame() + GAME.modOptions = GAME.modOptions or mod_loader:getModContentDefaults() + GAME.modLoadOrder = GAME.modLoadOrder or {} + + mod_loader:loadModContent(GAME.modOptions,GAME.modLoadOrder) + if GAME.squadTitles then for k, name in pairs(GAME.squadTitles) do modApi:overwriteTextTrue(k,name) end end + + oldLoadGame() end \ No newline at end of file diff --git a/game/scripts/mod_loader/mod_loader.lua b/game/scripts/mod_loader/mod_loader.lua index 785cdcc..acfca41 100644 --- a/game/scripts/mod_loader/mod_loader.lua +++ b/game/scripts/mod_loader/mod_loader.lua @@ -23,21 +23,12 @@ function mod_loader:init() end function mod_loader:enumerateMods() - --A better way to iterate over folders in the mods folder would be nice - --Would probably need help from the C++ side though - + local modlist = listdirs("mods") for key,value in pairs(modlist) do table.insert(self.mod_dirs,value) end - --for dir in io.popen([[dir ".\mods\" /b /ad]]):lines() do table.insert(self.mod_dirs,dir) end - --[[for file in lfs.dir("./mods") do - if file ~= "." and file ~= ".." then - table.insert(self.mod_dirs,dir) - end - end]] - for i, dir in pairs(self.mod_dirs) do local err = "" local path = string.format("mods/%s/scripts/init.lua",dir) @@ -339,6 +330,5 @@ function modApi:selectSquads() end end - modApi:init() mod_loader:init() \ No newline at end of file diff --git a/game/scripts/mod_loader/modapi.lua b/game/scripts/mod_loader/modapi.lua index d72b91f..e0e8c71 100644 --- a/game/scripts/mod_loader/modapi.lua +++ b/game/scripts/mod_loader/modapi.lua @@ -14,7 +14,7 @@ end function modApi:init() --package.path = prev_path..package.path - self.version = "1.2.0" + self.version = "1.2.1" LOG("MOD-API VERSION "..self.version) self.currentModSquads = {} self.currentModSquadText = {} @@ -121,8 +121,76 @@ function modApi:resetModContent() } self.nextTurnHooks = {} self.missionUpdateHooks = {} + self.missionStartHooks = {} + self.preMissionStartHooks = {} self.currentModSquads = {} self.currentModSquadText = {} + self.MissionEndHooks = { + --Pilot Message + function(mission,ret) + ret:AddScript([[local ret = SkillEffect() + local enemy_count = Board:GetEnemyCount() + if enemy_count == 0 then + ret:AddVoice("MissionEnd_Dead", -1) + elseif self.RetreatEndingMessage then + ret:AddVoice("MissionEnd_Retreat", -1) + end + Board:AddEffect(ret)]]) + end, + + --Population Event + function(mission,ret) + ret:AddScript([[local ret = SkillEffect() + local enemy_count = Board:GetEnemyCount() + + if CurrentMission:GetDamage() == 0 then + ret:AddScript("Board:StartPopEvent(\"Closing_Perfect\")") + elseif CurrentMission:GetDamage() > 4 then + ret:AddScript("Board:StartPopEvent(\"Closing_Bad\")") + elseif enemy_count > 0 then + ret:AddScript("Board:StartPopEvent(\"Closing\")") + else + ret:AddScript("Board:StartPopEvent(\"Closing_Dead\")") + end + Board:AddEffect(ret)]]) + end, + + --Enemy retreat + function(mission,ret) + ret:AddScript([[ + local ret = SkillEffect() + local effect = SpaceDamage() + effect.bEvacuate = true + effect.fDelay = 0.5 + + local board_size = Board:GetSize() + for i = 0, board_size.x - 1 do + for j = 0, board_size.y - 1 do + if Board:IsPawnTeam(Point(i,j),TEAM_ENEMY) then + effect.loc = Point(i,j) + ret:AddDamage(effect) + CurrentMission.delayToAdd = CurrentMission.delayToAdd - 0.5 + end + end + end + Board:AddEffect(ret)]]) + end, + + --End Delay + function(mission,ret) + ret:AddScript([[local ret = SkillEffect() + --ret:AddDelay(CurrentMission:GetEndDelay()) + Board:AddEffect(ret)]]) + end, + } + self.iMePilotMessage = 1 + self.iMePopEvent = 2 + self.iMeRetreat = 3 + self.iMeDelay = 4 + + local name, tbl = debug.getupvalue(oldGetPopulationTexts,1) + self.PopEvents = copy_table(tbl) + self.onGetPopEvent = {} end function modApi:setCurrentMod(mod) @@ -131,6 +199,7 @@ function modApi:setCurrentMod(mod) self.currentModSquadText[mod] = {} end +--The updated way to add squads function modApi:addSquadTrue(squad,name,desc,icon) assert(type(squad) == "table") assert(#squad == 4) @@ -142,8 +211,12 @@ function modApi:addSquadTrue(squad,name,desc,icon) table.insert(self.squad_icon,icon or "resources/mods/squads/unknown.png") end ---ONLY for compability with older mods. Using it is discouraged, it will likely get dropped in the future. Use addSquadTrue instead -function modApi:addSquad(id,squad,icon) +function modApi:addSquad(id,squad,desc,icon) + if type(id) == "table" and type(squad) == "string" and type(desc) == "string" then + return self:addSquadTrue(id,squad,desc) + end + + --The old way to add squads assert(id) assert(type(squad) == "table") assert(#squad == 4) @@ -173,7 +246,6 @@ function modApi:overwriteTextTrue(id,str) self.textOverrides[id] = str end ---ONLY for compability with older mods. Using it is discouraged, it will likely get dropped in the future. Use overwriteTextTrue instead function modApi:overwriteText(id,str) assert(type(id) == "string") assert(type(str) == "string") @@ -224,6 +296,16 @@ function modApi:overwriteText(id,str) self.textOverrides[id] = str end +function modApi:addPreMissionEndHook(fn) + assert(type(fn) == "function") + table.insert(self.preMissionStartHooks,fn) +end + +function modApi:addMissionStartHook(fn) + assert(type(fn) == "function") + table.insert(self.missionStartHooks,fn) +end + function modApi:addNextTurnHook(fn) assert(type(fn) == "function") table.insert(self.nextTurnHooks,fn) @@ -234,6 +316,30 @@ function modApi:addMissionUpdateHook(fn) table.insert(self.missionUpdateHooks,fn) end +function modApi:addMissionEndHook(fn,i) + assert(type(fn) == "function") + if i ~= nil then + assert(type(i) == "number") + assert(i > 0) + assert(math.floor(i) == i) + table.insert(self.MissionEndHooks,i,fn) + if i <= self.iMePilotMessage then + self.iMePilotMessage = self.iMePilotMessage + 1 + end + if i <= self.iMePopEvent then + self.iMePopEvent = self.iMePopEvent + 1 + end + if i <= self.iMeRetreat then + self.iMeRetreat = self.iMeRetreat + 1 + end + if i <= self.iMeDelay then + self.iMeDelay = self.iMeDelay + 1 + end + else + table.insert(self.MissionEndHooks,fn) + end +end + function modApi:addWeapon_Texts(tbl) assert(type(tbl) == "table") for k,v in pairs(tbl) do @@ -241,6 +347,29 @@ function modApi:addWeapon_Texts(tbl) end end +function modApi:addPopEvent(event,msg) + assert(type(event) == "string") + assert(type(msg) == "string") + if not self.PopEvents[event] then + self.PopEvents[event] = {} + end + + table.insert(self.PopEvents[event],msg) +end + +function modApi:setPopEventOdds(event,odds) + assert(type(event) == "string") + assert(self.PopEvents[event]) + assert(odds == nil or type(odds) == "number") + + self.PopEvents[event].Odds = odds +end + +function modApi:addOnPopEvent(fn) + assert(type(fn) == "function") + table.insert(self.onGetPopEvent,fn) +end + function modApi:appendAsset(resource,filePath) local f = io.open(filePath,"rb") assert(f,filePath) diff --git a/sdl-utils.cpp b/sdl-utils.cpp index 8ccdedc..f7db8cf 100644 --- a/sdl-utils.cpp +++ b/sdl-utils.cpp @@ -381,7 +381,7 @@ void log(const std::string & line) { } bool isshiftdown() { - return GetAsyncKeyState(VK_SHIFT) & 0x8000 == 0x8000; + return (GetAsyncKeyState(VK_SHIFT) & 0x8000) == 0x8000; } Timer::Timer() {