map.Offset is no long necessary in so many places. (removed BS where world-coords != map-coords)

This commit is contained in:
Bob
2009-10-29 22:10:54 +13:00
parent 066ce1a203
commit be39cdd054
8 changed files with 145 additions and 154 deletions

View File

@@ -44,9 +44,9 @@ namespace OpenRa.Game
+ "; add Traits= to units.ini for appropriate unit" );
}
public Actor( TreeReference tree, TreeCache treeRenderer, int2 mapOffset )
public Actor( TreeReference tree, TreeCache treeRenderer )
{
Location = new int2( tree.Location ) - mapOffset;
Location = new int2( tree.Location );
traits.Add( new Traits.Tree( treeRenderer.GetImage( tree.Image ) ) );
}

View File

@@ -53,14 +53,14 @@ namespace OpenRa.Game
map = new Map(mapFile);
FileSystem.Mount(new Package(map.Theater + ".mix"));
viewport = new Viewport(clientSize, map.Size, renderer);
viewport = new Viewport( clientSize, map.Offset, map.Offset + map.Size, renderer );
terrain = new TerrainRenderer(renderer, map, viewport);
world = new World();
treeCache = new TreeCache(map);
foreach (TreeReference treeReference in map.Trees)
world.Add(new Actor(treeReference, treeCache, map.Offset));
world.Add(new Actor(treeReference, treeCache));
BuildingInfluence = new BuildingInfluenceMap(8);
UnitInfluence = new UnitInfluenceMap();
@@ -90,7 +90,7 @@ namespace OpenRa.Game
//num=owner,type,health,location,facing,trigger,unknown,shouldRepair
var parts = s.Value.ToLowerInvariant().Split( ',' );
var loc = int.Parse( parts[ 3 ] );
world.Add( new Actor( parts[ 1 ], new int2( loc % 128 - map.Offset.X, loc / 128-map.Offset.Y ), players[ 0 ] ) );
world.Add( new Actor( parts[ 1 ], new int2( loc % 128, loc / 128 ), players[ 0 ] ) );
}
}
@@ -101,7 +101,7 @@ namespace OpenRa.Game
//num=owner,type,health,location,facing,action,trigger
var parts = s.Value.ToLowerInvariant().Split( ',' );
var loc = int.Parse( parts[ 3 ] );
world.Add( new Actor( parts[ 1 ], new int2( loc % 128 - map.Offset.X, loc / 128 - map.Offset.Y ), players[ 0 ] ) );
world.Add( new Actor( parts[ 1 ], new int2( loc % 128, loc / 128 ), players[ 0 ] ) );
}
}
@@ -142,8 +142,6 @@ namespace OpenRa.Game
if (BuildingInfluence.GetBuildingAt(a) != null) return false;
if (UnitInfluence.GetUnitAt(a) != null) return false;
a += map.Offset;
return map.IsInMap(a.X, a.Y) &&
TerrainCosts.Cost(umt,
terrain.tileSet.GetWalkability(map.MapTiles[a.X, a.Y])) < double.PositiveInfinity;

View File

@@ -73,7 +73,7 @@ namespace OpenRa.Game.Graphics
{
var location = new int2(x, y);
spriteRenderer.DrawSprite(smudgeSprites[tr.smudge - 1],
Game.CellSize * (float2)(location - map.Offset), 0);
Game.CellSize * (float2)location, 0);
}
var o = tr.overlay;
@@ -86,7 +86,7 @@ namespace OpenRa.Game.Graphics
else if (overlayIsOre[o]) spriteIndex = 11;
else if (overlayIsGems[o]) spriteIndex = 2;
spriteRenderer.DrawSprite(sprites[spriteIndex],
Game.CellSize * (float2)(location - map.Offset), 0);
Game.CellSize * (float2)location, 0);
}
}

View File

