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

wip trying to render all fog of war layers #435

Draft
wants to merge 1 commit into
base: pcen/save-types-without-references-save-game
Choose a base branch
from
Draft
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
29 changes: 19 additions & 10 deletions C7/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void processEngineMessages(GameData gameData) {
switch (msg) {
case MsgStartUnitAnimation mSUA:
MapUnit unit = gameData.GetUnit(mSUA.unitID);
if (unit != null && (controller.tileKnowledge.isTileKnown(unit.location) || controller.tileKnowledge.isTileKnown(unit.previousLocation))) {
if (unit != null && (controller.tileKnowledge.isKnown(unit.location) || controller.tileKnowledge.isKnown(unit.previousLocation))) {
// TODO: This needs to be extended so that the player is shown when AIs found cities, when they move units
// (optionally, depending on preferences) and generalized so that modders can specify whether custom
// animations should be shown to the player.
Expand All @@ -140,14 +140,13 @@ public void processEngineMessages(GameData gameData) {
break;
case MsgStartEffectAnimation mSEA:
Tile tile = gameData.map.tileAtIndex(mSEA.tileIndex);
if (tile != Tile.NONE && controller.tileKnowledge.isTileKnown(tile))
if (tile != Tile.NONE && controller.tileKnowledge.isKnown(tile)) {
animTracker.startAnimation(tile, mSEA.effect, mSEA.completionEvent, mSEA.ending);
else {
if (mSEA.completionEvent != null)
mSEA.completionEvent.Set();
} else {
mSEA.completionEvent?.Set();
}
break;
case MsgStartTurn mST:
case MsgStartTurn:
OnPlayerStartTurn();
break;

Expand All @@ -157,9 +156,15 @@ public void processEngineMessages(GameData gameData) {
mapView.addCity(city, cityTile);
break;

case MsgTileDiscovered mTD:
Tile discoveredTile = gameData.map.tileAtIndex(mTD.tileIndex);
mapView.discoverTile(discoveredTile, controller.tileKnowledge);
case MsgTileVisibilityChanged mTVC:
controller.tileKnowledge.ComputeVisibleTiles(controller.units);
Tile visibilityChangedTile = gameData.map.tileAtIndex(mTVC.tileIndex);
int i = 0;
foreach (Tile t in controller.tileKnowledge.AllKnownTiles()) {
i++;
mapView.onTileVisibilityChanged(t, controller.tileKnowledge);
}
GD.Print($"updated {i} tiles.");
break;
}
}
Expand Down Expand Up @@ -227,7 +232,7 @@ public override void _Process(double delta) {

// If "location" is not already near the center of the screen, moves the camera to bring it into view.
public void ensureLocationIsInView(Tile location) {
if (controller.tileKnowledge.isTileKnown(location) && location != Tile.NONE) {
if (controller.tileKnowledge.isKnown(location) && location != Tile.NONE) {
if (!camera.isTileInView(location, mapView)) {
camera.centerOnTile(location, mapView);
}
Expand Down Expand Up @@ -339,13 +344,17 @@ public override void _UnhandledInput(InputEvent @event) {
}
}
} else {

// Select unit on tile at mouse location
using (var gameDataAccess = new UIGameDataAccess()) {
var tile = mapView.tileAt(gameDataAccess.gameData.map, globalMousePosition);
if (tile != null) {
MapUnit to_select = tile.unitsOnTile.Find(u => u.movementPoints.canMove);
if (to_select != null && to_select.owner == controller)
setSelectedUnit(to_select);
var known = controller.tileKnowledge.isKnown(tile);
var vis = controller.tileKnowledge.isVisible(tile);
GD.Print($"known: {known}, visible: {vis}");
}
}
}
Expand Down
35 changes: 20 additions & 15 deletions C7/Map/MapView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,34 +566,39 @@ private void updateBuildingLayer(Tile tile) {
// semi-visible, indicating other layers should be updated.

private static int fogOfWarIndex(Tile tile, TileKnowledge tk) {
if (tk.isTileKnown(tile)) {
return 0;
}
int sum = 0;
// HACK: edge tiles have missing directions in neighbors map
if (tile.neighbors.Values.Count != 8) {
return sum;
}
if (tk.isTileKnown(tile.neighbors[TileDirection.NORTH]) || tk.isTileKnown(tile.neighbors[TileDirection.NORTHWEST]) || tk.isTileKnown(tile.neighbors[TileDirection.NORTHEAST])) {
// if (tile.neighbors.Values.Count != 8) {
// return sum;
// }
if (tk.isVisible(tile.neighbors[TileDirection.NORTH]) || tk.isVisible(tile.neighbors[TileDirection.NORTHWEST]) || tk.isVisible(tile.neighbors[TileDirection.NORTHEAST])) {
sum += 1 * 2;
} else if (tk.isKnown(tile.neighbors[TileDirection.NORTH]) || tk.isKnown(tile.neighbors[TileDirection.NORTHWEST]) || tk.isKnown(tile.neighbors[TileDirection.NORTHEAST])) {
sum += 1;
}
if (tk.isTileKnown(tile.neighbors[TileDirection.WEST]) || tk.isTileKnown(tile.neighbors[TileDirection.NORTHWEST]) || tk.isTileKnown(tile.neighbors[TileDirection.SOUTHWEST])) {
if (tk.isVisible(tile.neighbors[TileDirection.WEST]) || tk.isVisible(tile.neighbors[TileDirection.NORTHWEST]) || tk.isVisible(tile.neighbors[TileDirection.SOUTHWEST])) {
sum += 3 * 2;
} else if (tk.isKnown(tile.neighbors[TileDirection.WEST]) || tk.isKnown(tile.neighbors[TileDirection.NORTHWEST]) || tk.isKnown(tile.neighbors[TileDirection.SOUTHWEST])) {
sum += 3;
}
if (tk.isTileKnown(tile.neighbors[TileDirection.EAST]) || tk.isTileKnown(tile.neighbors[TileDirection.NORTHEAST]) || tk.isTileKnown(tile.neighbors[TileDirection.SOUTHEAST])) {
if (tk.isVisible(tile.neighbors[TileDirection.EAST]) || tk.isVisible(tile.neighbors[TileDirection.NORTHEAST]) || tk.isVisible(tile.neighbors[TileDirection.SOUTHEAST])) {
sum += 9 * 2;
} else if (tk.isKnown(tile.neighbors[TileDirection.EAST]) || tk.isKnown(tile.neighbors[TileDirection.NORTHEAST]) || tk.isKnown(tile.neighbors[TileDirection.SOUTHEAST])) {
sum += 9;
}
if (tk.isTileKnown(tile.neighbors[TileDirection.SOUTH]) || tk.isTileKnown(tile.neighbors[TileDirection.SOUTHWEST]) || tk.isTileKnown(tile.neighbors[TileDirection.SOUTHEAST])) {
if (tk.isVisible(tile.neighbors[TileDirection.SOUTH]) || tk.isVisible(tile.neighbors[TileDirection.SOUTHWEST]) || tk.isVisible(tile.neighbors[TileDirection.SOUTHEAST])) {
sum += 27 * 2;
} else if (tk.isKnown(tile.neighbors[TileDirection.SOUTH]) || tk.isKnown(tile.neighbors[TileDirection.SOUTHWEST]) || tk.isKnown(tile.neighbors[TileDirection.SOUTHEAST])) {
sum += 27;
}
return sum;
}

private bool updateFogOfWarLayer(Tile tile, TileKnowledge tk) {
if (tk.isTileKnown(tile)) {
eraseCell(Layer.FogOfWar, tile);
return true;
}
// if (tk.isVisible(tile)) {
// eraseCell(Layer.FogOfWar, tile);
// return true;
// }
int index = fogOfWarIndex(tile, tk);
setCell(Layer.FogOfWar, Atlas.FogOfWar, tile, new Vector2I(index % 9, index / 9));
return index > 0; // partially visible
Expand Down Expand Up @@ -644,7 +649,7 @@ private void updateGridLayer() {
}
}

public void discoverTile(Tile tile, TileKnowledge tk) {
public void onTileVisibilityChanged(Tile tile, TileKnowledge tk) {
HashSet<Tile> update = new HashSet<Tile>(tile.neighbors.Values);
foreach (Tile n in tile.neighbors.Values) {
foreach (Tile nn in n.neighbors.Values) {
Expand Down
4 changes: 2 additions & 2 deletions C7Engine/AI/UnitAI/ExplorerAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ private static int numUnknownNeighboringTiles(Player player, Tile t) {
}
//Calculate whether it, and its neighbors are in known tiles.
int discoverableTiles = 0;
if (!player.tileKnowledge.isTileKnown(t))
if (!player.tileKnowledge.isKnown(t))
{
discoverableTiles++;
}
foreach (Tile n in t.neighbors.Values)
{
if (!player.tileKnowledge.isTileKnown(n))
if (!player.tileKnowledge.isKnown(n))
{
discoverableTiles++;
}
Expand Down
4 changes: 2 additions & 2 deletions C7Engine/EntryPoints/MessageToUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public MsgCityBuilt(Tile tile) {

public class MsgStartTurn : MessageToUI {}

public class MsgTileDiscovered : MessageToUI {
public class MsgTileVisibilityChanged : MessageToUI {
public int tileIndex;

public MsgTileDiscovered(Tile tile) {
public MsgTileVisibilityChanged(Tile tile) {
this.tileIndex = EngineStorage.gameData.map.tileCoordsToIndex(tile.xCoordinate, tile.yCoordinate);
}
}
Expand Down
30 changes: 25 additions & 5 deletions C7Engine/MapUnitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,27 @@ public static void OnBeginTurn(this MapUnit unit) {
unit.defensiveBombardsRemaining = 1;
}

public static void OnEnterTile(this MapUnit unit, Tile tile) {
//Add to player knowledge of tiles
if (unit.owner.tileKnowledge.AddTilesToKnown(tile)) {
new MsgTileDiscovered(tile).send();
}
// public static void OnLeaveTile(this MapUnit unit, Tile tile) {
// // the tile that was just left should always still be visible
// foreach (Tile t in tile.neighbors.Values) {
// if (t.unitsOnTile.Find(u => u.owner.id == unit.owner.id) is not null) {
// continue;
// }
// bool visible = false;
// foreach (Tile n in t.neighbors.Values) {
// if (t.unitsOnTile.Find(u => u.owner.id == unit.owner.id) is not null) {
// visible = true;
// break;
// }
// }
// if (!visible) {
// unit.owner.tileKnowledge.RemoveTileFromVisible(t);
// new MsgTileVisibilityChanged(t).send();
// }
// }
// }

public static void OnEnterTile(this MapUnit unit, Tile tile) {
// Disperse barb camp
if (tile.hasBarbarianCamp && (!unit.owner.isBarbarians)) {
tile.DisbandNonDefendingUnits();
Expand Down Expand Up @@ -371,6 +386,11 @@ public static bool move(this MapUnit unit, TileDirection dir, bool wait = false)
unit.animate(MapUnit.AnimatedAction.RUN, wait);
unit.location = newLoc;
unit.movementPoints.onUnitMove(movementCost);

//Add to player knowledge of tiles
if (unit.owner.tileKnowledge.AddTilesToKnown(unit.location)) {
new MsgTileVisibilityChanged(unit.location).send();
}
}
return true;
}
Expand Down
20 changes: 19 additions & 1 deletion C7GameData/AIData/TileKnowledge.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;

namespace C7GameData
{
Expand All @@ -15,16 +17,32 @@ public bool AddTilesToKnown(Tile unitLocation) {
return added;
}

public void ComputeVisibleTiles(List<MapUnit> units) {
visibleTiles.Clear();
Console.WriteLine($"computing visible tiles based on {units.Count} units");
foreach (MapUnit unit in units) {
visibleTiles.Add(unit.location);
foreach (Tile t in unit.location.neighbors.Values) {
visibleTiles.Add(t);
}
}
Console.WriteLine($"Visible tiles: {visibleTiles.Count}");
}

// neighboring tiles should not be added when loading tile knowledge
// from a .sav file
internal bool AddTileToKnown(Tile unitLocation) {
return knownTiles.Add(unitLocation);
}

public bool isTileKnown(Tile t) {
public bool isKnown(Tile t) {
return knownTiles.Contains(t);
}

public bool isVisible(Tile t) {
return visibleTiles.Contains(t);
}

/**
* Returns a copy of the list of known tiles.
* This prevents external modifications.
Expand Down