Skip to content
Open
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
Binary file added CivOne-SelfContained.7z
Binary file not shown.
Binary file added CivOne_NeedRuntime.zip
Binary file not shown.
Binary file added SavedGamesForTest.7z
Binary file not shown.
671 changes: 671 additions & 0 deletions src/AStar.cs

Large diffs are not rendered by default.

21 changes: 18 additions & 3 deletions src/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,23 @@ internal static int CitizenGroup(Citizen citizen)
public static bool InCityRange(int x1, int y1, int x2, int y2) => new Rectangle(x2 - 2, y2 - 2, 5, 5).IntersectsWith(new Rectangle(x1, y1, 1, 1));

public static int DistanceToTile(int x1, int y1, int x2, int y2) => Math.Max(Math.Min(Math.Abs(x2 - x1), Math.Abs(Map.WIDTH - (x2 - x1))), Math.Abs(y2 - y1));

public static byte BinaryReadByte(BinaryReader reader, int position)

// The above function do not work properly at "dateline" ( methink is is just a "Math.Abs" that is missing ? ) I use the one below. JR
/* ******************************************************************************************************** */
public static int Distance( int X1, int Y1, int X2, int Y2 )
{
int X = Math.Abs( X1 - X2 );
int Y = Math.Abs( Y1 - Y2 );

if( X > Map.WIDTH / 2 )
{
X = Map.WIDTH - X;
}
if( X > Y ) return X;
return Y;
}

public static byte BinaryReadByte(BinaryReader reader, int position)
{
if (reader.BaseStream.Position != position)
reader.BaseStream.Seek(position, SeekOrigin.Begin);
Expand Down Expand Up @@ -358,4 +373,4 @@ public static bool AllowSaveGame
}
}
}
}
}
15 changes: 11 additions & 4 deletions src/Game.LoadSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,17 @@ private Game(IGameData gameData)
foreach (byte fortifiedUnit in cityData.FortifiedUnits)
{
IUnit unit = CreateUnit((UnitType)fortifiedUnit, city.X, city.Y);
unit.Status = (byte)(1 << 3);
unit.Owner = city.Owner;
unit.SetHome(city);
_units.Add(unit);
if ( unit != null )
{
unit.Status = ( byte )( 1 << 3 );
unit.Owner = city.Owner;
unit.SetHome( city );
_units.Add( unit );
}
else
{
Log( "Unknown fortified unit found: {0}", fortifiedUnit );
}
}

cityList.Add(cityData.Id, city);
Expand Down
59 changes: 47 additions & 12 deletions src/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CivOne.Advances;
using CivOne.Buildings;
using CivOne.Civilizations;
Expand Down Expand Up @@ -203,22 +205,53 @@ public void Update()
{
if (unit != null && !unit.Goto.IsEmpty)
{
int distance = unit.Tile.DistanceTo(unit.Goto);
ITile[] tiles = (unit as BaseUnit).MoveTargets.OrderBy(x => x.DistanceTo(unit.Goto)).ThenBy(x => x.Movement).ToArray();
if (tiles.Length == 0 || tiles[0].DistanceTo(unit.Goto) > distance)
{
// No valid tile to move to, cancel goto
unit.Goto = Point.Empty;
return;

ITile[] tiles = (unit as BaseUnit).MoveTargets.OrderBy(x => x.DistanceTo(unit.Goto)).ThenBy(x => x.Movement).ToArray();

if( Settings.Instance.PathFinding )
{
/* Use AStar */
AStar.sPosition Destination, Pos;
Destination.iX = unit.Goto.X;
Destination.iY = unit.Goto.Y;
Pos.iX = unit.X;
Pos.iY = unit.Y;

if( Destination.iX == Pos.iX && Destination.iY == Pos.iY )
{
unit.Goto = Point.Empty; // eh... never mind
return;
}
AStar AStar = new AStar();
AStar.sPosition NextPosition = AStar.FindPath( Destination, unit );
if (NextPosition.iX < 0)
{ // if no path found
unit.Goto = Point.Empty;
return;
}
unit.MoveTo(NextPosition.iX - Pos.iX, NextPosition.iY - Pos.iY);
return;

}
else if (tiles[0].DistanceTo(unit.Goto) == distance)
else
{
// Distance is unchanged, 50% chance to cancel goto
if (Common.Random.Next(0, 100) < 50)

int distance = unit.Tile.DistanceTo(unit.Goto);
if (tiles.Length == 0 || tiles[0].DistanceTo(unit.Goto) > distance)
{
// No valid tile to move to, cancel goto
unit.Goto = Point.Empty;
return;
}
else if (tiles[0].DistanceTo(unit.Goto) == distance)
{
// Distance is unchanged, 50% chance to cancel goto
if (Common.Random.Next(0, 100) < 50)
{
unit.Goto = Point.Empty;
return;
}
}
}

unit.MoveTo(tiles[0].X - unit.X, tiles[0].Y - unit.Y);
Expand Down Expand Up @@ -442,7 +475,8 @@ public IUnit ActiveUnit
_activeUnit = 0;

// Does the current unit still have moves left?
if (_units[_activeUnit].Owner == _currentPlayer && (_units[_activeUnit].MovesLeft > 0 || _units[_activeUnit].PartMoves > 0) && !_units[_activeUnit].Sentry && !_units[_activeUnit].Fortify)
if (_units[_activeUnit].Owner == _currentPlayer && (_units[_activeUnit].MovesLeft > 0 || _units[_activeUnit].PartMoves > 0)
&& !_units[_activeUnit].Sentry && !_units[_activeUnit].Fortify)
return _units[_activeUnit];

// Task busy, don't change the active unit
Expand All @@ -460,7 +494,8 @@ public IUnit ActiveUnit
}

// Loop through units
while (_units[_activeUnit].Owner != _currentPlayer || (_units[_activeUnit].MovesLeft == 0 && _units[_activeUnit].PartMoves == 0) || (_units[_activeUnit].Sentry || _units[_activeUnit].Fortify))
while (_units[_activeUnit].Owner != _currentPlayer || (_units[_activeUnit].MovesLeft == 0 && _units[_activeUnit].PartMoves == 0)
|| (_units[_activeUnit].Sentry || _units[_activeUnit].Fortify))
{
_activeUnit++;
if (_activeUnit >= _units.Count)
Expand Down
6 changes: 3 additions & 3 deletions src/IO/RLE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public static byte[] Encode(byte[] input)
if (repeat == 1) continue;
ms.WriteByte(RLE_REPEAT);
ms.WriteByte(repeat);
if (repeat == RLE_REPEAT) ms.WriteByte(RLE_ESCAPE);
i += (repeat - 1);
// if (repeat == RLE_REPEAT) ms.WriteByte(RLE_ESCAPE); Never RLE_ESCAPE after repeat byte
i += (repeat - 1);
}

return ms.ToArray();
}
}
}
}
}
Loading