Jump to content

Recommended Posts

Posted

Hello all!
So i have a bit of a odd question.
I have a Power generator that works all fine, but the power being all added in at the end of the burn time is not exactly what I want to do.
Let's say i want to add... 2500 power units over 1600 ticks. How would i go about doing that when the power system only accepts Integers to add?

 

I have tried

int power = 2500 / 1600;
addPower(power);

but obviously this won't work because it rounds the result to a Integer, only generating 1600 and not 2500 power units.

Any help on this would be great, thank you!

Posted

if addPower() accepts floats, just use a float

I don't know how that power system works but

int power = 2500/1600;

float powerLeft = 2500;
for(int i=1600; i>0; i--){ //make this run once every tick instead of this for loop, but remember to make i work the same
power = powerLeft/i;
addPower(power);
powerLeft -= power;
}

Posted

Disclaimer: I haven't used any energy system in my mods yet.

private int ratePerTick = 20; // adjust if needed
private int powerRemaining = 2500; // will be changed by other code

public void tick() {
    if (powerRemaining > ratePerTick) {
        addPower(ratePerTick);
        powerRemaining -= ratePerTick;
    } else {
        if (powerRemaining > 0) {
            addPower(powerRemaining);
        }
        powerRemaining = 0;
    }
}

 

Posted

Howdy

The basic algorithm for doing things like this is to add a different amount each tick.

For example, if you wanted to add 30 power units over 20 ticks, you would add

2 units on the first tick

1 unit on the second tick

2 units on the third tick

1 unit on the fourth tick

(etc) for 20 ticks in total.

 

You need to track the fractional part yourself

eg from your example

 

INITIALISING:

float ratePerTick = 2500.0/1600.0;

float remainder = 0.0;

 

EVERY TICK:

remainder += ratePerTick;

int powerToAddThisTick = (int)remainder;

remainder -= powerToAddThisTick;

addPower(powerToAddThisTick)

 

-TGG

 

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.