The buy-max button in my idle game buys one too many or one too few
When every purchase of an upgrade costs a bit more than the last, working out how many the player can afford is not a simple division. Looping is slow for big numbers and a formula is off by one at exactly the wrong moment. Here is the calculation with the safety step that fixes the edge.
Why this works
When each copy costs a fixed share more than the last, the total for many copies follows a pattern you can calculate in one step, so the game never has to loop. Because computers round, the estimate can miss by one, so a short correction at the end makes the answer exact.
When not to use it
Skip it when prices rise by a fixed amount, or follow a table you drew by hand. Then a plain division or a lookup is simpler and less error-prone. Also skip it if the cost depends on something other than how many you own.
In the full entry
- What to build, step by step
- Starting values and the reason for each
- 2 common mistakes and the fix
- 31 lines of example code with 5 checks that pass
- How your AI shows you that it works
Say this to your AI
Use the Spielerando entry "The buy-max button in my idle game buys one too many or one too few" (read_entry idle-upgrade-buy-max-cost-formula) in my game. Build it step by step, and show me how you checked that it works.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP