Skip to content

Commit

Permalink
State now knows what controller it is in, can assign delegates to eve…
Browse files Browse the repository at this point in the history
…nts within state to assign variables before state from controller.

Added example travel controller with states
  • Loading branch information
CryoMyst committed Oct 18, 2019
1 parent 65484d6 commit e8f9247
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Daedalus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@
<Compile Include="Eve\Bot\Behaviours\b_Space.cs" />
<Compile Include="Eve\Bot\Behaviours\b_Station.cs" />
<Compile Include="Eve\Bot\Controllers\Controller.cs" />
<Compile Include="Eve\Bot\Routine.cs" />
<Compile Include="Eve\Bot\Controllers\TravelController.cs" />
<Compile Include="Eve\Bot\States\DockState.cs" />
<Compile Include="Eve\Bot\States\State.cs" />
<Compile Include="Eve\Bot\States\UndockState.cs" />
<Compile Include="Eve\Bot\States\WarpAndJumpState.cs" />
<Compile Include="Eve\Cache\EntityCache.cs" />
<Compile Include="Eve\Cache\EveCache.cs" />
<Compile Include="Eve\Enums\ShipSlot.cs" />
Expand Down
11 changes: 9 additions & 2 deletions Eve/Bot/Controllers/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ public void Stop()
{
Active = false;
}

public void SetState(string stateName)
{
if (States.TryGetValue(stateName, out State state))
{
state.EndState();
CurrentStateName = stateName;
}
if (States.ContainsKey(stateName))
{

CurrentStateName = stateName;
} else
{
Expand All @@ -47,6 +52,7 @@ public void SetState(string stateName)

public void AddState(string name, State state, string nextState)
{
state.Controller = this;
StateProgression[name] = nextState;
AddState(name, state);
}
Expand All @@ -71,8 +77,9 @@ public void Pulse()
CurrentStateName = nextStateName;
} else
{
// No progression found so deactivating
Active = false;
return;
// No progression found
}

}
Expand Down
26 changes: 26 additions & 0 deletions Eve/Bot/Controllers/TravelController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Daedalus.Eve.Bot.States;
using EVE.ISXEVE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daedalus.Eve.Bot.Controllers
{
class TravelController : Controller
{
private SolarSystem Destination;

protected override void RegisterStates()
{
AddState("undock", new UndockState(), "travel");

WarpAndJumpState travel = new WarpAndJumpState();
travel.PreState += (state) => (state as WarpAndJumpState)?.SetDesto(Destination);

AddState("travel", travel, "dock");
AddState("dock", new DockState());
}
}
}
41 changes: 41 additions & 0 deletions Eve/Bot/States/DockState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Daedalus.Eve.Cache;
using Daedalus.Eve.Wrappers;
using EVE.ISXEVE;
using LavishVMAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daedalus.Eve.Bot.States
{
class DockState : State
{
int StationID = -1;

public override bool DoWork()
{
using (new FrameLock(true))
{
if (StationID == -1)
{
// Dock at top station if avaliable
List<DEntity> entities = EveCache.Instance.EntityCache.AllEntities;

// Shouldn't use GetRaw, should expose the data through DEntity
DEntity station = entities.Where(e => e.CategoryType == CategoryType.Station).FirstOrDefault();

if (station != null)
{
station.GetRaw().WarpToAndDock();
return true;
}


}
}
return false;
}
}
}
44 changes: 43 additions & 1 deletion Eve/Bot/States/State.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Daedalus.Eve.Bot.Controllers;
using Daedalus.Eve.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -8,6 +10,46 @@ namespace Daedalus.Eve.Bot.States
{
public abstract class State
{
public event StateAction PreState;
public event StateAction PostState;
public event StateAction OnError;
public event StateAction OnPanic;

public Controller Controller;

public bool FirstPulse = true;
public void StartState()
{

PreState.Invoke(this);
FirstPulse = false;
}

public void EndState()
{
PostState?.Invoke(this);
FirstPulse = true;
}


public bool Pulse()
{
if (FirstPulse)
StartState();

bool result = DoWork();
if (result)
EndState();

return result;
}

private void SetState(string state)
{
Controller.SetState(state);
}
public abstract bool DoWork();

public delegate void StateAction(State state);
}
}
35 changes: 35 additions & 0 deletions Eve/Bot/States/UndockState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Daedalus.Eve.Bot.Controllers;
using EVE.ISXEVE;
using LavishVMAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daedalus.Eve.Bot.States
{
class UndockState : State
{
private bool Undocked = false;
public override bool DoWork()
{
using (new FrameLock(true))
{
Me me = new Me();
EVE.ISXEVE.EVE eve = new EVE.ISXEVE.EVE();

if (!me.InSpace && me.InStation && !Undocked)
{
eve.Execute(ExecuteCommand.CmdExitStation);
Undocked = true;

} else if (Undocked && me.InSpace && !me.InStation)
{
return true;
}
}
return false;
}
}
}
54 changes: 54 additions & 0 deletions Eve/Bot/States/WarpAndJumpState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using EVE.ISXEVE;
using LavishVMAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daedalus.Eve.Bot.States
{
class WarpAndJumpState : State
{
private SolarSystem Desto;

public void SetDesto(SolarSystem desto)
{
this.Desto = desto;
}

public override bool DoWork()
{
using (new FrameLock(true))
{
if (Desto == null)
return false;

EVE.ISXEVE.EVE eve = new EVE.ISXEVE.EVE();
Me me = new Me();

if (me.SolarSystemID == Desto.ID)
return true;

List<int> Waypoints = eve.GetWaypoints();
if (Waypoints.Count > 1 && Waypoints.Contains(Desto.ID))
{
eve.ClearAllWaypoints();
// Wait till next pulse
return false;
} else if (Waypoints.Count == 1 && !Waypoints.Contains(Desto.ID))
{
Desto.SetDestination();
return false;
}

if (!me.AutoPilotOn)
{
eve.Execute(ExecuteCommand.CmdToggleAutopilot);
return false;
}
return false;
}
}
}
}
8 changes: 8 additions & 0 deletions Eve/Cache/EntityCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ protected override void PopulateItems(ref Dictionary<long, Entity> cache)
}
}
}

public List<DEntity> AllEntities
{
get
{
return GetAllWrappedItems();
}
}
}
}
1 change: 1 addition & 0 deletions Eve/Wrappers/DEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class DEntity : CacheWrapper<long, Entity, DEntity>, IEquatable<DEntity>
{
public int TypeID;
public long EntityID => Key;
public CategoryType CategoryType => GetRaw().CategoryType;
public bool ESIDataExists => ESICache.Instance.DataExists<ESIEntity>(TypeID);

// Will return null if doesnt exist yet
Expand Down

0 comments on commit e8f9247

Please sign in to comment.