Kronnect

World Map Strategy Kit => Support => Topic started by: Pollyfun on July 05, 2021, 10:24:57 PM

Title: Territories Importer issues
Post by: Pollyfun on July 05, 2021, 10:24:57 PM
I have a few issues when using this feature. I've added the image Province_01.png which contains provinces that should be created.
I've also attached the matching texture and watermap (used in Earth settings).

First, I add a WMSK_Editor component (to WorldMapStrategyKit object). I use these settings:
  Show layers: Countries + Provinces
  Country File: High Definition Geodata File
 
I click New Map. After that provinces10.txt and countries10.txt are empty. cities10.txt still contain its data (not sure if this is intentional or not).

I learned (after some time) that I then have to click 'Create Background/Pool Country', which creates this entry in countries10.txt:
|Pool$<Background>$2500000,2500000;2500000,-2500000;-2500000,-2500000;-2500000,2500000$1$1$$$$

Then I use Territories Importer with Provinces_01.png (Creation mode: Provinces, Detail: Coarse). It completes without any visible errors.
provinces10.txt then contains new data (683 kb) and countries10.txt is unchanged.

When I select Pool in the dropdown and then select a province I can see that it now contains regions.

[[File:Kronnect1.png]]

So, the first error is that most of the provinces doesn't contain a main-region. mainRegionIndex is -1

Title: Re: Territories Importer issues
Post by: Pollyfun on July 05, 2021, 10:32:34 PM
Then, after clicking Play and running the game, and then stopping all the regions in the provinces have been lost.

Title: Re: Territories Importer issues
Post by: Kronnect on July 06, 2021, 11:17:46 AM
Hello,

Just imported your map as countries, not provinces, and works fine:

(https://i.imgur.com/2OqmIkXl.png)

A few notes:
- The Texture Importer will automatically select if the texture map is for countries or provinces. If the filename contains the word "province", it automatically suggests "provinces". However, you should only import a province map if you have previously imported a countries map because provinces are second-level entities. If there're no matching countries when you import a provinces map, the provinces will not be generated correctly or you can end with a mix of a previous countries map (ie. the Earth countries) and your fictional map.
- So, in the texture importer I chose "Countries" and clicked "Import". This operation imported the texture correctly and replaced all countries with your new map.
- Cities are preserved. In general, the import tools are quite conservative, in case you want to add extra parts of the world. So if you don't want to preserve the old cities nor the previous countries, just select "New Map" before clicking "Territory Importer". You can also remove the cities.txt file from the WMSK/Resources/Geodata folder or use the Map Editor to remove the cities using the Erase tool.
- Don't forget to click "Save" after the import or any other manual change.


Title: Re: Territories Importer issues
Post by: Kronnect on July 06, 2021, 11:19:53 AM
Forgot to mention that, after the import, I also assigned your world texture to the inspector field:
(https://i.imgur.com/1Ass0J3l.png)
Title: Re: Territories Importer issues
Post by: Pollyfun on July 06, 2021, 01:19:46 PM
Thanks for the feedback. I created a new texture, Countries_01.png and imported as countries which seems to work. But after that when I import provinces I still get the same problem. Most provinces are missing a main-region. When I select a province in the dropdown-list it's not highlighted on the map (which seems to be because an exception is raised when there's no mainRegion).

I've attached the countries texture and a slightly updated provinces texture.
Title: Re: Territories Importer issues
Post by: Kronnect on July 06, 2021, 04:26:38 PM
I fixed an issue with the cities being reloaded after clicking "New Map". This fix will be included in next beta.

However, I managed to import the countries and provinces correctly. The steps were:
1) Create a New Map.
2) Import the countries textures.
3) Save the map.
4) Import the provinces textures.
5) Save the map again.
6) Run the scene.

Highlight seems to work fine as well. I'm sending you the geodata produced by pm so you can replace the Geodata folder.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 06, 2021, 10:25:14 PM
Thanks, using that sequence it does seem to work. :D

A related question, I will probably only have one country in the Countries_01.png, and then when loading the game I will loop through the provinces and
assign each to a country using data from a separate database. This is to easily support save/load games without changing any Geodata files.
Do you think CountryTransferProvinceRegion() is the best method for setting the owner?

Also, is there a best practice for handling sea provinces? I don't wan't them to have an owner, but I guess they must have one?
Title: Re: Territories Importer issues
Post by: Pollyfun on July 06, 2021, 11:55:02 PM
In addition, what's the difference between Coarse and Fine? Is it for the shape of the regions?
countries10.txt and provinces10.txt looks to be the same size with either choice.

Title: Re: Territories Importer issues
Post by: Kronnect on July 07, 2021, 09:04:50 AM
CountryTransferProvinceRegion() method will do that but it will also expand the country borders.
In WMSK, countries are mandatory, even if there's only one country. A province must belong to a country.

I've added a new CountryTransferProvince() method which is a simplified version of the above which just updates the link between a province and a country without messing with the country borders. This is a more convenient method to prepare your provinces when loading a new game. Use CountryTransferProvinceRegion() when you actually want the borders to be modified when one country is conquering provinces from another country. But if you want to keep the province shapes (ie. just coloring them to show ownership), then just use CountryTransferProvince() or simply color the provinces and don't change ownership at all.

