Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added logic to handle placement of ships #53

Merged
merged 3 commits into from
Jan 2, 2024
Merged
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
1 change: 0 additions & 1 deletion RTWLibPlus/dataWrappers/TGA/TGA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ public int ConvertCoordinatesToIndex(Vector2 coord)
}

public string GetName() => Path.GetFileName(this.LoadPath);

public string OutputPath { get; set; }
public string LoadPath { get; set; }

Expand Down
1 change: 1 addition & 0 deletions RTWLibPlus/dataWrappers/ds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using RTWLibPlus.helpers;
using RTWLibPlus.interfaces;
using RTWLibPlus.parsers.objects;
using System;
using System.Collections.Generic;
using System.Numerics;

Expand Down
74 changes: 72 additions & 2 deletions RTWLibPlus/map/city.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,57 @@
namespace RTWLibPlus.map;
using RTWLibPlus.dataWrappers;
using RTWLibPlus.dataWrappers.TGA;
using System;
using System.Collections.Generic;
using System.Numerics;

public class CityMap
{
public Dictionary<string, Vector2> CityCoordinates { get; set; }

public bool[,] WaterMap { get; set; }

public int Height { get; set; }
public int Width { get; set; }

public CityMap() { }

public CityMap(TGA image, DR dr)
{
this.CityCoordinates = new();
this.GetCityCoords(image, dr);
this.Height = image.RefHeader.Height;
this.Width = image.RefHeader.Width;
this.CityCoordinates = [];
this.GetCityCoords(image, dr);
this.WaterMap = new bool[this.Width, this.Height];
this.GetWaterMap(image);

}

public Vector2 GetClosestWater(Vector2 from)
{
float distance = 100000;
Vector2 water = new();
for (int x = 0; x < this.Width; x++)
{
for (int y = 0; y < this.Height; y++)
{
if (!this.WaterMap[x, y])
{
continue;
}

Vector2 current = new(x, y);
float tempDis = Vector2.Distance(from, current);
if (tempDis < distance)
{
distance = tempDis;
water = current;
}
}
}
this.WaterMap[(int)water.X, (int)water.Y] = false;
//water.Y = this.Height - water.Y;
return water;
}

private void GetCityCoords(TGA image, DR dr)
Expand All @@ -44,6 +77,43 @@ private void GetCityCoords(TGA image, DR dr)
}
}

private void GetWaterMap(TGA image)
{
for (int i = 0; i < image.Pixels.Length; i++)
{
Vector2 coord = this.ConvertIndexToCoordinates(i, image.RefHeader.Width);
Vector2 upCoord = new(coord.X, coord.Y + 1);
Vector2 rightCoord = new(coord.X + 1, coord.Y);
Vector2 leftCoord = new(coord.X - 1, coord.Y);
Vector2 downCoord = new(coord.X, coord.Y - 1);

coord.X = Math.Clamp(coord.X, 0, this.Width - 1);
coord.Y = Math.Clamp(coord.Y, 0, this.Height - 1);
PIXEL pixel = image.Pixels[i];
PIXEL up, left, right, down;
int iup = this.ConvertCoordinatesToIndex(upCoord, image.RefHeader.Width, image.RefHeader.Height);
int ileft = this.ConvertCoordinatesToIndex(leftCoord, image.RefHeader.Width, image.RefHeader.Height);
int iright = this.ConvertCoordinatesToIndex(rightCoord, image.RefHeader.Width, image.RefHeader.Height);
int idown = this.ConvertCoordinatesToIndex(downCoord, image.RefHeader.Width, image.RefHeader.Height);
up = image.Pixels[iup];
left = image.Pixels[ileft];
down = image.Pixels[idown];
right = image.Pixels[iright];
if (pixel.R == 41 && pixel.G == 140 && pixel.B == 233 &&
up.R == 41 && up.G == 140 && up.B == 233 &&
left.R == 41 && left.G == 140 && left.B == 233 &&
right.R == 41 && right.G == 140 && right.B == 233 &&
down.R == 41 && down.G == 140 && down.B == 233)// check for sea
{
this.WaterMap[(int)coord.X, (int)coord.Y] = true;
}
else
{
this.WaterMap[(int)coord.X, (int)coord.Y] = false;
}
}
}

private Vector2 ConvertIndexToCoordinates(int i, int width) => new(i % width, i / width);

private int ConvertCoordinatesToIndex(Vector2 coord, int width, int height)
Expand Down
8 changes: 8 additions & 0 deletions RTWLibPlus/randomiser/randDS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using RTWLibPlus.map;
using RTWLibPlus.Modifiers;
using RTWLibPlus.parsers.objects;
using System;
using System.Collections.Generic;
using System.Numerics;

Expand Down Expand Up @@ -106,6 +107,13 @@ private static void ChangeCharacterCoords(List<IBaseObj> regions, List<IBaseObj>
}

Vector2 coord = cm.CityCoordinates[regions[ri].Value];
Vector2 waterCoord = coord;
if (c.Value.Contains("admiral"))
{
waterCoord = cm.GetClosestWater(coord);
coord = waterCoord;
}

c.Value = DS.ChangeCharacterCoordinates(c.Value, coord);
ri++;
}
Expand Down
1 change: 1 addition & 0 deletions RTWLib_Tests/wrappers/Tests_ds.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace RTWLib_Tests.wrappers;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using RTWLib_CLI.cmd;
using RTWLibPlus.data;
using RTWLibPlus.dataWrappers;
using RTWLibPlus.helpers;
Expand Down
Loading