DaveyK Posted October 23, 2013 Share Posted October 23, 2013 Hey guys, Ive been tossing around the idea of creating one of my own games. I know its been done over and over again, but its more something for me personally. Anyway, I dislike the idea that a lot of games have static "cost" and "time" aspects. In real life, would you considering building something, the "cost" greatly influences the "time" and the "quality" of said building. Basically, I know pretty much that 3 variables is really hard. So for starters, I would set quality to 100%. So what I would like to do, is write a script where I could say something like this: Quality: 100% Normal building time: 1h Normal cost: 100 coins But if you spend 200 coins, the building time is decreased by a factor (say 0.08 for every increase), so say you spend: 200 coins -> (200/100)= 2 ; 2 * 0.08 = 0.16, which means you roughly save 10mins. (building time 50 minutes). It basically works like this image:http://www.the-self-build-guide.co.uk/image-files/time-cost-quality-triangle.jpg Regardless, do you have any tips how I should code this? I know the math behind it but the automation seems rather difficult... also, this should work both ways, so if I were to say I wanted the building time to be 30mins, I would have to know the respective building cost as well. And its not likely building cost are just going to be money (coins), but other "resources" as well. I have a feeling this system is easy to abuse though and I am not entirely sure how I would code this at all. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 23, 2013 Share Posted October 23, 2013 You'll end up tweaking things to be sure, but it sounds like all you need to do is reduce the problems to equations. You have five variables (quality, base time, computed time, base cost, additional cost) but you'll have values for all but one. Like the computed time: the base time and cost are predefined according to the building while the quality and additional cost are provided by the user. q = Quality (0-100%) t0 = Base time t = Computed time c0 = Base cost c = Total cost // example t = t0 * (1 - 0.08 * c / c0) * (q / 100) = 60 * (1 - 0.08 * 200 / 100) * (100 / 100) = 50.4You can even leave one of the inputs undefined and create a true equation. t = 60 * (1 - 0.08 * c / 100) * (100 / 100) And maybe graph it, giving a clearer view of how you can vary one value (here, cost) to affect the build time. Quote Link to comment Share on other sites More sharing options...
DaveyK Posted October 23, 2013 Author Share Posted October 23, 2013 Perfect, that's exactly the leg up i needed. I'll be back with some php soon Quote Link to comment Share on other sites More sharing options...
DaveyK Posted November 8, 2013 Author Share Posted November 8, 2013 I implemented the function you gave me, but I noticed that it would ALWAYS make the time shorter even if you decide to spend less money than the base amount of money. That is not very realistic. I went with: $quality = 100; $base_time = 60; $coin_factor = 0.16; $base_coin = 100; $actual_coin = 200; $t = round($base_time * (1 - ($coin_factor * (($actual_coin - $base_coin) / $base_coin))) * ($quality / 100), 3); At this point I do not know if Im entirely happy with the way that decreases the actual time. For now it works though. HOWEVER, I am facing a rather different issue. This makes it linear, and I would like the function to be a curve, however I really dont remember how to do that... It shouldnt be very steep and it shouldnt ever be able to go under 30% of the base time. This is probably going to be a really long function, considering I am planning on not just using coin as a "resource" but maybe some other things as well. I have no idea how I should manage that at this point. Quote Link to comment Share on other sites More sharing options...
DaveyK Posted November 8, 2013 Author Share Posted November 8, 2013 Also I know I can code a minimum using a min() or max() function, I would just like to do it inside of the equation... Quote Link to comment Share on other sites More sharing options...
requinix Posted November 9, 2013 Share Posted November 9, 2013 I implemented the function you gave me, but I noticed that it would ALWAYS make the time shorter even if you decide to spend less money than the base amount of money. That is not very realistic.Don't allow people to spend less than the minimum. Two simple ways of tackling the curve: 1. Asymptotes, like how y=1/x never reaches y=0. Normally a steep curve but you can scale it out. 2. Quadratics (or another even power), like how y=x^2 never goes below y=0. How about a sketch of what you're looking for? How much is too much curvature? Quote Link to comment Share on other sites More sharing options...
DaveyK Posted November 9, 2013 Author Share Posted November 9, 2013 God I used to be so good at this stuff, but I totally forgot about it. I remember the asymptote stuff, which is what I would like to achieve to limit the minimum time. The minimum amount of "resource" is something I just need to set in the php. This is the lineair plot: http://wolfr.am/1ax2KGH This looks more like the curve I would like to have in the end, though the equation itself isnt good: http://wolfr.am/1ax2XcL As you can see in the second example, the actual impact it now has on the time is super small, but the curve itself looks better (even though I am not sure this has an asymptote). Quote Link to comment Share on other sites More sharing options...
Irate Posted November 9, 2013 Share Posted November 9, 2013 Just some brief note, if you crucially rely on the information you get from the script you're building, works with integers instead of floats. Floats have rounding errors in all programming languages with the IEEE-754 standard, which causes simple computations to receive minimal errors, like this. $a = 0.1; $b = 0.2; $a + $b === 0.3; // false! Quote Link to comment Share on other sites More sharing options...
Barand Posted November 9, 2013 Share Posted November 9, 2013 Just to add to Irate's reply, if you are using floats then rather than test for equality you have to test that the difference is small enough to be considered acceptably close. $a = 0.1; $b = 0.2; $c = 0.3; echo ($a + $b === $c) ? 'TRUE' : 'FALSE'; // FALSE echo ($a + $b - $c < .000001) ? 'TRUE' : 'FALSE'; // TRUE Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.