Skip to content

Commit

Permalink
power: Spend all power at start of construction, instead of during.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyp committed Nov 7, 2011
1 parent 43ce2e5 commit b780ec8
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 95 deletions.
15 changes: 14 additions & 1 deletion lib/gamelib/gtime.h
Expand Up @@ -139,7 +139,7 @@ extern void getTimeComponents(UDWORD time, UDWORD *hours, UDWORD *minutes, UDWOR
extern float graphicsTimeFraction; ///< Private performance calculation. Do not use.
extern float realTimeFraction; ///< Private performance calculation. Do not use.

/// Returns the value times deltaGameTime, converted to seconds.
/// Returns the value times deltaGameTime, converted to seconds. The return value is rounded down.
static inline int32_t gameTimeAdjustedIncrement(int value)
{
return value * (int)deltaGameTime / GAME_TICKS_PER_SEC;
Expand All @@ -155,6 +155,19 @@ static inline float realTimeAdjustedIncrement(float value)
return value * realTimeFraction;
}

/// Returns numerator/denominator * (newTime - oldTime). Rounds up or down such that the average return value is right, if oldTime is always the previous newTime.
static inline WZ_DECL_CONST int quantiseFraction(int numerator, int denominator, int newTime, int oldTime)
{
int64_t newValue = (int64_t)newTime * numerator/denominator;
int64_t oldValue = (int64_t)oldTime * numerator/denominator;
return newValue - oldValue;
}
/// Returns the value times deltaGameTime, converted to seconds. The return value is rounded up or down, such that it is exactly right on average.
static inline int32_t gameTimeAdjustedAverage(int value)
{
return quantiseFraction(value, GAME_TICKS_PER_SEC, gameTime + deltaGameTime, gameTime);
}

void sendPlayerGameTime(void); ///< Sends a GAME_GAME_TIME message with gameTime plus latency to our game queues.
void recvPlayerGameTime(NETQUEUE queue); ///< Processes a GAME_GAME_TIME message.
bool checkPlayerGameTime(unsigned player); ///< Checks that we are not waiting for a GAME_GAME_TIME message from this player. (player can be NET_ALL_PLAYERS.)
Expand Down
2 changes: 1 addition & 1 deletion src/droid.cpp
Expand Up @@ -1024,7 +1024,7 @@ bool droidStartBuild(DROID *psDroid)
cancelBuild(psDroid);
return false;
}
psStruct->body /= 10; // structures start at 10% health
psStruct->body = (psStruct->body + 9) / 10; // Structures start at 10% health. Round up.
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/game.cpp
Expand Up @@ -4668,7 +4668,7 @@ static bool loadSaveStructure2(const char *pFileName, STRUCTURE **ppList)
psFactory->capacity = 0; // increased when built
psFactory->productionLoops = ini.value("Factory/productionLoops", psFactory->productionLoops).toInt();
psFactory->timeStarted = ini.value("Factory/timeStarted", psFactory->timeStarted).toInt();
psFactory->timeToBuild = ini.value("Factory/timeToBuild", psFactory->timeToBuild).toInt();
psFactory->buildPointsRemaining = ini.value("Factory/buildPointsRemaining", psFactory->buildPointsRemaining).toInt();
psFactory->timeStartHold = ini.value("Factory/timeStartHold", psFactory->timeStartHold).toInt();
psFactory->loopsPerformed = ini.value("Factory/loopsPerformed", psFactory->loopsPerformed).toInt();
psFactory->productionOutput = ini.value("Factory/productionOutput", psFactory->productionOutput).toInt();
Expand Down Expand Up @@ -4888,7 +4888,7 @@ bool writeStructFile(const char *pFileName)
ini.setValue("modules", psFactory->capacity);
ini.setValue("Factory/productionLoops", psFactory->productionLoops);
ini.setValue("Factory/timeStarted", psFactory->timeStarted);
ini.setValue("Factory/timeToBuild", psFactory->timeToBuild);
ini.setValue("Factory/buildPointsRemaining", psFactory->buildPointsRemaining);
ini.setValue("Factory/timeStartHold", psFactory->timeStartHold);
ini.setValue("Factory/loopsPerformed", psFactory->loopsPerformed);
ini.setValue("Factory/productionOutput", psFactory->productionOutput);
Expand Down
2 changes: 1 addition & 1 deletion src/intdisplay.cpp
Expand Up @@ -233,7 +233,7 @@ void intUpdateProgressBar(WIDGET *psWidget, W_CONTEXT *psContext)
Manufacture = StructureGetFactory(Structure);

Range = FactoryGetTemplate(Manufacture)->buildPoints / Manufacture->productionOutput;
timeToBuild = Manufacture->psSubject != NULL? Manufacture->timeToBuild : Range; // If psSubject == NULL, this is not yet synched, and Manufacture->timeToBuild is not set correctly.
timeToBuild = Manufacture->psSubject != NULL? Manufacture->buildPointsRemaining / Manufacture->productionOutput : Range; // If psSubject == NULL, this is not yet synched, and Manufacture->timeToBuild is not set correctly.
BuildPoints = Range - timeToBuild;
//set the colour of the bar to yellow
BarGraph->majorCol = WZCOL_YELLOW;
Expand Down
14 changes: 7 additions & 7 deletions src/power.cpp
Expand Up @@ -330,16 +330,16 @@ int64_t getPrecisePower(unsigned player)
return asPower[player].currentPower;
}

int requestPowerFor(STRUCTURE *psStruct, int32_t amount, int points)
bool requestPowerFor(STRUCTURE *psStruct, int32_t amount)
{
return requestPrecisePowerFor(psStruct, amount*FP_ONE, points);
return requestPrecisePowerFor(psStruct, amount*FP_ONE);
}

int requestPrecisePowerFor(STRUCTURE *psStruct, int64_t amount, int points)
bool requestPrecisePowerFor(STRUCTURE *psStruct, int64_t amount)
{
if (points == 0 || amount <= 0 || !powerCalculated)
if (amount <= 0 || !powerCalculated)
{
return points;
return true;
}

bool haveEnoughPower = addPowerRequest(psStruct->player, psStruct->id, amount);
Expand All @@ -349,8 +349,8 @@ int requestPrecisePowerFor(STRUCTURE *psStruct, int64_t amount, int points)
asPower[psStruct->player].currentPower -= amount;
delPowerRequest(psStruct);
syncDebug("requestPrecisePowerFor%d,%u amount%"PRId64"", psStruct->player, psStruct->id, amount);
return points;
return true;
}
syncDebug("requestPrecisePowerFor%d,%u wait,amount%"PRId64"", psStruct->player, psStruct->id, amount);
return 0; // no power this frame
return false; // Not enough power in the queue.
}
4 changes: 2 additions & 2 deletions src/power.h
Expand Up @@ -45,8 +45,8 @@ extern bool resetPlayerPower(UDWORD player, STRUCTURE *psStruct);
/** Check the available power. */
bool checkPower(int player, uint32_t quantity);

int requestPowerFor(STRUCTURE *psStruct, int32_t amount, int points);
int requestPrecisePowerFor(STRUCTURE *psStruct, int64_t amount, int points);
bool requestPowerFor(STRUCTURE *psStruct, int32_t amount);
bool requestPrecisePowerFor(STRUCTURE *psStruct, int64_t amount);

extern void addPower(int player, int32_t quantity);

Expand Down
3 changes: 3 additions & 0 deletions src/research.cpp
Expand Up @@ -1805,6 +1805,7 @@ void holdResearch(STRUCTURE *psBuilding, QUEUE_MODE mode)
}
}

delPowerRequest(psBuilding);
}

/*release a research facility from hold*/
Expand Down Expand Up @@ -1909,6 +1910,8 @@ void cancelResearch(STRUCTURE *psBuilding, QUEUE_MODE mode)

// Initialise the research facility's subject
psResFac->psSubject = NULL;

delPowerRequest(psBuilding);
}
}

Expand Down

0 comments on commit b780ec8

Please sign in to comment.