public override (Wheel[], ControlInfo) GetInstruction(Field field) { if (_strategy == null) { throw new DllNotFoundException(); } var nativeField = new Native.Field(field); if (ReverseCoordinate) { nativeField.Reverse(); } var env = new Native.Legacy.Environment(nativeField, whosball, gameState, userData); try { _strategy(ref env); userData = env.UserData; } catch (Exception e) { throw new DllException("Strategy", e); } var controlInfo = new ControlInfo { Command = ControlType.Continue }; var wheel = env.SelfRobots.Select(x => new Wheel() { LeftSpeed = (float)x.VelocityLeft, RightSpeed = (float)x.VelocityRight }).ToArray(); return(wheel, controlInfo); }
public override bool Load(string dllPath, bool reverse, out Exception exception) { if (IsLoaded) { exception = new Exception("DLL has loaded."); return(false); } ReverseCoordinate = reverse; _lastDllPath = dllPath; var hModule = LoadLibrary(dllPath); var errorCode = Marshal.GetLastWin32Error(); if (hModule == IntPtr.Zero) { exception = new Exception($"Error load {dllPath}, code {errorCode}"); return(false); } CurrentModule = hModule; try { //BEGIN UNMANAGED FUNCTIONS _create = LoadFunction <LegacyStrategyDelegate>("Create"); _strategy = LoadFunction <LegacyStrategyDelegate>("Strategy"); _destroy = LoadFunction <LegacyStrategyDelegate>("Destroy"); //END UNMANAGED FUNCTIONS } catch (ArgumentNullException e) { Unload(); exception = new Exception("Missing function", e); return(false); } var env = new Native.Legacy.Environment() { CurrentBall = new Native.Legacy.Ball { Position = new Native.Legacy.Vector3 { x = 50, y = 41.5 } }, SelfRobots = new[] { new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 90.5, y = 42 } }, new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 81, y = 23 } }, new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 81, y = 61 } }, new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 62, y = 23 } }, new Native.Legacy.Robot { Position = new Native.Legacy.Vector3 { x = 62, y = 61 } }, } }; _create?.Invoke(ref env); userData = env.UserData; exception = null; return(true); }