예제 #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!!!!!!!!!");
    }
}
예제 #3
0
void CEconomyTracker::UnitCreated(int unitID) {
	if (trackerOff) {
		return;
	}

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

	const bool iscommander = CUNIT::IsCommander(unitDef);

	if (unitDef == NULL || iscommander || unitDef->canDGun) {
		// ignore the commander
		return;
	}


	EconomyUnitTracker* economyUnitTracker = new EconomyUnitTracker;
	economyUnitTracker->clear();
	economyUnitTracker->economyUnitId = unitID;
	economyUnitTracker->createFrame = -frame;
	economyUnitTracker->alive = true;
	economyUnitTracker->category = GCAT(unitID);
	economyUnitTracker->unitDef = unitDef;

	SetUnitDefDataInTracker(economyUnitTracker);
	underConstructionEconomyUnitTrackers.push_back(economyUnitTracker);


	// find it (slow++)
	int  catIdx = int(CAT_COMM);
	bool found  = false;

	for (; catIdx < int(CAT_LAST); catIdx++ ) {
		std::list<BuildTask>::iterator i;

		for (i = ai->uh->BuildTasks[catIdx].begin(); i != ai->uh->BuildTasks[catIdx].end(); i++) {
			BuildTask bt = *i;

			if (bt.id == unitID) {
				// add this new unit to the list
				BuildingTracker tracker;
				tracker.clear();
				tracker.economyUnitTracker = economyUnitTracker;
				tracker.buildTask = true;
				tracker.category = UnitCategory(catIdx);
				tracker.unitUnderConstruction = unitID;
				allTheBuildingTrackers[catIdx].push_front(tracker);
				found = true;
				break;
			}
		}
	}

	if (!found) {
		// it is made by a factory
		float3 unitPos = ai->cb->GetUnitPos(unitID);
		UnitCategory category = GCAT(unitID);

		for (std::list<Factory>::iterator i = ai->uh->Factories.begin(); i != ai->uh->Factories.end(); i++) {
			Factory factory = *i;
			int factoryId = factory.id;
			// bad, no easy way to get the factory of the unit
			float3 factoryPos = ai->cb->GetUnitPos(factoryId);
			float distance = factoryPos.distance2D(unitPos);

			if (distance < 100.0f) {
				BuildingTracker tracker;
				tracker.clear();
				tracker.economyUnitTracker = economyUnitTracker;
				tracker.category = category;
				tracker.unitUnderConstruction = unitID;
				tracker.factory = factoryId;
				allTheBuildingTrackers[category].push_front(tracker);
				found = true;
				break;
			}
		}
	}

	// unit constructor not found?!
	// assert(found);
}
void CEconomyTracker::UnitCreated(int unit)
{
    if(trackerOff)
        return;
    int frame=ai->cb->GetCurrentFrame();
    if(frame == 0)
    {
        // Ignore the commander
        return;
    }

    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);
    underConstructionEconomyUnitTrackers.push_back(economyUnitTracker);


    //L("Started to make an " << ai->cb->GetUnitDef(unit)->humanName);
    // Find it (slow++)
    bool found = false;
    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;
            if(bt.id == unit)
            {
                // Add this new unit to the list
                BuildingTracker tracker;
                tracker.clear();
                tracker.economyUnitTracker = economyUnitTracker;
                tracker.buildTask = true;
                tracker.category = category;
                tracker.unitUnderConstruction = unit;
                allTheBuildingTrackers[category]->push_front(tracker);
                found = true;
                break;
            }
        }
    }
    if(!found)
    {
        // It is made by a factory
        float3 unitPos = ai->cb->GetUnitPos(unit);
        int category = GCAT(unit);
        for(list<Factory>::iterator i = ai->uh->Factories.begin(); i != ai->uh->Factories.end(); i++) {
            Factory factory = *i;
            int factoryId = factory.id;
            // This is bad, no easy way to get the factory of the unit...
            float3 factoryPos = ai->cb->GetUnitPos(factoryId);
            float distance = factoryPos.distance2D(unitPos);
            //L("factory distance: " << distance);
            if(distance < 100)
            {
                BuildingTracker tracker;
                tracker.clear();
                tracker.economyUnitTracker = economyUnitTracker;
                tracker.category = category;
                tracker.unitUnderConstruction = unit;
                tracker.factory = factoryId;
                allTheBuildingTrackers[category]->push_front(tracker);
                found = true;
                break;
            }
        }
    }
    if(!found) {
        //L("Unit constructor not found !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    }
    /*
    float EProductionNeed = unit->energyCost - ai->cb->GetEnergy();//ai->cb->GetEnergy() - unit->energyCost;
    float EProduction = ai->cb->GetEnergyIncome() - ai->cb->GetEnergyUsage();
    if(EProduction < 1)
    	EProduction = 1;
    ////L("EProductionNeed: " << EProductionNeed);
    ////L("EProduction: " << EProduction);
    float MProductionNeed = unit->metalCost - ai->cb->GetMetal();//ai->cb->GetMetal() - unit->metalCost;
    float MProduction = ai->cb->GetMetalIncome() - ai->cb->GetMetalUsage();
    if(MProduction < 1)
    	MProduction = 1;
    ////L("MProductionNeed: " << MProductionNeed);
    ////L("MProduction: " << MProduction);
    float timeToBuild = max (EProductionNeed / EProduction, MProductionNeed / MProduction);
    if(timeToBuild < 1)
    	timeToBuild = 1; // It can be built right now...  Make something more costly instead?
    ////L("timeToBuild: " << timeToBuild);
    //float METAL2ENERGY_current = ai->cb->GetEnergy() (ai->cb->GetEnergyIncome()*0.9 - ai->cb->GetEnergyUsage())
    //float buildtime = //unit->buildTime / builder->buildSpeed;
    // Take the buildtime, and use it smart.
    // Make a ratio of unit->buildTime / timeToBuild ???:
    // This realy needs to know its creators build speed.
    float buildTimeRatio =  unit->buildTime / timeToBuild;
    ////L("buildTimeRatio: " << buildTimeRatio);
    float buildTimeFix = unit->buildTime / 100;
    */
}