The "Fine" vs "Coarse" option determines the radius of the kernel when finding different colors in the texture. Each different color represent a different territory (country or province, depending on what you're importing). The Fine option will use a kernel of 3x3 while a Coarse uses a 5x5 area to determine if a color is enough represented in the texture. This usually has no effect when you have large land areas with solid colors but if you have very small land patches, like tiny islands of less than 5x5 pixels in the textures, those could be ignored unless "Fine" is used.

 

Title: Re: Territories Importer issues
Post by: Pollyfun on July 08, 2021, 12:11:33 AM
Sounds great. CountryTransferProvince() will be in the next release?

I haven't fully got how borders work yet, I'll have to look more into that to determine the best way to handle province ownership.
Regarding sea regions, I'll create a hidden country that contains them, and is handled with special logic.

Title: Re: Territories Importer issues
Post by: Pollyfun on July 11, 2021, 09:22:03 PM
I've continued and created a mostly complete map that's intended to be used. But I can't get it to import correctly.
There are errors on both the country level and the province level.

The country texture contains 2 colors, one used for land and one used for water. When importing the sea area is ignored,
and the land area is split incorrectly into two countries, and the polygons are incorrect.


Title: Re: Territories Importer issues
Post by: Kronnect on July 12, 2021, 04:04:24 PM
I've been able to reproduce the issue with the countries map. It seems that the right continent has too much detail for what the algorithm is designed and it fails to extract that region.

EDIT: it's related to a rogue white pixel in the original country texture. Still investigating...
Title: Re: Territories Importer issues
Post by: Kronnect on July 12, 2021, 06:40:44 PM
I've sent the geodata by pm with the fixed version which will be released as beta shortly.

A few notes:
1) The error was produced by a rogue white pixel which was producing a division in the borders of the right side continent. This has been fixed.
2) Territories must always be enclosed. This means that one can't create a territory that comprimes the non-land areas of the world map (ie. the ocean surrounding the continents).
3) Currently, the territory importer will import all provinces as long as they fall inside a country region. Overseas provinces are not supported - next beta will allow you to specify a country for overseas regions, so you will be able to specify it in the territory importer when you're importing provinces. Then, when sea provinces will be linked to that country (or be ignored if that field is not set in the form).

So, next beta will include the fix and also the abitlity to specify a country for overseas provinces. You could create this country after importing the country texture or choose any existing country if you wish (or leave this in blank which will produce overseas provinces will be ignored).

Regards
Title: Re: Territories Importer issues
Post by: Pollyfun on July 12, 2021, 10:21:33 PM
Great that you found the white pixel. The import worked better here after removing it.  :)

It sounds good that the next version will support sea provinces. What I tried to do here was to create all regions as land regions and then use the watermap
to make some of them look like sea regions. Since the textures doesn't specify which regions are land and which regions are sea I don't see how the importer knows about that (?)

Two of the sea provinces were imported (although with the wrong owner). What I need for the game is for them to render with white borders (as in the image), to be able to highlight them
(separately since in the gameplay they will have no owner), and using tooltip to show additional information about them. Optimally, it wouldn't highlight land regions inside them but I know that's a separate issue.

Thanks for the quick feedback. If I get this importer to work it will save me a ton of time.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 13, 2021, 11:33:09 PM
I had some progress with the sea provinces after adding a white frame in both the Countries and the Provinces textures. That seems to build around the problem that territories must be enclosed.
Two of the sea provinces and one of the land regions don't work, but hopefully I can update the textures somehow and fix that.



Title: Re: Territories Importer issues
Post by: Pollyfun on July 13, 2021, 11:38:12 PM
I have a couple of land provinces that are completely inside sea provinces. The tooltip etc doesn't work on them.
Can this be made to work somehow, or should I change the sea regions so they never have a complete land province inside them (so each land province in the ocean borders to at least two sea provinces) ?

Title: Re: Territories Importer issues
Post by: Kronnect on July 14, 2021, 09:57:08 AM
Try enabling enclaves. That option can be found under "Miscellanea" section at bottom of the inspector.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 14, 2021, 07:54:12 PM
I thought I had it enabled already, but that was in an earlier test. Thanks for pointing it out. Now those provinces work.

Additional question, after doing the import is there any way to access the color that was used in the texture for each province?
Something like 8CFFBA, 7CA3FF, 900AFF. If that's possible I could use that as a unique identifer, and connect it with the custom data I have for each.
Like:
(Province 0) 8CFFBA == "Weddell Island"
(Province 1) 7CA3FF == "Speedwell Island"
(Province 2) 900AFF == "Lafonia"
(Province 3) FF66AD == "Port Howard"

If not, do you know any suitable place during the import process where I can hook in and where the color is available so I can set a data-property (unique ID)?
Title: Re: Territories Importer issues
Post by: Kronnect on July 14, 2021, 08:40:31 PM
The importer could add that info although you can also fetch it in the reverse way.
The center of a province is stored in province.mainRegion.center, which is a vector2 in the -0.5..0.5 range. Just add a "0.5" to convert that to texture coordinates and you can fetch the color of your territory texture.

Code
Vector2 textureCoordinates = province.mainRegion.center + new Vector2(0.5f, 0.5f);
Title: Re: Territories Importer issues
Post by: Kronnect on July 16, 2021, 01:53:38 PM
Next beta now adds an entity attribute which can be accessed and modified using country.attrib["ImporterColor"] or province.attrib["ImporterColor"].
See attached.
The format of this value is RRGGBB in hexadecimal.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 17, 2021, 12:54:35 AM
I did get a rough test to work using the code you provided and Texture2D.GetPixel(). But I'm not sure that it will work if the province is C-shaped, or some other shape where the center is not inside the province.
And additionally, it would require including the Province texture in the game which would take up memory unnecessarily.

