Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

considerate bus stop behaviour #1690

Merged
merged 5 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 42 additions & 0 deletions TLM/TLM/Patch/_NetLane/CalculateStopPositionAndDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace TrafficManager.Patch._NetLane;
using ColossalFramework.Math;
using HarmonyLib;
using System;
using TrafficManager.Util;
using UnityEngine;

[HarmonyPatch(typeof(NetLane), nameof(NetLane.CalculateStopPositionAndDirection))]
krzychu124 marked this conversation as resolved.
Show resolved Hide resolved
internal static class CalculateStopPositionAndDirection {
internal static bool Prefix(ref NetLane __instance, float laneOffset, float stopOffset, out Vector3 position, out Vector3 direction) {
try {
ref Bezier3 bezier = ref __instance.m_bezier;
position = bezier.Position(laneOffset);
direction = bezier.Tangent(laneOffset);
if (stopOffset != 0) {
Vector3 sideDir = Vector3.Cross(Vector3.up, direction).normalized;

const float t1 = 0.12f; // bus starts turning from this offset to avoid going over the pavement.
const float t2_0 = 0.20f; // bus does not finish turning until this offset to avoid going over the pavement
float t2 = t1 + 0.7f / (__instance.m_length + 1); // bus finishes turning at this offset for a smooth turn (if segment has space).
t2 = Mathf.Clamp(t2, t2_0, 0.5f);

float t = laneOffset <= 0.5f ? laneOffset : 1 - laneOffset;
float d;
if (t < t1) {
d = 0;
} else if (t > t2) {
d = 1;
} else {
// t=[t1:t2] => d=[0:1]
d = (t - t1) / (t2 - t1);
}

position += sideDir * stopOffset * d;
}
return false;
} catch (Exception ex) {
ex.LogException(true);
throw;
}
}
}
1 change: 1 addition & 0 deletions TLM/TLM/TLM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<Compile Include="Manager\Impl\LaneConnection\LaneEnd.cs" />
<Compile Include="Patch\_CitizenAI\GetPathTargetPositionPatch.cs" />
<Compile Include="Patch\_External\RTramAIModPatch.cs" />
<Compile Include="Patch\_NetLane\CalculateStopPositionAndDirection.cs" />
<Compile Include="Patch\_PedestrianZoneRoadAI\BollardSimulationStepPatch.cs" />
<Compile Include="Patch\_VehicleAI\InvalidPathPatch.cs" />
<Compile Include="Patch\_VehicleAI\_BusAI\CalculateSegmentPositionPatch.cs" />
Expand Down