@@ -42,10 +42,10 @@ namespace OpenRa.Game.Graphics
int nv = 0;
int ni = 0;
for( int j = 0 ; j < map.Height ; j++ )
for (int i = 0; i < map.Width; i++)
for( int j = map.YOffset ; j < map.YOffset + map.Height ; j++ )
for( int i = map.XOffset ; i < map.XOffset + map.Width; i++ )
{
Sprite tile = tileMapping[map.MapTiles[i + map.XOffset, j + map.YOffset]];
Sprite tile = tileMapping[map.MapTiles[i, j]];
Util.FastCreateQuad(vertices, indices, Game.CellSize * new float2(i, j), tile, 0, nv, ni);
nv += 4;
ni += 6;
@@ -67,8 +67,8 @@ namespace OpenRa.Game.Graphics
int visibleRows = (int)(region.Size.Y / 24.0f + 2);
int firstRow = (int)((region.Position.Y + viewport.Location.Y) / 24.0f);
int lastRow = firstRow + visibleRows;
int firstRow = 0;// (int)( ( region.Position.Y + viewport.Location.Y ) / 24.0f );
int lastRow = 128;// firstRow + visibleRows;
if (lastRow < 0 || firstRow > map.Height)
return;

View File

@@ -6,35 +6,36 @@ namespace OpenRa.Game.Graphics
{
class Viewport
{
readonly float2 size;
readonly float2 mapSize;
readonly float2 screenSize;
float2 scrollPosition;
readonly Renderer renderer;
public float2 Location { get { return scrollPosition; } }
public float2 Size { get { return size; } }
public int Width { get { return (int)size.X; } }
public int Height { get { return (int)size.Y; } }
public int Width { get { return (int)screenSize.X; } }
public int Height { get { return (int)screenSize.Y; } }
public Cursor cursor = Cursor.Move;
SpriteRenderer cursorRenderer;
int2 mousePos;
float cursorFrame = 0f;
readonly float2 scrollLowBounds, scrollHighBounds;
public void Scroll(float2 delta)
{
scrollPosition = ( scrollPosition + delta ).Constrain( float2.Zero, mapSize );
scrollPosition = ( scrollPosition + delta ).Constrain( scrollLowBounds, scrollHighBounds );
}
public Viewport(float2 size, float2 mapSize, Renderer renderer)
public Viewport(float2 screenSize, int2 mapStart, int2 mapEnd, Renderer renderer)
{
this.size = size;
this.mapSize = Game.CellSize * mapSize - size + new float2(128, 0);
if( this.mapSize.X < 0 ) this.mapSize.X = 0;
if( this.mapSize.Y < 0 ) this.mapSize.Y = 0;
this.screenSize = screenSize;
this.scrollLowBounds = Game.CellSize * mapStart;
this.scrollHighBounds = float2.Max( scrollLowBounds, Game.CellSize * mapEnd - ( screenSize - new float2( 128, 0 ) ) );
this.renderer = renderer;
cursorRenderer = new SpriteRenderer(renderer, true);
this.scrollPosition = scrollLowBounds;
}
List<Region> regions = new List<Region>();
@@ -43,7 +44,7 @@ namespace OpenRa.Game.Graphics
public void DrawRegions()
{
float2 r1 = new float2(2, -2) / Size;
float2 r1 = new float2(2, -2) / screenSize;
float2 r2 = new float2(-1, 1);
renderer.BeginFrame(r1, r2, scrollPosition);

View File

@@ -59,14 +59,11 @@ namespace OpenRa.Game
SequenceProvider.ForcePrecache();
Game.world.Add( new Actor( "mcv", new int2( 5, 5 ), Game.players[ 1 ]) );
Game.world.Add( new Actor( "mcv", new int2( 7, 5 ), Game.players[ 2 ] ) );
Game.world.Add( new Actor( "mcv", new int2( 9, 5 ), Game.players[ 0 ] ) );
var jeep = new Actor( "jeep", new int2( 9, 15 ), Game.players[ 1 ] );
Game.world.Add( jeep );
var tank = new Actor( "3tnk", new int2( 12, 7 ), Game.players[ 1 ] );
Game.world.Add( tank );
tank.traits.Get<Traits.AttackTurreted>().target = jeep;
Game.world.Add( new Actor( "mcv", Game.map.Offset + new int2( 5, 5 ), Game.players[ 1 ]) );
Game.world.Add( new Actor( "mcv", Game.map.Offset + new int2( 7, 5 ), Game.players[ 2 ] ) );
Game.world.Add( new Actor( "mcv", Game.map.Offset + new int2( 9, 5 ), Game.players[ 0 ] ) );
Game.world.Add( new Actor( "jeep", Game.map.Offset + new int2( 9, 15 ), Game.players[ 1 ] ) );
Game.world.Add( new Actor( "3tnk", Game.map.Offset + new int2( 12, 7 ), Game.players[ 1 ] ) );
sidebar = new Sidebar(renderer);

View File

@@ -34,8 +34,7 @@ namespace OpenRa.Game
public List<int2> FindUnitPathToRange(int2 src, int2 dest, UnitMovementType umt, int range)
{
var tilesInRange = Game.FindTilesInCircle(dest, range)
.Where(t => Game.IsCellBuildable(t, umt))
.Select(t => t + map.Offset);
.Where(t => Game.IsCellBuildable(t, umt));
var path = FindUnitPath(tilesInRange, DefaultEstimator(src), umt);
path.Reverse();
@@ -44,19 +43,18 @@ namespace OpenRa.Game
public List<int2> FindPathToPath( int2 from, List<int2> path, UnitMovementType umt )
{
var offset = map.Offset;
var cellInfo = InitCellInfo();
var queue = new PriorityQueue<PathDistance>();
var estimator = DefaultEstimator( from );
var cost = 0.0;
var prev = path[ 0 ] + offset;
var prev = path[ 0 ];
for( int i = 0 ; i < path.Count ; i++ )
{
var sl = path[ i ] + offset;
var sl = path[ i ];
if( i == 0 || Game.BuildingInfluence.GetBuildingAt( path[ i ] ) == null & Game.UnitInfluence.GetUnitAt( path[ i ] ) == null )
{
queue.Add( new PathDistance( estimator( sl - offset ), sl ) );
queue.Add( new PathDistance( estimator( sl ), sl ) );
cellInfo[ sl.X, sl.Y ] = new CellInfo( cost, prev, false );
}
var d = sl - prev;
@@ -70,19 +68,17 @@ namespace OpenRa.Game
List<int2> FindUnitPath( int2 unitLocation, Func<int2,double> estimator, UnitMovementType umt )
{
var startLocation = unitLocation + map.Offset;
return FindUnitPath( new[] {startLocation}, estimator, umt );
return FindUnitPath( new[] { unitLocation }, estimator, umt );
}
List<int2> FindUnitPath(IEnumerable<int2> startLocations, Func<int2, double> estimator, UnitMovementType umt)
{
var offset = map.Offset;
var cellInfo = InitCellInfo();
var queue = new PriorityQueue<PathDistance>();
foreach (var sl in startLocations)
{
queue.Add(new PathDistance(estimator(sl - offset), sl));
queue.Add(new PathDistance(estimator(sl), sl));
cellInfo[sl.X, sl.Y].MinCost = 0;
}
@@ -91,7 +87,6 @@ namespace OpenRa.Game
List<int2> FindPath( CellInfo[ , ] cellInfo, PriorityQueue<PathDistance> queue, Func<int2, double> estimator, UnitMovementType umt, bool checkForBlock )
{
var offset = map.Offset;
while( !queue.Empty )
{
@@ -99,8 +94,8 @@ namespace OpenRa.Game
int2 here = p.Location;
cellInfo[ here.X, here.Y ].Seen = true;
if( estimator( here - offset ) == 0.0 )
return MakePath( cellInfo, here, offset );
if( estimator( here ) == 0.0 )
return MakePath( cellInfo, here );
foreach( int2 d in Util.directions )
{
@@ -110,9 +105,9 @@ namespace OpenRa.Game
continue;
if( passableCost[(int)umt][ newHere.X, newHere.Y ] == double.PositiveInfinity )
continue;
if (Game.BuildingInfluence.GetBuildingAt(newHere - offset) != null)
if (Game.BuildingInfluence.GetBuildingAt(newHere) != null)
continue;
if( checkForBlock && Game.UnitInfluence.GetUnitAt( newHere - offset ) != null )
if( checkForBlock && Game.UnitInfluence.GetUnitAt( newHere ) != null )
continue;
double cellCost = ( ( d.X * d.Y != 0 ) ? 1.414213563 : 1.0 ) * passableCost[(int)umt][ newHere.X, newHere.Y ];
@@ -124,7 +119,7 @@ namespace OpenRa.Game
cellInfo[ newHere.X, newHere.Y ].Path = here;
cellInfo[ newHere.X, newHere.Y ].MinCost = newCost;
queue.Add( new PathDistance( newCost + estimator( newHere - offset ), newHere ) );
queue.Add( new PathDistance( newCost + estimator( newHere ), newHere ) );
}
}
@@ -141,18 +136,18 @@ namespace OpenRa.Game
return cellInfo;
}
List<int2> MakePath( CellInfo[ , ] cellInfo, int2 destination, int2 offset )
List<int2> MakePath( CellInfo[ , ] cellInfo, int2 destination )
{
List<int2> ret = new List<int2>();
int2 pathNode = destination;
while( cellInfo[ pathNode.X, pathNode.Y ].Path != pathNode )
{
ret.Add( pathNode - offset );
ret.Add( pathNode );
pathNode = cellInfo[ pathNode.X, pathNode.Y ].Path;
}
ret.Add(pathNode - offset);
ret.Add(pathNode);
return ret;
}

View File

@@ -30,7 +30,7 @@ namespace OpenRa.Game.Traits
for (int i = 0; i < 2 * size; i++)
{
var p = self.Location + Game.map.Offset + new int2(i % size, i / size + bibOffset);
var p = self.Location + new int2(i % size, i / size + bibOffset);
Game.map.MapTiles[p.X, p.Y].smudge = (byte)(i + startIndex);
}
}