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

Add dots indicating player population on patch map #5880

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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
80 changes: 80 additions & 0 deletions assets/textures/gui/bevel/MapDotIndicator.svg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that maybe if the dot was fully filled in and made slightly less in opacity, it would be a lot more subtle effect? That way I think there wouldn't be really any complaining about the added visuals.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions assets/textures/gui/bevel/MapDotIndicator.svg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://cbkfqk0lk6dhl"
path="res://.godot/imported/MapDotIndicator.svg-0118f3fb709521b7aea1c36f416e97e1.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://assets/textures/gui/bevel/MapDotIndicator.svg"
dest_files=["res://.godot/imported/MapDotIndicator.svg-0118f3fb709521b7aea1c36f416e97e1.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
3 changes: 3 additions & 0 deletions simulation_parameters/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,9 @@ public static class Constants

public const float PATCH_GENERATION_CHANCE_BANANA_BIOME = 0.03f;

// Constants for displaying the patch map
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't seem to match what this is on? This should be an xml summary comment along the lines of "Sets how many population indicator dots appear on the patch map"

public const float PLAYER_POPULATION_POPULATION_FOR_PER_INDICATOR = 1.0f / 300.0f;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the name of this constant be the inverse? Because this is how many indicators there are per population. That's the difference between population / population per indicator and population * indicators per population. So I think this should be renamed for clarity.


/// <summary>
/// If set to true, then physics debug draw gets enabled when the game starts
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/macroscopic_stage/editor/MacroscopicEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ protected override void ResolveDerivedTypeNodeReferences()

protected override void InitEditor(bool fresh)
{
patchMapTab.SetMap(CurrentGame.GameWorld.Map);
patchMapTab.SetMap(CurrentGame.GameWorld.Map, CurrentGame.GameWorld.PlayerSpecies.ID);

base.InitEditor(fresh);

Expand Down
2 changes: 1 addition & 1 deletion src/microbe_stage/editor/MicrobeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected override void ResolveDerivedTypeNodeReferences()

protected override void InitEditor(bool fresh)
{
patchMapTab.SetMap(CurrentGame.GameWorld.Map);
patchMapTab.SetMap(CurrentGame.GameWorld.Map, CurrentGame.GameWorld.PlayerSpecies.ID);

base.InitEditor(fresh);

Expand Down
64 changes: 64 additions & 0 deletions src/microbe_stage/editor/PatchMapDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public partial class PatchMapDrawer : Control
[Export]
public NodePath LineContainerPath = null!;

public uint PlayerSpeciesID = int.MaxValue;

#pragma warning disable CA2213
[Export]
public ShaderMaterial MonochromeMaterial = null!;
Expand All @@ -47,6 +49,9 @@ public partial class PatchMapDrawer : Control
private PackedScene nodeScene = null!;
private Control patchNodeContainer = null!;
private Control lineContainer = null!;

[Export]
private Control populationIndicatorContainer = null!;
#pragma warning restore CA2213

private PatchMap map = null!;
Expand All @@ -55,6 +60,8 @@ public partial class PatchMapDrawer : Control

private bool alreadyDrawn;

private List<Control> playerSpeciesPopulationIndicators = new();

private Dictionary<Patch, bool>? patchEnableStatusesToBeApplied;

private Patch? selectedPatch;
Expand Down Expand Up @@ -1109,10 +1116,67 @@ private void AddPatchNode(Patch patch, Vector2 position)

patch.ApplyPatchEventVisuals(node);

var playerSpecies = patch.FindSpeciesByID(PlayerSpeciesID);
if (playerSpecies != null)
{
AddPlayerPopulationIndicators(patch, playerSpecies, node, position);
}

patchNodeContainer.AddChild(node);
nodes.Add(node.Patch, node);
}

private void AddPlayerPopulationIndicators(Patch patch, Species playerSpecies, Control node, Vector2 position)
{
var playerPopulationIndicatorAmount = (int)Math.Ceiling(patch.GetSpeciesSimulationPopulation(playerSpecies) *
Constants.PLAYER_POPULATION_POPULATION_FOR_PER_INDICATOR);

var indicatorExcess = Math.Clamp(playerSpeciesPopulationIndicators.Count - playerPopulationIndicatorAmount,
0,
playerSpeciesPopulationIndicators.Count);

// Hide excess from the end of the list
for (int i = 0; i < indicatorExcess; ++i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to keep things this way, I'll accept it, but I think a slightly easier way to program this would be to use a variable like, nextIndicatorIndex that starts at 0 and goes up for each used indicator. Then the excess indicators can be hidden by looping from the ending nextIndicatorIndex value up to the indicators count. And the noCached variable can be calculated as nextIndicatorIndex >= playerSpeciesPopulationIndicators.Count.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now with the persistent index for used indicators, I think this hiding excess indicators code needs to be put into a single place that is called after the entire map is drawn. Otherwise the last indicators get hidden over and over (and I think your indicatorExcess math was not updated anyway so that would lead to a bug in any case).

{
playerSpeciesPopulationIndicators[playerSpeciesPopulationIndicators.Count - i].Hide();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also when I'm looking at this code, isn't the math wrong? Because playerSpeciesPopulationIndicators.Count is not valid index, the first valid index is playerSpeciesPopulationIndicators.Count - 1 but that doesn't happen as i starts off as 0.

Also I think I just realized also another problem: the player being in multiple patches will reset the indicators. There needs to be a persistent nextIndicatorIndex variable that is only cleared on a full map re-draw not during a map draw when this method can be called multiple times which means that the separate calls for different patches on the map will interfere with each other.

}

for (int i = 0; i < playerPopulationIndicatorAmount; ++i)
{
var noCached = i >= playerSpeciesPopulationIndicators.Count;

Control indicator;
if (noCached)
{
indicator = new TextureRect
{
Texture = GD.Load<Texture2D>("res://assets/textures/gui/bevel/MapDotIndicator.svg"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This texture should be loaded just once in _Ready and not tried to be reloaded for each indicator.

ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize,
Size = new Vector2(6, 6),
};

populationIndicatorContainer.AddChild(indicator);
}
else
{
indicator = playerSpeciesPopulationIndicators[i];

indicator.Show();
}

indicator.MouseFilter = MouseFilterEnum.Ignore;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in the indicator creation as it doesn't seem like this value is ever updated so it wastes time to reapply the value on each loop iteration.


var nodeModifier = node.Position.LengthSquared();
var modifierSinus = MathF.Sin(i);

playerSpeciesPopulationIndicators.Add(indicator);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a major bug where the same indicator objects are added again and again to the same list?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is probably what caused this error when I moved patches in the editor:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at PatchMapDrawer.AddPlayerPopulationIndicators(Patch patch, Species playerSpecies, Control node, Vector2 position) in /home/hhyyrylainen/Projects/Thrive/src/microbe_stage/editor/PatchMapDrawer.cs:line 1141
   at PatchMapDrawer.AddPatchNode(Patch patch, Vector2 position) in /home/hhyyrylainen/Projects/Thrive/src/microbe_stage/editor/PatchMapDrawer.cs:line 1122
   at PatchMapDrawer.RebuildMap() in /home/hhyyrylainen/Projects/Thrive/src/microbe_stage/editor/PatchMapDrawer.cs:line 1069
   at PatchMapDrawer._Process(Double delta) in /home/hhyyrylainen/Projects/Thrive/src/microbe_stage/editor/PatchMapDrawer.cs:line 175
   at Godot.Node.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/Node.cs:line 2395
   at Godot.CanvasItem.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/CanvasItem.cs:line 1505
   at Godot.Control.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/Control.cs:line 2912
   at PatchMapDrawer.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /home/hhyyrylainen/Projects/Thrive/.godot/mono/temp/obj/Debug/Godot.SourceGenerators/Godot.SourceGenerators.ScriptMethodsGenerator/PatchMapDrawer_ScriptMethods.generated.cs:line 318
   at Godot.Bridge.CSharpInstanceBridge.Call(IntPtr godotObjectGCHandle, godot_string_name* method, godot_variant** args, Int32 argCount, godot_variant_call_error* refCallError, godot_variant* ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs:line 24

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good you caught that.


indicator.Position = position + node.Size * 0.5f + new Vector2(0, 20)
.Rotated(nodeModifier * 30) +
new Vector2(0, modifierSinus * 50).Rotated(i * 6 * modifierSinus + nodeModifier);
}
}

private void BuildPatchToRegionConnections(PatchRegion region1, PatchRegion region2, Vector2 regionPoint,
Vector2 regionPreviousPoint, MapElementVisibility regionVisibility)
{
Expand Down
29 changes: 20 additions & 9 deletions src/microbe_stage/editor/PatchMapDrawer.tscn
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=4 format=3 uid="uid://ui60hm6q35fb"]

[ext_resource path="res://src/microbe_stage/editor/PatchMapDrawer.cs" type="Script" id=1]
[ext_resource path="res://shaders/Monochrome.gdshader" type="Shader" id=2]
[ext_resource type="Script" path="res://src/microbe_stage/editor/PatchMapDrawer.cs" id="1"]
[ext_resource type="Shader" path="res://shaders/Monochrome.gdshader" id="2"]

[sub_resource type="ShaderMaterial" id=1]
shader = ExtResource( 2 )
[sub_resource type="ShaderMaterial" id="1"]
shader = ExtResource("2")

[node name="PatchMapDrawer" type="Control"]
[node name="PatchMapDrawer" type="Control" node_paths=PackedStringArray("populationIndicatorContainer")]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
mouse_filter = 1
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
size_flags_vertical = 3
script = ExtResource( 1 )
mouse_filter = 1
script = ExtResource("1")
DrawDefaultMapIfEmpty = true
PatchNodeContainerPath = NodePath("PatchNodeContainer")
LineContainerPath = NodePath("LineContainer")
MonochromeMaterial = SubResource( 1 )
MonochromeMaterial = SubResource("1")
populationIndicatorContainer = NodePath("PopulationIndicatorContainer")

[node name="PopulationIndicatorContainer" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0

[node name="LineContainer" type="Control" parent="."]
anchors_preset = 0

[node name="PatchNodeContainer" type="Control" parent="."]
anchors_preset = 0
3 changes: 2 additions & 1 deletion src/microbe_stage/editor/PatchMapEditorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public override void Init(TEditor owningEditor, bool fresh)
UpdateSeedLabel();
}

public void SetMap(PatchMap map)
public void SetMap(PatchMap map, uint playerSpeciesID)
{
mapDrawer.PlayerSpeciesID = playerSpeciesID;
mapDrawer.Map = map;
}

Expand Down
2 changes: 1 addition & 1 deletion src/multicellular_stage/editor/MulticellularEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected override void ResolveDerivedTypeNodeReferences()

protected override void InitEditor(bool fresh)
{
patchMapTab.SetMap(CurrentGame.GameWorld.Map);
patchMapTab.SetMap(CurrentGame.GameWorld.Map, CurrentGame.GameWorld.PlayerSpecies.ID);

base.InitEditor(fresh);

Expand Down