Client LuaCsForBarotrauma
LuaGame.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Xml.Linq;
7 using FarseerPhysics.Dynamics;
8 using Microsoft.Xna.Framework;
9 using MoonSharp.Interpreter;
10 
11 namespace Barotrauma
12 {
13  partial class LuaGame
14  {
17  public string SaveFolder => string.IsNullOrEmpty(GameSettings.CurrentConfig.SavePath) ? SaveUtil.DefaultSaveFolder : GameSettings.CurrentConfig.SavePath;
18 
19 #if CLIENT
21  {
22  get
23  {
24  return GameMain.Client;
25  }
26  }
27 
28  public bool Paused => GameMain.Instance?.Paused == true;
30  public byte MyID => SessionId; // compatibility
31 
33 
35  {
36  get
37  {
40  else
41  return GameMain.Client.ChatBox;
42  }
43  }
44 
45  public Sounds.SoundManager SoundManager
46  {
47  get
48  {
49  return GameMain.SoundManager;
50  }
51  }
52 
53  public Lights.LightManager LightManager
54  {
55  get
56  {
57  return GameMain.LightManager;
58  }
59  }
60 
62  {
63  get
64  {
66  }
67  }
68 
70  {
71  get
72  {
73  return GameMain.MainMenuScreen;
74  }
75  }
76 
77  public Particles.ParticleManager ParticleManager
78  {
79  get
80  {
82  }
83  }
84 
85  public bool IsSubEditor
86  {
87  get
88  {
90  }
91  }
92 
93 #else
94  public GameServer Server
95  {
96  get
97  {
98  return GameMain.Server;
99  }
100  }
101 
102  public bool IsDedicated
103  {
104  get
105  {
106  return GameMain.Server.ServerPeer is LidgrenServerPeer;
107  }
108  }
109 
110  public bool Paused => false;
111 #endif
112 
114  {
115  get
116  {
117 #if SERVER
118  return GameMain.Server.ServerSettings;
119 #else
120  return GameMain.Client.ServerSettings;
121 #endif
122  }
123  }
124 
126  {
127  get
128  {
129 #if SERVER
130  return GameMain.Server.RespawnManager;
131 #else
132  return GameMain.Client.RespawnManager;
133 #endif
134  }
135  }
136 
137  public bool? ForceVoice = null;
138  public bool? ForceLocalVoice = null;
139 
140  public DynValue Settings;
141 
142  public bool allowWifiChat = false;
143  public bool overrideTraitors = false;
144  public bool overrideRespawnSub = false;
145  public bool overrideSignalRadio = false;
146  public bool disableSpamFilter = false;
147  public bool disableDisconnectCharacter = false;
148  public bool enableControlHusk = false;
149 
151  {
152  get { return MapEntity.MapEntityUpdateInterval; }
153  set { MapEntity.MapEntityUpdateInterval = value; }
154  }
155 
156  public int GapUpdateInterval
157  {
158  get { return 1; }
159  set { }
160  }
161 
163  {
164  get { return MapEntity.PoweredUpdateInterval; }
165  set { MapEntity.PoweredUpdateInterval = value; }
166  }
167 
169  {
170  get { return Character.CharacterUpdateInterval; }
171  set { Character.CharacterUpdateInterval = value; }
172  }
173 
174 
175  public HashSet<Item> UpdatePriorityItems = new HashSet<Item>();
176  public HashSet<Character> UpdatePriorityCharacters = new HashSet<Character>();
177 
178  public void AddPriorityItem(Item item)
179  {
180  UpdatePriorityItems.Add(item);
181  }
182 
183  public void RemovePriorityItem(Item item)
184  {
185  UpdatePriorityItems.Remove(item);
186  }
187 
188  public void ClearPriorityItem()
189  {
190  UpdatePriorityItems.Clear();
191  }
192 
193  public void AddPriorityCharacter(Character character)
194  {
195  UpdatePriorityCharacters.Add(character);
196  }
197 
198  public void RemovePriorityCharacter(Character character)
199  {
200  UpdatePriorityCharacters.Remove(character);
201  }
202 
204  {
205  UpdatePriorityCharacters.Clear();
206  }
207 
208  public bool RoundStarted
209  {
210 
211  get
212  {
214 #if SERVER
215  return GameMain.Server?.GameStarted == true;
216 #else
217  return GameMain.Client?.GameStarted == true;
218 #endif
219  }
220  }
221 
223  {
224  get
225  {
226  return GameMain.GameSession;
227  }
228  }
229 
231  {
232  get
233  {
234  return GameMain.NetLobbyScreen;
235  }
236  }
237 
239  {
240  get
241  {
242  return GameMain.GameScreen;
243  }
244  }
245 
246  public World World
247  {
248  get
249  {
250  return GameMain.World;
251  }
252  }
253 
254 #if SERVER
255  public ServerPeer Peer
256  {
257  get
258  {
259  return GameMain.Server.ServerPeer;
260  }
261  }
262 #else
263  public ClientPeer Peer
264  {
265  get
266  {
267  return GameMain.Client.ClientPeer;
268  }
269  }
270 #endif
271 
272  public LuaGame()
273  {
274  LuaUserData.MakeFieldAccessible(UserData.RegisterType(typeof(GameSettings)), "currentConfig");
275  Settings = UserData.CreateStatic(typeof(GameSettings));
276  }
277 
278  public void OverrideTraitors(bool o)
279  {
280  overrideTraitors = o;
281  }
282 
283  public void OverrideRespawnSub(bool o)
284  {
285  overrideRespawnSub = o;
286  }
287 
288  public void AllowWifiChat(bool o)
289  {
290  allowWifiChat = o;
291  }
292 
293  public void OverrideSignalRadio(bool o)
294  {
296  }
297 
298  public void DisableSpamFilter(bool o)
299  {
300  disableSpamFilter = o;
301  }
302 
303  public void DisableDisconnectCharacter(bool o)
304  {
306  }
307 
308 
309  public void EnableControlHusk(bool o)
310  {
311  enableControlHusk = o;
312  }
313 
314  public static void Explode(Vector2 pos, float range = 100, float force = 30, float damage = 30, float structureDamage = 30, float itemDamage = 30, float empStrength = 0, float ballastFloraStrength = 0)
315  {
316  new Explosion(range, force, damage, structureDamage, itemDamage, empStrength, ballastFloraStrength).Explode(pos, null);
317  }
318 
319  public static string SpawnItem(string name, Vector2 pos, bool inventory = false, Character character = null)
320  {
321  string error;
322  DebugConsole.SpawnItem(new string[] { name, inventory ? "inventory" : "cursor" }, pos, character, out error);
323  return error;
324  }
325 
327  {
328  return ContentPackageManager.EnabledPackages.All.ToArray();
329  }
330 
331  public static ItemPrefab GetItemPrefab(string itemNameOrId)
332  {
333  ItemPrefab itemPrefab =
334  (MapEntityPrefab.Find(itemNameOrId, identifier: null, showErrorMessages: false) ??
335  MapEntityPrefab.Find(null, identifier: itemNameOrId, showErrorMessages: false)) as ItemPrefab;
336 
337  return itemPrefab;
338  }
339 
340  public static Submarine GetRespawnSub()
341  {
342 #if SERVER
343  if (GameMain.Server.RespawnManager == null) { return null; }
344  return GameMain.Server.RespawnManager.GetShuttle(CharacterTeamType.Team1);
345 #else
346  if (GameMain.Client.RespawnManager == null) { return null; }
347  return GameMain.Client.RespawnManager.GetShuttle(CharacterTeamType.Team1);
348 #endif
349  }
350 
351  public static Items.Components.Steering GetSubmarineSteering(Submarine sub)
352  {
353  foreach (Item item in Item.ItemList)
354  {
355  if (item.Submarine != sub) continue;
356 
357  var steering = item.GetComponent<Items.Components.Steering>();
358  if (steering != null)
359  {
360  return steering;
361  }
362  }
363 
364  return null;
365  }
366 
367  public static WifiComponent GetWifiComponent(Item item)
368  {
369  if (item == null) return null;
370  return item.GetComponent<WifiComponent>();
371  }
372 
374  {
375  if (item == null) return null;
376  return item.GetComponent<LightComponent>();
377  }
378 
380  {
381  if (item == null) return null;
382  return item.GetComponent<CustomInterface>();
383  }
384 
386  {
387  if (item == null) return null;
388  return item.GetComponent<Fabricator>();
389  }
390 
391  public static Holdable GetHoldableComponent(Item item)
392  {
393  if (item == null) return null;
394  return item.GetComponent<Holdable>();
395  }
396 
397  public static void ExecuteCommand(string command)
398  {
399  DebugConsole.ExecuteCommand(command);
400  }
401 
402  public static Signal CreateSignal(string value, int stepsTaken = 1, Character sender = null, Item source = null, float power = 0, float strength = 1)
403  {
404  return new Signal(value, stepsTaken, sender, source, power, strength);
405  }
406 
407  private List<DebugConsole.Command> luaAddedCommand = new List<DebugConsole.Command>();
408  public IEnumerable<DebugConsole.Command> LuaAddedCommand { get { return luaAddedCommand; } }
409 
410  public bool IsCustomCommandPermitted(Identifier command)
411  {
412  DebugConsole.Command[] permitted = new DebugConsole.Command[]
413  {
414  DebugConsole.FindCommand("cl_reloadluacs"),
415  DebugConsole.FindCommand("cl_lua"),
416  DebugConsole.FindCommand("cl_toggleluadebug"),
417  };
418 
419  foreach (var consoleCommand in LuaAddedCommand.Concat(permitted.AsEnumerable()))
420  {
421  if (consoleCommand.Names.Contains(command))
422  {
423  return true;
424  }
425  }
426 
427  return false;
428  }
429 
430  public void RemoveCommand(string name)
431  {
432  for (var i = 0; i < DebugConsole.Commands.Count; i++)
433  {
434  foreach (var cmdname in DebugConsole.Commands[i].Names)
435  {
436  if (cmdname == name)
437  {
438  luaAddedCommand.Remove(DebugConsole.Commands[i]);
439  DebugConsole.Commands.RemoveAt(i);
440  continue;
441  }
442  }
443  }
444  }
445 
446  public void AddCommand(string name, string help, LuaCsAction onExecute, LuaCsFunc getValidArgs = null, bool isCheat = false)
447  {
448  var cmd = new DebugConsole.Command(name, help, (string[] arg1) => onExecute(new object[] { arg1 }),
449  () =>
450  {
451  if (getValidArgs == null) return null;
452  var validArgs = getValidArgs();
453  if (validArgs is DynValue luaValue)
454  {
455  return luaValue.ToObject<string[][]>();
456  }
457  return (string[][])validArgs;
458  }, isCheat);
459 
460  luaAddedCommand.Add(cmd);
461  DebugConsole.Commands.Add(cmd);
462  }
463 
464  public List<DebugConsole.Command> Commands => DebugConsole.Commands;
465 
466  public void AssignOnExecute(string names, object onExecute) => DebugConsole.AssignOnExecute(names, (string[] a) => { GameMain.LuaCs.CallLuaFunction(onExecute, new object[] { a }); });
467 
468  public void SaveGame(string path)
469  {
470  if (!LuaCsFile.CanWriteToPath(path)) { throw new ScriptRuntimeException($"Saving files to {path} is disallowed."); }
471  SaveUtil.SaveGame(CampaignDataPath.CreateRegular(path));
472  }
473 
474  public void LoadGame(string path)
475  {
476  SaveUtil.LoadGame(CampaignDataPath.CreateRegular(path));
477  }
478 
479 #if SERVER
480  public void LoadCampaign(string path, Client client = null)
481  {
482  MultiPlayerCampaign.LoadCampaign(CampaignDataPath.CreateRegular(path), client);
483  }
484 
485  public static void SendMessage(string msg, ChatMessageType? messageType = null, Client sender = null, Character character = null)
486  {
487  GameMain.Server.SendChatMessage(msg, messageType, sender, character);
488  }
489 
490  public static void SendTraitorMessage(WriteOnlyMessage message, Client client)
491  {
492  GameMain.Server.SendTraitorMessage(message, client);
493  }
494 
495  public static void SendDirectChatMessage(string sendername, string text, Character sender, ChatMessageType messageType = ChatMessageType.Private, Client client = null, string iconStyle = "")
496  {
497  ChatMessage cm = ChatMessage.Create(sendername, text, messageType, sender);
498  cm.IconStyle = iconStyle;
499  GameMain.Server.SendDirectChatMessage(cm, client);
500  }
501 
502  public static void SendDirectChatMessage(ChatMessage chatMessage, Client client)
503  {
504  GameMain.Server.SendDirectChatMessage(chatMessage, client);
505  }
506 
507  public static void Log(string message, ServerLog.MessageType type)
508  {
509  GameServer.Log(message, type);
510  }
511 
512  public static void DispatchRespawnSub()
513  {
514  GameMain.Server.RespawnManager.DispatchShuttle(GameMain.Server.RespawnManager.GetTeamSpecificState(CharacterTeamType.Team1));
515  }
516 
517  public static GameServer.TryStartGameResult StartGame()
518  {
519  return GameMain.Server.TryStartGame();
520  }
521 
522  public static void EndGame()
523  {
524  GameMain.Server.EndGame();
525  }
526 
527  public void AssignOnClientRequestExecute(string names, object onExecute) => DebugConsole.AssignOnClientRequestExecute(names, (Client a, Vector2 b, string[] c) => { GameMain.LuaCs.CallLuaFunction(onExecute, new object[] { a, b, c }); });
528 
529 #endif
530 
531  public void Stop()
532  {
535 
536  foreach (var cmd in luaAddedCommand)
537  {
538  DebugConsole.Commands.Remove(cmd);
539  }
540  }
541  }
542 
543 }
ChatBox ChatBox
Present only in single player games. In multiplayer. The chatbox is found from GameSession....
Submarine Submarine
Definition: Entity.cs:53
Explosions are area of effect attacks that can damage characters, items and structures.
void Explode(Vector2 worldPosition, Entity damageSource, Character attacker=null)
static GameSession?? GameSession
Definition: GameMain.cs:88
static NetLobbyScreen NetLobbyScreen
Definition: GameMain.cs:55
static SubEditorScreen SubEditorScreen
Definition: GameMain.cs:68
static Lights.LightManager LightManager
Definition: GameMain.cs:78
static World World
Definition: GameMain.cs:105
static ChatMode ActiveChatMode
Definition: GameMain.cs:218
static bool IsMultiplayer
Definition: GameMain.cs:35
static bool IsSingleplayer
Definition: GameMain.cs:34
static GameScreen GameScreen
Definition: GameMain.cs:52
static MainMenuScreen MainMenuScreen
Definition: GameMain.cs:53
static ParticleManager ParticleManager
Definition: GameMain.cs:101
static GameClient Client
Definition: GameMain.cs:188
static GameMain Instance
Definition: GameMain.cs:144
static LuaCsSetup LuaCs
Definition: GameMain.cs:26
static Sounds.SoundManager SoundManager
Definition: GameMain.cs:80
static readonly List< Item > ItemList
static bool CanWriteToPath(string path)
Definition: LuaCsUtility.cs:52
DynValue CallLuaFunction(object function, params object[] args)
static Items.Components.Steering GetSubmarineSteering(Submarine sub)
Definition: LuaGame.cs:351
bool IsMultiplayer
Definition: LuaGame.cs:16
DynValue Settings
Definition: LuaGame.cs:140
void RemovePriorityItem(Item item)
Definition: LuaGame.cs:183
bool disableDisconnectCharacter
Definition: LuaGame.cs:147
bool? RoundStarted
Definition: LuaGame.cs:209
int MapEntityUpdateInterval
Definition: LuaGame.cs:151
Lights.LightManager LightManager
Definition: LuaGame.cs:54
static string SpawnItem(string name, Vector2 pos, bool inventory=false, Character character=null)
Definition: LuaGame.cs:319
Sounds.SoundManager SoundManager
Definition: LuaGame.cs:46
void OverrideTraitors(bool o)
Definition: LuaGame.cs:278
Particles.ParticleManager ParticleManager
Definition: LuaGame.cs:78
ChatMode ActiveChatMode
Definition: LuaGame.cs:32
string SaveFolder
Definition: LuaGame.cs:17
void EnableControlHusk(bool o)
Definition: LuaGame.cs:309
void OverrideSignalRadio(bool o)
Definition: LuaGame.cs:293
SubEditorScreen SubEditorScreen
Definition: LuaGame.cs:62
void SaveGame(string path)
Definition: LuaGame.cs:468
void DisableSpamFilter(bool o)
Definition: LuaGame.cs:298
List< DebugConsole.Command > Commands
Definition: LuaGame.cs:464
int CharacterUpdateInterval
Definition: LuaGame.cs:169
void AddPriorityItem(Item item)
Definition: LuaGame.cs:178
int PoweredUpdateInterval
Definition: LuaGame.cs:163
bool IsSingleplayer
Definition: LuaGame.cs:15
void AllowWifiChat(bool o)
Definition: LuaGame.cs:288
bool overrideRespawnSub
Definition: LuaGame.cs:144
static void Explode(Vector2 pos, float range=100, float force=30, float damage=30, float structureDamage=30, float itemDamage=30, float empStrength=0, float ballastFloraStrength=0)
Definition: LuaGame.cs:314
void RemoveCommand(string name)
Definition: LuaGame.cs:430
void DisableDisconnectCharacter(bool o)
Definition: LuaGame.cs:303
void ClearPriorityCharacter()
Definition: LuaGame.cs:203
IEnumerable< DebugConsole.Command > LuaAddedCommand
Definition: LuaGame.cs:408
static Holdable GetHoldableComponent(Item item)
Definition: LuaGame.cs:391
bool IsCustomCommandPermitted(Identifier command)
Definition: LuaGame.cs:410
static LightComponent GetLightComponent(Item item)
Definition: LuaGame.cs:373
HashSet< Item > UpdatePriorityItems
Definition: LuaGame.cs:175
void AssignOnExecute(string names, object onExecute)
void OverrideRespawnSub(bool o)
Definition: LuaGame.cs:283
static ContentPackage[] GetEnabledContentPackages()
Definition: LuaGame.cs:326
ClientPeer Peer
Definition: LuaGame.cs:264
static Signal CreateSignal(string value, int stepsTaken=1, Character sender=null, Item source=null, float power=0, float strength=1)
Definition: LuaGame.cs:402
bool enableControlHusk
Definition: LuaGame.cs:148
static Submarine GetRespawnSub()
Definition: LuaGame.cs:340
static void ExecuteCommand(string command)
Definition: LuaGame.cs:397
void RemovePriorityCharacter(Character character)
Definition: LuaGame.cs:198
static CustomInterface GetCustomInterface(Item item)
Definition: LuaGame.cs:379
bool disableSpamFilter
Definition: LuaGame.cs:146
bool overrideSignalRadio
Definition: LuaGame.cs:145
static Fabricator GetFabricatorComponent(Item item)
Definition: LuaGame.cs:385
void ClearPriorityItem()
Definition: LuaGame.cs:188
static ItemPrefab GetItemPrefab(string itemNameOrId)
Definition: LuaGame.cs:331
static WifiComponent GetWifiComponent(Item item)
Definition: LuaGame.cs:367
bool? ForceLocalVoice
Definition: LuaGame.cs:138
void AddCommand(string name, string help, LuaCsAction onExecute, LuaCsFunc getValidArgs=null, bool isCheat=false)
Definition: LuaGame.cs:446
void AddPriorityCharacter(Character character)
Definition: LuaGame.cs:193
void LoadGame(string path)
Definition: LuaGame.cs:474
HashSet< Character > UpdatePriorityCharacters
Definition: LuaGame.cs:176
bool overrideTraitors
Definition: LuaGame.cs:143
static void MakeFieldAccessible(IUserDataDescriptor IUUD, string fieldName)
Definition: LuaUserData.cs:141
static MapEntityPrefab Find(string name, string identifier=null, bool showErrorMessages=true)
Find a matching map entity prefab
static ChatMessage Create(string senderName, string text, ChatMessageType type, Entity sender, Client client=null, PlayerConnectionChangeType changeType=PlayerConnectionChangeType.None, Color? textColor=null)
delegate void LuaCsAction(params object[] args)
delegate object LuaCsFunc(params object[] args)
static CampaignDataPath CreateRegular(string savePath)
Creates a CampaignDataPath with the same load and save path.