Skip to content

Commit

Permalink
fix autocargo and autorepatriate
Browse files Browse the repository at this point in the history
improve logging
minor code cleaning
bump version
implement more helper methods for future use
  • Loading branch information
kokiddp committed Apr 1, 2021
1 parent 887cfae commit 5bef897
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 50 deletions.
134 changes: 128 additions & 6 deletions TBot/Includes/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public static void LogToConsole(LogType type, LogSender sender, string message)
public static void LogToFile(LogType type, LogSender sender, string message)
{
string path = Directory.GetCurrentDirectory() + "/log";
DirectoryInfo dir = new DirectoryInfo(path);
DirectoryInfo dir = new(path);
if (!dir.Exists)
dir.Create();
string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_TBot.log";
try
{
StreamWriter file = new StreamWriter(path + "/" + fileName, true);
StreamWriter file = new(path + "/" + fileName, true);
file.WriteLine("[" + type.ToString() + "] " + "[" + sender.ToString() + "] " + "[" + DateTime.Now.ToString() + "] - " + message);
file.Close();
}
Expand Down Expand Up @@ -253,7 +253,6 @@ public static int CalcShipSpeed(Buildables buildable, int combustionDrive, int i
case Buildables.Deathstar:
baseSpeed = 100;
bonus = hyperspaceDrive * 3;
if (playerClass == Classes.General) bonus += 10;
break;
case Buildables.Battlecruiser:
baseSpeed = 10000;
Expand All @@ -276,6 +275,31 @@ public static int CalcShipSpeed(Buildables buildable, int combustionDrive, int i
return (int)Math.Round(((float)baseSpeed * ((float)bonus + 10) / 10), MidpointRounding.ToZero);
}

public static int CalcSlowestSpeed(Ships fleet, Researches researches, Classes playerClass)
{
return CalcSlowestSpeed(fleet, researches.CombustionDrive, researches.ImpulseDrive, researches.HyperspaceDrive, playerClass);
}

public static int CalcSlowestSpeed(Ships fleet, int combustionDrive, int impulseDrive, int hyperspaceDrive, Classes playerClass)
{
int lowest = int.MaxValue;
foreach (PropertyInfo prop in fleet.GetType().GetProperties())
{
long qty = (long)prop.GetValue(fleet, null);

if (qty == 0) continue;
if (Enum.TryParse<Buildables>(prop.Name, out Buildables buildable))
{
if (buildable == Buildables.SolarSatellite || buildable == Buildables.Crawler)
continue;
int speed = CalcShipSpeed(buildable, combustionDrive, impulseDrive, hyperspaceDrive, playerClass);
if (speed < lowest)
lowest = speed;
}
}
return lowest;
}

public static int CalcFleetSpeed(Ships fleet, Researches researches, Classes playerClass)
{
return CalcFleetSpeed(fleet, researches.CombustionDrive, researches.ImpulseDrive, researches.HyperspaceDrive, playerClass);
Expand All @@ -297,6 +321,104 @@ public static int CalcFleetSpeed(Ships fleet, int combustionDrive, int impulseDr
return minSpeed;
}

public static int CalcShipConsumption(Buildables buildable, Researches researches, ServerData serverData, Classes playerClass)
{
return CalcShipConsumption(buildable, researches.ImpulseDrive, researches.HyperspaceDrive, serverData.GlobalDeuteriumSaveFactor, playerClass);
}

public static int CalcShipConsumption(Buildables buildable, int impulseDrive, int hyperspaceDrive, double deuteriumSaveFactor, Classes playerClass)
{
int baseConsumption;
switch (buildable)
{
case Buildables.SmallCargo:
baseConsumption = 20;
if (impulseDrive >= 5)
baseConsumption *= 2;
break;
case Buildables.LargeCargo:
baseConsumption = 50;
break;
case Buildables.LightFighter:
baseConsumption = 20;
break;
case Buildables.HeavyFighter:
baseConsumption = 75;
break;
case Buildables.Cruiser:
baseConsumption = 300;
break;
case Buildables.Battleship:
baseConsumption = 500;
break;
case Buildables.ColonyShip:
baseConsumption = 1000;
break;
case Buildables.Recycler:
baseConsumption = 2000;
if (hyperspaceDrive >= 15)
baseConsumption *= 3;
else if (impulseDrive >= 17)
baseConsumption *= 2;
break;
case Buildables.EspionageProbe:
baseConsumption = 1;
break;
case Buildables.Bomber:
baseConsumption = 700;
if (hyperspaceDrive >= 8)
baseConsumption *= 3 / 2;
break;
case Buildables.Destroyer:
baseConsumption = 1000;
break;
case Buildables.Deathstar:
baseConsumption = 1;
break;
case Buildables.Battlecruiser:
baseConsumption = 250;
break;
case Buildables.Reaper:
baseConsumption = 1100;
break;
case Buildables.Pathfinder:
baseConsumption = 300;
break;
default:
return 0;
}
float fuelConsumption = (float)(deuteriumSaveFactor * baseConsumption);
if (playerClass == Classes.General)
fuelConsumption /= 2;
return (int)Math.Round(fuelConsumption, MidpointRounding.ToZero);
}

public static int CalcFlightTime(Coordinate origin, Coordinate destination, Ships ships, Speeds speed, Researches researches, ServerData serverData, Classes playerClass)
{
return CalcFlightTime(origin, destination, ships, speed, researches.CombustionDrive, researches.ImpulseDrive, researches.HyperspaceDrive, serverData.Galaxies, serverData.Systems, serverData.DonutGalaxy, serverData.DonutSystem, serverData.SpeedFleet, playerClass);
}

public static int CalcFlightTime(Coordinate origin, Coordinate destination, Ships ships, Speeds speed, int combustionDrive, int impulseDrive, int hyperspaceDrive, int numberOfGalaxies, int numberOfSystems, bool donutGalaxies, bool donutSystems, int fleetSpeed, Classes playerClass)
{
var fleetSpeedPercent = speed switch
{
Speeds.HundredPercent => 1,
Speeds.NinetyPercent => 0.9,
Speeds.EightyPercent => 0.8,
Speeds.SeventyPercent => 0.7,
Speeds.SixtyPercent => 0.6,
Speeds.FiftyPercent => 0.5,
Speeds.FourtyPercent => 0.4,
Speeds.ThirtyPercent => 0.3,
Speeds.TwentyPercent => 0.2,
Speeds.TenPercent => 0.1,
_ => 1,
};
int slowestShipSpeed = CalcSlowestSpeed(ships, combustionDrive, impulseDrive, hyperspaceDrive, playerClass);
int distance = CalcDistance(origin, destination, numberOfGalaxies, numberOfSystems, donutGalaxies, donutSystems);
return (int)Math.Round(((3500 / fleetSpeedPercent) * (Math.Sqrt(distance * 10 / slowestShipSpeed) + 10) / fleetSpeed), MidpointRounding.AwayFromZero);
}

public static Resources CalcMaxTransportableResources(Ships ships, Resources resources, int hyperspaceTech, Classes playerClass)
{
var capacity = CalcFleetCapacity(ships, hyperspaceTech, playerClass);
Expand Down Expand Up @@ -614,7 +736,7 @@ public static long CalcDeuteriumProduction(Planet planet, int speedFactor, float

public static Resources CalcPlanetHourlyProduction(Planet planet, int speedFactor, float ratio = 1, Researches researches = null, Classes playerClass = Classes.NoClass, bool hasGeologist = false, bool hasStaff = false)
{
Resources hourlyProduction = new Resources
Resources hourlyProduction = new()
{
Metal = CalcMetalProduction(planet, speedFactor, ratio, researches, playerClass, hasGeologist, hasStaff),
Crystal = CalcCrystalProduction(planet, speedFactor, ratio, researches, playerClass, hasGeologist, hasStaff),
Expand All @@ -625,7 +747,7 @@ public static Resources CalcPlanetHourlyProduction(Planet planet, int speedFacto

public static Resources CalcPrice(Buildables buildable, int level)
{
Resources output = new Resources();
Resources output = new();

switch (buildable)
{
Expand Down Expand Up @@ -1049,7 +1171,7 @@ public static Buildables GetNextMineToBuild(Planet planet, int maxMetalMine = 10

dic.Add(mine, CalcPrice(mine, GetNextLevel(planet, mine)).ConvertedDeuterium);
}
if (dic.Count() == 0)
if (dic.Count == 0)
return Buildables.Null;

dic = dic.OrderBy(m => m.Value)
Expand Down
24 changes: 10 additions & 14 deletions TBot/Model/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,25 @@ public enum Missions

public enum Speeds
{
TenPercent = 1,
TwentyPercent = 2,
ThirtyPercent = 3,
FourtyPercent = 4,
FiftyPercent = 5,
SixtyPercent = 6,
SeventyPercent = 7,
EightyPercent = 8,
NinetyPercent = 9,
HundredPercent = 10
}

public enum Percents
{
FivePercent = 5,
TenPercent = 10,
FifteenPercent = 15,
TwentyPercent = 20,
TwentyfivePercent = 25,
ThirtyPercent = 30,
ThirtyfivePercent = 35,
FourtyPercent = 40,
FourtyfivePercent = 45,
FiftyPercent = 50,
FiftyfivePercent = 55,
SixtyPercent = 60,
SixtyfivePercent = 65,
SeventyPercent = 70,
SeventyfivePercent = 75,
EightyPercent = 80,
EightyfivePercent = 85,
NinetyPercent = 90,
NinetyfivePercent = 95,
HundredPercent = 100
}

Expand Down
6 changes: 3 additions & 3 deletions TBot/Model/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ public class ServerData
public bool ACS { get; set; }
public bool RapidFire { get; set; }
public bool DefToTF { get; set; }
public double DebrisFactor { get; set; }
public float DebrisFactor { get; set; }
public int DebrisFactorDef { get; set; }
public double RepairFactor { get; set; }
public float RepairFactor { get; set; }
public int NewbieProtectionLimit { get; set; }
public int NewbieProtectionHigh { get; set; }
public int TopScore { get; set; }
Expand All @@ -208,7 +208,7 @@ public class ServerData
public int WfMinimumRessLost { get; set; }
public int WfMinimumLossPercentage { get; set; }
public int WfBasicPercentageRepairable { get; set; }
public double GlobalDeuteriumSaveFactor { get; set; }
public float GlobalDeuteriumSaveFactor { get; set; }
public int Bashlimit { get; set; }
public int ProbeCargo { get; set; }
public int ResearchDurationDivisor { get; set; }
Expand Down
Loading

0 comments on commit 5bef897

Please sign in to comment.