コード例 #1
0
ファイル: UnitHandler.cpp プロジェクト: BrainDamage/spring
void CUnitHandler::UnitDestroyed(int unitID) {
	UnitCategory category = ai->ut->GetCategory(unitID);
	const UnitDef* unitDef = ai->cb->GetUnitDef(unitID);

	if (category != CAT_LAST) {
		assert(!ai->GetUnit(unitID)->isDead);
		ai->GetUnit(unitID)->isDead = true;

		AllUnitsByType[unitDef->id].remove(unitID);
		AllUnitsByCat[category].remove(unitID);
		IdleUnitRemove(unitID);
		BuildTaskRemove(unitID);

		if (category == CAT_DEFENCE) {
			ai->dm->RemoveDefense(ai->cb->GetUnitPos(unitID), unitDef);
		}
		if (category == CAT_MMAKER) {
			MMakerRemove(unitID);
		}
		if (category == CAT_FACTORY) {
			FactoryRemove(unitID);
		}

		if (category == CAT_BUILDER) {
			// remove the builder
			for (std::list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
				if ((*i)->builderID == unitID) {
					if ((*i)->buildTaskId)
						BuildTaskRemove(*i);
					if ((*i)->taskPlanId)
						TaskPlanRemove(*i);
					if ((*i)->factoryId)
						FactoryBuilderRemove(*i);

					BuilderTracker* builderTracker = *i;
					BuilderTrackers.erase(i);
					delete builderTracker;
					break;
				}
			}
		}

		if (category == CAT_MEX) {
			MetalExtractorRemove(unitID);
		}
		if (category == CAT_NUKE) {
			NukeSiloRemove(unitID);
		}
	}
}
コード例 #2
0
void CUnitHandler::UnitDestroyed(int unit)
{
	int category = ai->ut->GetCategory(unit);
	const UnitDef* unitDef = ai->cb->GetUnitDef(unit);
	if(category != -1){
		AllUnitsByType[unitDef->id]->remove(unit);
		AllUnitsByCat[category]->remove(unit);
		IdleUnitRemove(unit);
		BuildTaskRemove(unit);
		if(category == CAT_DEFENCE){
			ai->dm->RemoveDefense(ai->cb->GetUnitPos(unit),unitDef);
		}
		if(category == CAT_MMAKER){
			MMakerRemove(unit);
		}
		if(category == CAT_FACTORY)
		{
			FactoryRemove(unit);
		}
		
		if(category == CAT_BUILDER)
		{
			// Remove the builder
			for(list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++){
				if((*i)->builderID == unit)
				{
					if((*i)->buildTaskId)
						BuildTaskRemove(*i);
					if((*i)->taskPlanId)
						TaskPlanRemove(*i);
					if((*i)->factoryId)
						FactoryBuilderRemove(*i);
					BuilderTracker* builderTracker = *i;
					BuilderTrackers.erase(i);
					delete builderTracker; // Test this
					break;
				}
			}
			
		}
	}
}
コード例 #3
0
ファイル: UnitHandler.cpp プロジェクト: BrainDamage/spring
void CUnitHandler::BuildTaskCreate(int id) {
	const UnitDef* newUnitDef = ai->cb->GetUnitDef(id);
	const UnitCategory category = ai->ut->GetCategory(id);
	const float3 pos = ai->cb->GetUnitPos(id);

	if ((!newUnitDef->movedata || category == CAT_DEFENCE) && !newUnitDef->canfly && category != CAT_LAST) {
		// this needs to change, so that it can make more stuff
		if (category >= CAT_LAST) {
			return;
		}

		BuildTask bt;
		bt.id = -1;

		std::list<TaskPlan>::iterator i;

		for (i = TaskPlans[category].begin(); i != TaskPlans[category].end(); i++) {
			if (pos.distance2D(i->pos) < 1.0f && newUnitDef == i->def) {
				bt.category = category;
				bt.id       = id;
				bt.pos      = i->pos;
				bt.def      = newUnitDef;

				std::list<BuilderTracker*> moveList;

				for (std::list<BuilderTracker*>::iterator builder = i->builderTrackers.begin(); builder != i->builderTrackers.end(); builder++) {
					moveList.push_back(*builder);
				}

				for (std::list<BuilderTracker*>::iterator builder = moveList.begin(); builder != moveList.end(); builder++) {
					TaskPlanRemove(*builder);
					BuildTaskAddBuilder(&bt, *builder);
				}

				// there can not be more than one found TaskPlan
				break;
			}
		}

		if (bt.id == -1) {
			// buildtask creation error (can happen if builder manages
			// to restart a dead building, or a human has taken control),
			// make one anyway
			std::stringstream msg;
				msg << "[CUnitHandler::BuildTaskCreate()][frame=" << ai->cb->GetCurrentFrame() << "]\n";
				msg << "\tBuildTask Creation Error for task with ID " << id << "\n";
			ai->GetLogger()->Log(msg.str());

			if (category == CAT_DEFENCE) {
				ai->dm->AddDefense(pos, newUnitDef);
			}

			bt.category = category;
			bt.id = id;
			bt.pos = pos;
			bt.def = newUnitDef;

			// if we have any friendly builders
			for (std::list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
				BuilderTracker* builderTracker = *i;

				// check what builder is doing
				const CCommandQueue* cq = ai->cb->GetCurrentUnitCommands(builderTracker->builderID);

				if (!cq->empty()) {
					Command c = cq->front();

					const bool b0 = (c.id == -newUnitDef->id && c.params[0] == pos.x && c.params[2] == pos.z); // at this pos
					const bool b1 = (c.id == CMD_REPAIR && c.params[0] == id); // at this unit (id)
					const bool b2 = (c.id == CMD_GUARD  && c.params[0] == id); // at this unit (id)
					const bool b3 = b0 || b1 || b2;

					if (b3) {
						if (builderTracker->buildTaskId != 0) {
							BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);

							if (buildTask->builderTrackers.size() > 1) {
								BuildTaskRemove(builderTracker);
							} else {
								// only builder of this thing, and now idle
								BuildTaskRemove(builderTracker);
							}
						}

						if (builderTracker->taskPlanId != 0) {
							GetTaskPlan(builderTracker->taskPlanId);
							TaskPlanRemove(builderTracker);
						}
						if (builderTracker->factoryId != 0) {
							FactoryBuilderRemove(builderTracker);
						}

						// this builder is now free
						if (builderTracker->idleStartFrame == -2) {
							IdleUnitRemove(builderTracker->builderID);
						}

						// add it to this task
						BuildTaskAddBuilder(&bt, builderTracker);

						msg.str("");
							msg << "\tadded builder " << builderTracker->builderID << " to";
							msg << " build-task with ID " << builderTracker->buildTaskId << "\n";
						ai->GetLogger()->Log(msg.str());
					}
				}
			}

			// add the task anyway
			BuildTasks[category].push_back(bt);
		}
		else {
			if (category == CAT_DEFENCE)
				ai->dm->AddDefense(pos,newUnitDef);

			BuildTasks[category].push_back(bt);
		}
	}
}
コード例 #4
0
ファイル: UnitHandler.cpp プロジェクト: BrainDamage/spring
void CUnitHandler::DecodeOrder(BuilderTracker* builderTracker, bool reportError) {
	const int            frame     = ai->cb->GetCurrentFrame();
	const int            builderID = builderTracker->builderID;
	const CCommandQueue* builderQ  = ai->cb->GetCurrentUnitCommands(builderID);

	if (builderQ->size() > 0) {
		// builder has orders
		const Command* c   = &builderQ->front();
		const int      n   = c->params.size();
		const int      cID = c->id;

		if (builderQ->size() == 2 && cID == CMD_MOVE) {
			// it might have a move order before the real order,
			// take command nr. 2 if nr. 1 is a move order
			c = &builderQ->back();
		}

		if (reportError) {
			std::stringstream msg;
				msg << "[CUnitHandler::DecodeOrder()][frame=" << frame << "]\n";
				msg << "\tbuilder " << builderID << " claimed idle, but has";
				msg << " command " << cID << " with " << n << " parameters";
				msg << " (params[0]: " << ((n > 0)? c->params[0]: -1) << ")\n";
			ai->GetLogger()->Log(msg.str());
		}

		if (cID < 0) {
			assert(n >= 3);

			// it's building a unit
			float3 newUnitPos;
			newUnitPos.x = c->params[0];
			newUnitPos.y = c->params[1];
			newUnitPos.z = c->params[2];

			const UnitDef* newUnitDef = ai->ut->unitTypes[-cID].def;
			// make sure that no BuildTasks exists there
			BuildTask* buildTask = BuildTaskExist(newUnitPos, newUnitDef);

			if (buildTask) {
				BuildTaskAddBuilder(buildTask, builderTracker);
			} else {
				// make a new TaskPlan (or join an existing one)
				TaskPlanCreate(builderID, newUnitPos, newUnitDef);
			}
		}

		if (cID == CMD_REPAIR) {
			assert(n >= 1);

			// it's repairing, find the unit being repaired
			int guardingID = int(c->params[0]);
			bool found = false;

			UnitCategory cat = ai->ut->GetCategory(guardingID);
			std::list<BuildTask>::iterator i;

			if (cat == CAT_LAST) {
				return;
			}

			for (i = BuildTasks[cat].begin(); i != BuildTasks[cat].end(); i++) {
				if (i->id == guardingID) {
					// whatever the old order was, update it now
					bool hit = false;
					if (builderTracker->buildTaskId != 0) {
						hit = true;
						// why is this builder idle?
						BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);

						if (buildTask->builderTrackers.size() > 1) {
							BuildTaskRemove(builderTracker);
						} else {
							// only builder of this thing, and now idle?
							BuildTaskRemove(builderTracker);
						}
					}
					if (builderTracker->taskPlanId != 0) {
						assert(!hit);
						hit = true;
						TaskPlanRemove(builderTracker);
					}
					if (builderTracker->factoryId != 0) {
						assert(!hit);
						hit = true;
						FactoryBuilderRemove(builderTracker);
					}

					BuildTask* bt = &*i;
					BuildTaskAddBuilder(bt, builderTracker);
					found = true;
				}
			}
			if (!found) {
				// not found, just make a custom order
				builderTracker->idleStartFrame = -1;
			}
		}
	} else {
		// error: this function needs a builder with orders
		// should not be possible because IdleUnitUpdate()
		// calls us only if a unit's command-queue is NOT
		// empty?
		// assert(false);
		std::stringstream msg;
			msg << "[CUnitHandler::DecodeOrder()][frame=" << frame << "]\n";
			msg << "\tbuilder " << builderID << " should not have an empty queue!\n";
		ai->GetLogger()->Log(msg.str());
	}
}
コード例 #5
0
ファイル: UnitHandler.cpp プロジェクト: BrainDamage/spring
// use this only if the unit does not have any orders at the moment
void CUnitHandler::ClearOrder(BuilderTracker* builderTracker, bool reportError) {
	bool hit = false;

	const int frame        = ai->cb->GetCurrentFrame();
	const int builderID    = builderTracker->builderID;
	const int buildTaskID  = builderTracker->buildTaskId;
	const int factoryID    = builderTracker->factoryId;
	const CCommandQueue* q = ai->cb->GetCurrentUnitCommands(builderID);

	assert(q->empty() || !reportError);

	if (buildTaskID != 0) {
		hit = true;
		BuildTask* buildTask = GetBuildTask(buildTaskID);

		std::stringstream msg;
			msg << "[CUnitHandler::ClearOrder()][frame=" << frame << "]\n";
			msg << "\tbuilder " << builderID << " is reported idle but";
			msg << " still has a build-task with ID " << (buildTaskID) << "\n";
		ai->GetLogger()->Log(msg.str());

		if (buildTask->builderTrackers.size() > 1) {
			BuildTaskRemove(builderTracker);
		} else {
			// only builder of this thing, and now idle
			BuildTaskRemove(builderTracker);
		}
	}

	if (builderTracker->taskPlanId != 0) {
		assert(!hit);
		hit = true;

		const TaskPlan*    taskPlan = GetTaskPlan(builderTracker->taskPlanId);
		const std::string& taskName = taskPlan->def->humanName;

		std::stringstream msg;
			msg << "[CUnitHandler::ClearOrder()][frame=" << frame << "]\n";
			msg << "\tbuilder " << builderID << " is reported idle but";
			msg << " still has a task-plan named \"" << (taskName) << "\"\n";
		ai->GetLogger()->Log(msg.str());

		// mask this build-spot as bad
		ai->dm->MaskBadBuildSpot(taskPlan->pos);

		// TODO: remove all builders from this plan
		if (reportError) {
			std::list<BuilderTracker*> builderTrackers = taskPlan->builderTrackers;
			std::list<BuilderTracker*>::iterator i;

			for (i = builderTrackers.begin(); i != builderTrackers.end(); i++) {
				TaskPlanRemove(*i);
				ai->GetUnit((*i)->builderID)->Stop();
			}
		} else {
			TaskPlanRemove(builderTracker);
		}
	}

	if (factoryID != 0) {
		assert(!hit);
		hit = true;

		std::stringstream msg;
			msg << "[CUnitHandler::ClearOrder()][frame=" << frame << "]\n";
			msg << "\tbuilder " << builderID << " is reported idle but";
			msg << " still has a factory ID of " << factoryID << "\n";
		ai->GetLogger()->Log(msg.str());

		// remove the builder from its job
		FactoryBuilderRemove(builderTracker);
	}

	assert(builderTracker->buildTaskId == 0);
	assert(builderTracker->taskPlanId == 0);
	assert(builderTracker->factoryId == 0);
}
コード例 #6
0
void CUnitHandler::BuildTaskCreate(int id) {
	const UnitDef* newUnitDef = ai->cb->GetUnitDef(id);
	int category = ai->ut->GetCategory(id);
	float3 pos = ai->cb->GetUnitPos(id);

	if ((!newUnitDef->movedata || category == CAT_DEFENCE) && !newUnitDef->canfly && category != -1) {
		// this needs to change, so that it can make more stuff
		if (category == -1)
			return;

		assert(category >= 0);
		assert(category < LASTCATEGORY);

		BuildTask bt;
		bt.id = -1;

		redo:
		for (list<TaskPlan>::iterator i = TaskPlans[category].begin(); i != TaskPlans[category].end(); i++) {
			if(pos.distance2D(i->pos) < 1 && newUnitDef == i->def){
				assert(bt.id == -1); // There can not be more than one TaskPlan that is found;
				bt.category = category;
				bt.id = id;
				bt.pos = i->pos;
				bt.def = newUnitDef;
				list<BuilderTracker*> moveList;

				for (list<BuilderTracker*>::iterator builder = i->builderTrackers.begin(); builder != i->builderTrackers.end(); builder++) {
					moveList.push_back(*builder);
				}

				for (list<BuilderTracker*>::iterator builder = moveList.begin(); builder != moveList.end(); builder++) {
					TaskPlanRemove(*builder);
					BuildTaskAddBuilder(&bt, *builder);
				}

				goto redo;
			}
		}

		if (bt.id == -1) {
			// buildtask creation error (can happen if builder manages
			// to restart a dead building, or a human has taken control),
			// make one anyway
			bt.category = category;
			bt.id = id;

			if (category == CAT_DEFENCE)
				ai->dm->AddDefense(pos,newUnitDef);

			bt.pos = pos;
			bt.def = newUnitDef;
			char text[512];
			sprintf(text, "BuildTask Creation Error: %i", id);
			int num = BuilderTrackers.size();

			if (num == 0) {
				// no friendly builders found
			} else {
				// iterate over the list and find the builders
				for (list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++) {
					BuilderTracker* builderTracker = *i;

					// check what builder is doing
					const CCommandQueue* mycommands = ai->cb->GetCurrentUnitCommands(builderTracker->builderID);
					if (mycommands->size() > 0) {
						Command c = mycommands->front();

						if ((c.id == -newUnitDef->id && c.params[0] == pos.x && c.params[2] == pos.z) // at this pos
							|| (c.id == CMD_REPAIR  && c.params[0] == id)  // at this unit (id)
							|| (c.id == CMD_GUARD  && c.params[0] == id)) // at this unit (id)
						{
							if (builderTracker->buildTaskId != 0) {
								BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);

								if (buildTask->builderTrackers.size() > 1) {
									BuildTaskRemove(builderTracker);
								} else {
									// only builder of this thing, and now idle
									BuildTaskRemove(builderTracker);
								}
							}

							if (builderTracker->taskPlanId != 0) {
								GetTaskPlan(builderTracker->taskPlanId);
								TaskPlanRemove(builderTracker);
							}
							if (builderTracker->factoryId != 0) {
								FactoryBuilderRemove(builderTracker);
							}
							if (builderTracker->customOrderId != 0) {
								builderTracker->customOrderId = 0;
							}

							// this builder is now free
							if (builderTracker->idleStartFrame == -2)
								IdleUnitRemove(builderTracker->builderID);

							// add it to this task
							BuildTaskAddBuilder(&bt, builderTracker);
							sprintf(text, "Added builder %i to buildTaskId %i (human order?)", builderTracker->builderID, builderTracker->buildTaskId);
						}
					}
				}
			}

			// add the task anyway
			BuildTasks[category].push_back(bt);
		}
		else {
			if (category == CAT_DEFENCE)
				ai->dm->AddDefense(pos,newUnitDef);

			BuildTasks[category].push_back(bt);
		}
	}
}
コード例 #7
0
void CUnitHandler::DecodeOrder(BuilderTracker* builderTracker, bool reportError) {
	// take a look and see what it's doing
	const CCommandQueue* mycommands = ai->cb->GetCurrentUnitCommands(builderTracker->builderID);

	if (mycommands->size() > 0) {
		// builder has orders
		const Command* c = &mycommands->front();
		if (mycommands->size() == 2 && c->id == CMD_MOVE) { //&& (c->id == CMD_MOVE || c->id == CMD_RECLAIM))
			// it might have a move order before the real order,
			// take command nr. 2 if nr. 1 is a move order
			c = &mycommands->back();
		}

		if (reportError) {
			char text[512];
			sprintf(text, "builder %i: claimed idle, but has command c->id: %i, c->params[0]: %f", builderTracker->builderID, c->id, c->params[0]);
		}

		if (c->id < 0) {
			// it's building a unit
			float3 newUnitPos;
			newUnitPos.x = c->params[0];
			newUnitPos.y = c->params[1];
			newUnitPos.z = c->params[2];
			// c.id == -newUnitDef->id
			const UnitDef* newUnitDef = ai->ut->unitTypes[-c->id].def;
			// make sure that no BuildTasks exists there
			BuildTask* buildTask = BuildTaskExist(newUnitPos, newUnitDef);

			if (buildTask) {
				BuildTaskAddBuilder(buildTask, builderTracker);
			} else {
				// make a new TaskPlan (or join an existing one)
				TaskPlanCreate(builderTracker->builderID, newUnitPos, newUnitDef);
			}
		}

		if (c->id == CMD_REPAIR) {
			// it's repairing
			int guardingID = int(c->params[0]);
			// find the unit being repaired
			int category = ai->ut->GetCategory(guardingID);
			bool found = false;

			if (category == -1)
				return;

			for (list<BuildTask>::iterator i = BuildTasks[category].begin(); i != BuildTasks[category].end(); i++) {
				if (i->id == guardingID) {
					// whatever the old order was, update it now
					bool hit = false;
					if (builderTracker->buildTaskId != 0) {
						hit = true;
						// why is this builder idle?
						BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);

						if (buildTask->builderTrackers.size() > 1) {
							BuildTaskRemove(builderTracker);
						} else {
							// only builder of this thing, and now idle?
							BuildTaskRemove(builderTracker);
						}
					}
					if (builderTracker->taskPlanId != 0) {
						assert(!hit);
						hit = true;
						TaskPlanRemove(builderTracker);
					}
					if (builderTracker->factoryId != 0) {
						assert(!hit);
						hit = true;
						FactoryBuilderRemove(builderTracker);
					}
					if (builderTracker->customOrderId != 0) {
						assert(!hit);
						hit = true;
						builderTracker->customOrderId = 0;
					}
					BuildTask* bt = &*i;
					BuildTaskAddBuilder(bt, builderTracker);
					found = true;
				}
			}
			if (!found) {
				// not found, just make a custom order
				builderTracker->customOrderId = taskPlanCounter++;
				builderTracker->idleStartFrame = -1;
			}
		}
	}
	else {
		// error: this function needs a builder with orders
		assert(false);
	}
}
コード例 #8
0
// use this only if the unit does not have any orders at the moment
void CUnitHandler::ClearOrder(BuilderTracker* builderTracker, bool reportError) {
	bool hit = false;
	const CCommandQueue* mycommands = ai->cb->GetCurrentUnitCommands(builderTracker->builderID);
	assert(mycommands->empty() || !reportError);

	if (builderTracker->buildTaskId != 0) {
		// why is this builder idle?
		hit = true;
		BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);
		char text[512];
		sprintf(text, "builder %i: was idle, but it is on buildTaskId: %i  (stuck?)", builderTracker->builderID, builderTracker->buildTaskId);

		if (buildTask->builderTrackers.size() > 1) {
			BuildTaskRemove(builderTracker);
		} else {
			// only builder of this thing, and now idle
			BuildTaskRemove(builderTracker);
		}
	}

	if (builderTracker->taskPlanId != 0) {
		assert(!hit);
		hit = true;
		// why is this builder idle?
		TaskPlan* taskPlan = GetTaskPlan(builderTracker->taskPlanId);
		char text[512];
		sprintf(text, "builder %i: was idle, but it is on taskPlanId: %s (masking this spot)", builderTracker->builderID, taskPlan->def->humanName.c_str());

		ai->dm->MaskBadBuildSpot(taskPlan->pos);
		// TODO: fix this, remove all builders from this plan

		if (reportError) {
			list<BuilderTracker*> builderTrackers = taskPlan->builderTrackers;
			for (list<BuilderTracker*>::iterator i = builderTrackers.begin(); i != builderTrackers.end(); i++) {
				TaskPlanRemove(*i);
				ai->MyUnits[(*i)->builderID]->Stop();
			}
		} else {
			TaskPlanRemove(builderTracker);
		}
	}

	if (builderTracker->factoryId != 0) {
		assert(!hit);
		hit = true;
		// why is this builder idle?
		char text[512];
		sprintf(text, "builder %i: was idle, but it is on factoryId: %i (removing the builder from the job)", builderTracker->builderID, builderTracker->factoryId);

		FactoryBuilderRemove(builderTracker);
	}

	if (builderTracker->customOrderId != 0) {
		assert(!hit);
		hit = true;
		// why is this builder idle?
		// no tracking of custom orders yet
		// char text[512];
		// sprintf(text, "builder %i: was idle, but it is on customOrderId: %i (removing the builder from the job)", unit, builderTracker->customOrderId);
		builderTracker->customOrderId = 0;
	}

	assert(builderTracker->buildTaskId == 0);
	assert(builderTracker->taskPlanId == 0);
	assert(builderTracker->factoryId == 0);
	assert(builderTracker->customOrderId == 0);
}
コード例 #9
0
void CUnitHandler::BuildTaskCreate(int id) {
	const UnitDef* newUnitDef = ai->cb->GetUnitDef(id);
	int category = ai->ut->GetCategory(id);
	float3 pos = ai->cb->GetUnitPos(id);
	if((!newUnitDef->movedata  || category == CAT_DEFENCE) && !newUnitDef->canfly && category != -1){ // This thing need to change, so that it can make more stuff
		
		// TODO: Hack fix
		if(category == -1)
			return;
		assert(category >= 0);
		assert(category < LASTCATEGORY);
		
		BuildTask bt;
		bt.id = -1;
		//int killplan;
		//list<TaskPlan>::iterator killplan;
		redo:
		for(list<TaskPlan>::iterator i = TaskPlans[category]->begin(); i != TaskPlans[category]->end(); i++){
			if(pos.distance2D(i->pos) < 1 && newUnitDef == i->def){
				assert(bt.id == -1); // There can not be more than one TaskPlan that is found;
				bt.category = category;
				bt.id = id;
				bt.pos = i->pos;
				bt.def = newUnitDef;
				list<BuilderTracker*> moveList;
				for(list<BuilderTracker*>::iterator builder = i->builderTrackers.begin(); builder != i->builderTrackers.end(); builder++) {
					moveList.push_back(*builder);
					//L("Marking builder " << (*builder)->builderID << " for removal, from plan " << i->def->humanName);
				}
				for(list<BuilderTracker*>::iterator builder = moveList.begin(); builder != moveList.end(); builder++) {
					TaskPlanRemove(*builder);
					BuildTaskAddBuilder(&bt, *builder);
				}
				//bt.builders.push_back(i->builder);
				//killplan = i->builder;
				// This plan is gone now
				// Test it by redoing all:
				goto redo; // This is a temp
			}
		}
		if(bt.id == -1){
			//L("*******BuildTask Creation Error!*********");
			// This can happen.
			// Either a builder manges to restart a dead building, or a human have taken control...
			// Make a BuildTask anyway
			bt.category = category;
			bt.id = id;
			if(category == CAT_DEFENCE)
				ai->dm->AddDefense(pos,newUnitDef);
			bt.pos = pos;
			bt.def = newUnitDef;
			char text[512];
			sprintf(text, "BuildTask Creation Error: %i", id);
			AIHCAddMapPoint amp;
			amp.label = text;
			amp.pos = pos;
			////ai->cb->HandleCommand(&amp);
			// Try to find workers that nearby:
			int num = BuilderTrackers.size();
			
			if(num == 0)
			{
				// Well what now ??? 
				//L("Didnt find any friendly builders");
			} else
			{
				// Iterate over the list and find the builders
				for(list<BuilderTracker*>::iterator i = BuilderTrackers.begin(); i != BuilderTrackers.end(); i++)
				{
					BuilderTracker* builderTracker = *i;
					// Now take a look, and see what its doing:
					const deque<Command>* mycommands = ai->cb->GetCurrentUnitCommands(builderTracker->builderID);
					if(mycommands->size() > 0)
					{
						// It have orders
						Command c = mycommands->front();
						//L("builder: " << builderTracker->builderID);
						//L("c.id: " << c.id);
						//L("c.params[0]: " <<  c.params[0]);
						if( (c.id == -newUnitDef->id && c.params[0] == pos.x && c.params[2] == pos.z) // Its at this pos
							|| (c.id == CMD_REPAIR  && c.params[0] == id)  // Its at this unit (id)
							|| (c.id == CMD_GUARD  && c.params[0] == id) ) // Its at this unit (id)
						{
							// Its making this unit
							// Remove the unit from its current job:
							bool hit = false;
							if(builderTracker->buildTaskId != 0)
							{
								// Hmm, why is this builder idle ???
//								bool hit = true;
								BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);
								if(buildTask->builderTrackers.size() > 1)
								{
									BuildTaskRemove(builderTracker);
								}
								else
								{
									// This is the only builder of this thing, and now its idle...
									BuildTaskRemove(builderTracker); // IS this smart at all ???
								}
							}
							if(builderTracker->taskPlanId != 0)
							{
								assert(!hit);
//								bool hit = true;
								// Hmm, why is this builder idle ???
								// 
//								TaskPlan* taskPlan = GetTaskPlan(builderTracker->taskPlanId);
								TaskPlanRemove(builderTracker);
							}
							if(builderTracker->factoryId != 0)
							{
								assert(!hit);
//								bool hit = true;
								FactoryBuilderRemove(builderTracker);
							}
							if(builderTracker->customOrderId != 0)
							{
								assert(!hit);
//								bool hit = true;
								builderTracker->customOrderId = 0;
							}
							// This builder is now free.
							if(builderTracker->idleStartFrame == -2)
								IdleUnitRemove(builderTracker->builderID); // It was in the idle list
							// Add it to this task
							//L("Added builder " << builderTracker->builderID << " to this new unit buildTask");
							BuildTaskAddBuilder(&bt, builderTracker);
							sprintf(text, "Added builder %i: to buildTaskId: %i (human order?)", builderTracker->builderID, builderTracker->buildTaskId);
							AIHCAddMapPoint amp2;
							amp2.label = text;
							amp2.pos = ai->cb->GetUnitPos(builderTracker->builderID);
							////ai->cb->HandleCommand(&amp2);
						} else
						{
							// This builder have other orders.
						}
						
					} else
					{
						// This builder is without orders (idle)
					}
					
				}
			
			}
			// Add the task anyway
			BuildTasks[category]->push_back(bt);
			
		}
		else{
			if(category == CAT_DEFENCE)
				ai->dm->AddDefense(pos,newUnitDef);
			BuildTasks[category]->push_back(bt);
			//TaskPlanRemove(*killplan); // fix
		}
	}
}
コード例 #10
0
void CUnitHandler::DecodeOrder(BuilderTracker* builderTracker, bool reportError) {
	reportError = reportError;
	// If its without orders then try to find the lost command
	
	// TODO: All of it!!!!!!!!!!!!!!
	// Now take a look, and see what its doing:
	const deque<Command>* mycommands = ai->cb->GetCurrentUnitCommands(builderTracker->builderID);
	if(mycommands->size() > 0)
	{
		// It have orders
		const Command* c = &mycommands->front();
		if(mycommands->size() == 2 && c->id == CMD_MOVE)//&& (c->id == CMD_MOVE || c->id == CMD_RECLAIM))  
		{
			// Hmm, it might have a move order before the real order
			// Take command nr. 2 if nr.1 is a move order
			c = &mycommands->back();
		}
		
		
		//L("idle builder: " << builderTracker->builderID);
		//L("c->id: " << c->id);
		//L("c->params[0]: " <<  c->params[0]);
		char text[512];
		sprintf(text, "builder %i: was clamed idle, but it have a command c->id: %i, c->params[0]: %f", builderTracker->builderID, c->id, c->params[0]);
		AIHCAddMapPoint amp;
		amp.label = text;
		amp.pos = ai->cb->GetUnitPos(builderTracker->builderID);
		////ai->cb->HandleCommand(&amp);
		if(c->id < 0) // Its building a unit
		{
			float3 newUnitPos;
			newUnitPos.x = c->params[0];
			newUnitPos.y = c->params[1];
			newUnitPos.z = c->params[2];
			// c.id == -newUnitDef->id
			const UnitDef* newUnitDef = ai->ut->unittypearray[-c->id].def;
			// Now make shure that no BuildTasks exists there 
			BuildTask* buildTask = BuildTaskExist(newUnitPos, newUnitDef);
			if(buildTask)
			{
				BuildTaskAddBuilder(buildTask, builderTracker);
			}
			else // Make a new TaskPlan (or join an existing one)
				TaskPlanCreate(builderTracker->builderID, newUnitPos, newUnitDef);

		}
		if(c->id == CMD_REPAIR)  // Its repairing    ( || c.id == CMD_GUARD)
		{
			int guardingID = int(c->params[0]);
			// Find the unit its repairng
			int category = ai->ut->GetCategory(guardingID);
			if(category == -1)
				return; // This is bad....
			bool found = false;
			for(list<BuildTask>::iterator i = BuildTasks[category]->begin(); i != BuildTasks[category]->end(); i++){
				if(i->id == guardingID)
				{
					// Whatever the old order was, update it now...
					bool hit = false;
					if(builderTracker->buildTaskId != 0)
					{
						hit = true;
						// Hmm, why is this builder idle ???
						BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);
						if(buildTask->builderTrackers.size() > 1)
						{
							BuildTaskRemove(builderTracker);
						}
						else
						{
							// This is the only builder of this thing, and now its idle...
							BuildTaskRemove(builderTracker); // IS this smart at all ???
						}
					}
					if(builderTracker->taskPlanId != 0)
					{
						assert(!hit);
						hit = true;
						TaskPlanRemove(builderTracker);
						
						//return;
					}
					if(builderTracker->factoryId != 0)
					{
						assert(!hit);
						hit = true;
						FactoryBuilderRemove(builderTracker);
					}
					if(builderTracker->customOrderId != 0)
					{
						assert(!hit);
						hit = true;
						builderTracker->customOrderId = 0;
					}
					BuildTask* bt = &*i;
					//L("Adding builder to BuildTask " << bt->id << ": " << ai->cb->GetUnitDef(bt->id)->humanName);
					BuildTaskAddBuilder(bt, builderTracker);
					found = true;
				}
			}
			if(found == false)
			{
				// Not found, just make a custom order
				//L("Not found, just make a custom order");
				builderTracker->customOrderId = taskPlanCounter++;
				builderTracker->idleStartFrame = -1; // Its in use now
			}
		}
	}
	else
	{
		// Error: this function needs a builder with orders
		//L("Error: this function needs a builder with orders");
		assert(false);
	}
}
コード例 #11
0
/*
Use this only if the unit dont have any orders at the moment
*/
void CUnitHandler::ClearOrder(BuilderTracker* builderTracker, bool reportError)
{
	bool hit = false;
	const deque<Command>* mycommands = ai->cb->GetCurrentUnitCommands(builderTracker->builderID);
	assert(mycommands->empty() || !reportError);
	if(builderTracker->buildTaskId != 0)
	{
		// Hmm, why is this builder idle ???
		hit = true;
		//L("builder " << builderTracker->builderID << " was idle, but it is on buildTaskId : " << builderTracker->buildTaskId);
		BuildTask* buildTask = GetBuildTask(builderTracker->buildTaskId);
		char text[512];
		sprintf(text, "builder %i: was idle, but it is on buildTaskId: %i  (stuck?)", builderTracker->builderID, builderTracker->buildTaskId);
		AIHCAddMapPoint amp;
		amp.label = text;
		amp.pos = buildTask->pos;
		////ai->cb->HandleCommand(&amp);
		if(buildTask->builderTrackers.size() > 1)
		{
			BuildTaskRemove(builderTracker);
		}
		else
		{
			// This is the only builder of this thing, and now its idle...
			BuildTaskRemove(builderTracker); // IS this smart at all ???
		}
		//return;
	}
	if(builderTracker->taskPlanId != 0)
	{
		assert(!hit);
		hit = true;
		// Hmm, why is this builder idle ???
		// 
		TaskPlan* taskPlan = GetTaskPlan(builderTracker->taskPlanId);
		//L("builder " << builderTracker->builderID << " was idle, but it is on taskPlanId : " << taskPlan->def->humanName << " (masking this spot)");
		char text[512];
		sprintf(text, "builder %i: was idle, but it is on taskPlanId: %s (masking this spot)", builderTracker->builderID, taskPlan->def->humanName.c_str());
		AIHCAddMapPoint amp;
		amp.label = text;
		amp.pos = taskPlan->pos;
		////ai->cb->HandleCommand(&amp);
		ai->dm->MaskBadBuildSpot(taskPlan->pos);
		// TODO: fix this:  Remove all builders from this plan.
		if(reportError)
		{
			list<BuilderTracker*> builderTrackers = taskPlan->builderTrackers; // This is a copy of the list
			for(list<BuilderTracker*>::iterator i = builderTrackers.begin(); i != builderTrackers.end(); i++) {
				TaskPlanRemove(*i);
				ai->MyUnits[(*i)->builderID]->Stop(); // Stop the units on the way to this plan
			}
		} else
		{
			TaskPlanRemove(builderTracker);
		}
		//return;
	}
	if(builderTracker->factoryId != 0)
	{
		assert(!hit);
		hit = true;
		// Hmm, why is this builder idle ???
		//L("builder " << builderTracker->builderID << " was idle, but it is on factoryId : " << builderTracker->factoryId) << " (removing the builder from the job)";
		char text[512];
		sprintf(text, "builder %i: was idle, but it is on factoryId: %i (removing the builder from the job)", builderTracker->builderID, builderTracker->factoryId);
		AIHCAddMapPoint amp;
		amp.label = text;
		amp.pos = ai->cb->GetUnitPos(builderTracker->factoryId);
		////ai->cb->HandleCommand(&amp);
		FactoryBuilderRemove(builderTracker);
	}
	if(builderTracker->customOrderId != 0)
	{
		assert(!hit);
		hit = true;
		// Hmm, why is this builder idle ?
		// No tracking of custom orders yet... 
		//L("builder " << builderTracker->builderID << " was idle, but it is on customOrderId : " << builderTracker->customOrderId << " (removing the builder from the job)");
		
		//char text[512];
		//sprintf(text, "builder %i: was idle, but it is on customOrderId: %i (removing the builder from the job)", unit, builderTracker->customOrderId);
		//AIHCAddMapPoint amp;
		//amp.label = text;
		//amp.pos = ai->cb->GetUnitPos(builderTracker->builderID);
		//////ai->cb->HandleCommand(&amp);
		builderTracker->customOrderId = 0;
	}
	assert(builderTracker->buildTaskId == 0);
	assert(builderTracker->taskPlanId == 0);
	assert(builderTracker->factoryId == 0);
	assert(builderTracker->customOrderId == 0);
}