pioneer icon indicating copy to clipboard operation
pioneer copied to clipboard

Update CityOnPlanet.cpp

Open joonicks opened this issue 4 years ago • 24 comments

Optimized to use bitfield instead of a byte array for occupancy (5kb vs 40kb). City size is calculated different and slightly randomized (based on city seed). Uses precalculated distance-from-center instead of doing the math realtime. Largest cities on planets with population of 1Billion+. Mars is a 1B+ planet so Cydonia is now huge. Same with New Hope.

Fixes #4802, fixes #5198.

joonicks avatar Jul 12 '20 19:07 joonicks

I'll want to see what effect this has on the number of rendered building, how it might affect framerate etc, but code looks good after a quick look once-over.

fluffyfreak avatar Jul 13 '20 12:07 fluffyfreak

I'm not sure I understand what changes have been made here, or why.

Optimized to use bitfield instead of a byte array for occupancy (5kb vs 40kb).

Is code now cleaner and easier to read, or if not, was this code a bottleneck?

City size is calculated different and slightly randomized (based on city seed).

What's the old way, and what's the improvement in outcome?

impaktor avatar Jul 13 '20 13:07 impaktor

I'm not sure I understand what changes have been made here, or why.

Optimized to use bitfield instead of a byte array for occupancy (5kb vs 40kb).

Old code would use bytes to store occupancy, resulting in 40kb of memory needing to be cleared for each city generated. With a bitfield the occupancy can be stored in just 5kb.

Is code now cleaner and easier to read, or if not, was this code a bottleneck?

Apart from handling 3d models, clearing the 40kb was without a doubt the biggest operation in the whole routine. Clearing memory isnt just about actually setting it to zero, it also clobbers the data cache so the smaller the better.

As for bit manipulation being readable, I think any programmer uncomfortable with it might not be suitable for game development.

City size is calculated different and slightly randomized (based on city seed).

What's the old way, and what's the improvement in outcome?

int population = planet->GetSystemBody()->GetPopulation();

Old code was based on this line. The problem is, GetPopulation returns population in multiples of 1 billion, so... returns 1.0 for 1 billion, 0.5 for 500million, 0.1 for 100million, etc. Turning it to an int right away means "population" usually ends up as 0. So all cities would be essentially the same size if GetPopulation returns <1, atmospheric cities being one size, airless being the other. Ive written new code (but I dont know how to update the PR) to make city sizes more logarithmic and partially random (based on city seed). 1Billion+ cities are now maximally huge.

joonicks avatar Jul 13 '20 13:07 joonicks

OK, thanks for explenation.

If you want to change the last commit, see https://pioneerwiki.com/wiki/Using_git_and_GitHub#Common_fixes_for_mistakes (i.e. amend and force push.)

impaktor avatar Jul 13 '20 13:07 impaktor

And to be clear, this optimizing pass was a way for me to familiarize myself with the code again for future implementation of multiblock buildings.

joonicks avatar Jul 13 '20 15:07 joonicks

What are MultiBlock buildings?

fluffyfreak avatar Jul 13 '20 18:07 fluffyfreak

What are MultiBlock buildings?

cities are created in a 200 by 200 grid, 1 grid cell for each building. but the code supports multi-cell buildings already, the starport uses that code to prevent other buildings to be placed on top of it, but no other building does.

people have changed the cell size to accomodate bigger buildings, but that means more empty space between the smallest buildings. currently the "Library" model even spills over. but im working on changing Library to a multi-cell building so that I can shrink down the cellsize a little again. progress is good, I just finished code to load more building attributes from json.

joonicks avatar Jul 13 '20 20:07 joonicks

screenshot-20200714-000036

now support 2x2 buildings, configured in a json file: data/models/buildings/details.json

options that work: "layout" : 1 -- when placed, allocates 2x2 cells and moves the building to the middle of it "rarity" : <0.0 ..1.0> -- adjusts the rarity of the building (down only), 0.5 would be 50% less common going to split this option into "atmosphere-rarity" and "airless-rarity"

not yet functional: storage <true/false>, industry <true/false>, monument <true/false>, habitat <true/false>, x-offset & z-offset

joonicks avatar Jul 13 '20 22:07 joonicks

@joonicks is this PR still WIP or is it ready for review?

fluffyfreak avatar Jul 15 '20 08:07 fluffyfreak

I have a few more fixes and enhancements in mind at the moment but the main code changes are done I think

joonicks avatar Jul 15 '20 12:07 joonicks

ready to be inspected, I think...