I will test out the new attribute next. It looks much smoother.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 18, 2021, 04:44:34 PM
The ImporterColor worked as described, so now I've matched the provinces with the custom data for each, which is an important step forward.

Title: Re: Territories Importer issues
Post by: Kronnect on July 18, 2021, 04:47:07 PM
Nice!
Title: Re: Territories Importer issues
Post by: Pollyfun on July 18, 2021, 04:51:40 PM
Now I'm looking at assigning provinces during load game.

CountryTransferProvince - when I use this it's quick, but it seems the regions for the countries aren't updated, so tooltip/highlighting doesn't work afterwards.
CountryTransferProvinceRegion - when I use this it looks correct but it takes a lot of time (10-20s) for 84 provinces.

Any advice on this is appreciated. Could CountryTransferProvinceRegion() be made quicker somehow when doing all provinces at once?

And a general question, is there a good reason that geodata for regions have to exist both in country and in the province? It seems a bit redundant.

Thanks
Title: Re: Territories Importer issues
Post by: Kronnect on July 18, 2021, 08:14:37 PM
Try latest beta. The method CountryTransferProvince will now also add the province regions to the country regions list.

Regarding the redundancy of region layers, the issue is that the asset can use just countries or both countries and provinces, so the geodata and highlighting system expects that each entity level has its own region data (could use different detail levels). If you just use countries + provinces, then yes, it could be optimized so country regions could be ignored and use directly the province regions (assuming every part of the world is covered by a province).. but it's not how currently works.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 18, 2021, 11:25:26 PM
Thanks for the explanation, and for the quick fix. Now it assigns the provinces correctly, and quickly.  :)

One remaining issue is that the black borders between countries are not in the correct place. The 3 provinces with the black borders shouldn't have them,
and the red line I painted on top is where the country border should actually be. Is there an easy way to refresh just the border?

Title: Re: Territories Importer issues
Post by: Kronnect on July 19, 2021, 09:28:29 AM
The quick-transfer operation just adds the new regions of the provinces to the country region list so it will highilght independently.
The other function, which is slower, does the merging when land areas overlap. Could this be the reason? Feel free to send the geodata by pm so I can better understand the issue.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 19, 2021, 10:40:34 PM
I've sent the geodata in case it helps. I'll try to illustrate the situation better. After loading and assigning all provinces,
3 of the land provinces belongs to United Kingdom. The other land provinces belongs to Argentine, and all the ocean provinces
belongs to "Ocean Country". I believe there should a frontier/border between provinces owned by United Kingdom and provinces
owned by Argentine (the red line I drew).

In the second image I made frontiers bigger. They are not in the correct place, and there's also the black border around 3 provinces
(as before) which I don't know what it is.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 19, 2021, 11:07:40 PM
I sent a PM with a video of the problem. The file was too big for posting.
Title: Re: Territories Importer issues
Post by: Kronnect on July 20, 2021, 10:54:50 AM
Thank you, that was helpful.
Well, the issue with the new method (one of the reasons why the other one is slower) is that it didn't test if the province region would overlap any existing country region. This has been changed in latest beta, please try it.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 21, 2021, 10:10:12 AM
Thanks. But I can't quite get things to look correctly. I'll try to explain more.
After using the Territories Importer and running the game, I would expect the map to look as the example in "WorldMapStrategyKit\Demos\General Examples\103 Province Expansion".
Green frontiers around each country, and a black border only around the country that's currently highlighted. And white borders for provinces using 'Show Provinces'.

Title: Re: Territories Importer issues
Post by: Pollyfun on July 21, 2021, 10:14:15 AM
When using the importer with these textures, and running the game (without doing the load game or using CountryTransferProvince), there is permanent
black borders between countries which shouldn't be there.

Use the Detail:Fine and Snap to country frontiers when importing.

Title: Re: Territories Importer issues
Post by: Pollyfun on July 21, 2021, 10:36:17 AM
UPDATE. When I did what I described in a completely new project, the first steps looks correct. It must be I have some custom code that's disturbing the highlighting/border system.

I will test more, and update more if there's still an issue. Thanks

Title: Re: Territories Importer issues
Post by: Pollyfun on July 21, 2021, 10:36:18 PM
I finally got time to test a bit more. The first phase works, where five countries are used (1 land and 4 sea countries).
- The frontiers are correct
- Highlighted country gets a black border inside the green frontier
- Province borders are always white

After looping through the provinces and using CountryTransferProvince() to assign them to either Ocean, Argentine or United Kingdom, the following problems occur:
- The frontiers are not updated. It's still showing the old frontiers.
- Highlighted country gets a black border that follows the frontiers, but
- Province borders are black when the country is highlighted

