1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
#include "PowerManager.h"
#include "LegacyCpp/WeaponDef.h"
UnitInfoPower::UnitInfoPower(int UID, UnitInfo *UI, bool isActive, int listType)
{
unitID = UID;
U = UI;
active = isActive;
importance = 1.0;
type = listType;
index = 0;
}
cPowerManager::cPowerManager(IAICallback* callback, cRAI* global)
{
G=global;
l=G->l;
cb=callback;
*l<<"\n Loading cPowerManager ...";
MetalDifference = 0;
EnergyDifference = 0;
WeaponEnergyNeeded = 0;
OffEnergyDifference = 0;
OnEnergyDifference = 0;
EtoMNeeded = 0.0;
MtoENeeded = 0.0;
EtoMIncome = 0.0;
MtoEIncome = 0.0;
ExMMetalDifference = 0;
ExMEnergyDifference = 0;
EDrainSize = 0;
EDrainActive = 0;
EtoMSize = 0;
EtoMActive = 0;
MtoESize = 0;
MtoEActive = 0;
cb->GetValue(AIVAL_UNIT_LIMIT,&UIPLimit);
EDrain = new UnitInfoPower*[UIPLimit];
EtoM = new UnitInfoPower*[UIPLimit];
MtoE = new UnitInfoPower*[UIPLimit];
DebugUnitFinished = 0;
DebugUnitDestroyed = 0;
UIPLimit = 0;
}
cPowerManager::~cPowerManager()
{
if( RAIDEBUGGING )
{
*l<<"\n cPowerManager Debug:";
*l<<"\n MetalDifference = "<<MetalDifference;
*l<<"\n EnergyDifference = "<<EnergyDifference;
*l<<"\n WeaponEnergyNeeded = "<<WeaponEnergyNeeded;
*l<<"\n OffEnergyDifference = "<<OffEnergyDifference;
*l<<"\n OnEnergyDifference = "<<OnEnergyDifference;
*l<<"\n ExMMetalDifference = "<<ExMMetalDifference;
*l<<"\n ExMEnergyDifference = "<<ExMEnergyDifference;
*l<<"\n EtoMNeeded = "<<EtoMNeeded;
*l<<"\n EtoMIncome = "<<EtoMIncome;
*l<<"\n MtoEIncome = "<<MtoEIncome;
*l<<"\n MtoENeeded = "<<MtoENeeded;
*l<<"\n EDrain/EtoM/MtoE Size = "<<EDrainSize<<"/"<<EtoMSize<<"/"<<MtoESize;
*l<<"\n Debug-Units: Finished("<<DebugUnitFinished<<") - Destroyed("<<DebugUnitDestroyed<<") = "<<DebugUnitFinished-DebugUnitDestroyed;
if( (int)MetalDifference != 0 || (int)EnergyDifference != 0 || (int)WeaponEnergyNeeded != 0
|| (int)OffEnergyDifference != 0 || (int)OnEnergyDifference != 0 || (int)ExMMetalDifference != 0 || (int)ExMEnergyDifference != 0
|| (int)EtoMNeeded != 0 || (int)EtoMIncome != 0 || (int)MtoEIncome != 0 || (int)MtoENeeded != 0
|| EDrainSize != 0 || EtoMSize != 0 || MtoESize != 0 )
*l<<"\n (ERROR)";
}
// These should be empty at this point
delete [] EDrain;
delete [] EtoM;
delete [] MtoE;
}
void cPowerManager::UnitFinished(int unit, UnitInfo *U)
{
DebugUnitFinished++;
EnergyDifference += U->ud->energyMake;
MetalDifference += U->ud->metalMake;
WeaponEnergyNeeded += U->udr->WeaponEnergyDifference;
if( U->ud->canCloak && EDrainSize < UIPLimit )
{
if( U->udr->CloakMaxEnergyDifference < 0 )
{
U->CloakUI = new UnitInfoPower(unit,U,cb->IsUnitCloaked(unit),0);
U->CloakUI->importance = 100/-U->udr->CloakMaxEnergyDifference;
if( U->ud->isCommander )
U->CloakUI->importance *= 3;
InsertPI(EDrain,EDrainSize,U->CloakUI);
if( U->CloakUI->index < EDrainActive )
{
EDrainActive++;
if( !U->CloakUI->active )
GiveCloakOrder(unit,U,true);
OnEnergyDifference += U->udr->CloakMaxEnergyDifference;
}
else
{
if( U->CloakUI->active )
GiveCloakOrder(unit,U,false);
OffEnergyDifference += U->udr->CloakMaxEnergyDifference;
}
}
else if( !cb->IsUnitCloaked(unit) ) // 0 cost cloak? always use it
GiveCloakOrder(unit);
}
if( U->ud->onoffable )
{
if( U->udr->OnOffEnergyDifference < 0 && U->udr->OnOffMetalDifference > 0 && U->udr->MetalDifference < -U->udr->EnergyDifference*G->UDH->EnergyToMetalRatio && EtoMSize < UIPLimit )
{ // Metal Maker
U->OnOffUI = new UnitInfoPower(unit,U,cb->IsUnitActivated(unit),3);
U->OnOffUI->importance = U->udr->OnOffMetalDifference/-U->udr->OnOffEnergyDifference;
InsertPI(EtoM,EtoMSize,U->OnOffUI);
if( U->OnOffUI->index <= EtoMActive )
{
EtoMActive++;
if( !U->OnOffUI->active )
GiveOnOffOrder(unit,U,true);
ExMMetalDifference += U->udr->OnOffMetalDifference;
ExMEnergyDifference += U->udr->OnOffEnergyDifference;
}
else
{
if( U->OnOffUI->active )
GiveOnOffOrder(unit,U,false);
EtoMIncome += U->udr->OnOffMetalDifference;
EtoMNeeded += U->udr->OnOffEnergyDifference;
}
}
else if( U->udr->OnOffEnergyDifference > 0 && U->udr->OnOffMetalDifference < 0 && U->udr->EnergyDifference*G->UDH->EnergyToMetalRatio < -U->udr->MetalDifference && EtoMSize < UIPLimit )
{ // Energy Maker
U->OnOffUI = new UnitInfoPower(unit,U,cb->IsUnitActivated(unit),4);
U->OnOffUI->importance = U->udr->OnOffEnergyDifference/-U->udr->OnOffMetalDifference;
InsertPI(MtoE,MtoESize,U->OnOffUI);
if( U->OnOffUI->index <= MtoEActive )
{
MtoEActive++;
if( !U->OnOffUI->active )
GiveOnOffOrder(unit,U,true);
ExMMetalDifference += U->udr->OnOffMetalDifference;
ExMEnergyDifference += U->udr->OnOffEnergyDifference;
}
else
{
if( U->OnOffUI->active )
GiveOnOffOrder(unit,U,false);
MtoEIncome += U->udr->OnOffEnergyDifference;
MtoENeeded += U->udr->OnOffMetalDifference;
}
}
else if( U->ud->energyUpkeep > 0 && EDrainSize < UIPLimit )
{ // On/Off units that require energy
U->OnOffUI = new UnitInfoPower(unit,U,cb->IsUnitActivated(unit),1);
U->OnOffUI->importance = 100/-U->udr->OnOffEnergyDifference;
if( U->ud->isCommander || U->ud->extractsMetal > 0 )
U->OnOffUI->importance *= 3;
InsertPI(EDrain,EDrainSize,U->OnOffUI);
if( U->OnOffUI->index <= EDrainActive )
{
EDrainActive++;
if( !U->OnOffUI->active )
GiveOnOffOrder(unit,U,true);
OnEnergyDifference += U->udr->OnOffEnergyDifference;
}
else
{
if( U->OnOffUI->active )
GiveOnOffOrder(unit,U,false);
OffEnergyDifference += U->udr->OnOffEnergyDifference;
}
}
else
{ // On/Off units that do not require energy
EnergyDifference += U->udr->OnOffEnergyDifference;
MetalDifference += U->udr->OnOffMetalDifference;
if( !cb->IsUnitActivated(unit) ) // solar panals are turned off when given, RAI does not manage their states
GiveOnOffOrder(unit);
}
}
}
void cPowerManager::UnitDestroyed(int unit, UnitInfo *U)
{
DebugUnitDestroyed++;
EnergyDifference -= U->ud->energyMake;
MetalDifference -= U->ud->metalMake;
WeaponEnergyNeeded -= U->udr->WeaponEnergyDifference;
if( U->CloakUI != 0 )
{
if( U->CloakUI->active )
{
OnEnergyDifference -= U->udr->CloakMaxEnergyDifference;
EDrainActive--;
}
else
OffEnergyDifference -= U->udr->CloakMaxEnergyDifference;
RemovePI(EDrain,EDrainSize,U->CloakUI);
delete U->CloakUI;
}
if( U->OnOffUI != 0 )
{
if( U->OnOffUI->type == 3 )
{
if( U->OnOffUI->active )
{
ExMMetalDifference -= U->udr->OnOffMetalDifference;
ExMEnergyDifference -= U->udr->OnOffEnergyDifference;
EtoMActive--;
}
else
{
EtoMIncome -= U->udr->OnOffMetalDifference;
EtoMNeeded -= U->udr->OnOffEnergyDifference;
}
RemovePI(EtoM,EtoMSize,U->OnOffUI);
}
else if( U->OnOffUI->type == 4 )
{
if( U->OnOffUI->active )
{
ExMMetalDifference -= U->udr->OnOffMetalDifference;
ExMEnergyDifference -= U->udr->OnOffEnergyDifference;
MtoEActive--;
}
else
{
MtoEIncome -= U->udr->OnOffEnergyDifference;
MtoENeeded -= U->udr->OnOffMetalDifference;
}
RemovePI(MtoE,MtoESize,U->OnOffUI);
}
else if( U->OnOffUI->type == 1 )
{
if( U->OnOffUI->active )
{
OnEnergyDifference -= U->udr->OnOffEnergyDifference;
EDrainActive--;
}
else
OffEnergyDifference -= U->udr->OnOffEnergyDifference;
RemovePI(EDrain,EDrainSize,U->OnOffUI);
}
delete U->OnOffUI;
}
else if( U->ud->onoffable )
{
EnergyDifference -= U->udr->OnOffEnergyDifference;
MetalDifference -= U->udr->OnOffMetalDifference;
}
}
void cPowerManager::Update()
{
sRAIUnitDef* udr;
float factor = (FUPDATE_POWER/30.0);
float energyDif = cb->GetEnergyIncome()-cb->GetEnergyUsage();
float metalDif = cb->GetMetalIncome()-cb->GetMetalUsage();
float energyCurrent = cb->GetEnergy() + energyDif*2*factor;
float metalCurrent = cb->GetMetal() + metalDif*2*factor;
float metalNext,energyNext;
bool ConvertEtoM;
bool ShiftEtoM = G->UDH->EnergyToMetalRatio*metalDif < energyDif;
if( EtoMActive > 0 )
ConvertEtoM = true;
else if( MtoEActive > 0 )
ConvertEtoM = false;
else
ConvertEtoM = ShiftEtoM;
float energyLimit = 0.8*cb->GetEnergyStorage();
float metalLimit = 0.8*cb->GetMetalStorage();
energyDif -= ExMEnergyDifference;
bool ShiftPowerON = energyDif > 0 && cb->GetEnergy() > 0;
// *l<<"\n ShiftPowerON="<<ShiftPowerON;
// *l<<"\n ShiftEtoM="<<ShiftEtoM;
// *l<<"\n energy="<<cb->GetEnergyIncome()<<"-"<<cb->GetEnergyUsage()<<"-"<<ExMEnergyDifference<<"="<<energyDif;
// *l<<"\n energyCurrent = "<<energyCurrent<<" of "<<energyLimit;
// *l<<"\n cb->GetEnergy()="<<cb->GetEnergy();
// *l<<"\n metalDif="<<metalDif;
// *l<<"\n metalCurrent = "<<metalCurrent<<" of "<<metalLimit;
if( ShiftPowerON )
{
//*l<<" (P-ON)="<<EDrainActive<<" of "<<EDrainSize<<":";
while( EDrainActive < EDrainSize && energyCurrent > 0 )
{
//*l<<" "<<EDrainActive;
udr = EDrain[EDrainActive]->U->udr;
if( EDrain[EDrainActive]->type == 0 )
energyNext = energyCurrent + udr->CloakMaxEnergyDifference*2*factor;
else
energyNext = energyCurrent + udr->OnOffEnergyDifference*2*factor;
if( energyNext > 0 )
{ // Turn ON
if( EDrain[EDrainActive]->type == 0 )
{
GiveCloakOrder(EDrain[EDrainActive]->unitID,EDrain[EDrainActive]->U,true);
OffEnergyDifference -= udr->CloakMaxEnergyDifference;
OnEnergyDifference += udr->CloakMaxEnergyDifference;
energyCurrent += udr->CloakMaxEnergyDifference*factor;
}
else
{
GiveOnOffOrder(EDrain[EDrainActive]->unitID,EDrain[EDrainActive]->U,true);
OffEnergyDifference -= udr->OnOffEnergyDifference;
OnEnergyDifference += udr->OnOffEnergyDifference;
energyCurrent += udr->OnOffEnergyDifference*factor;
metalCurrent += udr->OnOffMetalDifference*factor;
}
EDrainActive++;
}
else
break;
}
}
else
{
//*l<<" (P-OFF)="<<EDrainActive<<" of "<<EDrainSize<<":";
while( EDrainActive > 0 && energyCurrent < 0 )
{
//*l<<" "<<EDrainActive;
udr = EDrain[EDrainActive-1]->U->udr;
if( EDrain[EDrainActive-1]->type != 0 || udr->OnOffMetalDifference <= 0 || metalCurrent-udr->OnOffMetalDifference*factor > 0.5*cb->GetMetalStorage() )
{ // Turn OFF
EDrainActive--;
if( EDrain[EDrainActive]->type == 0 )
{
GiveCloakOrder(EDrain[EDrainActive]->unitID,EDrain[EDrainActive]->U,false);
OffEnergyDifference += udr->CloakMaxEnergyDifference;
OnEnergyDifference -= udr->CloakMaxEnergyDifference;
energyCurrent -= udr->CloakMaxEnergyDifference*factor;
}
else
{
GiveOnOffOrder(EDrain[EDrainActive]->unitID,EDrain[EDrainActive]->U,false);
OffEnergyDifference += udr->OnOffEnergyDifference;
OnEnergyDifference -= udr->OnOffEnergyDifference;
energyCurrent -= udr->OnOffEnergyDifference*factor;
metalCurrent -= udr->OnOffMetalDifference*factor;
}
}
else
break;
}
}
if( ConvertEtoM )
{
if( ShiftEtoM )
{
//*l<<" (EM-ON)="<<EtoMActive<<" of "<<EtoMSize<<":";
while( EtoMActive < EtoMSize && energyCurrent > energyLimit )
{
//*l<<" "<<EtoMActive;
udr = EtoM[EtoMActive]->U->udr;
energyNext = energyCurrent + udr->OnOffEnergyDifference*factor;
if( energyNext > energyLimit )
energyNext = energyLimit;
metalNext = metalCurrent + udr->OnOffMetalDifference*factor;
if( metalNext > metalLimit )
metalNext = metalLimit;
//*l<<"\n energyNext="<<energyNext;
//*l<<"\n metalNext="<<metalNext;
if( G->UDH->EnergyToMetalRatio*metalNext+energyNext > G->UDH->EnergyToMetalRatio*metalCurrent+energyLimit )
{ // Turn ON
GiveOnOffOrder(EtoM[EtoMActive]->unitID,EtoM[EtoMActive]->U,true);
EtoMActive++;
EtoMIncome -= udr->OnOffMetalDifference;
EtoMNeeded -= udr->OnOffEnergyDifference;
ExMMetalDifference += udr->OnOffMetalDifference;
ExMEnergyDifference += udr->OnOffEnergyDifference;
metalCurrent += udr->OnOffMetalDifference*factor;
energyCurrent += udr->OnOffEnergyDifference*factor;
}
else
break;
}
}
else
{
//*l<<" (EM-OFF)="<<EtoMActive<<" of "<<EtoMSize<<":";
while( EtoMActive > 0 && energyCurrent < 0.1*cb->GetEnergyStorage() )
{
//*l<<" "<<EtoMActive;
udr = EtoM[EtoMActive-1]->U->udr;
energyNext = energyCurrent - udr->OnOffEnergyDifference*factor;
if( energyNext > energyLimit )
energyNext = energyLimit;
metalNext = metalCurrent - udr->OnOffMetalDifference*factor;
if( metalNext > metalLimit )
metalNext = metalLimit;
if( G->UDH->EnergyToMetalRatio*metalNext+energyNext > G->UDH->EnergyToMetalRatio*metalCurrent+energyCurrent )
{ // Turn OFF
EtoMActive--;
GiveOnOffOrder(EtoM[EtoMActive]->unitID,EtoM[EtoMActive]->U,false);
EtoMIncome += udr->OnOffMetalDifference;
EtoMNeeded += udr->OnOffEnergyDifference;
ExMMetalDifference -= udr->OnOffMetalDifference;
ExMEnergyDifference -= udr->OnOffEnergyDifference;
metalCurrent -= udr->OnOffMetalDifference*factor;
energyCurrent -= udr->OnOffEnergyDifference*factor;
}
else
break;
}
}
}
else
{
if( !ShiftEtoM )
{
//*l<<" (ME-ON)="<<MtoEActive<<" of "<<MtoESize<<":";
while( MtoEActive < MtoESize && metalCurrent > metalLimit )
{
//*l<<" "<<MtoEActive;
udr = MtoE[MtoEActive]->U->udr;
energyNext = energyCurrent + udr->OnOffEnergyDifference*factor;
if( energyNext > energyLimit )
energyNext = energyLimit;
metalNext = metalCurrent + udr->OnOffMetalDifference*factor;
if( metalNext > metalLimit )
metalNext = metalLimit;
//*l<<"\n energyNext="<<energyNext;
//*l<<"\n metalNext="<<metalNext;
if( G->UDH->EnergyToMetalRatio*metalNext+energyNext > G->UDH->EnergyToMetalRatio*metalLimit+energyCurrent )
{ // Turn ON
GiveOnOffOrder(MtoE[MtoEActive]->unitID,MtoE[MtoEActive]->U,true);
MtoEActive++;
MtoEIncome -= udr->OnOffEnergyDifference;
MtoENeeded -= udr->OnOffMetalDifference;
ExMMetalDifference += udr->OnOffMetalDifference;
ExMEnergyDifference += udr->OnOffEnergyDifference;
metalCurrent += udr->OnOffMetalDifference*factor;
energyCurrent += udr->OnOffEnergyDifference*factor;
}
else
break;
}
}
else
{
//*l<<" (ME-OFF)="<<MtoEActive<<" of "<<MtoESize<<":";
while( MtoEActive > 0 && metalCurrent < 0.1*cb->GetMetalStorage() )
{
//*l<<" "<<MtoEActive;
udr = MtoE[MtoEActive-1]->U->udr;
energyNext = energyCurrent - udr->OnOffEnergyDifference*factor;
if( energyNext > energyLimit )
energyNext = energyLimit;
metalNext = metalCurrent - udr->OnOffMetalDifference*factor;
if( metalNext > metalLimit )
metalNext = metalLimit;
if( G->UDH->EnergyToMetalRatio*metalNext+energyNext > G->UDH->EnergyToMetalRatio*metalCurrent+energyCurrent )
{ // Turn OFF
MtoEActive--;
GiveOnOffOrder(MtoE[MtoEActive]->unitID,MtoE[MtoEActive]->U,false);
MtoEIncome += udr->OnOffEnergyDifference;
MtoENeeded += udr->OnOffMetalDifference;
ExMMetalDifference -= udr->OnOffMetalDifference;
ExMEnergyDifference -= udr->OnOffEnergyDifference;
metalCurrent -= udr->OnOffMetalDifference*factor;
energyCurrent -= udr->OnOffEnergyDifference*factor;
}
else
break;
}
}
}
}
void cPowerManager::InsertPI(UnitInfoPower **PIA, int &PIASize, UnitInfoPower *P)
{
for(P->index = PIASize; P->index>0; P->index-- )
if( P->importance > PIA[P->index-1]->importance )
{
PIA[P->index] = PIA[P->index-1];
PIA[P->index]->index = P->index;
}
else
break;
PIA[P->index] = P;
PIASize++;
}
void cPowerManager::RemovePI(UnitInfoPower **PIA, int &PIASize, UnitInfoPower *P)
{
for(int i=P->index; i<PIASize-1; i++ )
{
PIA[i] = PIA[i+1];
PIA[i]->index = i;
}
PIASize--;
}
void cPowerManager::GiveCloakOrder(const int &unitID, UnitInfo *U, bool state )
{
if( U != 0 )
U->CloakUI->active = state;
Command c;
c.id=CMD_CLOAK;
c.params.push_back(state);
cb->GiveOrder(unitID, &c);
}
void cPowerManager::GiveOnOffOrder(const int &unitID, UnitInfo *U, bool state )
{
if( U != 0 )
U->OnOffUI->active = state;
Command c;
c.id=CMD_ONOFF;
c.params.push_back(state);
cb->GiveOrder(unitID, &c);
}
|