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 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
|
/***************************************************************************
valuation.cpp - description
-------------------
begin : Fri Mar 30 2001
copyright : (C) 2001 by Martin Bickel
email : bickel@asc-hq.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <iostream>
#include "ai_common.h"
#include "../actions/attackcommand.h"
#include "../actions/moveunitcommand.h"
const int value_armorfactor = 100;
const int value_weaponfactor = 3000;
const int ccbt_repairfacility = 200; // basic threatvalues for buildings
const int ccbt_hq = 10000;
const int ccbt_recycling = 50;
const int ccbt_training = 150;
class CalculateThreat_VehicleType {
protected:
AI* ai;
const Vehicletype* fzt;
int weapthreat[8];
int value;
virtual int getdamage ( void ) { return 0; };
virtual int getexpirience ( void ) { return 0; };
virtual int getammunition( int i ) { return 1; };
virtual int getheight ( void ) { return 255; };
public:
void calc_threat_vehicletype ( const Vehicletype* _fzt );
CalculateThreat_VehicleType ( AI* _ai ) { ai = _ai; };
virtual ~CalculateThreat_VehicleType() {};
};
class CalculateThreat_Vehicle : public CalculateThreat_VehicleType {
protected:
Vehicle* eht;
virtual int getdamage ( void );
virtual int getexpirience ( void );
virtual int getammunition( int i );
virtual int getheight ( void );
public:
void calc_threat_vehicle ( Vehicle* _eht );
CalculateThreat_Vehicle ( AI* _ai ) : CalculateThreat_VehicleType ( _ai ) {};
};
void CalculateThreat_VehicleType :: calc_threat_vehicletype ( const Vehicletype* _fzt )
{
fzt = _fzt;
for ( int j = 0; j < 8; j++ )
weapthreat[j] = 0;
for ( int i = 0; i < fzt->weapons.count; i++)
if ( fzt->weapons.weapon[i].shootable() )
if ( fzt->weapons.weapon[i].offensive() )
for ( int j = 0; j < 8; j++)
if ( fzt->weapons.weapon[i].targ & (1 << j) ) {
int d = 0;
int m = 0;
AttackFormula af( ai->getMap() );
for ( int e = (fzt->weapons.weapon[i].mindistance + maxmalq - 1)/ maxmalq; e <= fzt->weapons.weapon[i].maxdistance / maxmalq; e++ ) { // the distance between two fields is maxmalq
d++;
int n = int( WeapDist::getWeaponStrength( &fzt->weapons.weapon[i], 0, e*maxmalq ) * fzt->weapons.weapon[i].maxstrength * af.strength_damage(getdamage()) * ( 1 + af.strength_experience(getexpirience())) );
m += int( n / log10(double(10*d)));
}
if (getammunition(i) == 0)
m /= 2;
if ( (fzt->weapons.weapon[i].sourceheight & getheight()) == 0)
m /= 2;
/*
if ( !(getheight() & ( 1 << j )))
m /= 2;
*/
if (m > weapthreat[j])
weapthreat[j] = m;
}
if ( !fzt->aiparam[ai->getPlayerNum()] )
fzt->aiparam[ ai->getPlayerNum() ] = new AiValue ( log2 ( fzt->height ));
for ( int l = 0; l < 8; l++ )
fzt->aiparam[ ai->getPlayerNum() ]->threat.threat[l] = weapthreat[l];
value = fzt->armor * value_armorfactor * (100 - getdamage()) / 100;
for ( int s = 0; s < 7; )
if ( weapthreat[s] < weapthreat[s+1] ) {
int temp = weapthreat[s];
weapthreat[s] = weapthreat[s+1];
weapthreat[s+1] = temp;
if ( s > 0 )
s--;
} else
s++;
for ( int k = 0; k < 8; k++ )
value += weapthreat[k] * value_weaponfactor / (k+1);
fzt->aiparam[ ai->getPlayerNum() ]->setValue ( value );
fzt->aiparam[ ai->getPlayerNum() ]->valueType = 0;
}
int CalculateThreat_Vehicle :: getammunition( int i )
{
return eht->ammo[i];
}
int CalculateThreat_Vehicle :: getheight(void)
{
return eht->height;
}
int CalculateThreat_Vehicle :: getdamage(void)
{
return eht->damage;
}
int CalculateThreat_Vehicle :: getexpirience(void)
{
return eht->experience;
}
void CalculateThreat_Vehicle :: calc_threat_vehicle ( Vehicle* _eht )
{
eht = _eht;
calc_threat_vehicletype ( vehicleTypeRepository.getObject_byID ( eht->typ->id ) );
if ( !eht->aiparam[ai->getPlayerNum()] )
eht->aiparam[ai->getPlayerNum()] = new AiParameter ( eht );
AiParameter* aip = eht->aiparam[ai->getPlayerNum()];
for ( int l = 0; l < 8; l++ )
aip->threat.threat[l] = eht->typ->aiparam[ ai->getPlayerNum() ]->threat.threat[l];
int value = eht->typ->aiparam[ ai->getPlayerNum() ]->getValue();
for ( ContainerBase::Cargo::const_iterator i = eht->getCargo().begin(); i != eht->getCargo().end(); ++i )
if ( *i ) {
if ( !(*i)->aiparam[ai->getPlayerNum()] ) {
CalculateThreat_Vehicle ctv ( ai );
ctv.calc_threat_vehicle( *i );
}
value += (*i)->aiparam[ai->getPlayerNum()]->getValue();
}
aip->setValue ( value );
if ( aip->getJob() == AiParameter::job_undefined )
if ( eht->canMove() )
aip->setJob( AI::chooseJob ( eht->typ ));
/*
generatethreatvalue();
int l = 0;
for ( int b = 0; b <= 7; b++) {
eht->threatvalue[b] = weapthreatvalue[b];
if (weapthreatvalue[b] > l)
l = weapthreatvalue[b];
} <Zwischenablage leer>
eht->completethreatvalue = threatvalue2 + l;
eht->completethreatvaluesurr = threatvalue2 + l;
eht->threats = 0;
*/
}
AiParameter::JobList AI::chooseJob ( const Vehicletype* typ )
{
AiParameter::JobList jobList;
if ( typ->recommendedAIJob != AiParameter::job_undefined ) {
jobList.push_back ( typ->recommendedAIJob );
return jobList;
}
int maxmove = minint;
for ( int i = 0; i< 8; i++ )
if ( typ->height & ( 1 << i ))
maxmove = max ( typ->movement[i] , maxmove );
int maxstrength = minint;
for ( int w = 0; w < typ->weapons.count; w++ )
if ( typ->weapons.weapon[w].offensive() )
maxstrength= max ( typ->weapons.weapon[w].maxstrength, maxstrength );
bool service = false;
for ( int w = 0; w < typ->weapons.count; w++ )
if ( typ->weapons.weapon[w].service() )
service = true;
if ( ( typ->hasFunction( ContainerBaseType::ExternalRepair ) || service) && maxmove >= minmalq && maxstrength < 45)
jobList.push_back ( AiParameter::job_supply );
if ( typ->hasFunction( ContainerBaseType::ConquerBuildings ) ) {
/* if ( functions & cf_trooper ) {
if ( typ->height & chfahrend )
jobList.push_back ( AiParameter::job_conquer );
} else { */
if ( maxstrength < maxmove )
jobList.push_back ( AiParameter::job_conquer );
// }
}
if ( ( maxstrength*1.5 < typ->view
|| (maxstrength*1.5 < typ->jamming && !typ->hasFunction( ContainerBaseType::JamsOnlyOwnField )))
&& maxmove > minmalq )
jobList.push_back ( AiParameter::job_recon );
if ( maxstrength > 0 )
jobList.push_back ( AiParameter::job_fight );
jobList.push_back ( AiParameter::job_undefined );
return jobList;
}
void AI :: calculateThreat ( const Vehicletype* vt)
{
CalculateThreat_VehicleType ctvt ( this );
ctvt.calc_threat_vehicletype( vt );
}
void AI :: calculateThreat ( Vehicle* eht )
{
CalculateThreat_Vehicle ctv ( this );
ctv.calc_threat_vehicle( eht );
}
void AI :: calculateThreat ( Building* bld )
{
calculateThreat ( bld, getPlayerNum());
// calculateThreat ( bld, 8 );
}
void AI :: calculateThreat ( Building* bld, int player )
{
if ( !bld->aiparam[ player ] )
bld->aiparam[ player ] = new AiValue ( log2 ( bld->typ->height ) );
int b;
// Since we have two different resource modes now, this calculation should be rewritten....
int value = (bld->plus.energy / 10) + (bld->plus.fuel / 10) + (bld->plus.material / 10) + (bld->actstorage.energy / 20) + (bld->actstorage.fuel / 20) + (bld->actstorage.material / 20);
for ( ContainerBase::Cargo::const_iterator i = bld->getCargo().begin(); i != bld->getCargo().end(); ++i )
if ( *i ) {
if ( !(*i)->aiparam[ player ] )
calculateThreat ( *i );
value += (*i)->aiparam[ player ]->getValue();
}
for (b = 0; b < bld->getProduction().size(); b++)
if ( bld->getProduction()[b] ) {
if ( !bld->getProduction()[b]->aiparam[ player ] )
calculateThreat ( bld->getProduction()[b] );
value += bld->getProduction()[b]->aiparam[ player ]->getValue() / 10;
}
if (bld->typ->hasFunction( ContainerBaseType::InternalUnitRepair ))
value += ccbt_repairfacility;
if (bld->typ->hasFunction( ContainerBaseType::TrainingCenter ) )
value += ccbt_training;
if (bld->typ->hasFunction( ContainerBaseType::RecycleUnits ) )
value += ccbt_recycling;
bld->aiparam[ player ]->setValue ( value );
}
void AI :: WeaponThreatRange :: run ( Vehicle* _veh, int x, int y, AiThreat* _threat )
{
threat = _threat;
veh = _veh;
for ( height = 0; height < 8; height++ )
for ( weap = 0; weap < veh->typ->weapons.count; weap++ )
if ( veh->height & veh->typ->weapons.weapon[weap].sourceheight )
if ( (1 << height) & veh->typ->weapons.weapon[weap].targ )
if ( veh->typ->weapons.weapon[weap].shootable() && veh->typ->weapons.weapon[weap].offensive() ) {
initsearch ( MapCoordinate(x, y), veh->typ->weapons.weapon[weap].maxdistance/maxmalq, veh->typ->weapons.weapon[weap].mindistance/maxmalq );
startsearch();
}
}
void AI :: WeaponThreatRange :: testfield ( const MapCoordinate& mc )
{
if ( dist*maxmalq <= veh->typ->weapons.weapon[weap].maxdistance )
if ( dist*maxmalq >= veh->typ->weapons.weapon[weap].mindistance ) {
AttackFormula af ( ai->getMap() );
int strength = int ( WeapDist::getWeaponStrength( &veh->typ->weapons.weapon[weap], ai->getMap()->getField(mc)->getWeather(), dist*maxmalq, veh->height, 1 << height )
* veh->typ->weapons.weapon[weap].maxstrength
* (1 + af.strength_experience ( veh->experience ) + af.strength_attackbonus ( gamemap->getField(startPos)->getattackbonus() ))
* af.strength_damage ( veh->damage )
);
if ( strength ) {
int pos = mc.x + mc.y * ai->getMap()->xsize;
if ( strength > threat[pos].threat[height] )
threat[pos].threat[height] = strength;
}
}
}
void AI :: calculateFieldInformation ( void )
{
if ( fieldNum && fieldNum != activemap->xsize * activemap->ysize ) {
delete[] fieldInformation;
fieldInformation = NULL;
fieldNum = 0;
}
if ( !fieldInformation ) {
fieldNum = activemap->xsize * activemap->ysize;
fieldInformation = new FieldInformation[ fieldNum ];
} else
for ( int a = 0; a < fieldNum; a++ )
fieldInformation[ a ].reset();
AiThreat* singleUnitThreat = new AiThreat[fieldNum];
// we now check the whole map
for ( int y = 0; y < activemap->ysize; y++ ) {
checkKeys();
for ( int x = 0; x < activemap->xsize; x++ ) {
tfield* fld = activemap->getField ( x, y );
if ( config.wholeMapVisible || fieldvisiblenow ( fld, getPlayerNum() ) )
if ( fld->vehicle && getPlayer().diplomacy.isHostile( fld->vehicle->getOwner() )) {
WeaponThreatRange wr ( this );
if ( !fld->vehicle->typ->wait ) {
// The unit may have already moved this turn.
// So we give it the maximum movementrange
TemporaryContainerStorage tus ( fld->vehicle );
fld->vehicle->setMovement ( fld->vehicle->maxMovement(), 0);
if ( MoveUnitCommand::avail ( fld->vehicle )) {
MoveUnitCommand muc ( fld->vehicle );
muc.searchFields();
const set<MapCoordinate3D>& fields = muc.getReachableFields();
for ( set<MapCoordinate3D>::const_iterator i = fields.begin(); i != fields.end(); ++i )
wr.run ( fld->vehicle, i->x, i->y, singleUnitThreat );
const set<MapCoordinate3D>& ifields = muc.getReachableFieldsIndirect();
for ( set<MapCoordinate3D>::const_iterator i = ifields.begin(); i != ifields.end(); ++i )
wr.run ( fld->vehicle, i->x, i->y, singleUnitThreat );
}
tus.restore();
} else
wr.run ( fld->vehicle, x, y, singleUnitThreat );
for ( int a = 0; a < fieldNum; a++ ) {
for ( int b = 0; b < 8; b++ )
fieldInformation[a].threat.threat[b] += singleUnitThreat[a].threat[b];
singleUnitThreat[ a ].reset();
}
}
FieldInformation& fi = fieldInformation[y*getMap()->xsize+x];
for ( int i = 0; i< sidenum; i++ ) {
tfield* f = getMap()->getField ( getNeighbouringFieldCoordinate ( MapCoordinate(x,y), i ));
if ( f && f->vehicle && f->vehicle->weapexist() && f->vehicle->color < 8*8 )
fi.units[f->vehicle->color/8] += 1;
}
int n = 0;
int c = -1;
for ( int i = 0; i < 8; i++ ) {
if ( fi.units[i] > n ) {
n = fi.units[i];
c = i;
}
}
fi.control = c;
}
}
delete[] singleUnitThreat;
}
void AI :: calculateAllThreats( void )
{
// Calculates the basethreats for all vehicle types
if ( !baseThreatsCalculated ) {
for ( int w = 0; w < vehicleTypeRepository.getNum(); w++) {
Vehicletype* fzt = vehicleTypeRepository.getObject_byPos(w);
if ( fzt )
calculateThreat( fzt );
}
baseThreatsCalculated = 1;
}
// Some further calculations that only need to be done once.
if ( maxTrooperMove == 0) {
for ( int v = 0; v < vehicleTypeRepository.getNum(); v++) {
Vehicletype* fzt = vehicleTypeRepository.getObject_byPos( v );
if ( fzt )
if ( fzt->hasFunction( ContainerBaseType::ConquerBuildings ) )
if ( fzt->movement[log2(chfahrend)] > maxTrooperMove ) // buildings can only be conquered on ground level, or by moving to adjecent field which is less
maxTrooperMove = fzt->movement[log2(chfahrend)];
}
}
if ( maxTransportMove == 0 ) {
for (int v = 0; v < vehicleTypeRepository.getNum(); v++) {
Vehicletype* fzt = vehicleTypeRepository.getObject_byPos( v );
if ( fzt )
for ( int w = 0; w <= 7; w++) // cycle through all levels of height
if (fzt->movement[w] > maxTransportMove)
maxTransportMove = fzt->movement[w];
}
maxUnitMove = maxTransportMove;
}
for ( int height = 0; height < 8; height++ )
if ( maxWeapDist[height] < 0 ) {
maxWeapDist[height] = 0; // It may be possible that there is no weapon to shoot to a specific height
for ( int v = 0; v < vehicleTypeRepository.getNum(); v++) {
Vehicletype* fzt = vehicleTypeRepository.getObject_byPos( v );
if ( fzt )
for ( int w = 0; w < fzt->weapons.count ; w++)
if ( fzt->weapons.weapon[w].maxdistance > maxWeapDist[height] )
if ( fzt->weapons.weapon[w].targ & ( 1 << height )) // targ is a bitmap, each bit standing for a level of height
maxWeapDist[height] = fzt->weapons.weapon[w].maxdistance;
}
}
// There are only 8 players in ASC, but there may be neutral units (player == 8)
for ( int v = 0; v < 9; v++)
if (activemap->player[v].exist() || v == 8) {
// Now we cycle through all units of this player
for ( Player::VehicleList::iterator vi = getPlayer(v).vehicleList.begin(); vi != getPlayer(v).vehicleList.end(); vi++ ) {
Vehicle* veh = *vi;
// if ( !veh->aiparam[ getPlayerNum() ] )
calculateThreat ( veh );
}
// Now we cycle through all buildings
for ( Player::BuildingList::iterator bi = getPlayer(v).buildingList.begin(); bi != getPlayer(v).buildingList.end(); bi++ )
calculateThreat ( *bi );
}
}
void AI :: FieldInformation :: reset ( )
{
threat.reset();
for ( int i = 0; i< 8; i++ )
units[i] = 0;
control = -1;
}
AiThreat& AI :: getFieldThreat ( int x, int y )
{
if ( !fieldInformation )
calculateFieldInformation ();
return fieldInformation[y * activemap->xsize + x ].threat;
}
AI::FieldInformation& AI :: getFieldInformation ( int x, int y )
{
if ( !fieldInformation )
calculateFieldInformation ();
return fieldInformation[y * activemap->xsize + x ];
}
void AI :: Section :: init ( AI* _ai, int _x, int _y, int xsize, int ysize, int _xp, int _yp )
{
ai = _ai;
init ( _x, _y, xsize, ysize, _xp, _yp );
}
void AI :: Section :: init ( int _x, int _y, int xsize, int ysize, int _xp, int _yp )
{
x1 = _x;
y1 = _y;
x2 = _x + xsize;
y2 = _y + ysize;
xp = _xp;
yp = _yp;
if ( x1 < 0 ) x1 = 0;
if ( y1 < 0 ) y1 = 0;
if ( x2 >= ai->activemap->xsize ) x2 = ai->activemap->xsize-1;
if ( y2 >= ai->activemap->ysize ) y2 = ai->activemap->ysize-1;
centerx = (x1 + x2) / 2;
centery = (y1 + y2) / 2;
numberOfFields = (x2-x1+1) * ( y2-y1+1);
absUnitThreat.reset();
absFieldThreat.reset();
for ( int j = 0; j < aiValueTypeNum; j++ )
value[j] = 0;
for ( int y = y1; y <= y2; y++ )
for ( int x = x1; x <= x2; x++ ) {
absFieldThreat += ai->getFieldThreat ( x, y );
tfield* fld = ai->activemap->getField ( x, y );
if ( fld->vehicle && ai->getPlayer().diplomacy.isHostile( fld->vehicle->getOwner() ) ) {
if ( !fld->vehicle->aiparam[ ai->getPlayerNum() ] )
ai->calculateThreat ( fld->vehicle );
AiParameter& aip = * fld->vehicle->aiparam[ ai->getPlayerNum() ];
absUnitThreat += aip.threat;
value[ aip.valueType ] += aip.getValue();
}
}
for ( int i = 0; i < absUnitThreat.threatTypes; i++ ) {
avgUnitThreat.threat[i] = absUnitThreat.threat[i] / numberOfFields;
avgFieldThreat.threat[i] = absFieldThreat.threat[i] / numberOfFields;
}
}
int AI :: Section :: numberOfAccessibleFields ( const Vehicle* veh )
{
int num = 0;
for ( int y = y1; y <= y2; y++ )
for ( int x = x1; x <= x2; x++ )
if ( fieldAccessible ( ai->activemap->getField ( x, y ), veh ) == 2)
num++;
return num;
}
AI :: Sections :: Sections ( AI* _ai ) : ai ( _ai ) , section ( NULL )
{
sizeX = 8;
sizeY = 16;
numX = ai->activemap->xsize * 2 / sizeX + 1;
numY = ai->activemap->ysize * 2 / sizeY + 1;
}
void AI :: Sections :: reset ()
{
if ( section ) {
delete[] section;
section = NULL;
}
}
AI :: Sections :: ~Sections()
{
reset();
}
void AI :: Sections :: calculate ( void )
{
if ( !section ) {
section = new Section[ numX*numY ]; // ( ai );
for ( int x = 0; x < numX; x++ )
for ( int y = 0; y < numY; y++ )
section[ x + numX * y ].init ( ai, x * ai->activemap->xsize / numX, y * ai->activemap->ysize / numY, sizeX, sizeY, x, y );
}
}
AI::Section& AI :: Sections :: getForCoordinate ( int xc, int yc )
{
if ( !section )
calculate();
int dist = maxint;
Section* sec = NULL;
for ( int x = 0; x < numX; x++ )
for ( int y = 0; y < numY; y++ ) {
Section& s2 = getForPos ( x, y );
int d = beeline ( xc, yc, s2.centerx, s2.centery);
if ( d < dist ) {
dist = d;
sec = &getForPos ( x, y );
}
}
return *sec;
}
AI::Section& AI :: Sections :: getForPos ( int xn, int yn )
{
if ( xn >= numX || yn >= numY || xn < 0 || yn < 0 )
displaymessage( "AI :: Sections :: getForPos - invalid parameters: %d %d", 2, xn, yn );
return section[xn+yn*numX];
}
/*
This is like some ball rolling down from some high potential spike
*/
MapCoordinate AI :: Sections :: getAlternativeField( const MapCoordinate& pos, map<MapCoordinate,int>* destinationCounter, int height )
{
MapCoordinate result = pos;
int potential = destinationCounter->operator[](pos);
for ( int d = 0; d< 6; ++d ) {
MapCoordinate m = getNeighbouringFieldCoordinate( pos, d );
tfield* fld = ai->getMap()->getField(m );
if ( fld && (fld->a.temp & height) ) {
if ( destinationCounter->find( m ) == destinationCounter->end() )
return m;
else {
if ( (destinationCounter->find(m)->second * 11 / 10) < potential ) {
result = m;
potential = destinationCounter->find(m)->second;
}
}
}
}
if ( result == pos )
return result;
else
return getAlternativeField( result , destinationCounter, height );
}
AI::Section* AI :: Sections :: getBest ( int pass, Vehicle* veh, MapCoordinate3D* dest, bool allowRefuellOrder, bool secondRun, map<MapCoordinate,int>* destinationCounter )
{
/*
In the first pass wwe check were all the units would go if there wouldn't be
a threat anywhere.
In the second pass the threat of a section is devided by the number of units that
are going there
*/
AStar3D* ast = 0;
RefuelConstraint* rfc = NULL;
if ( RefuelConstraint::necessary ( veh, *ai )) {
if ( secondRun )
rfc = new RefuelConstraint ( *ai, veh, veh->maxMovement()*5 );
else
rfc = new RefuelConstraint ( *ai, veh );
rfc->findPath();
} else {
ast = new AStar3D ( ai->getMap(), veh );
ast->findAllAccessibleFields ( );
}
AiParameter& aip = *veh->aiparam[ ai->getPlayerNum() ];
float d = minfloat;
// float nd = minfloat;
AI::Section* frst = NULL;
float maxSectionThread = 0;
for ( int y = 0; y < numY; y++ )
for ( int x = 0; x < numX; x++ ) {
AI::Section& sec = getForPos( x, y );
int threat = sec.avgUnitThreat.threat[aip.valueType];
if ( threat > maxSectionThread )
maxSectionThread = threat;
}
TemporaryContainerStorage tus ( veh );
veh->resetMovement(); // to make sure the wait-for-attack flag doesn't hinder the attack
veh->attacked = 0;
int sectionsPossibleWithMaxFuell = 0;
for ( int h = 1; h < 0xff; h<<= 1 )
if ( veh->typ->height & h )
for ( int y = 0; y < numY; y++ )
for ( int x = 0; x < numX; x++ ) {
int xtogoSec = -1;
int ytogoSec = -1;
AI::Section& sec = getForPos( x, y );
float t = 0;
for ( int i = 0; i < aiValueTypeNum; i++ )
t += aip.threat.threat[i] * sec.value[i];
float f = t;
if ( sec.avgUnitThreat.threat[ veh->getValueType(h) ] ) {
int relThreat = int( 4*maxSectionThread / sec.avgUnitThreat.threat[veh->getValueType(h)] + 1);
f /= relThreat;
}
/*
if ( sec.avgUnitThreat.threat[aip.valueType] >= 0 )
f = t / log( sec.avgUnitThreat.threat[aip.valueType] );
else
f = t;
*/
int dist = beeline ( veh->xpos, veh->ypos, sec.centerx, sec.centery ) + 3 * veh->maxMovement();
if ( dist )
f /= log(double(dist));
if ( f > d ) {
int ac = 0;
int nac = 0;
int mindist = maxint;
int targets = 0;
for ( int yp = sec.y1; yp <= sec.y2; yp++ )
for ( int xp = sec.x1; xp <= sec.x2; xp++ ) {
tfield* fld = ai->getMap()->getField(xp, yp );
if ( fld->a.temp & h ) {
int mandist = abs( sec.centerx - xp ) + 2*abs ( sec.centery - yp );
if ( mandist < mindist ) {
mindist = mandist;
if ( destinationCounter && destinationCounter->find( MapCoordinate(xp,yp)) != destinationCounter->end() ) {
/* we are checking if there are too many units heading for this field already
check how many are for neighbouringfields */
MapCoordinate alt = getAlternativeField( MapCoordinate(xp,yp), destinationCounter, h );
xtogoSec = alt.x;
ytogoSec = alt.y;
} else {
xtogoSec = xp;
ytogoSec = yp;
}
}
ai->_vision = visible_all;
ac++;
veh->xpos = xp;
veh->ypos = yp;
veh->height = h;
if ( AttackCommand::avail( veh )) {
AttackCommand attack ( veh );
attack.searchTargets();
targets += attack.getAttackableUnits().size();
}
ai->_vision = visible_ago;
} else
nac++;
}
if ( xtogoSec >= 0 && ytogoSec >= 0 ) {
if ( !rfc || rfc->returnFromPositionPossible ( MapCoordinate3D( xtogoSec, ytogoSec, h ))) {
int notAccessible = 100 * nac / (nac+ac);
if ( notAccessible < 85 && targets ) { // less than 85% of fields not accessible
float nf = f * ( 100-notAccessible) / 100; // * ( 100 - notAccessible );
if ( nf > d ) {
d = nf;
// nd = nf;
frst = &getForPos ( x, y );
if ( dest )
*dest = MapCoordinate3D ( xtogoSec, ytogoSec, h );
}
}
} else
if ( allowRefuellOrder ) {
if ( rfc && rfc->returnFromPositionPossible ( MapCoordinate3D( xtogoSec, ytogoSec, h ), veh->getStorageCapacity().fuel ))
sectionsPossibleWithMaxFuell++;
}
}
}
}
tus.restore();
delete ast;
delete rfc;
if ( !frst ) {
if ( sectionsPossibleWithMaxFuell && allowRefuellOrder )
ai->issueRefuelOrder ( veh, false );
else
if ( RefuelConstraint::necessary ( veh, *ai ) && !secondRun )
return getBest ( pass, veh, dest, allowRefuellOrder, true );
}
return frst;
}
|