Skip to content

Commit efab855

Browse files
committed
Fixed rare bug where me.forward would not cause the bot to move forward, but in some other direction.
1 parent b10b837 commit efab855

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

Farmtronics/Bot/BotObject.cs

+30-5
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,29 @@ public int PlaceItem() {
461461
return 0;
462462
}
463463

464-
public void MoveForward() {
464+
public static int DxForDirection(int direction) {
465+
if (direction == 1) return 1;
466+
if (direction == 3) return -1;
467+
return 0;
468+
}
469+
470+
public static int DyForDirection(int direction) {
471+
if (direction == 2) return 1;
472+
if (direction == 0) return -1;
473+
return 0;
474+
}
475+
476+
public void Move(int dColumn, int dRow) {
477+
// Face in the specified direction
478+
if (dRow < 0) farmer.faceDirection(0);
479+
else if (dRow > 0) farmer.faceDirection(2);
480+
else if (dColumn < 0) farmer.faceDirection(3);
481+
else if (dColumn > 0) farmer.faceDirection(1);
482+
483+
// make sure the terrain in that direction isn't blocked
484+
Vector2 newTile = farmer.getTileLocation() + new Vector2(dColumn, dRow);
485+
465486
// make sure the terrain in that direction isn't blocked
466-
Location newTileLoc = farmer.nextPositionTile();
467-
Vector2 newTile = newTileLoc.ToVector2();
468487
bool isPassable = TileInfo.IsPassable(currentLocation, newTile);
469488
if (!isPassable) {
470489
ModEntry.instance.Monitor.Log($"MoveForward: tile {newTile} is not passable");
@@ -474,7 +493,7 @@ public void MoveForward() {
474493

475494
// start moving
476495
targetPos = newTile.GetAbsolutePosition();
477-
ModEntry.instance.Monitor.Log($"MoveForward: Position: {Position} / targetPos: {targetPos}");
496+
ModEntry.instance.Monitor.Log($"MoveForward: Facing: {facingDirection}; Position: {Position}; newTile: {newTile}; targetPos: {targetPos}");
478497

479498
// Do collision actions (shake the grass, etc.)
480499
if (currentLocation.terrainFeatures.ContainsKey(newTile)) {
@@ -484,6 +503,10 @@ public void MoveForward() {
484503
feature.doCollisionAction(posRect, farmer.Speed, newTile, farmer, currentLocation);
485504
}
486505
}
506+
507+
public void MoveForward() {
508+
Move(DxForDirection(farmer.FacingDirection), DyForDirection(farmer.FacingDirection));
509+
}
487510

488511
public bool IsMoving() {
489512
return (Position != targetPos);
@@ -492,7 +515,7 @@ public bool IsMoving() {
492515
public void Rotate(int stepsClockwise) {
493516
farmer.faceDirection((farmer.FacingDirection + 4 + stepsClockwise) % 4);
494517
data.Update();
495-
//ModEntry.instance.Monitor.Log($"{Name} Rotate({stepsClockwise}): now facing {farmer.FacingDirection}");
518+
ModEntry.instance.Monitor.Log($"{Name} Rotate({stepsClockwise}): now facing {farmer.FacingDirection}");
496519
}
497520

498521
void ApplyScytheToTile() {
@@ -572,8 +595,10 @@ public void Update(GameTime gameTime) {
572595
// face target position
573596
float dx = targetPos.X - Position.X;
574597
float dy = targetPos.Y - Position.Y;
598+
int prevDir = farmer.FacingDirection;
575599
if (MathF.Abs(dx) > MathF.Abs(dy)) farmer.FacingDirection = dx > 0 ? 1 : 3;
576600
else farmer.FacingDirection = dy > 0 ? 2 : 0;
601+
if (farmer.FacingDirection != prevDir) ModEntry.instance.Monitor.Log($"Update: changed facing from {prevDir} to {farmer.FacingDirection} because dx={dx}, dy={dy}");
577602
// try to move; if fail, abandon movement
578603
var oldPos = farmer.Position;
579604
farmer.tryToMoveInDirection(farmer.FacingDirection, false, 0, false);

NexusReadme.txt

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
FARMTRONICS - 1.0
1+
FARMTRONICS
22
Add the Farmtronics Computer to your TV, and Farmtronics Bots to your farm.
33
Both include built-in help and are fully programmable.
44

55
:: REQUIREMENTS ::
66
• Stardew Valley up to date
77
• Latest version of SMAPI
8-
• Currently supports only desktop
8+
• Currently supports only desktop (Windows/Mac/Linux)
99

1010
:: FEATURES ::
1111
• Identical programmable computers on TV and on every bot
@@ -16,8 +16,6 @@ Both include built-in help and are fully programmable.
1616

1717
:: KNOWN ISSUES ::
1818
• Bots may not work outside the farmhouse and farm
19-
• This mod has not been tested (and probably does not work correctly) with multiplayer
20-
• Documentation is still a bit thin
2119
• Currently available only in English
2220

2321
:: GETTING HELP ::

0 commit comments

Comments
 (0)