Beispiel #1
0
void CEconomyTracker::UnitFinished(int unit) {
	if (trackerOff)
		return;

	const int frame = ai->cb->GetCurrentFrame();
	const UnitDef* unitDef = ai->cb->GetUnitDef(unit);

	if (unitDef == NULL || CUNIT::IsCommander(unitDef) || unitDef->canDGun) {
		// add the commander to an EconomyUnitTracker
		EconomyUnitTracker * economyUnitTracker = new EconomyUnitTracker;
		economyUnitTracker->clear();
		economyUnitTracker->economyUnitId = unit;
		economyUnitTracker->createFrame = frame;
		economyUnitTracker->alive = true;
		economyUnitTracker->category = GCAT(unit);
		economyUnitTracker->unitDef = unitDef;
		SetUnitDefDataInTracker(economyUnitTracker);
		newEconomyUnitTrackers.push_back(economyUnitTracker);
		return;
	}

	// move the new EconomyUnitTrackers
	bool found = false;
	std::list<EconomyUnitTracker*> removeList;
	for (std::list<EconomyUnitTracker*>::iterator i = underConstructionEconomyUnitTrackers.begin(); i != underConstructionEconomyUnitTrackers.end(); i++) {
		EconomyUnitTracker *bt = *i;
		if (bt->economyUnitId == unit) {
			 bt->createFrame = frame;
			assert(bt->alive);
			newEconomyUnitTrackers.push_back(bt);
			removeList.push_back(bt);
			found = true;
			break;
		}
	}

	assert(found);
	// remove them from underConstructionEconomyUnitTrackers
	for (std::list<EconomyUnitTracker*>::iterator i = removeList.begin(); i != removeList.end(); i++) {
		underConstructionEconomyUnitTrackers.remove(*i);
	}

	UnitCategory category = ai->ut->GetCategory(unit);
	found = false;

	if (category != CAT_LAST) {
		for (std::list<BuildingTracker>::iterator i = allTheBuildingTrackers[category].begin(); i != allTheBuildingTrackers[category].end(); i++) {
			BuildingTracker* bt = &*i;
			if (bt->unitUnderConstruction == unit) {
				updateUnitUnderConstruction(bt);
				found = true;
				allTheBuildingTrackers[category].erase(i);
				break;
			}
		}
		if (!found) {
			// unit not in a BuildingTracker?
		}
	}
}
void CEconomyTracker::UnitFinished(int unit)
{
    if(trackerOff)
        return;
    int frame=ai->cb->GetCurrentFrame();

    if(frame == 0)
    {
        // Add the commander to a EconomyUnitTracker
        EconomyUnitTracker * economyUnitTracker = new EconomyUnitTracker;
        economyUnitTracker->clear();
        economyUnitTracker->economyUnitId = unit;
        economyUnitTracker->createFrame = frame;
        economyUnitTracker->alive = true;
        economyUnitTracker->category = GCAT(unit);
        economyUnitTracker->unitDef = ai->cb->GetUnitDef(unit);
        SetUnitDefDataInTracker(economyUnitTracker);
        newEconomyUnitTrackers.push_back(economyUnitTracker);
        return;
    }

    // Move the new EconomyUnitTrackers
    bool found = false;
    list<EconomyUnitTracker*> removeList;
    for(list<EconomyUnitTracker*>::iterator i = underConstructionEconomyUnitTrackers.begin(); i != underConstructionEconomyUnitTrackers.end(); i++) {
        EconomyUnitTracker *bt = *i; // This is bad, but needed
        if(bt->economyUnitId == unit)
        {
            bt->createFrame = frame;
            // Move it to the new list
            assert(bt->alive);
            newEconomyUnitTrackers.push_back(bt);
            removeList.push_back(bt);
            //L("Moved "  << bt->unitDef->humanName << " to newEconomyUnitTrackers");
            found = true;
            break;
        }
    }
    assert(found);
    // Remove them from underConstructionEconomyUnitTrackers:
    for(list<EconomyUnitTracker*>::iterator i = removeList.begin(); i != removeList.end(); i++) {
        underConstructionEconomyUnitTrackers.remove(*i);
    }

    //L("Finished a " << ai->cb->GetUnitDef(unit)->humanName);
    int category = ai->ut->GetCategory(unit);
    found = false;
    if(category != -1)
        for(list<BuildingTracker>::iterator i = allTheBuildingTrackers[category]->begin(); i != allTheBuildingTrackers[category]->end(); i++) {
            BuildingTracker *bt = &*i;
            if(bt->unitUnderConstruction == unit)
            {
                updateUnitUnderConstruction(bt);
                found = true;
                allTheBuildingTrackers[category]->erase(i);
                break;
            }
        }
    if(!found) {
        //L("This unit was not in a BuildingTracker!!!!!!!!!");
    }
}
Beispiel #3
0
void CEconomyTracker::frameUpdate(int frame) {
	if (trackerOff) {
		return;
	}

/*
	// iterate over all the BuildTasks
	for (UnitCategory category = 0; category < CAT_LAST; category++) {
		for (std::list<BuildTask>::iterator i = ai->uh->BuildTasks[category]->begin(); i != ai->uh->BuildTasks[category]->end(); i++) {
			BuildTask bt = *i;
			updateUnitUnderConstruction(&bt);
		}
	}
*/

	for (int category = 0; category < CAT_LAST; category++) {
		for (std::list<BuildingTracker>::iterator i = allTheBuildingTrackers[category].begin(); i != allTheBuildingTrackers[category].end(); i++) {
			BuildingTracker* bt = &(*i);
			updateUnitUnderConstruction(bt);
		}
	}

	constructionEnergySum += constructionEnergy;
	constructionMetalSum += constructionMetal;

	// move the new EconomyUnitTrackers
	std::list<EconomyUnitTracker*> removeList;

	for (std::list<EconomyUnitTracker*>::iterator i = newEconomyUnitTrackers.begin(); i != newEconomyUnitTrackers.end(); i++) {
		EconomyUnitTracker* bt = *i;
		assert(frame - bt->createFrame <= 16);

		if (frame - bt->createFrame == 16) {
			// move it to the active list
			assert(bt->alive);
			activeEconomyUnitTrackers.push_back(bt);
			removeList.push_back(bt);
		}
	}

	// remove them from newEconomyUnitTrackers
	for (std::list<EconomyUnitTracker*>::iterator i = removeList.begin(); i != removeList.end(); i++) {
		newEconomyUnitTrackers.remove(*i);
	}

	// update the units in activeEconomyUnitTrackers, add their production/usage to total
	float energyProduction = 0.0f;
	float metalProduction = 0.0f;
	// these are exclusive of what is used by builders
	float energyUsage = 0.0f;
	float metalUsage = 0.0f;

	if (frame % 16 == 0) {
		for (std::list<EconomyUnitTracker*>::iterator i = activeEconomyUnitTrackers.begin(); i != activeEconomyUnitTrackers.end(); i++) {
			EconomyUnitTracker* bt = *i;
			assert(bt->alive);

			// if the unit is a builder then we track its resource usage already
			// if it's using more than its weapon fire or upkeep
			UnitResourceInfo resourceInfo;
			bool isAlive = ai->cb->GetUnitResourceInfo(bt->economyUnitId, &resourceInfo);
			assert(isAlive);

			// add the change from last update
			bt->totalEnergyMake += bt->lastUpdateEnergyMake = resourceInfo.energyMake - bt->lastUpdateEnergyMake;
			bt->totalMetalMake += bt->lastUpdateMetalMake = resourceInfo.metalMake - bt->lastUpdateMetalMake;
			bt->totalEnergyUsage += bt->lastUpdateEnergyUsage = resourceInfo.energyUse - bt->lastUpdateEnergyUsage;
			bt->totalMetalUsage += bt->lastUpdateMetalUsage = resourceInfo.metalUse - bt->lastUpdateMetalUsage;
			energyProduction += bt->lastUpdateEnergyMake;
			metalProduction += bt->lastUpdateMetalMake;

			if (!bt->unitDef->builder) {
				energyUsage += bt->lastUpdateEnergyUsage;
				metalUsage += bt->lastUpdateMetalUsage;
			}
		}
	}

	float energy = ai->cb->GetEnergy();
	float metal = ai->cb->GetMetal();
	// float deltaEnergy = energy - oldEnergy + constructionEnergy;
	// float deltaMetal = metal - oldMetal + constructionMetal;

	if (frame % 16 == 0) {
		makePrediction(frame + 320 + 160);
	}

	oldEnergy = energy;
	oldMetal = metal;
	constructionEnergy = 0;
	constructionMetal = 0;
}
void CEconomyTracker::frameUpdate()
{
    if(trackerOff)
        return;
    int frame=ai->cb->GetCurrentFrame();
    //L(frame);
    // Iterate over all the BuildTasks:
    /*
    for(int category = 0; category < LASTCATEGORY; category++ )
    {
    	for(list<BuildTask>::iterator i = ai->uh->BuildTasks[category]->begin(); i != ai->uh->BuildTasks[category]->end(); i++){
    		BuildTask bt = *i;
    		updateUnitUnderConstruction(&bt);
    	}
    }*/
    for(int category = 0; category < LASTCATEGORY; category++ )
    {
        for(list<BuildingTracker>::iterator i = allTheBuildingTrackers[category]->begin(); i != allTheBuildingTrackers[category]->end(); i++) {
            BuildingTracker *bt = &*i; // This is bad, but needed
            updateUnitUnderConstruction(bt);
        }
    }
    constructionEnergySum += constructionEnergy;
    constructionMetalSum += constructionMetal;
    // Iterate over all the factories:


    //	bool IsUnitActivated (int unitid);

    // Move the new EconomyUnitTrackers
    list<EconomyUnitTracker*> removeList;
    for(list<EconomyUnitTracker*>::iterator i = newEconomyUnitTrackers.begin(); i != newEconomyUnitTrackers.end(); i++) {
        EconomyUnitTracker *bt = *i; // This is bad, but needed
        assert(frame - bt->createFrame <= 16);
        if(frame - bt->createFrame == 16)
        {
            // Move it to the active list
            assert(bt->alive);
            activeEconomyUnitTrackers.push_back(bt);
            removeList.push_back(bt);
            //L("Moved "  << bt->unitDef->humanName << " to activeEconomyUnitTrackers");
        }
    }
    // Remove them from newEconomyUnitTrackers:
    for(list<EconomyUnitTracker*>::iterator i = removeList.begin(); i != removeList.end(); i++) {
        newEconomyUnitTrackers.remove(*i);
    }
    // Update the units in activeEconomyUnitTrackers, add their production/usage to total
    float energyProduction = 0;
    float metalProduction = 0;
    float energyUsage = 0; // This is without what is used by builders
    float metalUsage = 0; // This is without what is used by builders
    if(frame % 16 == 0)
    {
        // Update
        for(list<EconomyUnitTracker*>::iterator i = activeEconomyUnitTrackers.begin(); i != activeEconomyUnitTrackers.end(); i++) {
            EconomyUnitTracker *bt = *i; // This is bad, but needed
            assert(bt->alive);
            ////L("Adding "  << bt->unitDef->humanName << " UnitResourceInfo....");
            // Hmmm
            // If the unit is a builder then we track its resource usage already
            // if its useing more then its weapon fire or upkeep
            UnitResourceInfo resourceInfo;
            bool isAlive = ai->cb->GetUnitResourceInfo(bt->economyUnitId, &resourceInfo);
            assert(isAlive);

            // Add the change from last update
            bt->totalEnergyMake += bt->lastUpdateEnergyMake = resourceInfo.energyMake - bt->lastUpdateEnergyMake;
            bt->totalMetalMake += bt->lastUpdateMetalMake = resourceInfo.metalMake - bt->lastUpdateMetalMake;
            bt->totalEnergyUsage += bt->lastUpdateEnergyUsage = resourceInfo.energyUse - bt->lastUpdateEnergyUsage;
            bt->totalMetalUsage += bt->lastUpdateMetalUsage = resourceInfo.metalUse - bt->lastUpdateMetalUsage;
            energyProduction += bt->lastUpdateEnergyMake;
            metalProduction += bt->lastUpdateMetalMake;
            if(!bt->unitDef->builder)
            {
                // Hmmmm
                energyUsage += bt->lastUpdateEnergyUsage;
                metalUsage += bt->lastUpdateMetalUsage;
            }
        }
    }

    float energy = ai->cb->GetEnergy();
    float metal = ai->cb->GetMetal();
    float deltaEnergy = energy - oldEnergy + constructionEnergy;
    float deltaMetal = metal - oldMetal + constructionMetal;
    myCalcEnergy -= constructionEnergy;
    myCalcMetal -= constructionMetal;
    if(frame % 16 == 0)
    {
        // Add the activeEconomyUnitTrackers change now
        myCalcEnergy += energyProduction - energyUsage;
        myCalcMetal += metalProduction - metalUsage;
        if(myCalcEnergy > ai->cb->GetEnergyStorage())
            myCalcEnergy = ai->cb->GetEnergyStorage();
        if(myCalcMetal > ai->cb->GetMetalStorage())
            myCalcMetal = ai->cb->GetMetalStorage();
        //L("energy:         " << ai->cb->GetEnergy() << ", metal:     " << ai->cb->GetMetal());
        //L("myCalcEnergy: " << myCalcEnergy <<        ", myCalcMetal: " << myCalcMetal);
        //L("energyProduction: " << energyProduction << ", energyUsage: " << energyUsage);
        //L("metalProduction: " << metalProduction << ", metalUsage: " << metalUsage);

        makePrediction(frame + 320 + 160);

    }
    //if(frame % 30 == 0)
    //	//L("energy:         " << ai->cb->GetEnergy() << ", metal:     " << ai->cb->GetMetal());
    ////L("myCalcEnergy: " << myCalcEnergy <<        ", myCalcMetal: " << myCalcMetal);
    if(fabs(deltaEnergy) > 0.001 || fabs(deltaMetal) > 0.001)
        //L("deltaEnergy:  " << deltaEnergy <<         ", deltaMetal:  " << deltaMetal);
        if(frame % 32 == 0)
            //L("constructionEnergy: " << constructionEnergy << ", constructionMetal: " << constructionMetal);
            if(frame % 32 == 0)
            {
                //L("constructionEnergySum: " << constructionEnergySum << ", constructionMetalSum: " << constructionMetalSum);
            }
    oldEnergy = energy;
    oldMetal = metal;
    constructionEnergy = 0;
    constructionMetal = 0;

    //L(frame << ", end");
    //L("");
}