Submind YouTube summaries
Thumbnail for (Ep 7.0) Buy Max, Explaining the Math! - Unity C# Idle Game Tutorial Series [2021 Edition]

(Ep 7.0) Buy Max, Explaining the Math! - Unity C# Idle Game Tutorial Series [2021 Edition]

Watch on YouTube

Video summary

In this episode of the Unity C# Idle Game Tutorial Series, the creator Crypto Grounds provides a detailed mathematical breakdown of the "Buy Max" equation used to calculate upgrade costs and quantities in an idle game. The primary goal of this video is to demystify how these equations are derived rather than simply presenting them as arbitrary formulas, ensuring that developers understand the underlying logic so they can debug issues effectively if bugs arise. While the creator acknowledges that the content may be challenging for those less comfortable with mathematics, he emphasizes that grasping these fundamental concepts is crucial for building robust game mechanics without blindly implementing code without understanding its function. The tutorial focuses heavily on deriving a closed-form solution for exponential cost growth to avoid inefficient computational loops. The creator starts with the standard exponential cost formula where cost equals base times a multiplier raised to the power of the level, then expands this into a summation series when purchasing multiple upgrades. By manipulating algebraic rules and recognizing geometric series patterns, he simplifies the complex sum of powers into a single, efficient O(1) equation. This process involves dividing by the base cost, multiplying by terms related to the multiplier, adding one, and finally applying a logarithm to solve for the maximum number of upgrades affordable with a given currency amount. A significant portion of the video addresses the practical application of these derived formulas within game logic. The creator explains that once the algebraic equation is solved for the number of upgrades (N), the result must be "floored" to ensure only whole numbers of upgrades are purchased, as fractional upgrades do not exist in this specific game design. He contrasts this with a "ceiling" function which would round up, clarifying why flooring is preferred here. However, he also notes a limitation regarding linear cost equations; despite his attempts, he found that deriving a similar efficient Buy Max equation for linear costs resulted in an overly complex and "ugly" formula that was not worth implementing in the code, so viewers are encouraged to solve that specific variation on their own if they wish. The video concludes by summarizing the final set of equations needed for the next episode, which will focus on implementing these mathematical concepts directly into the Unity C# codebase. The creator reiterates that while the exponential Buy Max equation is fully explained and ready for use, the linear version remains an open challenge for the audience to tackle independently. He ends with a friendly reminder to like, subscribe, and support the channel, promising that the upcoming episode will bridge the gap between this theoretical math explanation and practical coding implementation.
Read the full video transcript
Hi everyone. It's Crypto Grounds here. Welcome back to another idle game tutorial series. This is episode 7 and today we're going to begin working on the Biomax equation. So for this video, I'm going to be discussing the math side of things just so everyone understands how the Biomax actually works instead of just pulling a random equation and throwing it at you guys and not really explaining it that well. So if you guys have seen the old series, the 2019 edition of this of this whole series, I did do a Biomax math explain video. However, I kind of just had the cost equation and then came up with it out of the spot and not actually figure out how I got it because I personally didn't know how I was able to get this equation. I kind of just did some of the math and not explain everything. So this video is going to cover everything and if you're not really a math person, this video may be a little tough for you. So just I want to make sure you understand that this equation isn't just made up or anything and that there is some like mathematical reason behind them. Yeah, again, this is just going to be the math explain video. In the next one, 7.1, we're going to be actually um we're going to be implementing these equations in the code. One more heads-up, I did make a promise recording the the whiteboard clips that I would do an explanation for the exponential and linear equations. However, I wasn't able to figure out the linear ones. You'll see me explain why once I get there and I was only able to do exponential equations. So I hope this video ends up being helpful and I'll transition to the whiteboard. All right, so we are on the whiteboards and as you can see, there is a lot of math. There is even another side for this. So if you're not a very um if you're not a very good math person, this might be a little tricky to keep up with, but understanding fundamentally how the Biomax equation and all that stuff works is kind of important so you just don't throw it into your game blindly just not knowing what it does and how it works. And if you don't know how it works and if there's a bug for some reason, then you won't be able to fix it. So, I'll be doing my best to explain how this buy max equation works. So, the first one we'll be talking about is the exponential equation. So, after this we will be talking about the linear one. So, we'll start here. So, assuming that our cost is equal to this exponential equation, which in our case it will be obviously, um cost is equal to base times multiplier to the power of level, then we're going to use the C equals B times M to the power of L to solve for various equations. So, for one we're going to start with here. So, if we're buying just one upgrade at a time, then our equation is simple. Our cost is going to be B times M to the power of L. However, if we were to buy N amount of upgrades, so let's say we're buying two, three, piss off. Sorry, people are playing with flutes outside of our house for whatever reason. I don't I don't know. But anyways, let's say we were buying more than one upgrade and we're buying N amount of upgrades. So, that just could be any amount, two, three, four, or a hundred, or so on. Then our equation's going to be equal to the sum of B times M to the power of L plus K. So, basically how this works is that if this N was a one, we are calculating the sum of this one time. And K starts at zero. Now, if N was two, and end up with K equals one. And we get some kind of addition down here, but let's just stick with the sum. So, we're basically just adding up this space on whatever K is N amount of times. And the problem with this is that involves four loops, which we do not want. If we use four loops, we'll get an O N algorithm and that could be improved to O 1, where we just got to run it once. We don't have to do any four loops and stuff like that. So, let's expand this sum to something like this. So, basically all I did was just pull out the K from the L plus K exponent, which um M to the power of L plus K is equal to M to the power of L times M to the power of K. So, it would look like that. And since there's no K inside B or M to the L, we can take this out of the sum because that's not going to change what our result is. So, we're basically just multiplying this by the sum of M to the power of K. And if we were to expand this sum, you could see it looks something like this. So, it's B times M to the power of L and all that times M to the power of 0 plus M to the power of 1 and so on, all the way to all the way up to M to the power of N. And so, this is what I meant by sum. We're taking the sum of all of these based on whatever N is. All right. So, for example, uh just ignore this equation here. We'll I'll explain how we get here later on. But, let's just say our N is equal to four. For example, our our cost for the four upgrades is going to be equal to B times M to the power of L, all that multiplied by M to the power of 0 and all the way till M to the power of 3. However, we can simplify this equation. So, let's head to the other side. So, this is kind of weird how I did this. It probably could have been done in a much easier fashion, probably with an integral or something like that, but I did it this way anyways cuz that's just what came to mind. But basically, I set these two equations equal to each other. So, we have our m to the power of zero all the way to m to the power of three, and that's going to be equal to our next cost multiplier, so m to the power of four. So, these are the cost mults from zero to three, and this is the next one, okay? So, let's say our cost multiplier m is equal to three. So, you basically just set m to all these threes, which that's just m to the zero, m to the one, m to the two, m to the three. And you add them up, and you get 40. And m to the four is 81. You can see that these are not equal to each other, but we want we want our m4 term to equal to this term. We want to simplify this, cuz we don't want to take any sort of sums or for or use for loops in order to solve for what this is. So, what we can do is we notice that if we subtract one from 81, we get 80, and then we can divide that by two, and that's 40, okay? I'll explain this in a second. Let's try m equals four. So, let's just see this works for any value of m. Um so, we do m, so we get 1 + 4 + 16 + 64 is equal to 256. And we know that 85 is definitely not equal to 256, so we can do the same thing. We subtract one from 256, we divide this term 255 by three, and we get 85. So, there's a cool pattern here. So, if you may notice that this top numerator term for here m equals three and m equals four is literally just m to the power of four minus m naught. So, here it's just four to the power of four minus one, which is just m to the power of zero. And it's the same thing up here, but with m is equal to three. And for the denominator, it's just m minus one. So, now we can use this knowledge to say that m to the power of four is equal to m to the power of four minus one over m minus one. And now this is nice because we know what our multiplier is and we know what that is to the power of four. And we have just one and it's m minus one. So, we don't need to add up anything. We don't need to do m naught plus m one and all that junk. And therefore, our m to the power of n is equal to the same thing as above but with n. So, it's just very simple, friendly equation. So, then what we can do is that we can substitute this this long sequence here or this potentially long sequence with just a simple equation. So, for our n equals four example, all we have here is just b times n to the power of l multiplied by m to the power of four minus one over m minus one. And then therefore, we can say that our cost for however many upgrades we want to buy based on n is equal to this equation. So, it's very nice. We don't need to take any sums. We just plug in a few numbers and we get our answer right away. So, this is considered an O one algorithm. So, it only runs one time. And if we were to use this one above or this sum actually, we would just get O n which is less efficient than O one. But that is our goal. We want to simplify our equations as much as possible, get rid of any form of for loops, and we did exactly just that. All right. So, we have our total cost. We need to figure out what n is because we don't know what that is. And the purpose of the buy max is to see what is the highest and we could get. So that's where the second white board comes into play. All right, so just to remind you guys that the cost of buying n amount of upgrades is this equation right here where b is equal to the base cost, m is equal to cost mult, and l is equal to the current level. And n is how many upgrades are buying. So we need to find what n is, which is the max amount of upgrades we can afford based on c. And we will use that by just rewriting our equation. So we first start off with this. And then we divide both sides by b times m to the power of l. And then we multiply both sides by m minus 1. And then we add 1 to both sides. And then we take the log base m to get rid of this exponent. And this right here is a basic uh log rule where basically if we take any log with the base x, we can get rid of this x right here. So log base x of x to the power of y is just going to equal y. So then we have our n. But there's one more step we need to do, and that is to floor this entire equation. And the reason we want to do this is because we want round numbers only. Now, if for some reason your upgrades are not rounded up to the to to the next whole number then you can ignore this step, but since my upgrades are always whole numbers, we floor this. So basically what this does is just round it down to the previous whole number. So floor 4.8 is going to equal to 4. Now, if we were to do the same thing for floor, I don't know, 5.2, we're going to get 5. Now, the ceiling will do the opposite. Ceiling will round it up to the next whole number. So now in the next episode, you're going to need this equation, the cost, and your maximum amount of upgrades you can buy equations. So, that's this one. And just to remind you what all these mean, here you go. And notice that C is not cost anymore. It's the currency you have. So, it's the currency you're wanting to spend to get an amount of upgrades. So, I know that was a lot of math, but hopefully you can understand that these equations just don't come from nowhere. All right. Now, it's time to talk about the linear equations. So, I tried doing by max for linear. However, it um it's it's kind of not working out. It looks very ugly. And for some reason this is much harder than the exponential equation. So, I'm going to skip the linear equation for by max. I Yeah. Sorry if it's an inconvenience for anyone, but I just don't have the skill level to be able to create a by max equation from scratch using the linear cost equation. Base equals mult times level. Yeah, anyways, I'm just not going to include this in code. So, if you can somehow figure out, that'd be awesome. And yeah. Anyways, I hope this explanation was very helpful and you're excited to implement your first by max if you haven't already. And if it was helpful and you enjoyed it, please leave a like as it really helps out the videos. Subscribe my channel and turn on the notifications if you want to be notified for future videos. And YouTube recently added the thanks button to support creators like me. So, if you're interested in that, hit that button below. And that is it for this video. So, I will see everyone in the next episode 7.1, and that will be implementing the by max in the code. I hope you all have a wonderful day, and I can't wait to see you guys in the next one. Peace. We out here be up in class, but my mind is in the clouds though. Know the teacher's mad cuz my music be in loud ho. Tell me keep it down, say I kill it on the down low. And if I turn it up then I'm bound to attract a crowd, so no wonder me and Tim B out of state doing things you can't imagine. Criss Angel on the mic, give me a beat I'll show you magic. We born in different places