public Triggerable(LuaFunction function, ScriptContext context, Actor self) { Function = (LuaFunction)function.CopyReference(); Context = context; Self = self.ToLuaValue(Context); }
public LuaTable ReinforceWithTransport(Player owner, string actorType, string[] cargoTypes, CPos[] entryPath, CPos[] exitPath = null, LuaFunction actionFunc = null, LuaFunction exitFunc = null) { var transport = CreateActor(owner, actorType, true, entryPath[0], entryPath.Length > 1 ? entryPath[1] : (CPos?)null); var cargo = transport.TraitOrDefault <Cargo>(); var passengers = new List <Actor>(); if (cargo != null && cargoTypes != null) { foreach (var cargoType in cargoTypes) { var passenger = CreateActor(owner, cargoType, false, entryPath[0]); passengers.Add(passenger); cargo.Load(transport, passenger); } } for (var i = 1; i < entryPath.Length; i++) { Move(transport, entryPath[i]); } if (actionFunc != null) { var af = (LuaFunction)actionFunc.CopyReference(); transport.QueueActivity(new CallFunc(() => { using (af) using (LuaValue t = transport.ToLuaValue(Context), p = passengers.ToArray().ToLuaValue(Context)) af.Call(t, p); })); } else { var aircraftInfo = transport.TraitOrDefault <Aircraft>(); if (aircraftInfo != null) { if (!aircraftInfo.IsPlane) { transport.QueueActivity(new Turn(transport, aircraftInfo.Info.InitialFacing)); transport.QueueActivity(new HeliLand(transport, true)); } else { transport.QueueActivity(new Land(transport, Target.FromCell(transport.World, entryPath.Last()))); } transport.QueueActivity(new Wait(15)); } if (cargo != null) { transport.QueueActivity(new UnloadCargo(transport, true)); transport.QueueActivity(new WaitFor(() => cargo.IsEmpty(transport))); } transport.QueueActivity(new Wait(aircraftInfo != null ? 50 : 25)); } if (exitFunc != null) { var ef = (LuaFunction)exitFunc.CopyReference(); transport.QueueActivity(new CallFunc(() => { using (ef) using (var t = transport.ToLuaValue(Context)) ef.Call(t); })); } else if (exitPath != null) { foreach (var wpt in exitPath) { Move(transport, wpt); } transport.QueueActivity(new RemoveSelf()); } var ret = Context.CreateTable(); using (LuaValue tKey = 1, tValue = transport.ToLuaValue(Context), pKey = 2, pValue = passengers.ToArray().ToLuaValue(Context)) { ret.Add(tKey, tValue); ret.Add(pKey, pValue); } return(ret); }
public CallLuaFunc(LuaFunction function, ScriptContext context) { this.function = (LuaFunction)function.CopyReference(); this.context = context; }
public LuaTable ReinforceWithTransport(Player owner, string actorType, string[] cargoTypes, CPos[] entryPath, CPos[] exitPath = null, LuaFunction actionFunc = null, LuaFunction exitFunc = null, int dropRange = 3) { var transport = CreateActor(owner, actorType, true, entryPath[0], entryPath.Length > 1 ? entryPath[1] : (CPos?)null); var cargo = transport.TraitOrDefault <Cargo>(); var passengers = new List <Actor>(); if (cargo != null && cargoTypes != null) { foreach (var cargoType in cargoTypes) { var passenger = CreateActor(owner, cargoType, false, entryPath[0]); passengers.Add(passenger); cargo.Load(transport, passenger); } } for (var i = 1; i < entryPath.Length; i++) { Move(transport, entryPath[i]); } if (actionFunc != null) { var af = (LuaFunction)actionFunc.CopyReference(); transport.QueueActivity(new CallFunc(() => { using (af) using (LuaValue t = transport.ToLuaValue(Context), p = passengers.ToArray().ToLuaValue(Context)) af.Call(t, p); })); } else { var aircraft = transport.TraitOrDefault <Aircraft>(); // Scripted cargo aircraft must turn to default position before unloading. // TODO: pass facing through UnloadCargo instead. if (aircraft != null) { transport.QueueActivity(new Land(transport, Target.FromCell(transport.World, entryPath.Last()), WDist.FromCells(dropRange), aircraft.Info.InitialFacing)); } if (cargo != null) { transport.QueueActivity(new UnloadCargo(transport, WDist.FromCells(dropRange))); } } if (exitFunc != null) { var ef = (LuaFunction)exitFunc.CopyReference(); transport.QueueActivity(new CallFunc(() => { using (ef) using (var t = transport.ToLuaValue(Context)) ef.Call(t); })); } else if (exitPath != null) { foreach (var wpt in exitPath) { Move(transport, wpt); } transport.QueueActivity(new RemoveSelf()); } var ret = Context.CreateTable(); using (LuaValue tKey = 1, tValue = transport.ToLuaValue(Context), pKey = 2, pValue = passengers.ToArray().ToLuaValue(Context)) { ret.Add(tKey, tValue); ret.Add(pKey, pValue); } return(ret); }
public LuaTable ReinforceWithTransport(Player owner, string actorType, string[] cargoTypes, CPos[] entryPath, CPos[] exitPath = null, LuaFunction actionFunc = null, LuaFunction exitFunc = null, int dropRange = 3) { var transport = CreateActor(owner, actorType, true, entryPath[0], entryPath.Length > 1 ? entryPath[1] : (CPos?)null); var cargo = transport.TraitOrDefault <Cargo>(); var passengers = new List <Actor>(); if (cargo != null && cargoTypes != null) { foreach (var cargoType in cargoTypes) { var passenger = CreateActor(owner, cargoType, false, entryPath[0]); passengers.Add(passenger); cargo.Load(transport, passenger); } } for (var i = 1; i < entryPath.Length; i++) { Move(transport, entryPath[i]); } if (actionFunc != null) { var af = (LuaFunction)actionFunc.CopyReference(); transport.QueueActivity(new CallFunc(() => { using (af) using (LuaValue t = transport.ToLuaValue(Context), p = passengers.ToArray().ToLuaValue(Context)) af.Call(t, p); })); } else { var aircraft = transport.TraitOrDefault <Aircraft>(); if (aircraft != null) { var destination = entryPath.Last(); // Try to find an alternative landing spot if we can't land at the current destination if (!aircraft.CanLand(destination) && dropRange > 0) { var locomotors = cargo.Passengers .Select(a => a.Info.TraitInfoOrDefault <MobileInfo>()) .Where(m => m != null) .Distinct() .Select(m => m.LocomotorInfo) .ToList(); foreach (var c in transport.World.Map.FindTilesInCircle(destination, dropRange)) { if (!aircraft.CanLand(c)) { continue; } if (!locomotors.All(m => domainIndex.IsPassable(destination, c, m))) { continue; } destination = c; break; } } if (aircraft.Info.VTOL) { if (destination != entryPath.Last()) { Move(transport, destination); } transport.QueueActivity(new Turn(transport, aircraft.Info.InitialFacing)); transport.QueueActivity(new Land(transport)); } else { transport.QueueActivity(new Land(transport, Target.FromCell(transport.World, destination))); } transport.QueueActivity(new Wait(15)); } if (cargo != null) { transport.QueueActivity(new UnloadCargo(transport, true)); transport.QueueActivity(new WaitFor(() => cargo.IsEmpty(transport))); } transport.QueueActivity(new Wait(aircraft != null ? 50 : 25)); } if (exitFunc != null) { var ef = (LuaFunction)exitFunc.CopyReference(); transport.QueueActivity(new CallFunc(() => { using (ef) using (var t = transport.ToLuaValue(Context)) ef.Call(t); })); } else if (exitPath != null) { foreach (var wpt in exitPath) { Move(transport, wpt); } transport.QueueActivity(new RemoveSelf()); } var ret = Context.CreateTable(); using (LuaValue tKey = 1, tValue = transport.ToLuaValue(Context), pKey = 2, pValue = passengers.ToArray().ToLuaValue(Context)) { ret.Add(tKey, tValue); ret.Add(pKey, pValue); } return(ret); }
public void RegisterCallback(Trigger trigger, LuaFunction func, ScriptContext context) { Triggers[trigger].Add(Pair.New((LuaFunction)func.CopyReference(), context)); }
public LuaTable ReinforceWithTransport(Player owner, string actorType, string[] cargoTypes, CPos[] entryPath, CPos[] exitPath = null, LuaFunction actionFunc = null, LuaFunction exitFunc = null) { var transport = CreateActor(owner, actorType, true, entryPath[0], entryPath.Length > 1 ? entryPath[1] : (CPos?)null); var cargo = transport.TraitOrDefault <Cargo>(); var passengers = new List <Actor>(); if (cargo != null && cargoTypes != null) { foreach (var cargoType in cargoTypes) { var passenger = CreateActor(owner, cargoType, false, entryPath[0]); passengers.Add(passenger); cargo.Load(transport, passenger); } } for (var i = 1; i < entryPath.Length; i++) { Move(transport, entryPath[i]); } if (actionFunc != null) { var af = actionFunc.CopyReference() as LuaFunction; transport.QueueActivity(new CallFunc(() => { af.Call(transport.ToLuaValue(context), passengers.ToArray().ToLuaValue(context)); af.Dispose(); })); } else { var heli = transport.TraitOrDefault <Helicopter>(); if (heli != null) { transport.QueueActivity(new Turn(transport, heli.Info.InitialFacing)); transport.QueueActivity(new HeliLand(true)); transport.QueueActivity(new Wait(15)); } if (cargo != null) { transport.QueueActivity(new UnloadCargo(transport, true)); transport.QueueActivity(new WaitFor(() => cargo.IsEmpty(transport))); } transport.QueueActivity(new Wait(heli != null ? 50 : 25)); } if (exitFunc != null) { var ef = exitFunc.CopyReference() as LuaFunction; transport.QueueActivity(new CallFunc(() => { ef.Call(transport.ToLuaValue(context)); ef.Dispose(); })); } else if (exitPath != null) { foreach (var wpt in exitPath) { Move(transport, wpt); } transport.QueueActivity(new RemoveSelf()); } var ret = context.CreateTable(); ret.Add(1, transport.ToLuaValue(context)); ret.Add(2, passengers.ToArray().ToLuaValue(context)); return(ret); }
public CallLuaFunc(LuaFunction func) { function = (LuaFunction)func.CopyReference(); }
public void RegisterProductionCallback(LuaFunction func, ScriptContext context) { onProduction.Add(Pair.New((LuaFunction)func.CopyReference(), context)); }
public void RegisterKilledCallback(LuaFunction func, ScriptContext context) { onKilled.Add(Pair.New((LuaFunction)func.CopyReference(), context)); }