Train and autopilot tried in the game: prefab, wagons upright, no circling
The train piece is in the scene's prefab list, its icon is made, and the wagons keep the axis turn their FBX root brings. The autopilot gives up on a point it has circled for seven seconds without getting closer, and takes sharp turns close by slowly. -demoauto: on the easy planet the tractor unloads and goes back to mowing, and the train lays 826 m of track to Timothy, closes 532 sectors and earns about 13.7k by the time its reach is mown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
77
Assets/Scripts/Core/DemoBot.Auto.cs
Normal file
77
Assets/Scripts/Core/DemoBot.Auto.cs
Normal file
@ -0,0 +1,77 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GrassCutter
|
||||
{
|
||||
public partial class DemoBot
|
||||
{
|
||||
/// <summary>
|
||||
/// -demoauto [-difficulty 2]: a fresh planet; a mowing tractor on autopilot with its hopper nearly full (it should go
|
||||
/// and unload, then come back to mow), and a grass train laying its track to the next station and starting to run.
|
||||
/// Time goes six times as fast. Logs both every ten seconds and takes pictures of them.
|
||||
/// </summary>
|
||||
IEnumerator AutoDemo()
|
||||
{
|
||||
TeamState.DeleteSave();
|
||||
yield return new WaitForSeconds(2f);
|
||||
NetSession.Instance.HostLocal();
|
||||
while (!Me || !Ts) yield return null;
|
||||
yield return new WaitForSeconds(3f);
|
||||
Clock(11f);
|
||||
Log($"{Difficulties.Name(Difficulties.Current)} gen {Difficulties.Gen}: radius {Planet.Radius:0} m, sector {GameDefs.SectorArea:0.0} m², planet {GameDefs.PlanetArea / 1e6:0.00} km², wire {Ts.Size.Value}");
|
||||
Log($"perks {Ts.Perks.Count} of {(int)PerkId.Count}, installs {TeamState.TotalInstalls}");
|
||||
Ts.ServerSetMoney(200000d);
|
||||
double before = Ts.Money.Value;
|
||||
Ts.BuyPerkRpc((int)PerkId.BotReach);
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
Log($"Long Leash bought: {before - Ts.Money.Value:0} paid, level {Ts.Level(PerkId.BotReach)}");
|
||||
|
||||
Quaternion Face(Vector3 p, Vector3 way) => Quaternion.LookRotation(Vector3.ProjectOnPlane(way, Planet.Up(p)), Planet.Up(p));
|
||||
var tAt = Soil17(70f, 60f);
|
||||
var tractor = Spawner.Instance.SpawnVehicle(VehicleKind.Tractor, Me.SaveId, tAt + Planet.Up(tAt) * 1.2f, Face(tAt, Vector3.right));
|
||||
yield return new WaitForSeconds(1f);
|
||||
if (!tractor) { Log("tractor NOT spawned"); yield break; }
|
||||
before = Ts.Money.Value;
|
||||
Ts.TuneVehicleRpc(tractor.NetworkObjectId, (byte)TuneId.Autopilot);
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
Log($"autopilot bought for {before - Ts.Money.Value:0}: CanAuto {tractor.CanAuto}");
|
||||
tractor.Tank.Value = tractor.Capacity - 25f;
|
||||
tractor.ServerStartAuto(tractor.transform.position, tractor.transform.forward);
|
||||
|
||||
var sAt = Soil17(-40f, -70f);
|
||||
var piece = Spawner.Instance.SpawnPiece(PieceId.Train, sAt, Face(sAt, Vector3.ProjectOnPlane(sAt - Planet.FromBase(0f, 0f) * Planet.Radius, Planet.Up(sAt))), sAt, true);
|
||||
yield return new WaitForSeconds(1f);
|
||||
var train = piece ? piece.GetComponent<Train>() : null;
|
||||
Log($"train built: {(train ? "ok" : "MISSING")}");
|
||||
|
||||
Time.timeScale = 6f;
|
||||
var start = tractor.transform.position;
|
||||
double money = Ts.Money.Value;
|
||||
for (int k = 1; k <= 18; k++)
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(10f);
|
||||
Log($"t+{k * 60} game s: earned {Ts.Money.Value - money:0}, closed {Ts.Closed.Value}");
|
||||
Log($" tractor: {tractor.Job}, {GameDefs.Kilos(tractor.Tank.Value)} of {GameDefs.Kilos(tractor.Capacity)}, {Vector3.Distance(start, tractor.transform.position):0} m from start, speed {tractor.Body.linearVelocity.magnitude:0.0}, tilt {Vector3.Angle(tractor.transform.up, Planet.Up(tractor.transform.position)):0} deg");
|
||||
if (train) Log($" train: {train.Prompt}");
|
||||
if (k == 3 || k == 12)
|
||||
{
|
||||
Time.timeScale = 1f;
|
||||
var tp = tractor.transform.position;
|
||||
var up = Planet.Up(tp);
|
||||
yield return Look($"auto{k:00}_tractor", tp + up * 9f + Vector3.ProjectOnPlane(Vector3.forward, up).normalized * -14f, tp, 0.3f);
|
||||
if (train && train.loco)
|
||||
{
|
||||
var lp = train.loco.position;
|
||||
var lu = Planet.Up(lp);
|
||||
yield return Look($"auto{k:00}_train", lp + lu * 6f + Vector3.ProjectOnPlane(Vector3.right, lu).normalized * 12f, lp, 0.3f);
|
||||
}
|
||||
if (piece) yield return Look($"auto{k:00}_station", sAt + Planet.Up(sAt) * 10f - piece.transform.forward * 14f, sAt + piece.transform.forward * 10f, 0.3f);
|
||||
Time.timeScale = 6f;
|
||||
}
|
||||
}
|
||||
Time.timeScale = 1f;
|
||||
Log("auto done");
|
||||
Application.Quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Core/DemoBot.Auto.cs.meta
Normal file
2
Assets/Scripts/Core/DemoBot.Auto.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4327551bd94af824da28a066f9d9012a
|
||||
@ -3013,6 +3013,11 @@ namespace GrassCutter
|
||||
yield return Perf(int.Parse(perfSlot));
|
||||
yield break;
|
||||
}
|
||||
if (CommandLine.Has("-demoauto"))
|
||||
{
|
||||
yield return AutoDemo();
|
||||
yield break;
|
||||
}
|
||||
if (CommandLine.Has("-demobots"))
|
||||
{
|
||||
yield return Bots();
|
||||
|
||||
@ -273,14 +273,18 @@ namespace GrassCutter
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>How each model stands in its own frame (an FBX with one root object brings its axis turn on that root).</summary>
|
||||
readonly Dictionary<Transform, Quaternion> _rest = new Dictionary<Transform, Quaternion>();
|
||||
|
||||
void Place(Transform t, float d, bool onRails)
|
||||
{
|
||||
if (!t) return;
|
||||
if (!_rest.TryGetValue(t, out var rest)) _rest[t] = rest = t.localRotation;
|
||||
var p = Pos(d);
|
||||
var up = Planet.Up(p);
|
||||
var f = Vector3.ProjectOnPlane(Ahead(d), up);
|
||||
if (f.sqrMagnitude < 1e-6f) f = Vector3.ProjectOnPlane(transform.forward, up);
|
||||
t.SetPositionAndRotation(p + up * (onRails ? 0.22f : 0f), Quaternion.LookRotation(f.normalized, up));
|
||||
t.SetPositionAndRotation(p + up * (onRails ? 0.22f : 0f), Quaternion.LookRotation(f.normalized, up) * rest);
|
||||
}
|
||||
|
||||
// ───────────────────────── the track ─────────────────────────
|
||||
|
||||
2
Assets/Scripts/Gameplay/Train.cs.meta
Normal file
2
Assets/Scripts/Gameplay/Train.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b769e94835e00e2489dd6274ee0ecb85
|
||||
@ -31,6 +31,9 @@ namespace GrassCutter
|
||||
int _next, _field;
|
||||
float _stuck, _back, _backSteer, _lastSteer;
|
||||
int _stuckTimes;
|
||||
/// <summary>The closest it has come to where it's going, and when: circling round a point inside its turn, it gives up on it.</summary>
|
||||
float _bestDist, _bestAt;
|
||||
int _goalKey = -1;
|
||||
Vector3 _unloadAt;
|
||||
|
||||
static readonly List<Vector2Int> s_fields = new List<Vector2Int>();
|
||||
@ -242,12 +245,23 @@ namespace GrassCutter
|
||||
|
||||
var to = Vector3.ProjectOnPlane(goal - p, up);
|
||||
float dist = to.magnitude;
|
||||
if (job == AutoJob.Mowing && dist < 1.6f)
|
||||
if (job == AutoJob.Mowing && dist < 2.2f)
|
||||
{
|
||||
_next++;
|
||||
_stuckTimes = 0;
|
||||
return new Vector2(_lastSteer, 0.4f);
|
||||
}
|
||||
int key = (int)job * 100000 + (job == AutoJob.Mowing ? _next : 0);
|
||||
if (key != _goalKey) { _goalKey = key; _bestDist = dist; _bestAt = Time.time; }
|
||||
else if (dist < _bestDist - 1f) { _bestDist = dist; _bestAt = Time.time; }
|
||||
else if (Time.time - _bestAt > 7f)
|
||||
{
|
||||
// round and round a point it can't turn into: on to the next one (or the next stroke)
|
||||
_goalKey = -1;
|
||||
if (job == AutoJob.Mowing) _next++;
|
||||
else if (++_stuckTimes >= 5) Auto.Value = (byte)AutoJob.Done;
|
||||
return new Vector2(_lastSteer, 0.4f);
|
||||
}
|
||||
|
||||
var fwd = Vector3.ProjectOnPlane(transform.forward, up).normalized;
|
||||
float ang = Vector3.SignedAngle(fwd, to, up);
|
||||
@ -265,6 +279,8 @@ namespace GrassCutter
|
||||
float cap = job == AutoJob.Mowing ? 0.7f : dist > 20f ? 1f : 0.6f;
|
||||
float thr = cap * Mathf.Lerp(1f, 0.4f, Mathf.Clamp01(Mathf.Abs(ang) / 90f));
|
||||
if (dist < 6f) thr = Mathf.Min(thr, 0.3f + dist * 0.08f);
|
||||
// a sharp turn close by is taken slowly: the slower it goes, the tighter it turns
|
||||
if (Mathf.Abs(ang) > 50f && dist < 10f) thr = Mathf.Min(thr, 0.22f);
|
||||
_lastSteer = steer;
|
||||
|
||||
if (speed < 0.5f && thr > 0.2f)
|
||||
|
||||
2
Assets/Scripts/Gameplay/Vehicle.Auto.cs.meta
Normal file
2
Assets/Scripts/Gameplay/Vehicle.Auto.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f48ca0b3d49f54140ba770685b721579
|
||||
Reference in New Issue
Block a user