Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Hump yard marker
- include "agmhump.gs"
- class AGMHumpYard isclass TrackMark
- {
- public bool RouteScanOverflow = false;
- public int[] RouteScanBadJunctionIds = null;
- public int[] RouteScanCycleJunctionIds = null;
- public bool AutoMode = true;
- public bool ManualMode = true;
- public bool RoutingEnabled = true;
- public float[] AutoModeCutLengthWeight;
- public float[] AutoModeTrackBaseWeight;
- public float[] AutoModeTrackSelectWeight;
- public AGMHumpRouter HumpRouter;
- public AGMHumpDecoupler HumpDecoupler;
- public AGMHumpTasks HumpTasks;
- public bool ScanRoutesRunning = false;
- public bool StopUI = false;
- public void ReportTaskDone(Vehicle veh, int trackNumber)
- {
- Soup data = Constructors.NewSoup();
- data.SetNamedTag("vehicle-id", veh.GetId());
- data.SetNamedTag("track-number", trackNumber);
- PostMessage(null, "AGMHump-298469.Notify", "TaskDone", data, 0.0);
- Interface.Log("AGMHump-298469.Notify, TaskDone"); LUtil.FlushLog();
- }
- public void ReportTaskExpired(Vehicle veh, int trackNumber)
- {
- Soup data = Constructors.NewSoup();
- data.SetNamedTag("vehicle-id", veh.GetId());
- data.SetNamedTag("track-number", trackNumber);
- PostMessage(null, "AGMHump-298469.Notify", "TaskExpired", data, 0.0);
- Interface.Log("AGMHump-298469.Notify, TaskExpired"); LUtil.FlushLog();
- }
- public void ReportRoutingFailure(Vehicle veh, int targetTrackNumber, int realTrackNumber, int junctionId)
- {
- Soup data = Constructors.NewSoup();
- data.SetNamedTag("vehicle-id", veh.GetId());
- data.SetNamedTag("target-track-number", targetTrackNumber);
- data.SetNamedTag("real-track-number", realTrackNumber);
- data.SetNamedTag("junction-id", junctionId);
- PostMessage(null, "AGMHump-298469.Notify", "RoutingFailure", data, 0.0);
- Interface.Log("AGMHump-298469.Notify, RoutingFailure"); LUtil.FlushLog();
- }
- bool scanRoutesState = false;
- thread void ScanRoutesThread()
- {
- ScanRoutesRunning = true;
- AGMHumpRouteScanResult result = HumpRouter.ScanRoutes();
- RouteScanOverflow = result.Overflow;
- if (result.BadJunctionIds)
- RouteScanBadJunctionIds = result.BadJunctionIds;
- if (result.CyclicJunctionIds)
- RouteScanCycleJunctionIds = result.CyclicJunctionIds;
- if (AutoModeTrackBaseWeight.size() != HumpRouter.Tracks.size()) {
- int oldCount = AutoModeTrackBaseWeight.size();
- int newCount = HumpRouter.Tracks.size();
- if (oldCount > newCount)
- AutoModeTrackBaseWeight[newCount, ] = null;
- else {
- float[] newWeights = new float[newCount];
- float lastWeight = 0.0f;
- int i;
- for (i = 0; i < oldCount; ++i) {
- lastWeight = AutoModeTrackBaseWeight[i];
- newWeights[i] = lastWeight;
- }
- for (; i < newCount; ++i)
- newWeights[i] = lastWeight;
- AutoModeTrackBaseWeight = newWeights;
- }
- } //*/
- LUtil.FlushLog();
- ScanRoutesRunning = false;
- scanRoutesState = false;
- }
- public void ScanRoutes()
- {
- if (scanRoutesState)
- return;
- scanRoutesState = true;
- ScanRoutesThread();
- }
- bool decouplerState = false;
- thread void RunDecouplerThread()
- {
- HumpDecoupler.MonitorLoop();
- StopUI = true;
- decouplerState = false;
- }
- public void RunDecoupler()
- {
- if (decouplerState)
- return;
- decouplerState = true;
- RunDecouplerThread();
- }
- bool taskMonitorState = false;
- thread void RunTaskMonitorThread()
- {
- HumpTasks.MonitorLoop();
- taskMonitorState = false;
- }
- public void RunTaskMonitor()
- {
- if (taskMonitorState)
- return;
- taskMonitorState = true;
- RunTaskMonitorThread();
- }
- bool uiState = false;
- thread void RunUIThread()
- {
- AGMHumpUI ui = new AGMHumpUI();
- ui.Owner = me;
- ui.MainLoop();
- uiState = false;
- }
- public void RunUI()
- {
- StopUI = false;
- if (uiState)
- return;
- uiState = true;
- RunUIThread();
- }
- int[] trackRankCars;
- int[] trackIndex;
- int[] rankIndex;
- string tos(int[] tracks)
- {
- HTMLBuffer ret = HTMLBufferStatic.Construct();
- int i, count = tracks.size();
- for (i = 0; i < count; ++i) {
- ret.Print(tracks[i]);
- if (i < count - 1)
- ret.Print("\t");
- }
- return ret.AsString();
- }
- string tos(float[] tracks)
- {
- HTMLBuffer ret = HTMLBufferStatic.Construct();
- int i, count = tracks.size();
- for (i = 0; i < count; ++i) {
- ret.Print(tracks[i]);
- if (i < count - 1)
- ret.Print("\t");
- }
- return ret.AsString();
- }
- int SelectRandomTrack(AGMHumpTrackStats[] stats, float cutLength)
- {
- int trackCount = stats.size();
- AGMHumpTrack[] tracks = HumpRouter.Tracks;
- if (!trackRankCars or trackRankCars.size() < trackCount) {
- trackRankCars = new int[trackCount];
- trackIndex = new int[trackCount];
- rankIndex = new int[trackCount + 1];
- }
- int i;
- for (i = 0; i < trackCount; ++i) {
- trackRankCars[i] = stats[i].CarCount;
- trackIndex[i] = i;
- }
- AGMHumpUtils.Sort(trackRankCars, trackIndex);
- Interface.Log("SRT> cars: " + tos(trackRankCars));
- Interface.Log("SRT> ix: " + tos(trackIndex));
- rankIndex[0] = 0;
- int ranksCount = 1;
- int lastRankedTrack = 0;
- for (i = 0; i < trackCount; ++i) {
- int index = trackIndex[i];
- bool notFilled = stats[index].UsedLength + cutLength <= stats[index].Length;
- bool notEqual = i > 0 and stats[lastRankedTrack].CarCount != stats[index].CarCount;
- if (notFilled and notEqual) {
- lastRankedTrack = index;
- rankIndex[ranksCount++] = i;
- }
- }
- rankIndex[ranksCount] = trackCount;
- Interface.Log("SRT> rank ix: " + tos(rankIndex));
- int weightsCount = AutoModeTrackSelectWeight.size();
- int maxRank = Math.Min(ranksCount, weightsCount);
- int chosenRank = -1;
- bool chosenLastRank = false;
- while (chosenRank < 0) {
- chosenRank = AGMHumpUtils.WeightedRand(AutoModeTrackSelectWeight);
- chosenLastRank = chosenRank == weightsCount - 1;
- // We're reversing rank since we sorted tracks by usage ascending
- // and weights are given by usage descending
- chosenRank = (maxRank - 1) - chosenRank;
- }
- Interface.Log("SRT> rank: " + chosenRank);
- int rankIndexStart = rankIndex[chosenRank];
- int rankIndexEnd = rankIndex[chosenRank + 1];
- if (chosenLastRank) {
- rankIndexStart = rankIndex[0];
- rankIndexEnd = rankIndex[(ranksCount - weightsCount) + 1];
- }
- int rankTrackCount = rankIndexEnd - rankIndexStart;
- int chosenRankTrack = 0;
- if (rankTrackCount > 1) {
- float[] rankTrackWeights = new float[rankTrackCount];
- for (i = 0; i < rankTrackCount; ++i) {
- int index = trackIndex[rankIndexStart + i];
- rankTrackWeights[i] = AutoModeTrackBaseWeight[index];
- }
- Interface.Log("SRT> weights: " + tos(rankTrackWeights));
- chosenRankTrack = rankIndexStart + AGMHumpUtils.WeightedRand(rankTrackWeights);
- }
- else
- chosenRankTrack = rankIndexStart;
- Interface.Log("SRT> rank index: " + chosenRankTrack);
- Interface.Log("SRT> index: " + trackIndex[chosenRankTrack]);
- LUtil.FlushLog();
- return tracks[trackIndex[chosenRankTrack]].Number;
- }
- void UpdateTrackStats(AGMHumpTrackStats[] stats, int trackNumber, int cutCount, float cutLength)
- {
- int i, statCount = stats.size();
- for (i = 0; i < statCount; ++i) {
- if (stats[i].TrackNumber == trackNumber) {
- stats[i].UsedLength = stats[i].UsedLength + cutLength;
- stats[i].CarCount = stats[i].CarCount + cutCount;
- }
- }
- }
- float GetCutLength(Vehicle[] vehs, int first, int last)
- {
- int i = Math.Max(0, Math.Min(first, last));
- int end = Math.Min(vehs.size(), Math.Max(first, last));
- float ret = 0.0f;
- for(; i < end; ++i)
- ret = ret + vehs[i].GetLength();
- return ret;
- }
- void CreateRandomTask(Train train)
- {
- Interface.Log("AGMHumpYard.CreateRandomTask"); LUtil.FlushLog();
- AGMHumpTrack[] tracks = HumpRouter.Tracks;
- int trackCount = tracks.size();
- Vehicle[] vehs = train.GetVehicles();
- int i, length = vehs.size();
- int start = 0;
- int trainDir = 1;
- if (train.GetFrontmostLocomotive() != vehs[0]) {
- start = length - 1;
- trainDir = -1;
- }
- while (Flags.Test(vehs[start].GetVehicleTypeFlags(), Vehicle.TYPE_LOCOMOTIVE)) {
- start = start + trainDir;
- if (start < 0 or start >= length)
- break;
- }
- AGMHumpTrackStats[] trackStats = HumpRouter.GetAllTracksStats();
- int cutLength = 0;
- int track = 0;
- for (i = start;
- 0 <= i and i < length;
- i = i + trainDir)
- {
- if (cutLength <= 0) {
- cutLength = 1 + AGMHumpUtils.WeightedRand(AutoModeCutLengthWeight);
- float cutLengthMeters = GetCutLength(vehs, i, i + trainDir * cutLength);
- int oldTrack = track;
- int maxTrials = 10;
- while (oldTrack == track and --maxTrials >= 0)
- track = SelectRandomTrack(trackStats, cutLengthMeters);
- UpdateTrackStats(trackStats, track, cutLength, cutLengthMeters);
- }
- HumpTasks.Add(vehs[i], track, 1200);
- Interface.Log("RandTask: '"+vehs[i].GetLocalisedName()+"' => "+track);
- --cutLength;
- }
- }
- void HandleTrainOnHump(Train train)
- {
- Interface.Log("AGMHumpYard.HandleTrainOnHump");
- if (AutoMode and !HumpTasks.ContainsAny(train.GetVehicles()))
- CreateRandomTask(train);
- RunDecoupler();
- if (ManualMode)
- RunUI();
- }
- void HandleTrainOnJunction(Train train, Junction jn)
- {
- if (!RoutingEnabled)
- return;
- Vehicle[] vehs = train.GetVehicles();
- Vehicle head = vehs[0];
- int dstTrack = HumpTasks.GetVehicleDestination(head);
- if (dstTrack > 0) {
- bool result = HumpRouter.SetJunctionToTrack(jn.GetId(), dstTrack);
- if (!result)
- dstTrack = -1;
- Interface.Print("JN '"+jn.GetLocalisedName()+"' -> "+dstTrack);
- }
- int i, count = vehs.size();
- for (i = 0; i < count; ++i) {
- int vehDstTrack = HumpTasks.GetVehicleDestination(vehs[i]);
- if (vehDstTrack != dstTrack) {
- ReportRoutingFailure(vehs[i], vehDstTrack, dstTrack, jn.GetId());
- HumpTasks.Remove(vehs[i]);
- }
- HumpTasks.SetFlags(vehs[i], AGMHumpTask.StatusRouting,
- AGMHumpTask.StatusRouting);
- }
- }
- void HandleTrainOnTrack(Train train, AGMHumpTrackMarker mrk)
- {
- Interface.Log("AGMHumpYard.HandleTrainOnTrack");
- Interface.Log("Train '" + train.GetVehicles()[0].GetLocalisedName() + "', track " + mrk.HumpTrack);
- Vehicle[] vehs = train.GetVehicles();
- int i, length = vehs.size();
- for (i = 0; i < length; ++i) {
- int taskTrack = HumpTasks.GetVehicleDestination(vehs[i]);
- if (taskTrack >= 0) {
- HumpTasks.Remove(vehs[i]);
- if (taskTrack == mrk.HumpTrack)
- ReportTaskDone(vehs[i], taskTrack);
- else
- ReportRoutingFailure(vehs[i], taskTrack, mrk.HumpTrack, 0);
- }
- }
- }
- void OnObjectEnter(Message msg)
- {
- Train src = cast <Train> msg.src;
- if (!src) return;
- if (msg.dst == me)
- HandleTrainOnHump(src);
- else if (msg.dst.isclass(Junction)) {
- Junction dst = cast <Junction> msg.dst;
- if (msg.major == "Object" and msg.minor == "InnerEnter")
- HandleTrainOnJunction(src, dst);
- }
- else if (msg.dst.isclass(AGMHumpTrackMarker)) {
- AGMHumpTrackMarker dst = cast <AGMHumpTrackMarker> msg.dst;
- if (msg.major == "Object" and msg.minor == "Enter")
- HandleTrainOnTrack(src, dst);
- }
- }
- void OnControlAddTask(Soup data)
- {
- Vehicle veh = cast <Vehicle> Router.GetGameObject(data.GetNamedTagAsInt("vehicle-id"));
- if (veh) {
- int trackNumber = data.GetNamedTagAsInt("track-number", 0);
- int ttl = data.GetNamedTagAsInt("time-to-live", 0);
- if (trackNumber > 0 and ttl > 0)
- HumpTasks.Add(veh, trackNumber, ttl);
- }
- }
- void OnControlRemoveTask(Soup data)
- {
- Vehicle veh = cast <Vehicle> Router.GetGameObject(data.GetNamedTagAsInt("vehicle-id"));
- if (veh)
- HumpTasks.Remove(veh);
- }
- void OnHumpControl(Message msg)
- {
- if (msg.minor == "AddTask") {
- if (!msg.paramSoup)
- return;
- OnControlAddTask(msg.paramSoup);
- }
- else if (msg.minor == "AddTasks") {
- Soup data = cast <Soup> msg.paramSoup;
- if (!data)
- return;
- int i, count = data.CountTags();
- for (i = 0; i < count; ++i)
- OnControlAddTask(data.GetNamedSoup(i));
- }
- else if (msg.minor == "RemoveTask") {
- Soup data = cast <Soup> msg.paramSoup;
- if (!data)
- return;
- OnControlRemoveTask(data);
- }
- else if (msg.minor == "RemoveTasks") {
- Soup data = cast <Soup> msg.paramSoup;
- if (!data)
- return;
- int i, count = data.CountTags();
- for (i = 0; i < count; ++i)
- OnControlRemoveTask(data.GetNamedSoup(i));
- }
- else if (msg.minor == "ClearTasks") {
- HumpTasks.Clear();
- }
- }
- bool initialized = false;
- void InitHandlers()
- {
- int i, jnCount = HumpRouter.Junctions.size();
- for (i = 0; i < jnCount; ++i) {
- GameObject src = Router.GetGameObject(HumpRouter.Junctions[i].JnId);
- if (src)
- Sniff(src, "Object", "InnerEnter", true);
- }
- int mrkCount = HumpRouter.Tracks.size();
- for (i = 0; i < mrkCount; ++i) {
- GameObject src = Router.GetGameObject(HumpRouter.Tracks[i].MarkerId);
- if (src)
- Sniff(src, "Object", "Enter", true);
- }
- }
- void InitObject()
- {
- Interface.Log("AGMHumpYard.InitObject");
- HumpRouter = new AGMHumpRouter();
- HumpRouter.Owner = me;
- HumpDecoupler = new AGMHumpDecoupler();
- HumpDecoupler.Owner = me;
- HumpTasks = new AGMHumpTasks();
- HumpTasks.Owner = me;
- AddHandler(me, "Object", "InnerEnter", "OnObjectEnter");
- AddHandler(me, "Object", "Enter", "OnObjectEnter");
- }
- void InitDefaults()
- {
- HumpRouter.Junctions = new AGMHumpJunction[0];
- HumpRouter.Tracks = new AGMHumpTrack[0];
- AutoModeCutLengthWeight = new float[10];
- AutoModeCutLengthWeight[0] = 15.38461538f;
- AutoModeCutLengthWeight[1] = 23.07692308f;
- AutoModeCutLengthWeight[2] = 15.38461538f;
- AutoModeCutLengthWeight[3] = 11.15384615f;
- AutoModeCutLengthWeight[4] = 8.846153846f;
- AutoModeCutLengthWeight[5] = 7.307692308f;
- AutoModeCutLengthWeight[6] = 6.153846154f;
- AutoModeCutLengthWeight[7] = 5.000000000f;
- AutoModeCutLengthWeight[8] = 4.230769231f;
- AutoModeCutLengthWeight[9] = 3.461538462f;
- AutoModeTrackBaseWeight = new float[0];
- AutoModeTrackSelectWeight = new float[6];
- AutoModeTrackSelectWeight[0] = 25.0f;
- AutoModeTrackSelectWeight[1] = 30.0f;
- AutoModeTrackSelectWeight[2] = 35.0f;
- AutoModeTrackSelectWeight[3] = 10.0f;
- AutoModeTrackSelectWeight[4] = 10.0f;
- AutoModeTrackSelectWeight[5] = 20.0f;
- }
- void OnWorldModuleInit(Message msg)
- {
- Interface.Log("AGMHumpYard.OnWorldModuleInit, Module = " + World.GetCurrentModule());
- if (World.GetCurrentModule() == World.DRIVER_MODULE) {
- InitHandlers();
- RunTaskMonitor();
- }
- }
- public void Init(Asset asset)
- {
- inherited(asset);
- Interface.Log("AGMHumpYard.Init");
- if (!initialized) {
- initialized = true;
- InitObject();
- }
- AddHandler(me, "World", "ModuleInit", "OnWorldModuleInit");
- AddHandler(me, "AGMHump-298469.Control", "", "OnHumpControl");
- }
- public Soup GetProperties()
- {
- Interface.Log("AGMHumpYard.GetProperties");
- Soup sp = inherited();
- Soup hump = sp.GetNamedSoupAdd("agmhump");
- hump.SetNamedSoup("router", HumpRouter.GetProperties());
- hump.SetNamedSoup("tasks", HumpTasks.GetProperties());
- hump.SetNamedSoup("decoupler", HumpDecoupler.GetProperties());
- hump.SetNamedTag("auto-mode", AutoMode);
- hump.SetNamedTag("manual-mode", ManualMode);
- hump.SetNamedTag("routing", RoutingEnabled);
- hump.SetNamedSoup("auto-mode-cut-length-weight", AGMHumpUtils.ToSoup(AutoModeCutLengthWeight));
- hump.SetNamedSoup("auto-mode-track-base-weight", AGMHumpUtils.ToSoup(AutoModeTrackBaseWeight));
- hump.SetNamedSoup("auto-mode-track-select-weight", AGMHumpUtils.ToSoup(AutoModeTrackSelectWeight));
- return sp;
- }
- public void SetProperties(Soup sp)
- {
- Interface.Log("AGMHumpYard.SetProperties");
- inherited(sp);
- Soup hump = sp.GetNamedSoup("agmhump");
- if (hump.CountTags() <= 0) {
- InitDefaults();
- return;
- }
- HumpRouter.SetProperties(hump.GetNamedSoup("router"));
- HumpTasks.SetProperties(hump.GetNamedSoup("tasks"));
- HumpDecoupler.SetProperties(hump.GetNamedSoup("decoupler"));
- AutoMode = hump.GetNamedTagAsBool("auto-mode");
- ManualMode = hump.GetNamedTagAsBool("manual-mode");
- RoutingEnabled = hump.GetNamedTagAsBool("routing");
- AutoModeCutLengthWeight = AGMHumpUtils.ToFloatArray(hump.GetNamedSoup("auto-mode-cut-length-weight"));
- AutoModeTrackBaseWeight = AGMHumpUtils.ToFloatArray(hump.GetNamedSoup("auto-mode-track-base-weight"));
- AutoModeTrackSelectWeight = AGMHumpUtils.ToFloatArray(hump.GetNamedSoup("auto-mode-track-select-weight"));
- }
- final string Case(bool cond, string ifTrue, string ifFalse)
- {
- if (cond) return ifTrue;
- return ifFalse;
- }
- string TrackString(int[] tracks)
- {
- HTMLBuffer ret = HTMLBufferStatic.Construct();
- int i, count = tracks.size();
- for (i = 0; i < count; ++i) {
- ret.Print(tracks[i]);
- if (i < count - 1)
- ret.Print(",");
- }
- return ret.AsString();
- }
- float Sum(float[] a)
- {
- if (!a)
- return 0.0f;
- int i, len = a.size();
- float sum = 0.0f;
- for (i = 0; i < len; ++i)
- sum = sum + a[i];
- return sum;
- }
- string ProbPercent(float w, float sum)
- {
- if (sum == 0.0f)
- return "0";
- return (string) (int) ((w / sum * 100.0f) + 0.5f);
- }
- bool HasErrors()
- {
- return RouteScanOverflow or RouteScanBadJunctionIds or RouteScanCycleJunctionIds;
- }
- void ClearErrors()
- {
- RouteScanOverflow = false;
- RouteScanBadJunctionIds = null;
- RouteScanCycleJunctionIds = null;
- }
- bool ShowRoutesHTML = false;
- bool ShowTracksHTML = false;
- StringTable ST;
- final string T(string s)
- {
- return ST.GetString0(s);
- //return "T(\"" + s + "\")";
- //return s;
- }
- final string T(string s, string arg0)
- {
- return ST.GetString1(s, arg0);
- //return "T(\"" + s + "\")";
- //return s;
- }
- final string P(string pID)
- {
- return "live://property/" + pID;
- //return "live://property/" + pID + "'><" + pID + "></a><a href='";
- }
- void ModeHTML(HTMLBuffer buf)
- {
- HTMLWindow h = HTMLWindow;
- buf.Print(h.StartTable());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-mode-auto-title"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(h.CheckBox(P("mode-auto"), AutoMode));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-mode-manual-title"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(h.CheckBox(P("mode-manual"), ManualMode));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-routing-title"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(h.CheckBox(P("routing"), RoutingEnabled));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.EndTable());
- }
- void DecouplerHTML(HTMLBuffer buf)
- {
- HTMLWindow h = HTMLWindow;
- buf.Print(h.StartTable());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-decoupler-max-length-title"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(h.MakeLink(P("decoupler-max-length"), HumpDecoupler.MaxCutLength));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-decoupler-max-heading-title"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(h.MakeLink(P("decoupler-max-heading"), HumpDecoupler.MaxHeading));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.EndTable());
- }
- void RouterHTML(HTMLBuffer buf)
- {
- HTMLWindow h = HTMLWindow;
- buf.Print(h.StartTable());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-router-scan-title"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- if (!ScanRoutesRunning)
- buf.Print(h.MakeLink(P("router-scan"), T("html-router-scan")));
- else
- buf.Print(h.MakeLink(P("router-scan-refresh"), T("html-router-scan-refresh")));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print("");
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(h.MakeLink(P("router-set-route"), T("html-router-set-route")));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.EndTable());
- if (HasErrors()) {
- buf.Print(h.StartTable("bgcolor=#ff0000"));
- buf.Print(h.StartRow());
- buf.Print(h.MakeCell(h.MakeBold(T("html-router-errors"))));
- buf.Print(h.EndRow());
- if (RouteScanOverflow) {
- buf.Print(h.StartRow());
- buf.Print(h.MakeCell(T("html-router-error-overflow")));
- buf.Print(h.EndRow());
- }
- if (RouteScanBadJunctionIds) {
- int i, count = RouteScanBadJunctionIds.size();
- for (i = 0; i < count; ++i) {
- Junction jn = cast <Junction> Router.GetGameObject(RouteScanBadJunctionIds[i]);
- string name = Case(jn != null, jn.GetLocalisedName(), "");
- buf.Print(h.StartRow());
- buf.Print(h.MakeCell(T("html-router-error-bad-junction", name)));
- buf.Print(h.EndRow());
- }
- }
- if (RouteScanCycleJunctionIds) {
- int i, count = RouteScanCycleJunctionIds.size();
- for (i = 0; i < count; ++i) {
- Junction jn = cast <Junction> Router.GetGameObject(RouteScanCycleJunctionIds[i]);
- string name = Case(jn != null, jn.GetLocalisedName(), "");
- buf.Print(h.StartRow());
- buf.Print(h.MakeCell(T("html-router-error-cycle-junction", name)));
- buf.Print(h.EndRow());
- }
- }
- buf.Print(h.EndTable());
- ClearErrors();
- }
- }
- void RoutesHTML(HTMLBuffer buf)
- {
- HTMLWindow h = HTMLWindow;
- buf.Print(h.StartTable());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-routes-junction-header"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(T("html-routes-left-tracks-header"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(T("html-routes-right-tracks-header"));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- int i, count = HumpRouter.Junctions.size();
- for (i = 0; i < count; ++i) {
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- Junction junc = cast <Junction>
- Router.GetGameObject(HumpRouter.Junctions[i].JnId);
- buf.Print(junc.GetLocalisedName());
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(TrackString(HumpRouter.Junctions[i].LeftTracks));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(TrackString(HumpRouter.Junctions[i].RightTracks));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- }
- buf.Print(h.EndTable());
- }
- void TracksHTML(HTMLBuffer buf)
- {
- HTMLWindow h = HTMLWindow;
- buf.Print(h.StartTable());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-tracks-number-header"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(T("html-tracks-length-header"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(T("html-tracks-weight-header"));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- int i, count = HumpRouter.Tracks.size();
- float weightSum = Sum(AutoModeTrackBaseWeight);
- bool baseWeightSet = AutoModeTrackBaseWeight
- and AutoModeTrackBaseWeight.size() == count;
- for (i = 0; i < count; ++i) {
- int trackNumber = HumpRouter.Tracks[i].Number;
- AGMHumpTrackStats stats = HumpRouter.GetTrackStats(trackNumber);
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(trackNumber);
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- int length = (int) (stats.Length + 0.5f);
- if (!stats.HasEndMarker)
- buf.Print(h.MakeItalic(length));
- else
- buf.Print(length);
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- buf.Print(h.StartLink(P("track-weight#"+i)));
- buf.Print(AutoModeTrackBaseWeight[i]);
- buf.Print(" (");
- buf.Print(ProbPercent(AutoModeTrackBaseWeight[i], weightSum));
- buf.Print("%)");
- buf.Print(h.EndLink());
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- }
- buf.Print(h.EndTable());
- }
- void AutoModeHTML(HTMLBuffer buf)
- {
- HTMLWindow h = HTMLWindow;
- buf.Print(h.StartTable());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-auto-track-select-weights-title"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- int i, count = AutoModeTrackSelectWeight.size();
- float weightSum = Sum(AutoModeTrackSelectWeight);
- for (i = 0; i < count; ++i) {
- buf.Print(h.StartLink(P("auto-track-select-weights#"+i)));
- buf.Print(AutoModeTrackSelectWeight[i]);
- buf.Print(" (");
- buf.Print(ProbPercent(AutoModeTrackSelectWeight[i], weightSum));
- buf.Print("%)");
- buf.Print(h.EndLink());
- if (i < count - 1)
- buf.Print(", ");
- }
- if (count == 0)
- buf.Print(h.MakeLink(P("auto-track-select-weights#0"), " "));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(T("html-auto-cut-length-weights-title"));
- buf.Print(h.EndCell());
- buf.Print(h.StartCell());
- count = AutoModeCutLengthWeight.size();
- weightSum = Sum(AutoModeCutLengthWeight);
- for (i = 0; i < count; ++i) {
- buf.Print(h.StartLink(P("auto-cut-length-weights#"+i)));
- buf.Print(AutoModeCutLengthWeight[i]);
- buf.Print(" (");
- buf.Print(ProbPercent(AutoModeCutLengthWeight[i], weightSum));
- buf.Print("%)");
- buf.Print(h.EndLink());
- if (i < count - 1)
- buf.Print(", ");
- }
- if (count == 0)
- buf.Print(h.MakeLink(P("auto-cut-length-weights#0"), " "));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.EndTable());
- }
- public string GetDescriptionHTML(void)
- {
- ST = GetAsset().GetStringTable();
- HTMLBuffer buf = HTMLBufferStatic.Construct();
- HTMLWindow h = HTMLWindow;
- StringTable st = GetAsset().GetStringTable();
- buf.Print("<html><body>");
- buf.Print(h.StartTable());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(h.MakeBold(h.MakeFontSizeLarge(h.MakeFontColor(T("html-title"), "ff0000"))));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- ModeHTML(buf);
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- DecouplerHTML(buf);
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- if (RoutingEnabled) {
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- RouterHTML(buf);
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(h.MakeLink(P("show-routes"), Case(ShowRoutesHTML, T("html-routes-hide"), T("html-routes-show"))));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- if (ShowRoutesHTML) {
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- RoutesHTML(buf);
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- }
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- buf.Print(h.MakeLink(P("show-tracks"), Case(ShowTracksHTML, T("html-tracks-hide"), T("html-tracks-show"))));
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- if (ShowTracksHTML) {
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- TracksHTML(buf);
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- }
- if (AutoMode) {
- buf.Print(h.StartRow());
- buf.Print(h.StartCell());
- AutoModeHTML(buf);
- buf.Print(h.EndCell());
- buf.Print(h.EndRow());
- }
- }
- buf.Print(h.EndTable());
- buf.Print("</body></html>");
- string ret = buf.AsString();
- LUtil.FlushLog();
- ST = null;
- return ret;
- }
- string GetPropertyName(string propertyID)
- {
- if ( propertyID == "decoupler-max-length"
- or propertyID == "decoupler-max-heading"
- or propertyID == "router-set-route")
- return GetAsset().GetStringTable().GetString0("desc-" + propertyID);
- else if ( TrainUtil.HasPrefix(propertyID, "track-weight#")
- or TrainUtil.HasPrefix(propertyID, "auto-track-select-weights#")
- or TrainUtil.HasPrefix(propertyID, "auto-cut-length-weights#")) {
- string[] parts = Str.Tokens(propertyID, "#");
- propertyID = parts[0];
- int index = Str.ToInt(parts[1]);
- if (propertyID == "track-weight")
- index = HumpRouter.Tracks[index].Number;
- else if (propertyID == "auto-track-select-weights")
- index = index + 1;
- else if (propertyID == "auto-cut-length-weights")
- index = index + 1;
- return GetAsset().GetStringTable().GetString1("desc-" + propertyID, index);
- }
- return inherited(propertyID);
- }
- string GetPropertyDescription(string propertyID)
- {
- if ( propertyID == "decoupler-max-length"
- or propertyID == "decoupler-max-heading"
- or propertyID == "router-set-route")
- return GetAsset().GetStringTable().GetString0("desc-" + propertyID);
- else if ( TrainUtil.HasPrefix(propertyID, "track-weight#")
- or TrainUtil.HasPrefix(propertyID, "auto-track-select-weights#")
- or TrainUtil.HasPrefix(propertyID, "auto-cut-length-weights#")) {
- string[] parts = Str.Tokens(propertyID, "#");
- propertyID = parts[0];
- int index = Str.ToInt(parts[1]);
- if (propertyID == "track-weight")
- index = HumpRouter.Tracks[index].Number;
- else if (propertyID == "auto-track-select-weights")
- index = index + 1;
- else if (propertyID == "auto-cut-length-weights")
- index = index + 1;
- return GetAsset().GetStringTable().GetString1("desc-" + propertyID, index);
- }
- return inherited(propertyID);
- }
- string GetPropertyType(string propertyID)
- {
- if (propertyID == "mode-auto")
- return "link";
- else if (propertyID == "mode-manual")
- return "link";
- else if (propertyID == "routing")
- return "link";
- else if (propertyID == "decoupler-max-length")
- return "int,1,9,1";
- else if (propertyID == "decoupler-max-heading")
- return "float,0,100,1";
- else if (propertyID == "router-scan")
- return "link";
- else if (propertyID == "router-scan-refresh")
- return "link";
- else if (propertyID == "router-set-route")
- return "int,1,999,1";
- else if (propertyID == "show-routes")
- return "link";
- else if (propertyID == "show-tracks")
- return "link";
- else if (TrainUtil.HasPrefix(propertyID, "track-weight#"))
- return "float,0,1000,1";
- else if (TrainUtil.HasPrefix(propertyID, "auto-track-select-weights#"))
- return "string";
- else if (TrainUtil.HasPrefix(propertyID, "auto-cut-length-weights#"))
- return "string";
- return inherited(propertyID);
- }
- string GetPropertyValue(string propertyID)
- {
- if (propertyID == "decoupler-max-length")
- return (string) HumpDecoupler.MaxCutLength;
- else if (propertyID == "decoupler-max-heading")
- return (string) HumpDecoupler.MaxHeading;
- else if (propertyID == "router-set-route")
- return "";
- else if ( TrainUtil.HasPrefix(propertyID, "track-weight#")
- or TrainUtil.HasPrefix(propertyID, "auto-track-select-weights#")
- or TrainUtil.HasPrefix(propertyID, "auto-cut-length-weights#")) {
- string[] parts = Str.Tokens(propertyID, "#");
- propertyID = parts[0];
- int index = Str.ToInt(parts[1]);
- float[] data;
- if (propertyID == "track-weight")
- data = AutoModeTrackBaseWeight;
- else if (propertyID == "auto-track-select-weights")
- data = AutoModeTrackSelectWeight;
- else if (propertyID == "auto-cut-length-weights")
- data = AutoModeCutLengthWeight;
- if (index < data.size())
- return (string) data[index];
- return "";
- }
- return inherited(propertyID);
- }
- void SetPropertyValue(string propertyID, int value)
- {
- if (propertyID == "decoupler-max-length") {
- HumpDecoupler.MaxCutLength = value;
- return;
- }
- else if (propertyID == "router-set-route") {
- HumpRouter.SetRouteToTrack(value);
- return;
- }
- inherited(propertyID, value);
- }
- void SetPropertyValue(string propertyID, float value)
- {
- if (propertyID == "decoupler-max-heading") {
- HumpDecoupler.MaxHeading = value;
- return;
- }
- else if (TrainUtil.HasPrefix(propertyID, "track-weight#")) {
- string[] parts = Str.Tokens(propertyID, "#");
- int index = Str.ToInt(parts[1]);
- AutoModeTrackBaseWeight[index] = value;
- return;
- }
- inherited(propertyID, value);
- }
- void SetPropertyValue(string propertyID, string value)
- {
- if ( TrainUtil.HasPrefix(propertyID, "auto-track-select-weights#")
- or TrainUtil.HasPrefix(propertyID, "auto-cut-length-weights#")) {
- string[] parts = Str.Tokens(propertyID, "#");
- propertyID = parts[0];
- int index = Str.ToInt(parts[1]);
- float[] data;
- if (propertyID == "auto-track-select-weights")
- data = AutoModeTrackSelectWeight;
- else if (propertyID == "auto-cut-length-weights")
- data = AutoModeCutLengthWeight;
- string[] valueTokens = Str.Tokens(value, ",");
- float[] values = new float[valueTokens.size()];
- int i;
- for (i = 0; i < valueTokens.size(); ++i)
- values[i] = Str.ToFloat(valueTokens[i]);
- data[index, index + 1] = values;
- return;
- }
- inherited(propertyID, value);
- }
- void LinkPropertyValue(string propertyID)
- {
- if (propertyID == "mode-auto") {
- AutoMode = !AutoMode;
- if (AutoMode)
- RoutingEnabled = true;
- return;
- }
- else if (propertyID == "mode-manual") {
- ManualMode = !ManualMode;
- if (ManualMode)
- RoutingEnabled = true;
- return;
- }
- else if (propertyID == "routing") {
- RoutingEnabled = !RoutingEnabled;
- if (!RoutingEnabled) {
- AutoMode = false;
- ManualMode = false;
- }
- return;
- }
- else if (propertyID == "router-scan") {
- ScanRoutes();
- return;
- }
- else if (propertyID == "router-scan-refresh") {
- return;
- }
- else if (propertyID == "show-routes") {
- ShowRoutesHTML = !ShowRoutesHTML;
- return;
- }
- else if (propertyID == "show-tracks") {
- ShowTracksHTML = !ShowTracksHTML;
- return;
- }
- inherited(propertyID);
- } //*/
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement