Exemple #1
0
CSpringUnit* CSpringGame::CreateUnit(springai::Unit* unit, bool addToVectors) {
    std::map<int, CSpringUnit*>::iterator i = aliveUnits.find(unit->GetUnitId());
    if (i == aliveUnits.end()) {
        CSpringUnit* u = new CSpringUnit(callback, unit, this);
        aliveUnits[unit->GetUnitId()] = u;

        if (addToVectors) {
            if (u->Team() == GetTeamID()) {
                teamUnits.push_back(u);
                friendlyUnits.push_back(u);
            } else if (u->IsAlly(game->GetMyAllyTeam())) {
                friendlyUnits.push_back(u);
            } else {
                enemyUnits.push_back(u);
            }
        }

        return u;
    } else {
        delete unit;
        return i->second;
    }
}
Exemple #2
0
int cpptestai::CCppTestAI::HandleEvent(int topic, const void* data) {

	if ( false == game->Me()->IsRunning() ) {
		return 0;
	}

	switch (topic) {
		case EVENT_INIT: {
			game->Me()->Init();
			break;
		}
		case EVENT_RELEASE: {
			game->Me()->GameEnd();
			break;
		}
		case EVENT_UPDATE: {
			game->Me()->Update();
			break;
		}
		case EVENT_UNIT_GIVEN: {
			struct SUnitGivenEvent* evt = (struct SUnitGivenEvent*) data;
			if(evt->unitId < 0){
				game->SendToConsole("shard-runtime warning: unitgiven evt->unit < 0");
				break;
			}
			CSpringUnit* u = game->GetUnitById(evt->unitId);
			if (!u) {
				u = game->CreateUnit(evt->unitId);
			}

			if (u) {
				// it might not have been given to us! Could have been given to another team
				if(game->GetTeamID() == u->Team()){
					game->Me()->UnitGiven(u);
				}
			} else {
				game->SendToConsole("shard-runtime warning: unitgiven unit NULL");
			}
			break;
		}
		case EVENT_UNIT_CREATED: {
			struct SUnitCreatedEvent* evt = (struct SUnitCreatedEvent*) data;
			if(evt->unit < 0){
				game->SendToConsole("shard-runtime warning: unitcreated evt->unit < 0");
				break;
			}
			CSpringUnit* u = game->CreateUnit(evt->unit);
			if (u) {
				game->Me()->UnitCreated(u);
			} else {
				game->SendToConsole("shard-runtime warning: unitcreated unit NULL");
			}
			break;
		}
		case EVENT_UNIT_FINISHED: {
			struct SUnitFinishedEvent* evt = (struct SUnitFinishedEvent*) data;
			if(evt->unit < 0){
				game->SendToConsole("shard-runtime warning: unitfinished evt->unit < 0");
				break;
			}
			CSpringUnit* u = game->GetUnitById(evt->unit);
			if(u == 0){
				u = game->CreateUnit(evt->unit);
			}
			if(u != 0){
				game->Me()->UnitBuilt(u);
			} else {
				game->SendToConsole("shard-runtime warning: unitfinished unit NULL");
			}
			break;
		}
		case EVENT_UNIT_DESTROYED: {
			struct SUnitDestroyedEvent* evt = (struct SUnitDestroyedEvent*) data;
			if(evt->unit < 0){
				game->SendToConsole("shard-runtime warning: unitdestroyed evt->unit < 0");
				break;
			}
			CSpringUnit* u = game->GetUnitById(evt->unit);
			if(!u) {
				u = game->CreateUnit(evt->unit);
			}
			if(u){
				game->Me()->UnitDead(u);
				game->DestroyUnit(evt->unit);
			} else {
				game->SendToConsole("shard-runtime warning: unitdestroyed unit NULL");
			}
			break;
		}
		case EVENT_ENEMY_DESTROYED: {
			struct SEnemyDestroyedEvent* evt = (struct SEnemyDestroyedEvent*) data;
			if(evt->enemy < 0){
				game->SendToConsole("shard-runtime warning: enemydestroyed evt->unit < 0");
				break;
			}
			CSpringUnit* u = game->GetUnitById(evt->enemy);
			if (!u) {
				u = game->CreateUnit(evt->enemy);
			}
			/*if(u){
				// @TODO: Add enemy dead event
				game->Me()->UnitDead(u);
			}*/
			game->DestroyUnit(evt->enemy);
			break;
		}
		case EVENT_ENEMY_CREATED: {
			struct SEnemyCreatedEvent* evt = (struct SEnemyCreatedEvent*) data;
			game->CreateUnit(evt->enemy);
			break;
		}
		case EVENT_ENEMY_FINISHED: {
			struct SEnemyFinishedEvent* evt = (struct SEnemyFinishedEvent*) data;
			game->CreateUnit(evt->enemy);
			break;
		}
		case EVENT_UNIT_DAMAGED: {
			struct SUnitDamagedEvent* evt = (struct SUnitDamagedEvent*) data;
			if(evt->unit < 0){
				game->SendToConsole("shard-runtime warning: unitdamaged evt->unit < 0");
				break;
			}
			CSpringDamage::Ptr damage(new CSpringDamage(game, callback, evt));

			CSpringUnit* u = game->GetUnitById(evt->unit);
			if (!u) {
				u = game->CreateUnit(evt->unit);
			}

			//attacker is allowed to be -1 if attacker cannot be seen or determined.
			CSpringUnit* a = NULL;
			if (evt->attacker >= 0) {
				a = game->GetUnitById(evt->attacker);
				if (!a) {
					game->CreateUnit(evt->attacker);
				}
			}
			if(u) {
				game->Me()->UnitDamaged(u,a,damage);
			} else {
				if (!u)
					game->SendToConsole("shard-runtime warning: attacked unit not found.");
			}
			break;
		}
		case EVENT_UNIT_IDLE: {
			struct SUnitIdleEvent* evt = (struct SUnitIdleEvent*) data;
			if(evt->unit < 0){
				game->SendToConsole("shard-runtime warning: unitidle evt->unit < 0");
				break;
			}
			CSpringUnit* u = game->GetUnitById(evt->unit);
			if (!u) {
				game->CreateUnit(evt->unit);
			}
			if (u) {
				game->Me()->UnitIdle(u);
			} else {
				game->SendToConsole("shard-runtime warning: unitidle unit was NULL");
			}
			break;
		}
		case EVENT_UNIT_MOVE_FAILED: {
			struct SUnitMoveFailedEvent* evt = (struct SUnitMoveFailedEvent*) data;
			if(evt->unit < 0){
				game->SendToConsole("shard-runtime warning: SUnitMoveFailedEvent evt->unit < 0");
				break;
			}
			CSpringUnit* u = game->GetUnitById(evt->unit);
			if (!u) {
				u = game->CreateUnit(evt->unit);
			}
			if (u) {
				game->Me()->UnitMoveFailed(u);
			} else {
				game->SendToConsole("shard-runtime warning: Did not now about move failed unit");
			}
			break;
		}
		case EVENT_LUA_MESSAGE: {
			struct SLuaMessageEvent* evt = (struct SLuaMessageEvent*) data;
			game->Me()->GameMessage(evt->inData);

			break;
		}
		default: {
			break;
		}
	}

	// signal: everything went OK
	return 0;
}
Exemple #3
0
void CSpringGame::DestroyUnit(int id) {
    CSpringUnit* u = GetUnitById(id);
    if (u) {
        u->SetDead(true);
    }
}