I'll sent a video in PM.
Title: Re: Territories Importer issues
Post by: Kronnect on July 21, 2021, 11:46:43 PM
CountryTransferProvince has its limitations as it’s intended to just reassign provinces to countries without border matching/updating.
I wonder if you get the right results using the robust CountryTransferProvinceRegion method instead (although slower since you’re calling it for many provinces).
Title: Re: Territories Importer issues
Post by: Kronnect on July 22, 2021, 07:46:10 PM
Try latest beta. It should now remove the original regions from the source country.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 23, 2021, 12:00:37 AM
When I use CountryTransferProvinceRegion instead of CountryTransferProvince, it takes a very long time (more than a minute),
and some frontiers and borders are better, but some frontiers are also incorrect.

Title: Re: Territories Importer issues
Post by: Pollyfun on July 23, 2021, 12:03:29 AM
The latest beta didn't behave differently. But I notice that new statements are never executed:

if (sourceCountry.regions.Contains(provinceRegion)) {   <--- always false
  sourceCountry.regions.Remove(provinceRegion);
  requireCountryGeometryRefresh = true;
}

Title: Re: Territories Importer issues
Post by: Pollyfun on July 23, 2021, 12:05:27 AM
I will create a minimal Unity project containing these texture and relevant code and zip it. Then perhaps you can more easily find a fix. I'll look at it tomorrow.
Title: Re: Territories Importer issues
Post by: Kronnect on July 23, 2021, 09:09:43 AM
Thanks, it looks like the best way to tackle these issues.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 24, 2021, 12:31:56 AM
I've sent the project. After unzipping, the Assets\WorldMapStrategyKit folder has to be added.
Then open MapTest5\Assets\Scenes\SampleScene.unity.

Borders and frontiers intially looks correct. After clicking the "Assign provinces" button which
use CountryTransferProvince() they are off.

You can use Countries_3px.png and Provinces 3px.png above if you want to rerun Territories Importer.

Thanks for checking.

Title: Re: Territories Importer issues
Post by: Kronnect on July 26, 2021, 09:48:27 AM
Thank you, will take a look and get back soon.
Title: Re: Territories Importer issues
Post by: Kronnect on July 28, 2021, 12:44:12 PM
Hi,

Using your repro I noticed something I should have warned before. When merging all the provinces that are adjacent to the world edges, you will end in a big outer polygon which is just a big quad, plus the interior border. This is not supported because regions are defined by a single polygon so it's recommended that the "ocean country" is split in at least 2 or 3 parts. Fortunately you have already split the ocean in 4 countries ("Ocean North East", "Ocean North West" and so on) so I have improved the CountryTransferCountry method to accept a new parameter called "mergeRegions" which we can use to just add up the 4 ocean countries as 4 regions of a same country and rename it. For example:

Code
_map.CountryTransferCountry(_map.GetCountryIndex("Ocean South West"), _map.GetCountryIndex("Ocean South East"), redraw: false, mergeRegions: false);
_map.CountryTransferCountry(_map.GetCountryIndex("Ocean South West"), _map.GetCountryIndex("Ocean North East"), redraw: false, mergeRegions: false);
_map.CountryTransferCountry(_map.GetCountryIndex("Ocean South West"), _map.GetCountryIndex("Ocean North West"), redraw: false, mergeRegions: false);
_map.CountryRename("Ocean South West", "Ocean");
_map.Redraw();

