Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b64e349
Reformat CSUtil.Commons/*
kvakvs Jul 16, 2019
cd692c4
Partial formatting; Debug switches now accessed via Enum
kvakvs Jul 16, 2019
fe75603
Modernize CustomCarAI
kvakvs Jul 16, 2019
4e15a86
Modernize CargoTruckAI
kvakvs Jul 16, 2019
9257f2b
DebugSettings now can be accessed via static properties = shorter code
kvakvs Jul 16, 2019
4fe6ccd
Modernize CustomHumanAI
kvakvs Jul 16, 2019
680f7b0
Modernize CustomPassengerCarAI
kvakvs Jul 16, 2019
9914685
Modernize Custom{PoliceCar, PostVan, Resident, RoadBase, Ship, Taxi, …
kvakvs Jul 16, 2019
63eab24
Modernize CustomTrainAI
kvakvs Jul 16, 2019
5832f6c
Modernize CustomTramBaseAI
kvakvs Jul 16, 2019
35ff979
Modernize CustomVehicleAI
kvakvs Jul 16, 2019
1f02a0c
Restore base. in calls, apparently it is important
kvakvs Jul 16, 2019
cd62d13
Modernize CustomPathFind (v1)
kvakvs Jul 16, 2019
cfe7d49
Modernize CustomPathManager, StockPathFind
kvakvs Jul 17, 2019
ada982a
In Release logLogic variable still had its old name: debug
kvakvs Jul 17, 2019
7745725
Revert and untabify StockPathFind
kvakvs Jul 17, 2019
50a46fa
Modernize AdvancedParkingManager & new shorter log invocations
kvakvs Jul 17, 2019
e04fc4d
Log simplification
kvakvs Jul 17, 2019
57f70d6
Restore performance in Debug
kvakvs Jul 18, 2019
deb9e3b
Refactor/rename pathfinding module; Dump other settings into editorco…
kvakvs Jul 18, 2019
cb59e99
Minor editorconfig changes
kvakvs Jul 18, 2019
cb01465
Change var to types in affected files: CSUtil.Common, TLM/Custom
kvakvs Jul 19, 2019
a1fd3b6
Review notes on #430; Removed DEBUGGEO now DEBUG
kvakvs Jul 20, 2019
103d3b8
Removed old PF, renamed PF2 define to PF_DIJKSTRA
kvakvs Jul 20, 2019
f94d046
Simplify SegmentEndId Equals
kvakvs Jul 20, 2019
985ea26
Rider/Resharper style settings: Use var when type is evident, otherwi…
kvakvs Jul 20, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions TLM/CSUtil.Commons/ArrowDirection.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CSUtil.Commons {
public enum ArrowDirection {
None = 0,
Left = 1,
Forward = 2,
Right = 3,
Turn = 4
}
}
namespace CSUtil.Commons {
public enum ArrowDirection {
None = 0,
Left = 1,
Forward = 2,
Right = 3,
Turn = 4
}
}
87 changes: 14 additions & 73 deletions TLM/CSUtil.Commons/ArrowDirectionUtil.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSUtil.Commons {
public class ArrowDirectionUtil {
public static ArrowDirection InvertLeftRight(ArrowDirection dir) {
if (dir == ArrowDirection.Left)
dir = ArrowDirection.Right;
else if (dir == ArrowDirection.Right)
dir = ArrowDirection.Left;
return dir;
}

/// <summary>
/// Calculates the direction of <paramref name="toRelDir"/> in relation to ArrowDirection.TURN.
/// </summary>
/// <param name="fromDir">source direction</param>
/// <param name="toRelDir">target direction, relative to <paramref name="fromDir"/></param>
/// <returns></returns>
public static ArrowDirection MakeAbsolute(ArrowDirection fromDir, ArrowDirection toRelDir) {
if (fromDir == ArrowDirection.None) {
// invalid direction
return ArrowDirection.None;
}

if (toRelDir == ArrowDirection.None) {
// invalid direction
return ArrowDirection.None;
}

if (fromDir == ArrowDirection.Turn) {
// toRelDir is already relative to TURN
return toRelDir;
}

if (toRelDir == ArrowDirection.Turn) {
// toRelDir is fromDir
return fromDir;
}

int fromDirBase0 = (int)fromDir - 1;
int toRelDirBase1 = (int)toRelDir;
/*
* Direction | Base 0 | Base 1
* ==========+========+=======
* Left | 0 | 1
* Forward | 1 | 2
* Right | 2 | 3
*
*
* Direction 1 | Direction 2 | Dir. 1 B0 | Dir. 2 B1 | Sum | (Sum + 1) % 4 | Desired dir.
* ============+=============+===========+===========+=====|===============+=============
* Left | Left | 0 | 1 | 1 | 2 | (Forward, 2)
* Left | Forward | 0 | 2 | 2 | 3 | (Right, 3)
* Left | Right | 0 | 3 | 3 | 0 | (Turn, 4)
* Forward | Left | 1 | 1 | 2 | 3 | (Right, 3)
* Forward | Forward | 1 | 2 | 3 | 0 | (Turn, 4)
* Forward | Right | 1 | 3 | 4 | 1 | (Left, 1)
* Right | Left | 2 | 1 | 3 | 0 | (Turn, 4)
* Right | Forward | 2 | 2 | 4 | 1 | (Left, 1)
* Right | Right | 2 | 3 | 5 | 2 | (Forward, 2)
*/

