Ejemplo n.º 1
0
	string CUBuild::GetDEFENCE(){
		NLOG("CUBuild::GetDEFENCE");

		const vector<CommandDescription>* di = G->cb->GetUnitCommands(uid);

		if(di == 0) return string("");

		list<string> possibles;

		int defnum = 0;

		for(vector<CommandDescription>::const_iterator is = di->begin(); is != di->end();++is){

			if(is->id<0){

				// retrieve the unit type information
				CUnitTypeData* p =G->UnitDefLoader->GetUnitTypeDataByName(is->name);
				const UnitDef* pd = p->GetUnitDef();

				if(Useless(p)) continue;

				if((p->GetUnitDef()->weapons.empty() == false)&&(!p->GetUnitDef()->isFeature)&&(!p->IsMobile())){
					if((G->info->spacemod == true)||(water == true)){
						if(G->Pl->feasable(p,utd)==false) continue;
						possibles.push_back(p->GetName());
						defnum++;
					} else if (pd->floater == false){
						if(G->Pl->feasable(p,utd)==false) continue;
						possibles.push_back(p->GetName());
						defnum++;
					}
				}
			}
		}

		if(possibles.empty() == false){
			defnum = G->mrand()%defnum;
			int j = 0;
			for(list<string>::iterator k = possibles.begin(); k != possibles.end(); ++k){
				if(j == defnum){
					return *k;
				}else{
					j++;
				}
			}
			return possibles.front();
		}
		
		return string("");
	}
Ejemplo n.º 2
0
bool CActions::RepairNearbyUnfinishedMobileUnits(int uid, float radius){
    NLOG("CActions::RepairNearbyUnfinishedMobileUnits");
    const UnitDef* udi = G->cb->GetUnitDef(uid);
    if(udi == 0) return false;
    float3 pos = G->GetUnitPos(uid);
    int* hn = new int[10000];
    int h = G->cb->GetFriendlyUnits(hn, pos, radius);
    if( h>0){
        for( int i = 0; i<h; i++){

            if(G->cb->UnitBeingBuilt(hn[i]) == true){
				CUnitTypeData* btd = G->UnitDefLoader->GetUnitTypeDataByUnitId(hn[i]);

				if(!btd->IsMobile()){
					continue;
				}

                float Remainder = 1.0f - G->cb->GetUnitHealth(hn[i]) / G->cb->GetUnitMaxHealth(hn[i]);
                float RemainingTime = btd->GetUnitDef()->buildTime / udi->buildSpeed * Remainder;

				if (RemainingTime < 30.0f){
					continue;
				}

				float EPerS = btd->GetUnitDef()->energyCost / (btd->GetUnitDef()->buildTime / udi->buildSpeed);
                float MPerS = btd->GetUnitDef()->metalCost / (btd->GetUnitDef()->buildTime / udi->buildSpeed);

                if (G->cb->GetMetal() + (G->Pl->GetMetalIncome() - MPerS) * RemainingTime > 0 && G->cb->GetEnergy() + (G->Pl->GetEnergyIncome() - EPerS) * RemainingTime > 0){
					
					if (RemainingTime > 120.0f || btd->GetUnitDef()->buildSpeed > 0){
                        int target = hn[i];
                        delete [] hn;
                        return Repair(uid, target);
                    }

                }

            }
        }

    }else{
        h = G->cb->GetFriendlyUnits(hn);

		if(h <1){
			return false;
		}

        for( int i = 0; i<h; i++){

            if((G->cb->UnitBeingBuilt(hn[i])||(G->cb->GetUnitMaxHealth(hn[i])*0.6f > G->cb->GetUnitHealth(hn[i])))&&(G->cb->GetUnitTeam(hn[i]) == G->Cached->team)){
                CUnitTypeData* btd = G->UnitDefLoader->GetUnitTypeDataByUnitId(hn[i]);
                
				float Remainder = 1.0f - G->cb->GetUnitHealth(hn[i]) / G->cb->GetUnitMaxHealth(hn[i]);
                float RemainingTime = btd->GetUnitDef()->buildTime / udi->buildSpeed * Remainder;
				
				if (RemainingTime < 30.0f){
					continue;
				}

                float EPerS = btd->GetUnitDef()->energyCost / (btd->GetUnitDef()->buildTime / udi->buildSpeed);
                float MPerS = btd->GetUnitDef()->metalCost / (btd->GetUnitDef()->buildTime / udi->buildSpeed);

                if (G->cb->GetMetal() + (G->Pl->GetMetalIncome() - MPerS) * RemainingTime > 0	&& G->cb->GetEnergy() + (G->Pl->GetEnergyIncome() - EPerS) * RemainingTime > 0	){
                    if (RemainingTime > 120.0f || btd->GetUnitDef()->buildSpeed > 0)	{
                        int target = hn[i];
                        delete [] hn;
                        return Repair(uid, target);
                    }
                }

            }
        }
    }
    delete [] hn;
    return false;
}
Ejemplo n.º 3
0
bool CActions::AttackNear(int unit, float LOSmultiplier){
    NLOG("CActions::AttackNear");

    const UnitDef* ud = G->GetUnitDef(unit);
    if(ud == 0) return false;

	CUnitTypeData* utd = G->UnitDefLoader->GetUnitTypeDataById(ud->id);

    int* en = new int[10000];
    int e = G->GetEnemyUnits(en, G->GetUnitPos(unit), max(G->cb->GetUnitMaxRange(unit), ud->losRadius)*LOSmultiplier);

    if(e>0){
        float best_score = 0;
        int best_target = 0;
        float tempscore = 0;
        bool mobile = true;

        for(int i = 0; i < e; i++){
            if(en[i] < 1) continue;

            const UnitDef* endi = G->GetEnemyDef(en[i]);

            if(endi == 0){
                continue;
            }else{
				CUnitTypeData* etd = G->UnitDefLoader->GetUnitTypeDataById(endi->id);

                bool tmobile = false;

                tempscore = G->GetTargettingWeight(ud->name, endi->name);

                //tempscore = G->Ch->ApplyGrudge(en[i], tempscore);

                if(etd->IsMobile()){
					if(!G->info->hardtarget){
						tempscore = tempscore/4;
					}
                    tmobile = true;
                }else if(!endi->weapons.empty()){
                    tempscore /= 2;
                }

                if(tempscore > best_score){
                    best_score = tempscore;
                    best_target = en[i];
                    mobile = tmobile;
                }

                tempscore = 0;
            }
        }

        if(ud->highTrajectoryType == 2){
            Trajectory(unit, (int)mobile);
        }

        if(best_target > 0){
            delete [] en;
            return Attack(unit, best_target, mobile);
        }

        delete [] en;
        return true;
    }else{
        delete [] en;
        return false;
    }
}