joonicks avatar Jul 15 '20 18:07 joonicks

Mostly good and it works just fine in testing, I've made a couple of suggestions and have some questions but overall good 👍

fluffyfreak avatar Jul 18 '20 11:07 fluffyfreak

population config & debug output taken care of...

joonicks avatar Jul 19 '20 13:07 joonicks

@joonicks ok I'm alright with this going in with the hardcoded station name for now so long as we fix it later. You just need to use the Clang-formatter to fix that now and I'll merge it.

fluffyfreak avatar Jul 21 '20 11:07 fluffyfreak

ok I'm alright with this going in with the hardcoded station name for now so long as we fix it later.

To my ears, this sounds like something that should be fixed in this PR before merge

impaktor avatar Jul 22 '20 06:07 impaktor

ok I'm alright with this going in with the hardcoded station name for now so long as we fix it later.

To my ears, this sounds like something that should be fixed in this PR before merge

I believe the code that loads the .model files should be revisited and rewritten to use json & add support for arbitrary attributes to be loaded, doing away with the details.json which I now realise was a mistake (cant be modded), the ultimate goal being to limit the number of files (collecting all relevant metadata in the same place) and doing away with duplicate code (loading configs from .ini, .model, .json, ...)

however, I do believe the city changes stand well on their own, while the loading of configs could spiral out of control way quicker.

me on my own deciding to put attributes in buildings/default.json is part of the problem. none of you have even realized the modding problem and thats an issue. there should probably be a clear policy on adding files somewhere for all to refer to, before this project gets yet another config file format included in its codebase.

right now I feel that further changes are a bit beyong my own capacity, so to whom it matters, please include it as-is.

joonicks avatar Jul 22 '20 18:07 joonicks

@joonicks this is just waiting on you to fix the clang formatting, then I'm happy to merge it in.

fluffyfreak avatar Sep 05 '20 10:09 fluffyfreak

@fluffyfreak if you're merging, please be sure to include TODOs as outlined by joonicks' last comment.

sturnclaw avatar Sep 06 '20 20:09 sturnclaw

Will review this once I get a little time, I know it's been sitting open for a while.

sturnclaw avatar Sep 10 '21 03:09 sturnclaw

Still awating review.

joonicks avatar Nov 08 '21 17:11 joonicks

Jesus. That went from "just fix the clang" to "do total rewrite" real fast. You know what? Never mind.

joonicks avatar Nov 08 '21 21:11 joonicks

@joonicks come on, it's not that much to rewrite

Gliese852 avatar Nov 08 '21 21:11 Gliese852

@joonicks it's a few comments to explain stuff that is difficult to understand, one bit that needs clarifying how it avoids overflowing the data-type, and changing some arrays to structs.

That's a very mild set of requests.

fluffyfreak avatar Nov 09 '21 21:11 fluffyfreak

@fluffyfreak @Gliese852 Request 1: "lengthy documentation" Request 2: unknown - jargon overload instead of plain english Request 3: rewrite Request 4: rewrite Request 5: remove #ifdefs so that compiler throws multiple unused variable warnings instead Request 6: minor Request 7: minor Request 8: documentation Request 9: not sure Request 10: rewrite Request 11: rewrite Request 12: documentation Request 13: rewrite

joonicks avatar Nov 10 '21 14:11 joonicks

@joonicks @fluffyfreak @impaktor @Web-eWorks @Gliese852 any Update on This PR?

OmarAglan avatar Nov 14 '22 06:11 OmarAglan

@Omaranwa yes, the other day @Web-eWorks contemplated on reviving this, (rebase to master, and implement suggested changes above), but if you're looking to do it, that would also be great :)

impaktor avatar Nov 14 '22 10:11 impaktor

I'm about 40% done with a complete rewrite of this PR (the underlying algorithm being very similar, but the expression of said algorithm entirely different) to address some of the fundamental issues that were mentioned during review process, as well as dealing with the architectural and code-style issues. As joonicks has indicated he has no desire to make further changes to this PR, that work will be merged via a new pull request when completed.

sturnclaw avatar Nov 14 '22 21:11 sturnclaw

@impaktor @Web-eWorks thanks looking for it

OmarAglan avatar Nov 17 '22 13:11 OmarAglan

@joonicks as for this

@Omaranwa yes, the other day @Web-eWorks contemplated on reviving this, (rebase to master, and implement suggested changes above), but if you're looking to do it, that would also be great :)

I have other plans to implement more features in the graphic area.

OmarAglan avatar Nov 17 '22 13:11 OmarAglan