int ret = (fromDirBase0 + toRelDirBase1 + 1) % 4;
if (ret == 0) {
ret = 4;
}
return (ArrowDirection)ret;
}
}
}
namespace CSUtil.Commons {
public static class ArrowDirectionUtil {
public static ArrowDirection InvertLeftRight(ArrowDirection dir) {
switch (dir) {
case ArrowDirection.Left:
return ArrowDirection.Right;
case ArrowDirection.Right:
return ArrowDirection.Left;
}

return dir;
}
}
}
21 changes: 10 additions & 11 deletions TLM/CSUtil.Commons/EnumUtil.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSUtil.Commons {
using System;
using System.Collections.Generic;
using System.Linq;

namespace CSUtil.Commons {
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
}
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
}
127 changes: 66 additions & 61 deletions TLM/CSUtil.Commons/Log.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,78 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using UnityEngine;
namespace CSUtil.Commons {
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using UnityEngine;

namespace CSUtil.Commons {
public static class Log {
private static readonly object LogLock_ = new object();

public static class Log {
private enum LogLevel {
Trace,
Debug,
Info,
Warning,
Error
}
// TODO refactor log filename to configuration
private static readonly string LogFilename_
= Path.Combine(Application.dataPath, "TMPE.log");

private static object logLock = new object();
private enum LogLevel {
Trace,
Debug,
Info,
Warning,
Error
}

private static string logFilename = Path.Combine(Application.dataPath, "TMPE.log"); // TODO refactor log filename to configuration
private static Stopwatch sw = Stopwatch.StartNew();
private static Stopwatch sw = Stopwatch.StartNew();

static Log() {
try {
if (File.Exists(logFilename)) {
File.Delete(logFilename);
}
} catch (Exception) {

}
}
static Log() {
try {
if (File.Exists(LogFilename_)) {
File.Delete(LogFilename_);
}
}
catch (Exception) { }
}

[Conditional("DEBUG")]
public static void _Debug(string s) {
LogToFile(s, LogLevel.Debug);
}
[Conditional("DEBUG")]
public static void _Debug(string s) {
LogToFile(s, LogLevel.Debug);
}

[Conditional("TRACE")]
public static void _Trace(string s) {
LogToFile(s, LogLevel.Trace);
}
[Conditional("TRACE")]
public static void _Trace(string s) {
LogToFile(s, LogLevel.Trace);
}

public static void Info(string s) {
LogToFile(s, LogLevel.Info);
}
public static void Info(string s) {
LogToFile(s, LogLevel.Info);
}

public static void Warning(string s) {
LogToFile(s, LogLevel.Warning);
}
[Conditional("DEBUG")]
public static void _DebugOnlyWarning(string s) {
LogToFile(s, LogLevel.Warning);
}

public static void Error(string s) {
LogToFile(s, LogLevel.Error);
}
public static void Warning(string s) {
LogToFile(s, LogLevel.Warning);
}

private static void LogToFile(string log, LogLevel level) {
try {
Monitor.Enter(logLock);

using (StreamWriter w = File.AppendText(logFilename)) {
w.WriteLine($"[{level.ToString()}] @ {sw.ElapsedTicks} {log}");
if (level == LogLevel.Warning || level == LogLevel.Error) {
w.WriteLine((new System.Diagnostics.StackTrace()).ToString());
w.WriteLine();
}
}
} finally {
Monitor.Exit(logLock);
}
}
}
public static void Error(string s) {
LogToFile(s, LogLevel.Error);
}

}
private static void LogToFile(string log, LogLevel level) {
try {
Monitor.Enter(LogLock_);

using (StreamWriter w = File.AppendText(LogFilename_)) {
w.WriteLine($"[{level.ToString()}] @ {sw.ElapsedTicks} {log}");
if (level == LogLevel.Warning || level == LogLevel.Error) {
w.WriteLine((new System.Diagnostics.StackTrace()).ToString());
w.WriteLine();
}
}
}
finally {
Monitor.Exit(LogLock_);
}
}
}
}
27 changes: 11 additions & 16 deletions TLM/CSUtil.Commons/LogicUtil.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSUtil.Commons {
public static class LogicUtil {
public static bool CheckFlags(uint flags, uint flagMask, uint? expectedResult = null) {
var res = flags & flagMask;
if (expectedResult == null) {
return res != 0;
}

namespace CSUtil.Commons {
public static class LogicUtil {
public static bool CheckFlags(uint flags, uint flagMask, uint? expectedResult=null) {
uint res = flags & flagMask;
if (expectedResult == null) {
return res != 0;
} else {
return res == expectedResult;
}
}
}
}
return res == expectedResult;
}
}
}
22 changes: 10 additions & 12 deletions TLM/CSUtil.Commons/TernaryBool.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSUtil.Commons {
public enum TernaryBool {
Undefined = 0,
False = 1,
True = 2
}
}
namespace CSUtil.Commons {
/// <summary>
/// Tri-bool: a boolean value which can also be undefined
/// </summary>
public enum TernaryBool {
Undefined = 0,
False = 1,
True = 2
}
}
59 changes: 28 additions & 31 deletions TLM/CSUtil.Commons/TernaryBoolUtil.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSUtil.Commons {
using System;

namespace CSUtil.Commons {
public static class TernaryBoolUtil {
public static bool ToBool(TernaryBool tb) {
if (tb == TernaryBool.Undefined) {
throw new ArgumentException("Cannot determine boolean value for undefined ternary bool");
}
public static class TernaryBoolUtil {
public static bool ToBool(TernaryBool tb) {
if (tb == TernaryBool.Undefined) {
throw new ArgumentException("Cannot determine boolean value for undefined ternary bool");
}

return tb == TernaryBool.True;
}
return tb == TernaryBool.True;
}

public static bool? ToOptBool(TernaryBool tb) {
if (tb == TernaryBool.Undefined) {
return null;
}
public static bool? ToOptBool(TernaryBool tb) {
if (tb == TernaryBool.Undefined) {
return null;
}

return tb == TernaryBool.True;
}
return tb == TernaryBool.True;
}

public static TernaryBool ToTernaryBool(bool? b) {
switch (b) {
case null:
default:
return TernaryBool.Undefined;
case false:
return TernaryBool.False;
case true:
return TernaryBool.True;
}
}
}
}
public static TernaryBool ToTernaryBool(bool? b) {
switch (b) {
case null:
default:
return TernaryBool.Undefined;
case false:
return TernaryBool.False;
case true:
return TernaryBool.True;
}
}
}
}
Loading