Opened 9 years ago

Closed 9 years ago

#3321 closed enhancement (fixed)

[PATCH] Add ability to query percentage of map controlled

Reported by: s0600204 Owned by: s0600204
Priority: Nice to Have Milestone: Alpha 19
Component: UI & Simulation Keywords: patch
Cc: sanderd17 Patch:

Description

The first of the two patches adds a function to the c++ component TerritoryManager that adds the ability for JS Component scripts to query what percentage of the game world is controlled by the player, as defined by territory boundaries.

This can be useful for several reasons, including:

  • Permitting a possible new win condition - when a player controls a given quantity of the map (ie. 90%) then they win.
  • Permitting the possibility for triggers to trip when a player exceeds a certain limit, or gets too small.
  • Permitting the JS ResourceTrickle component to be amended to be conditional on territory control (simulating taxation, perhaps)

The second of the two patches, included with the above as an example of how this new function might be used, implements the last of these options, rewriting the ResourceTrickle component to add a new optional "Type" field with three choices:

  • global - the default, and is what is currently implemented, with a constant rate as long as the entity exist
  • garrison - the trickle rate is per garrisoned unit. Good for corral or a mountain/shaft mine
  • territory - the trickle rate is limited by the percentage of the map the player controls as defined by territory boundaries

Please note that I'm very new to coding in c++, so... please go easy on me for any (newbie) mistakes...

Attachments (7)

1_TerritoryManager.patch (3.1 KB ) - added by s0600204 9 years ago.
First patch (see ticket description)
2_ResourceTrickle.patch (6.3 KB ) - added by s0600204 9 years ago.
Second patch (see ticket description)
1_TerritoryManager-v2.patch (3.8 KB ) - added by s0600204 9 years ago.
1_TerritoryManager-v3.patch (4.3 KB ) - added by s0600204 9 years ago.
1_TerritoryManager-v4.patch (4.0 KB ) - added by s0600204 9 years ago.
Updated patch with Itms' suggestions applied
3_Statistics.patch (7.8 KB ) - added by s0600204 9 years ago.
Use the new functionality in the Statistics component
3_Statistics-v2.patch (7.6 KB ) - added by s0600204 9 years ago.
Rebased on top of r16932

Download all attachments as: .zip

Change History (15)

by s0600204, 9 years ago

Attachment: 1_TerritoryManager.patch added

First patch (see ticket description)

by s0600204, 9 years ago

Attachment: 2_ResourceTrickle.patch added

Second patch (see ticket description)

comment:1 by leper, 9 years ago

Is the casting to float needed? Couldn't we just multiply with 100 (possibly cast to u16 to avoid issues) and then divide?

Why does Gaia own every tile? Shouldn't it be easier to just have ++m_TerritoryCounts[owner] and remove the if and the increment for Gaia?

Apart from that it seems like it would be better either calculating all of it only once (when queried and marking that function as expensive), or doing a proper init (all owned by Gaia) and then handle each ownership changed message (which we already do, well needn't be message but we do handle tile ownership changes in there).

by s0600204, 9 years ago

Attachment: 1_TerritoryManager-v2.patch added

comment:2 by s0600204, 9 years ago

Is the casting to float needed? Couldn't we just multiply with 100 (possibly cast to u16 to avoid issues) and then divide?

Yeah, that would make more sense. Fixed.

Why does Gaia own every tile? Shouldn't it be easier to just have ++m_TerritoryCounts[owner] and remove the if and the increment for Gaia?

I can see why you might be led to that assumption, but index 0 of m_TerritoryCounts != gaia. Gaia cannot own territory, and so I was using index 0 to store a count of total passible cells.

Hopefully the updated patch is better.

Last edited 9 years ago by s0600204 (previous) (diff)

comment:3 by Niek, 9 years ago

"Permitting the JS ResourceTrickle component to be amended to be conditional on territory control (simulating taxation, perhaps)"

I've suggested a similar idea before in the forums but the reaction was generally negative. (should be somewhere in the Suggestions topi)

comment:4 by leper, 9 years ago

Cc: sanderd17 added

You should do some init in Init(), and I'd prefer a normal array over std::array, though a vector could also be used. That would also be nicer than having that hardcoded 9 in there. (Using the player count (for a vector) or ARRAY_SIZE for an array. (Or .size() for std::array.)

GetTerritoryPercentage should do some range checks (ENSURE or returning 0, though I'd prefer ENSURE there).

CCing Sander since he worked on that code last and might have some other suggestions.

by s0600204, 9 years ago

Attachment: 1_TerritoryManager-v3.patch added

comment:5 by Itms, 9 years ago

Keywords: review removed

Hi, finally taking a look ;)

  • I suggest you replace your array by a vector. The cmpPlayerManager will always be here (or it has to be fixed) on line 393. So you can lazily initialize the vector size at that point.
  • Linked to the previous bullet point: don't store percentages, just store the number of cells (so your vector line 394 can be removed), and compute the actual percentage directly in GetTerritoryPercentage. That way you won't have initialization order problems.
  • I would rather initially set m_TerritoryTotalPassableCellCount to 0 and ENSURE it is non-zero when you have to divide by it. Else errors won't be noticeable.
  • Remove the new line 90 so it's clear that those variables work together.
  • line 539, you probably want to exclude player 0 (gaia). You should return 0 instead of crashing in that kind of situations, because modders will most probably try to call the function for wrong players at some point.

After that, it would be nice to actually use that code somewhere. You could create a second patch adding that value to the statistics tracking (you can take a look at the percentage of explored map for inspiration). That would allow you to track down any issue before we commit that patch.

Thanks a lot for working on this! :)

by s0600204, 9 years ago

Attachment: 1_TerritoryManager-v4.patch added

Updated patch with Itms' suggestions applied

by s0600204, 9 years ago

Attachment: 3_Statistics.patch added

Use the new functionality in the Statistics component

comment:6 by s0600204, 9 years ago

Keywords: review added

Hi Itms, thanks for the review. Hopefully the improved patch should satisfy your comments.

After that, it would be nice to actually use that code somewhere. [...] That would allow you to track down any issue before we commit that patch.

This is why the second patch above (2_ResourceTrickle.patch) exists. I was trying to avoid touching the statistics/summary code seeing as there is already two/three pending tickets with patches that make changes in that area... however, since you asked so nicely...

comment:7 by Itms, 9 years ago

The patch looks way better this way IMHO :) I have only one suggestion, line 532 you could cast to size_t to compare with an object size, but that's more or less cosmetics here.

Sorry for missing your trickle patch, that's the problem of going through a number of reviewable patches :D It is a nice idea but subject to design discussions, so focusing on the statistics patch is a better choice I think. I don't have time to review it tonight but I think you will have to rebase it onto r16932, if you have time to do that before I review it ;)

Thanks for your work.

by s0600204, 9 years ago

Attachment: 3_Statistics-v2.patch added

Rebased on top of r16932

comment:8 by Itms, 9 years ago

Keywords: review removed
Resolution: fixed
Status: newclosed

Committed the patch and the summary patch in r16933. I only changed the code in UpdateTerritories to avoid reallocating the vector each time.

Thanks for your work!

Note: See TracTickets for help on using tickets.