pcfantasy / MoreEffectiveTransfer

Cities: Skylines Mod: Optimize transfer manager in vanilla game. match the shortest transfer bettween offers
https://steamcommunity.com/sharedfiles/filedetails/?id=1680840913
MIT License
14 stars 11 forks source link

Use routing when calculating distance #15

Open jini-zh opened 4 years ago

jini-zh commented 4 years ago

I am playing the map Sunset Cliffs. I have the main city up a large cliff and several villages down at the shore. They are connected by a long road winding down the side of the cliff. You can see the road in the map screenshots; it is very long. The problem is that the villages are very close to the main city in terms of Euclidian distance: they are just down the cliff, so even with your mod enabled the services are sent up and down the cliff, taking them a long time to commute. I think that it could be solved if MoreEffectiveTransfer used routing when calculating distance. I realize that it will result in higher CPU usage, so maybe it should be an option. Could you consider making it?

me22 commented 3 years ago

The basic problem here is that there's potentially a quadratic growth in the number of paths that would need to be calculated, so it wouldn't just be a bit more CPU, it'd be massively slower.

And heuristics that would make sense in your city that could help filter things down? Are there facilities on the shore too, just slightly further away euclidian? I'm imagining that it could maybe do the pathfinding only for the 5 euclidian-closest facilities, or all the ones within ~1km (about one tile) or similar.

gab commented 3 years ago

Can't you just throw time at the problem? Given enough time where the player is just staring at the map and not changing anything, you can compute as complex a path you want. Even with a simple dumb flood fill along all roads from every service provider. Just compute as much as you can every frame. You can always use the "bad", fast to compute path in the meanwhile while you generate the perfect one.

If the player could be shown a progress bar approximating how much time is left to compute perfect service paths as long as he doesn't change any road on the map, I think it would work great, and even be a bit realistic - real life planning takes a lot of time.

jini-zh commented 3 years ago

I eventually installed Enhanced District Services. It allows to specify which services serve which districts, and I have developed the city very far with it. I don't really need MoreEffectiveTransfer now, but setting up each district and each service is quite cumbersome, so I would love to have an automatic solution.

I have finally found the time to take a look at the code, and I see what the problem is. Note that the routing distance is never more than the euclidian distance, so there is no need to calculate it for every node except in the worst case. Doing the pathfinding for the 5 euclidian-closest facilities also looks okay (the 5 could be an option). I had the facilities on the shore from the time I started building there, so it probably shouldn't be difficult to find a closer offer.

me22 commented 3 years ago

If anyone's still looking for a transfer mod that uses route distance, Chaos has been working on one: https://steamcommunity.com/id/vanatu/myworkshopfiles?browsefilter=myfiles&sortmethod=creationorder&section=items&appid=255710&requiredtags%5B%5D=Mod

(No directly link because the beta is currently closed.)

pcfantasy commented 3 years ago

@jini-zh @gab @me22

Use route to find a close offer, will need to use path-finding(or even TMPE path-finding), that is very slow I think.

drok commented 3 years ago

@pcfantasy indeed, routing-based matchmaking is very demanding on the CPU.

In my mod, Transfer Broker, I use routing to make matches, but in order to accomplish it, I had to use multi-threading, and run the match-maker on a few separate worker threads instead of the Simulation thread. The base game also uses several separate threads for pathfinding for the same reason, as it is CPU-heavy, or computationally intensive in O(n) terms, as @me22 mentioned above.

In fact, implementing and testing the threading part of the mod was much more time consuming than the visible, functional part of the mod.

My mod is currently in beta-testing, and I invite you to give it a try if you're interested in route-based match-making. This one requirement was the motivation for starting the project. It's on the workshop at:

https://steamcommunity.com/sharedfiles/filedetails/?id=2389228470