From 7c8536127ceb4254cc2775a6f8610845d0083d29 Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Tue, 30 Nov 2021 11:36:00 -0300 Subject: [PATCH] added a way for lua to override people's jobs --- .../ServerSource/Networking/GameServer.cs | 87 ++++++++++--------- .../SharedSource/Lua/LuaCustomConverters.cs | 6 ++ docs/lua/Hooks.lua | 8 +- 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index 3d52593b8..5bb6def3f 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -2260,7 +2260,10 @@ namespace Barotrauma.Networking AssignJobs(teamClients); + var result = new LuaResult(GameMain.Lua.hook.Call("jobsAssigned")); + List characterInfos = new List(); + foreach (Client client in teamClients) { client.NeedsMidRoundSync = false; @@ -2340,59 +2343,61 @@ namespace Barotrauma.Networking spawnWaypoints = mainSubWaypoints; } Debug.Assert(spawnWaypoints.Count == mainSubWaypoints.Count); - - for (int i = 0; i < teamClients.Count; i++) + if (!result.Bool()) { - Character spawnedCharacter = Character.Create(teamClients[i].CharacterInfo, spawnWaypoints[i].WorldPosition, teamClients[i].CharacterInfo.Name, isRemotePlayer: true, hasAi: false); - spawnedCharacter.AnimController.Frozen = true; - spawnedCharacter.TeamID = teamID; - teamClients[i].Character = spawnedCharacter; - var characterData = campaign?.GetClientCharacterData(teamClients[i]); - if (characterData == null) + for (int i = 0; i < teamClients.Count; i++) { - spawnedCharacter.GiveJobItems(mainSubWaypoints[i]); - if (campaign != null) + Character spawnedCharacter = Character.Create(teamClients[i].CharacterInfo, spawnWaypoints[i].WorldPosition, teamClients[i].CharacterInfo.Name, isRemotePlayer: true, hasAi: false); + spawnedCharacter.AnimController.Frozen = true; + spawnedCharacter.TeamID = teamID; + teamClients[i].Character = spawnedCharacter; + var characterData = campaign?.GetClientCharacterData(teamClients[i]); + if (characterData == null) { - characterData = campaign.SetClientCharacterData(teamClients[i]); - characterData.HasSpawned = true; - } - } - else - { - if (!characterData.HasItemData && !characterData.CharacterInfo.StartItemsGiven) - { - //clients who've chosen to spawn with the respawn penalty can have CharacterData without inventory data spawnedCharacter.GiveJobItems(mainSubWaypoints[i]); + if (campaign != null) + { + characterData = campaign.SetClientCharacterData(teamClients[i]); + characterData.HasSpawned = true; + } } else { - characterData.SpawnInventoryItems(spawnedCharacter, spawnedCharacter.Inventory); + if (!characterData.HasItemData && !characterData.CharacterInfo.StartItemsGiven) + { + //clients who've chosen to spawn with the respawn penalty can have CharacterData without inventory data + spawnedCharacter.GiveJobItems(mainSubWaypoints[i]); + } + else + { + characterData.SpawnInventoryItems(spawnedCharacter, spawnedCharacter.Inventory); + } + characterData.ApplyHealthData(spawnedCharacter); + characterData.ApplyOrderData(spawnedCharacter); + spawnedCharacter.GiveIdCardTags(mainSubWaypoints[i]); + spawnedCharacter.LoadTalents(); + + characterData.HasSpawned = true; + } + if (GameMain.GameSession?.GameMode is MultiPlayerCampaign mpCampaign && spawnedCharacter.Info != null) + { + spawnedCharacter.Info.SetExperience(Math.Max(spawnedCharacter.Info.ExperiencePoints, mpCampaign.GetSavedExperiencePoints(teamClients[i]))); + mpCampaign.ClearSavedExperiencePoints(teamClients[i]); } - characterData.ApplyHealthData(spawnedCharacter); - characterData.ApplyOrderData(spawnedCharacter); - spawnedCharacter.GiveIdCardTags(mainSubWaypoints[i]); - spawnedCharacter.LoadTalents(); - characterData.HasSpawned = true; + spawnedCharacter.OwnerClientEndPoint = teamClients[i].Connection.EndPointString; + spawnedCharacter.OwnerClientName = teamClients[i].Name; } - if (GameMain.GameSession?.GameMode is MultiPlayerCampaign mpCampaign && spawnedCharacter.Info != null) + + for (int i = teamClients.Count; i < teamClients.Count + bots.Count; i++) { - spawnedCharacter.Info.SetExperience(Math.Max(spawnedCharacter.Info.ExperiencePoints, mpCampaign.GetSavedExperiencePoints(teamClients[i]))); - mpCampaign.ClearSavedExperiencePoints(teamClients[i]); + Character spawnedCharacter = Character.Create(characterInfos[i], spawnWaypoints[i].WorldPosition, characterInfos[i].Name, isRemotePlayer: false, hasAi: true); + spawnedCharacter.TeamID = teamID; + spawnedCharacter.GiveJobItems(mainSubWaypoints[i]); + spawnedCharacter.GiveIdCardTags(mainSubWaypoints[i]); + // talents are only avilable for players in online sessions, but modders or someone else might want to have them loaded anyway + spawnedCharacter.LoadTalents(); } - - spawnedCharacter.OwnerClientEndPoint = teamClients[i].Connection.EndPointString; - spawnedCharacter.OwnerClientName = teamClients[i].Name; - } - - for (int i = teamClients.Count; i < teamClients.Count + bots.Count; i++) - { - Character spawnedCharacter = Character.Create(characterInfos[i], spawnWaypoints[i].WorldPosition, characterInfos[i].Name, isRemotePlayer: false, hasAi: true); - spawnedCharacter.TeamID = teamID; - spawnedCharacter.GiveJobItems(mainSubWaypoints[i]); - spawnedCharacter.GiveIdCardTags(mainSubWaypoints[i]); - // talents are only avilable for players in online sessions, but modders or someone else might want to have them loaded anyway - spawnedCharacter.LoadTalents(); } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs index 6ac7e659f..c9f6f5f7b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs @@ -45,6 +45,12 @@ namespace Barotrauma return (GUITextBox.OnEnterHandler)((GUITextBox a, string b) => new LuaResult(LuaSetup.luaSetup.CallFunction(function, a, b)).Bool()); }); #endif + + + Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(Pair), v => + { + return new Pair((JobPrefab)v.Table.Get(1).ToObject(), (int)v.Table.Get(2).CastToNumber()); + }); } public static void RegisterAction() diff --git a/docs/lua/Hooks.lua b/docs/lua/Hooks.lua index 05a37efb1..2cbb7bf24 100644 --- a/docs/lua/Hooks.lua +++ b/docs/lua/Hooks.lua @@ -151,7 +151,13 @@ function serverLog(text, serverLogMessageType) end --- Called each time a new round start job has been assigned, this context allows for you to change the role before it's applied in game. -- @realm shared -function jobAssigned(text, serverLogMessageType) end +function jobsAssigned() end +-- @usage +-- Hook.Add("jobsAssigned", "", function () +-- for key, value in pairs(Client.ClientList) do +-- value.AssignedJob = {JobPrefab.Get("assistant"), 0} +-- end +-- end) --- Check if a client is allowed to hear radio voice to another client, return true to allow, false to disallow. -- @realm shared