The code above does the following:
1) It transfer each other country to "Ocean South West". Passing "mergeRegions = false" produces that the other countries are just added as regions to the first country in the function call. Borders are not merged so the operation is fast and we don't have the limitation of having a single country that covers the entire world.
2) The resulting country is "Ocean South West" which we can rename to just "Ocean" (so you don't need to create an artifical country called Ocean in your code).

The only downside is that you can still see tiny black borders between the 4 big regions although they're not very apparent.

Please get the latest beta and try the code above. You can just replace the "AssignOwners" code in Logic.cs with this code.

Note: also it's worth noting that tranferring provinces using CountryTransferProvinceRegion should work fine when you're just transfering one or two provinces at the same time. However in terms of gameplay, the recommended way to illustrate ownership is to use colors or textures or other symbols, instead of merging regions because this is an operation that can't be reversed (in case you want players to conquer and reconquer the same provinces for example). Allowing players to conquer other provinces and using CountryTransferProvinceRegion can lead to unexpected results if the resulting geometry is too complex like in this case where all ocean provinces where surrounding the whole map.
Title: Re: Territories Importer issues
Post by: Pollyfun on July 28, 2021, 10:14:57 PM
Ok, thanks for the update. It sounds like it should simplify the handling of ocean regions a bit. I’m away for a couple of days but I’ll try it out soon.

About  frontiers between countries. Are they saved in the geodata (and difficult to recreate/refresh)?



Title: Re: Territories Importer issues
Post by: Kronnect on July 28, 2021, 10:27:15 PM
Geodata files only get modified using the MapEditor.
Title: Re: Territories Importer issues
Post by: Pollyfun on August 01, 2021, 05:27:57 PM
I've integrated the new code, and it works as described. Now I don't need to create the extra Ocean country.
The described problems with borders and frontiers are still there though. I found that the project I sent wasn't complete, so
perhaps you could'nt reproduce the problems. I had removed code needed for the tooltip (showing province name and country name),
and the world textures. I've added those, so hopefully the new project I've sent is complete.

After clicking "Assign provinces", where all provinces are assigned from "Main Country" to either Argentine or United Kingdom, it's
the following errors:
- highlighting are making all province borders black (they should remain slim white as before)
- old country frontiers remain (should be removed if possible)
- new country frontiers aren't shown (perhaps I can live without them if I show ownership some other way)

Like you mentioned, I don't want to use any non-reversable methods (like CountryTransferProvinceRegion) since all provinces
could change owners repeatedly during a game session. The world I use is static in the sense that during the game I will not
change any of the land or sea, only changing ownership of land provinces. Optimally, no geodata will be changed during a game
session. But if country frontiers aren't part of geodata, perhaps they can be regenerated after all provinces have changed their owner?
Title: Re: Territories Importer issues
Post by: Pollyfun on August 02, 2021, 10:14:40 AM
I've started to debug DrawFrontiers() since the incorrect Frontiers gameobject is created there.
As the last step of the "Assign Provinces" logic, I have the _map.Redraw() call.
In this situation needOptimizeFrontiers is false, so the frontiers aren't recreated.
When I override the code and set it to true, the frontiers are regenerated here, but things are still incorrect.
All the land province borders have become frontiers.
This seems directly related to the first issue with the black border highlighting, so the root cause of all issues might be the same.
Title: Re: Territories Importer issues
Post by: Kronnect on August 02, 2021, 02:42:57 PM
Hi,

I got your updated code. The issue is that you're using CountryTransferProvince() method which is designed for adding provinces to a country but it doesn't merge frontiers (so it's a lot faster).
The correct method is CountryTransferProvinceRegion(). This method is slower but merges regions correctly after they change owners (if you uncomment those lines in your Logic.cs you can verify this).

I'm adding a new method similar to CountryTransferProvinceRegion() that accepts multiple provinces so it's faster as it optimizes some operations.
Title: Re: Territories Importer issues
Post by: Kronnect on August 02, 2021, 07:11:58 PM
New beta.
It includes the new CountryTransferProvinces method, which allows you to pass a list of provinces. It does the job significantly faster, from a previous benchmark of 50 seconds to 8.
I've also sent you a pm with the updated project which makes use of this new API.
I think it will be harder to reduce that timing since there're many provinces and regions involved in the operation but give it a try and let me know how it goes.

p.s. the old CountryTransferProvince has been removed since it was not operating correctly (was fast, but incomplete).
Title: Re: Territories Importer issues
Post by: Pollyfun on August 03, 2021, 09:22:48 AM
Thanks, that was a great update. Now the important things looks correct (borders, frontiers, highlighting) after loading.
I think the loading time is ok for this scenario (with around 80 provinces). I'll show a loading screen.

A theoretical question, could skipping frontiers for all coastlines speed things up? For this scenario, it would mean
only creating a frontier where Argentine and United Kingdom directly borders each other.

Title: Re: Territories Importer issues
Post by: Kronnect on August 03, 2021, 10:01:04 AM
Not really. The slowest part has to do with polygon sanitization. This is a process that takes place after the polygon operations have finished that ensure that the resulting polygons are correct. I'll keep trying to reduce those times in next updates.
Title: Re: Territories Importer issues
Post by: Pollyfun on August 03, 2021, 10:29:52 AM
Ok, I understand. Later optimizations are always welcome. :)

But lets say for purely graphical reasons, would it be difficult to add a parameter to skip the creation of all coastline frontiers?
I get the impression that this scenario could look better/clearer that way.
Title: Re: Territories Importer issues
Post by: Kronnect on August 03, 2021, 10:39:37 AM
Visually speaking, yes. You have some options to hide coast lines in the inspector. See attached.
Title: Re: Territories Importer issues
Post by: Pollyfun on August 03, 2021, 10:51:19 AM
Ah, okay. I got it to work in the default world map, but not in this scenario. I suspect the sea provinces are treated just as land provinces so the frontier is created anyway. Any idea for this?
Thanks

Update: I could easily loop through the provinces and set a property on each one. I saw there's an attrib property. Perhaps this one could be used, or a new property to mark it as ocean?
Then this property could be used for ignoring sea provinces when creating frontiers (when Coastline is unchecked).
Title: Re: Territories Importer issues
Post by: Pollyfun on August 03, 2021, 04:04:35 PM
Another general question. Why are regions merged at all? Is for performance reasons (fewer separate objects to render simultaneously), and/or is it a necessity before creating frontiers?
Title: Re: Territories Importer issues
Post by: Kronnect on August 03, 2021, 05:17:38 PM
A region represents a continuous area of land or domain. It's represented by a polygons, which are the ones that get merged. Polygons are then triangulated to create a color or texture surface when highlighting or customizing the region.
In this case, regions are merged so a continuous polygon shows the frontiers of a country.
Title: Re: Territories Importer issues
Post by: Pollyfun on August 04, 2021, 07:41:13 AM
Update: I could easily loop through the provinces and set a property on each one. I saw there's an attrib property. Perhaps this one could be used, or a new property to mark it as ocean?
Then this property could be used for ignoring sea provinces when creating frontiers (when Coastline is unchecked).

You can skip this request. I'm trying to fix frontiers in another way. I'll give an update later.
Title: Re: Territories Importer issues
Post by: Pollyfun on August 07, 2021, 06:04:57 PM
I've tested more using the new CountryTransferProvinces() when loading countries, and then using the CountryTransferProvinceRegion() method
when changing province owners one at a time. It often works as expected for a few provinces, but then some problem occur. I've recorded two examples.

In this first case I can change owner of 3 provinces but clicking the fourth the game crashes with an exception, and the following call stack.

Code
NullReferenceException: Object reference not set to an instance of an object
WorldMapStrategyKit.Poly2Tri.DTSweep.FlipEdgeEvent (WorldMapStrategyKit.Poly2Tri.DTSweepContext tcx, WorldMapStrategyKit.Poly2Tri.TriangulationPoint ep, WorldMapStrategyKit.Poly2Tri.TriangulationPoint eq, WorldMapStrategyKit.Poly2Tri.DelaunayTriangle t, WorldMapStrategyKit.Poly2Tri.TriangulationPoint p) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/Triangulation/Delaunay/Sweep/DTSweep.cs:585)
WorldMapStrategyKit.Poly2Tri.DTSweep.FlipEdgeEvent (WorldMapStrategyKit.Poly2Tri.DTSweepContext tcx, WorldMapStrategyKit.Poly2Tri.TriangulationPoint ep, WorldMapStrategyKit.Poly2Tri.TriangulationPoint eq, WorldMapStrategyKit.Poly2Tri.DelaunayTriangle t, WorldMapStrategyKit.Poly2Tri.TriangulationPoint p) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/Triangulation/Delaunay/Sweep/DTSweep.cs:612)
WorldMapStrategyKit.Poly2Tri.DTSweep.EdgeEvent (WorldMapStrategyKit.Poly2Tri.DTSweepContext tcx, WorldMapStrategyKit.Poly2Tri.TriangulationPoint ep, WorldMapStrategyKit.Poly2Tri.TriangulationPoint eq, WorldMapStrategyKit.Poly2Tri.DelaunayTriangle triangle, WorldMapStrategyKit.Poly2Tri.TriangulationPoint point) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/Triangulation/Delaunay/Sweep/DTSweep.cs:579)
WorldMapStrategyKit.Poly2Tri.DTSweep.EdgeEvent (WorldMapStrategyKit.Poly2Tri.DTSweepContext tcx, WorldMapStrategyKit.Poly2Tri.TriangulationPoint ep, WorldMapStrategyKit.Poly2Tri.TriangulationPoint eq, WorldMapStrategyKit.Poly2Tri.DelaunayTriangle triangle, WorldMapStrategyKit.Poly2Tri.TriangulationPoint point) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/Triangulation/Delaunay/Sweep/DTSweep.cs:537)
WorldMapStrategyKit.Poly2Tri.DTSweep.EdgeEvent (WorldMapStrategyKit.Poly2Tri.DTSweepContext tcx, WorldMapStrategyKit.Poly2Tri.DTSweepConstraint edge, WorldMapStrategyKit.Poly2Tri.AdvancingFrontNode node) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/Triangulation/Delaunay/Sweep/DTSweep.cs:353)
WorldMapStrategyKit.Poly2Tri.DTSweep.Sweep (WorldMapStrategyKit.Poly2Tri.DTSweepContext tcx) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/Triangulation/Delaunay/Sweep/DTSweep.cs:101)
WorldMapStrategyKit.Poly2Tri.DTSweep.Triangulate (WorldMapStrategyKit.Poly2Tri.DTSweepContext tcx) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/Triangulation/Delaunay/Sweep/DTSweep.cs:64)
WorldMapStrategyKit.Poly2Tri.P2T.Triangulate (WorldMapStrategyKit.Poly2Tri.TriangulationContext tcx) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/P2T.cs:75)
WorldMapStrategyKit.Poly2Tri.P2T.Triangulate (WorldMapStrategyKit.Poly2Tri.TriangulationAlgorithm algorithm, WorldMapStrategyKit.Poly2Tri.ITriangulatable t) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/P2T.cs:68)
WorldMapStrategyKit.Poly2Tri.P2T.Triangulate (WorldMapStrategyKit.Poly2Tri.Polygon p) (at Assets/WorldMapStrategyKit/Scripts/ThirdParty/PolyTri/P2T.cs:44)
WorldMapStrategyKit.WMSK.GenerateCountryRegionSurface (System.Int32 countryIndex, System.Int32 regionIndex, UnityEngine.Material material, UnityEngine.Vector2 textureScale, UnityEngine.Vector2 textureOffset, System.Single textureRotation) (at Assets/WorldMapStrategyKit/Scripts/Core/Internal/WMSKPrivCountry.cs:796)
WorldMapStrategyKit.WMSK.HighlightCountryRegionSingle (System.Int32 countryIndex, System.Int32 regionIndex, System.Boolean refreshGeometry, System.Boolean drawOutline) (at Assets/WorldMapStrategyKit/Scripts/Core/Internal/WMSKPrivCountry.cs:660)
WorldMapStrategyKit.WMSK.HighlightCountryRegion (System.Int32 countryIndex, System.Int32 regionIndex, System.Boolean refreshGeometry, System.Boolean drawOutline) (at Assets/WorldMapStrategyKit/Scripts/Core/Internal/WMSKPrivCountry.cs:608)
WorldMapStrategyKit.WMSK.CheckMousePos () (at Assets/WorldMapStrategyKit/Scripts/Core/Internal/WMSKPrivate.cs:1801)
WorldMapStrategyKit.WMSK.PerformUserInteraction () (at Assets/WorldMapStrategyKit/Scripts/Core/Internal/WMSKPrivate.cs:699)
WorldMapStrategyKit.WMSK.Update () (at Assets/WorldMapStrategyKit/Scripts/Core/Internal/WMSKPrivate.cs:245)

https://www.youtube.com/watch?v=VzYHv-9sZ7Q (https://www.youtube.com/watch?v=VzYHv-9sZ7Q)

In the second case I change owner of the provinces No Man's Land and Teal Inlet to UK. Then I change back Teal Inlet to Argentine.
This makes No Man's Land province to disappear. At least it's not possible to interact with it anymore.

https://www.youtube.com/watch?v=HT_8EBhEhq4 (https://www.youtube.com/watch?v=HT_8EBhEhq4)

Note that you won't have to try and fix these issues, I just want to explain why I won't use these methods going forward. I've come to the conclusion that these operations can probably never be 100% succesful due to all the low-level operations taking place.
For the game I'm making stability of the core features (like changing province owner) must be 100 %. So there can be no merging of regions and so on.

I will inactivate frontiers, and use the CountryTransferProvince() method instead. It's much quicker, and hopefully reliable (as far as I've tested it).
I plan to create other frontiers that are totally separate objects that are rendered on top of the world objects.

Title: Re: Territories Importer issues
Post by: Pollyfun on August 07, 2021, 08:51:03 PM
When disabling frontiers and using CountryTransferProvince() for loading provinces and for changing owner one by one, it mostly works. But there's a couple of issues that would be great if they could be fixed. The first one is that regions are not removed from the sourcecountry. I'm using this code:

Code
public bool CountryTransferProvince(int targetCountryIndex, int provinceIndex) {

if (provinceIndex < 0 || targetCountryIndex < 0 || targetCountryIndex >= _countries.Length)
return false;

// Province must belong to another country
Province province = provinces[provinceIndex];
int sourceCountryIndex = province.countryIndex;
if (sourceCountryIndex == targetCountryIndex)
return false;

// Remove province form source country
Country sourceCountry = _countries[sourceCountryIndex];

if (sourceCountry.provinces != null) {
List<Province> sourceProvinces = new List<Province>(sourceCountry.provinces);
if (sourceProvinces.Contains(province)) {
sourceProvinces.Remove(province);
sourceCountry.provinces = sourceProvinces.ToArray();
}
}

// Adds province to target country
Country targetCountry = _countries[targetCountryIndex];
List<Province> destProvinces;
if (targetCountry.provinces == null) {
destProvinces = new List<Province>();
} else {
destProvinces = new List<Province>(targetCountry.provinces);
}
destProvinces.Add(province);
destProvinces.Sort(ProvinceSizeComparer);
targetCountry.provinces = destProvinces.ToArray();

// Update owner index
province.countryIndex = targetCountryIndex;

// Adds province regions to target country regions
bool requireCountryGeometryRefresh = false;
if (province.regions == null) ReadProvincePackedString(province);
int regionsCount = province.regions.Count;
for (int k = 0; k < regionsCount; k++) {
Region provinceRegion = province.regions[k];
if (!targetCountry.Contains(provinceRegion)) {
Region newRegion = provinceRegion.Clone();
newRegion.entity = targetCountry;
targetCountry.regions.Add(newRegion);
requireCountryGeometryRefresh = true;
}

if (sourceCountry.regions.Contains(provinceRegion)) {
sourceCountry.regions.Remove(provinceRegion);
requireCountryGeometryRefresh = true;
}
}

if (requireCountryGeometryRefresh) {
RefreshCountryGeometry(targetCountry);
    }

    return true;
}

There's code to remove the region, but it doesnt' work
Code
if (sourceCountry.regions.Contains(provinceRegion)) {
    sourceCountry.regions.Remove(provinceRegion);
    requireCountryGeometryRefresh = true;
}


Here's a video showing the results.
https://www.youtube.com/watch?v=N2H75ECjwrs (https://www.youtube.com/watch?v=N2H75ECjwrs)

Title: Re: Territories Importer issues
Post by: Kronnect on August 09, 2021, 12:43:41 PM
Hi,

The function CountryTransferProvince was replaced by an overload to CountryTransferProvinceRegion in latest version because it was misleading. Both methods operate similarly now, except that the first method requires a province index and the second one allows you to pass a Region. They do the same thing: they will try to extract from the source country and add it to the target country affecting country frontiers, cities, mount points and merging any affected regions (among other limitations, the old CountryTransferProvince method could not remove regions from source country properly because the country regions are different objects).

In latest beta I added 3 new methods:
- CountrySetProvinces
- CountryAddProvinces
- CountryRemoveProvinces

The way they work is quite different to CountryTransferProvince method.

- CountrySetProvinces will replace ALL provinces in a given country with the passed list. This call will also update the country regions by adding all regions from the provinces and merging them (optionally, by default, mergeRegions parameter is true).
- CountryAddProvinces will add ONE OR MORE provinces to an existing country and merge the regions of those provinces into the country regions.
- CountryRemoveProvinces will call CountrySetProvinces but excluding the given provinces.

The key difference here wrt CountryTransferRegion is that there's no substraction operation on the source country which can cause issues with some polygons.

To summarize:
- CountryTransferProvince will extract one province from a source country and add it to a target country. It performs substraction on the origin country polygon and addition to the target country polygon.
- The same result can be achieved by calling CountryRemoveProvinces on the source country and CountryAddProvinces on the target country. In these cases however, the country provinces list and regions are being recreated completely on the source country. There's no substraction. This operation can be slower (should be slower) on countries with many provinces but should be safer as there's no substraction.

For example, to replace the provinces in Spain with just 2 provinces (Sevilla and Cádiz):

Code
                int spain = map.GetCountryIndex("Spain");
                List<Province> provs = new List<Province>();
                provs.Add(map.GetProvince("Sevilla", "Spain"));
                provs.Add(map.GetProvince("Cádiz", "Spain"));
                map.CountrySetProvinces(spain, provs);
                map.Redraw(true);

Then to add Málage province:

Code
                int spain = map.GetCountryIndex("Spain");
                List<Province> provs = new List<Province>();
                provs.Add(map.GetProvince("Málaga", "Spain"));
                map.CountryAddProvinces(spain, provs);
                map.Redraw(true);

Please get the latest beta and let me know how this alternative works better for you (CountryRemoveProvinces + CountryAddProvinces).
Title: Re: Territories Importer issues
Post by: Pollyfun on August 09, 2021, 01:11:06 PM
Sounds good  8) I will try it out soon.
Title: Re: Territories Importer issues
Post by: Pollyfun on August 11, 2021, 09:21:31 AM
I've done some testing now. I can assign provinces one at a time. I used this code

Code
_map.CountryRemoveProvinces(province.countryIndex, provincesToAdd, false);
_map.CountryAddProvinces(targetCountryIndex, provincesToAdd, false, false, false);

https://youtu.be/Srceaq6K1-U

It seems stable when shifting the owner forth and back, so that's very positive.

There's still some issues to work out though. When I'm loading the world, and assigning provinces

"Main Country" -> "Argentine"
"Main Country" -> "United Kingdom"

The assigning works so far:
_map.CountrySetProvinces(_map.GetCountryIndex("United Kingdom"), provincesToAddUK, false, false, false);
_map.CountrySetProvinces(_map.GetCountryIndex("Argentine"), provincesToAddArgentine, false, false, false);

But when I try to remove all provinces from "Main Country" the game gets exceptions right afterwards.

Code
_map.CountryRemoveProvinces(_map.GetCountryIndex("Main Country"), provincesToRemoveMainCountry, false);

When provinces belongs both to the sourcecountry and the destinationcountry, is it the same objects or is it clones?
Is there a safe way to delete a country and all it's provinces and regions? (when the provinces and regions also belongs to another country like in this case)
Title: Re: Territories Importer issues
Post by: Kronnect on August 11, 2021, 10:18:13 AM
I've refreshed the latest beta with a safety check in that function so it doesn't break if the country doesn't have any region.

The Provinces objects are not cloned so they're the same object in both countries (so CountryAddProvinces / CountryRemoveProvinces should be called in sequence if you're transferring a province from one country to another). The only case where you don't need to use CountryRemoveProvines is during an initial setup for example, where you have all provinces assigned to some kind of global pool or main country and you distribute them to the real countries to finally completely removing the pool country.

You can fully remove a country calling CountryDelete method.
Title: Re: Territories Importer issues
Post by: Pollyfun on August 11, 2021, 12:50:44 PM
Thanks, I got it working.

A possible code improvement. It would be good if we could create an empty country, without needing to add a province and a region to it.
Currently I use this code to create a country, and get country.provinces initialized.

Code
Vector2[] pointsArgentine =
{
new Vector2(-0.2f, 0.59f),
new Vector2(-0.1f, 0.59f),
new Vector2(-0.1f, 0.55f),
new Vector2(-0.2f, 0.55f)
};
int countryIndex = CreateCountry("Argentine", pointsArgentine, 11);
CreateProvince("Arg Province 1", countryIndex, pointsArgentine);   // create a province to initialize country.provinces
_map.ProvinceDelete(_map.provinces.Length-1);  // delete province
_map.GetCountry(countryIndex).regions.Clear();  // delete region


It would be great is this was enough:
Code
int countryIndex = CreateCountry("Argentine", 11);

So two parts:
Skipping the points parameter in CreateCountry()
Initializing country.provinces = new Province[0] during CreateCountry() instead of leaving null.


UPDATE: I noticed that the CreateCountry() method was not part of the core WMSK, but from one of the samples.
So I updated it myself.

Code
int CreateCountry(string name, int uniqueId) {
   Country country = new Country(name, "Continent", uniqueId);
   int countryIndex = _map.CountryAdd(country);
   return countryIndex;
}

Initializing country.provinces would still be good though, to not get errors when using it before adding provinces (or creating a dummy province to avoid those errors)
Title: Re: Territories Importer issues
Post by: Kronnect on August 12, 2021, 09:15:51 AM
There's a CountryCreate method already that takes a name and the continent.

I've added a province initializer to the Country constructor (in Country.cs):
Code
		/// <summary>
/// Creates a new country
/// </summary>
/// <param name="name">Name.</param>
/// <param name="continent">Continent.</param>
/// <param name="uniqueId">For user created countries, use a number between 1-999 to uniquely identify this country.</param>
public Country(string name, string continent, int uniqueId) {
this.name = name;
this.continent = continent;
this.regions = new List<Region>();
this.uniqueId = uniqueId;
this.attrib = new JSONObject();
this.mainRegionIndex = -1;
this.provinces = new Province[0];
}
Title: Re: Territories Importer issues
Post by: Pollyfun on August 12, 2021, 01:12:42 PM
Thanks, I've added the same codeline so there's no need to create a new beta just for this.