Skip to content

Instantly share code, notes, and snippets.

@Schaka
Created February 21, 2018 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schaka/6a4f17800c6a861773a5ea413328ece6 to your computer and use it in GitHub Desktop.
Save Schaka/6a4f17800c6a861773a5ea413328ece6 to your computer and use it in GitHub Desktop.
public class AutoVendor
{
private static List<Npc> _savedNpcs = new List<Npc>();
public static void Start()
{
FiniteStateMachineEvents.OnBeforeCheckIfNeedToRunState += AddVendorHandler;
FiniteStateMachineEvents.OnAfterRunState += ResetVendorListHandler;
}
public static void Stop()
{
FiniteStateMachineEvents.OnBeforeCheckIfNeedToRunState -= AddVendorHandler;
FiniteStateMachineEvents.OnAfterRunState -= ResetVendorListHandler;
ResetVendorList();
}
private static void AddVendorHandler(Engine engine, State state, CancelEventArgs cancelable)
{
//TODO: DO NOT SWITCH VENDORS WHILE ALREADY IN TOTOWN STATE!
//for wRobot to even consider activating go to town, we need to have an available vendor/repair
if (state != null &&
state.GetType() == typeof(ToTown)
&& (!Logging.Status.ToLower().Contains("town") || !ToTown.GoToTownInProgress)
&& !ToTown.ForceToTown
)
{
//Npc closestRepair = GetClosestVendorNpc(Npc.NpcType.Repair);
//Npc closestVendor = GetClosestVendorNpc(Npc.NpcType.Vendor);
Npc closestRepair = new Npc(0,0,0);
Npc closestVendor = new Npc(0,0,0);
if (NpcDB.ListNpc.FirstOrDefault(o => o.Entry == closestRepair.Entry && o.Type == Npc.NpcType.Repair) == null)
{
NpcDB.AddNpc(closestRepair, false, true);
}
if (NpcDB.ListNpc.FirstOrDefault(o => o.Entry == closestVendor.Entry && o.Type == Npc.NpcType.Vendor) == null)
{
NpcDB.AddNpc(closestVendor, false, true);
}
}
if (state != null && state.GetType() == typeof(ToTown) && state.NeedToRun)
{
NpcDB.AcceptOnlyProfileNpc = true;
_savedNpcs.AddRange(NpcDB.ListNpc.FindAll(_dbFilter));
NpcDB.ListNpc.RemoveAll(_dbFilter);
//Npc repairVendor = GetClosestVendorNpc(Npc.NpcType.Repair);
//Npc itemVendor = GetClosestVendorNpc(Npc.NpcType.Vendor);
Npc closestRepair = new Npc(0,0,0);
Npc closestVendor = new Npc(0,0,0);
//PluginLog.Log($@"Choosing vendor {itemVendor.Name} and repair {repairVendor.Name} for ToTown state!");
NpcDB.AddNpc(itemVendor, false, true);
NpcDB.AddNpc(repairVendor, false, true);
state.Run();
}
}
private static void ResetVendorListHandler(Engine engine, State state)
{
if (state != null && state.GetType() == typeof(ToTown))
{
ResetVendorList();
}
}
private static void ResetVendorList()
{
_inProgress = false;
if (!_savedNpcs.IsNullOrEmpty())
{
_savedNpcs.Clear();
//PluginLog.Log("Sold/repaired successfully, resetting NPC db");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment