Skip to content

Commit

Permalink
Server now also converted to use a quadtree to store the map
Browse files Browse the repository at this point in the history
If getting a tile from the map remember to check if you get a null from now on, which means there is no tile there (aka space)!
  • Loading branch information
ostaf committed Oct 17, 2013
1 parent 830c9c1 commit 53b62a8
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 126 deletions.
1 change: 0 additions & 1 deletion ClientServices/ClientServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
<Compile Include="Lighting\QuadRenderer.cs" />
<Compile Include="Lighting\ShadowMapResolver.cs" />
<Compile Include="Map\MapManager.cs" />
<Compile Include="Map\RectangleTree\RectangleTree.cs" />
<Compile Include="MessageLogging\MessageLogger.cs" />
<Compile Include="MessageLogging\MessageLoggerService.cs" />
<Compile Include="Network\NetworkGrapher.cs" />
Expand Down
6 changes: 4 additions & 2 deletions ClientServices/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public bool LoadTileMap(NetIncomingMessage message)
int posY = y*TileSpacing;

byte index = message.ReadByte();
if (index == 255) // No tile here
continue;
var state = (TileState) message.ReadByte();

Tile created = GenerateNewTile(GetTileString(index), state, new Vector2D(posX, posY));
Expand All @@ -157,8 +159,8 @@ public bool LoadTileMap(NetIncomingMessage message)
for (int y = 0; y < _mapLoadWidth; y++)
{
Tile T = GetTileAt(new Point(x * TileSpacing, y * TileSpacing));
/*if (T == null)
continue;*/
if (T == null)
continue;
if (T.ConnectSprite) //Was wall check.
{
byte i = SetSprite(x, y);
Expand Down
75 changes: 41 additions & 34 deletions ClientServices/Map/Tiles/Wall/Wall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,110 +178,117 @@ public override void RenderPos(float x, float y, int tileSpacing, int lightSize)

if (l < x)
{
if (!surroundingTiles[1].Opaque || (surroundingTiles[1].Opaque && surroundingTiles[2].Opaque))
if (!IsOpaque(1) || (IsOpaque(1) && IsOpaque(2)))
RenderOccluder(Direction.East, from, x, y, tileSpacing);

if (surroundingTiles[2].surroundingTiles[3].Opaque && !surroundingTiles[3].Opaque)
if (surroundingTiles[2] != null && surroundingTiles[2].surroundingTiles[3] != null && surroundingTiles[2].surroundingTiles[3].Opaque && !IsOpaque(3))
RenderOccluder(Direction.West, from, x, y, tileSpacing);

if (l < y)
{
if (!surroundingTiles[2].Opaque || (surroundingTiles[2].Opaque && !surroundingTiles[0].Opaque))
if (!IsOpaque(2) || (IsOpaque(2) && !IsOpaque(0)))
{
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
if (!surroundingTiles[2].Opaque)
if (!IsOpaque(2))
RenderOccluder(Direction.West, from, x, y, tileSpacing);
}
else if (l > y + tileSpacing)
{
if (!surroundingTiles[0].Opaque ||
(surroundingTiles[0].Opaque && !surroundingTiles[2].Opaque &&
(l < x + tileSpacing && !surroundingTiles[1].Opaque)))
if (!IsOpaque(0) || (IsOpaque(0) && !IsOpaque(2) &&
(l < x + tileSpacing && !IsOpaque(1))))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
if (surroundingTiles[1].Opaque && surroundingTiles[3].Opaque)
if (IsOpaque(1) && IsOpaque(3))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
else if (l >= y && l <= y + tileSpacing)
{
if (!surroundingTiles[2].Opaque || (surroundingTiles[2].Opaque && !surroundingTiles[0].Opaque))
if (!IsOpaque(2) || (IsOpaque(2) && !IsOpaque(0)))
{
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
if (!surroundingTiles[2].Opaque)
if (!IsOpaque(2))
RenderOccluder(Direction.West, from, x, y, tileSpacing);

if (!surroundingTiles[0].Opaque || (surroundingTiles[0].Opaque && !surroundingTiles[2].Opaque))
if (!IsOpaque(0) || (IsOpaque(0) && !IsOpaque(2)))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
}
else if (l > x + tileSpacing)
{
if (!surroundingTiles[3].Opaque || (surroundingTiles[3].Opaque && surroundingTiles[2].Opaque))
if (!IsOpaque(3) || (IsOpaque(3) && IsOpaque(2)))
RenderOccluder(Direction.West, from, x, y, tileSpacing);
if (surroundingTiles[2].surroundingTiles[1].Opaque && !surroundingTiles[1].Opaque)
if (surroundingTiles[2] != null && surroundingTiles[2].surroundingTiles[1] != null &&
surroundingTiles[2].surroundingTiles[1].Opaque && !IsOpaque(1))
RenderOccluder(Direction.East, from, x, y, tileSpacing);

if (l < y)
{
if (!surroundingTiles[2].Opaque || (surroundingTiles[2].Opaque && !surroundingTiles[0].Opaque))
if (!IsOpaque(2) || (IsOpaque(2) && !IsOpaque(0)))
{
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
if (!surroundingTiles[2].Opaque)
if (!IsOpaque(2))
RenderOccluder(Direction.East, from, x, y, tileSpacing);
}
else if (l > y + tileSpacing)
{
if (!surroundingTiles[0].Opaque ||
(surroundingTiles[0].Opaque && !surroundingTiles[2].Opaque &&
(l < x + tileSpacing && !surroundingTiles[1].Opaque)))
if (!IsOpaque(0) ||
(IsOpaque(0) && !IsOpaque(2) &&
(l < x + tileSpacing && !IsOpaque(1))))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
if (surroundingTiles[1].Opaque)
if (IsOpaque(1))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
else if (l >= y && l <= y + tileSpacing)
{
if (!surroundingTiles[2].Opaque || (surroundingTiles[2].Opaque && !surroundingTiles[0].Opaque))
if (!IsOpaque(2) || (IsOpaque(2) && !IsOpaque(0)))
{
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
if (!surroundingTiles[2].Opaque)
if (!IsOpaque(2))
RenderOccluder(Direction.East, from, x, y, tileSpacing);
if (!surroundingTiles[0].Opaque || (surroundingTiles[0].Opaque && !surroundingTiles[2].Opaque))
if (!IsOpaque(0) || (IsOpaque(0) && !IsOpaque(2)))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
}
else if (l >= x && l <= x + tileSpacing)
{
if (!surroundingTiles[1].Opaque || (surroundingTiles[1].Opaque && surroundingTiles[2].Opaque))
if (!IsOpaque(1) || (IsOpaque(1) && IsOpaque(2)))
RenderOccluder(Direction.East, from, x, y, tileSpacing);
if (!surroundingTiles[3].Opaque || (surroundingTiles[3].Opaque && surroundingTiles[2].Opaque))
if (!IsOpaque(3) || (IsOpaque(3) && IsOpaque(2)))
RenderOccluder(Direction.West, from, x, y, tileSpacing);

if (l < y)
{
if (!surroundingTiles[2].Opaque || (surroundingTiles[2].Opaque && !surroundingTiles[0].Opaque))
if (!IsOpaque(2) || (IsOpaque(2) && !IsOpaque(0)))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
else if (l > y + tileSpacing)
{
if (!surroundingTiles[0].Opaque ||
(surroundingTiles[0].Opaque && !surroundingTiles[2].Opaque &&
(l < x + tileSpacing && !surroundingTiles[1].Opaque)))
if (!IsOpaque(0) ||
(IsOpaque(0) && !IsOpaque(2) &&
(l < x + tileSpacing && !IsOpaque(1))))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
else if (l >= y && l <= y + tileSpacing)
{
if (!surroundingTiles[2].Opaque || (surroundingTiles[2].Opaque && !surroundingTiles[0].Opaque))
if (!IsOpaque(2) || (IsOpaque(2) && !IsOpaque(0)))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
if (!surroundingTiles[0].Opaque || (surroundingTiles[0].Opaque && !surroundingTiles[2].Opaque))
if (!IsOpaque(0) || (IsOpaque(0) && !IsOpaque(2)))
RenderOccluder(Direction.North, from, x, y, tileSpacing);
}
}
}

private bool IsOpaque(int i)
{
if (surroundingTiles[i] != null && surroundingTiles[i].Opaque)
return true;
return false;
}

public override void RenderPosOffset(float x, float y, int tileSpacing, Vector2D lightPosition)
{
Vector2D lightVec = lightPosition - new Vector2D(x + tileSpacing/2.0f, y + tileSpacing/2.0f);
Expand All @@ -302,13 +309,13 @@ public override void RenderPosOffset(float x, float y, int tileSpacing, Vector2D
if (lightVec.Y > 0)
lightVec.Y = 3;

if (surroundingTiles[0] != null && surroundingTiles[0].Opaque && lightVec.Y < 0) // tile to north
if (surroundingTiles[0] != null && IsOpaque(0) && lightVec.Y < 0) // tile to north
lightVec.Y = 2;
if (surroundingTiles[1] != null && surroundingTiles[1].Opaque && lightVec.X > 0)
if (surroundingTiles[1] != null && IsOpaque(1) && lightVec.X > 0)
lightVec.X = -2;
if (surroundingTiles[2] != null && surroundingTiles[2].Opaque && lightVec.Y > 0)
if (surroundingTiles[2] != null && IsOpaque(2) && lightVec.Y > 0)
lightVec.Y = -2;
if (surroundingTiles[3] != null && surroundingTiles[3].Opaque && lightVec.X < 0)
if (surroundingTiles[3] != null && IsOpaque(3) && lightVec.X < 0)
lightVec.X = 2;

Gorgon.CurrentRenderTarget.FilledRectangle(x + lightVec.X, y + lightVec.Y, sideSprite.Width + 1,
Expand Down
9 changes: 7 additions & 2 deletions ClientServices/State/States/GameScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,9 @@ private void CalculateLightArea(ILight l)
if (area.Calculated)
return;
area.LightPosition = l.Position; //mousePosWorld; // Set the light position
ITile t = MapManager.GetITileAt(l.Position);
if (t == null)
return;
if (MapManager.GetITileAt(l.Position).Opaque)
{
area.LightPosition = new Vector2D(area.LightPosition.X,
Expand Down Expand Up @@ -1408,7 +1411,8 @@ private void DrawWallsRelativeToLight(ILightArea area)
for (int y = yS; y <= yE; y++)
{
t = (Tile)MapManager.GetITileAt(x * tilespacing, y * tilespacing);

if (t == null)
continue;
if (t.Opaque)
{
Vector2D pos = area.ToRelativePosition(t.Position);
Expand All @@ -1434,7 +1438,8 @@ private void DrawTiles(int xStart, int xEnd, int yStart, int yEnd)
for (int y = yStart; y <= yEnd; y++)
{
t = (Tile)MapManager.GetITileAt(x * tilespacing, y * tilespacing);

if (t == null)
continue;
if (t.Opaque)
{
t.Render(WindowOrigin.X, WindowOrigin.Y, tilespacing, _wallBatch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using SS13_Shared.GO.Component.Damageable.Health.LocationalHealth;
using ServerInterfaces.Map;
using ServerInterfaces.Player;
using ServerInterfaces.Tiles;

namespace SGO
{
Expand Down Expand Up @@ -44,25 +45,25 @@ public override void Update(float frameTime)
if (statuscomp == null)
return;

if (!map.IsWorldPositionInBounds(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position))
ITile t = map.GetTileFromWorldPosition(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);

if (t == null)
{
statuscomp.AddEffect("Hypoxia", 5); //Out of map bounds, you is asphyxiatin to death bitch
}
else
{
bool hasInternals = HasInternals();
Point tilePos =
map.GetTileArrayPositionFromWorldPosition(
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
if (map.GetTileAt(tilePos.X, tilePos.Y).GasCell.GasAmount(GasType.Toxin) > 0.01 && !hasInternals)

if (t.GasCell.GasAmount(GasType.Toxin) > 0.01 && !hasInternals)
//too much toxin in the air, bro
{
statuscomp.AddEffect("ToxinInhalation", 20);
}
if (!hasInternals && map.GetTileAt(tilePos.X, tilePos.Y).GasCell.Pressure < 10 //Less than 10kPa
if (!hasInternals && t.GasCell.Pressure < 10 //Less than 10kPa
||
(map.GetTileAt(tilePos.X, tilePos.Y).GasCell.GasAmount(GasType.Oxygen)/
map.GetTileAt(tilePos.X, tilePos.Y).GasCell.TotalGas) < 0.10f) //less than 10% oxygen
(t.GasCell.GasAmount(GasType.Oxygen)/
t.GasCell.TotalGas) < 0.10f) //less than 10% oxygen
//Not enough oxygen in the mixture, or pressure is too low.
statuscomp.AddEffect("Hypoxia", 5);
}
Expand Down
37 changes: 16 additions & 21 deletions SGO/Component/LargeObject/BasicDoorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,56 +130,51 @@ private void OpenDoor(bool force = false)
if (disabled && !force) return;

var map = IoCManager.Resolve<IMapManager>();
Point occupiedTilePos =
map.GetTileArrayPositionFromWorldPosition(
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
var occupiedTile = map.GetTileAt(occupiedTilePos.X, occupiedTilePos.Y) as Tile;
Tile t = (Tile)map.GetTileFromWorldPosition(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
Open = true;
Owner.SendMessage(this, ComponentMessageType.DisableCollision);
Owner.SendMessage(this, ComponentMessageType.SetSpriteByKey, openSprite);
occupiedTile.GasPermeable = true;
if(t != null)
t.GasPermeable = true;
}

private void CloseDoor(bool force = false)
{
if (disabled && !force) return;

var map = IoCManager.Resolve<IMapManager>();
Point occupiedTilePos =
map.GetTileArrayPositionFromWorldPosition(
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
var occupiedTile = map.GetTileAt(occupiedTilePos.X, occupiedTilePos.Y) as Tile;
Tile t = (Tile)map.GetTileFromWorldPosition(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
Open = true;
Open = false;
timeOpen = 0;
Owner.SendMessage(this, ComponentMessageType.EnableCollision);
Owner.SendMessage(this, ComponentMessageType.SetSpriteByKey, closedSprite);
occupiedTile.GasPermeable = false;
if (t != null)
t.GasPermeable = false;
}

private void SetImpermeable()
{
var map = IoCManager.Resolve<IMapManager>();
Point occupiedTilePos =
map.GetTileArrayPositionFromWorldPosition(
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
var occupiedTile = map.GetTileAt(occupiedTilePos.X, occupiedTilePos.Y) as Tile;
occupiedTile.GasPermeable = false;
Tile t = (Tile)map.GetTileFromWorldPosition(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
if (t != null)
t.GasPermeable = false;
}

private void SetImpermeable(Vector2 position)
{
var map = IoCManager.Resolve<IMapManager>();
Point occupiedTilePos = map.GetTileArrayPositionFromWorldPosition(position);
var occupiedTile = map.GetTileAt(occupiedTilePos.X, occupiedTilePos.Y) as Tile;
occupiedTile.GasPermeable = false;
Tile t = (Tile)map.GetTileFromWorldPosition(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
if (t != null)
t.GasPermeable = false;
}

private void SetPermeable(Vector2 position)
{
var map = IoCManager.Resolve<IMapManager>();
Point occupiedTilePos = map.GetTileArrayPositionFromWorldPosition(position);
var occupiedTile = map.GetTileAt(occupiedTilePos.X, occupiedTilePos.Y) as Tile;
occupiedTile.GasPermeable = true;
Tile t = (Tile)map.GetTileFromWorldPosition(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
if (t != null)
t.GasPermeable = true;
}

public override void SetParameter(ComponentParameter parameter)
Expand Down
10 changes: 6 additions & 4 deletions SGO/Component/WallMountedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ private void OnMove(object sender, VectorEventArgs args)
var map = IoCManager.Resolve<IMapManager>();

Point tilePositionOld = map.GetTileArrayPositionFromWorldPosition(args.VectorFrom);
var previousTile = map.GetTileAt(tilePositionOld.X, tilePositionOld.Y) as Tile;
var previousTile = map.GetTileFromIndex(tilePositionOld.X, tilePositionOld.Y) as Tile;

previousTile.TileChange -= TileChanged;

Point tilePositionNew = map.GetTileArrayPositionFromWorldPosition(args.VectorTo);
var currentTile = map.GetTileAt(tilePositionNew.X, tilePositionNew.Y) as Tile;
var currentTile = map.GetTileFromIndex(tilePositionNew.X, tilePositionNew.Y) as Tile;

if (currentTile == null) return;

currentTile.TileChange += TileChanged;

Expand All @@ -98,14 +100,14 @@ public void AttachToTile(Vector2 tilePos)
{
var map = IoCManager.Resolve<IMapManager>();

var currentTile = map.GetTileAt((int) tilePos.X, (int) tilePos.Y) as Tile;
var currentTile = map.GetTileFromIndex((int) tilePos.X, (int) tilePos.Y) as Tile;

if (currentTile == null) return;

Point tilePositionOld =
map.GetTileArrayPositionFromWorldPosition(
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
var previousTile = map.GetTileAt(tilePositionOld.X, tilePositionOld.Y) as Tile;
var previousTile = map.GetTileFromIndex(tilePositionOld.X, tilePositionOld.Y) as Tile;

previousTile.TileChange -= TileChanged;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Drawing;

namespace ClientServices.Map
namespace SS13_Shared
{
public class RectangleTree<T>
{
Expand Down
Loading

0 comments on commit 53b62a8

Please sign in to comment.