CBaseAI *CAIFactory::CreateAI( CMonster *pMonster ) { CBaseAI *pAI = CreateAI( pMonster->GetAIType() ); if( pAI != NULL ) { pAI->SetAIType( pMonster->GetAIType() ); pAI->SetOwner( pMonster ); } return pAI; }
/* TODO: Check if TileGrid[y][x] is actually avaliable, else dont spawn the creature */ Creature::Creature(uint64 ObjID, Map* pMap, uint16 x, uint16 y, CreatureTemplate* pTemplate) : Unit (ObjID), pTemplate (pTemplate) { Position = Vector2i(x, y); this->pMap = pMap; pMap->AddToTileGrid(this); this->Health = pTemplate->MaxHealth; this->Power = pTemplate->MaxPower; pAI = CreateAI(pTemplate->ScriptName, this); MovementGenerator = new Pathfinder(this); }
AI* AIFactory::CreateAI(unsigned which){ switch(which){ case 0: return CreateAI(); case 1: return CreateAlfaAI(); case 2: return CreateMoveOrderingAlfaAI(); } // fancyesd ai in all other cases // because unsigned it will be more fancy return CreateMoveOrderingAlfaZorbristAI(); }
KAILogic* KAIManager::GetAILogic(int nAIType) { KAILogic* pLogic = NULL; KAIInfo* pInfo = NULL; KAI_TABLE::iterator it = m_AITable.find(nAIType); KGLOG_PROCESS_ERROR(it != m_AITable.end()); pInfo = &it->second; if (!pInfo->pLogic) { pInfo->pLogic = CreateAI(nAIType, pInfo->dwScriptID); } pLogic = pInfo->pLogic; Exit0: return pLogic; }
BOOL KAIManager::LoadAITabFile() { BOOL bResult = false; int nRetCode = false; ITabFile* piAITabFile = NULL; char szFilePath[] = SETTING_DIR"/AIType.tab"; int nHeight = 0; char szColumnName[MAX_PATH] = ""; BOOL bAILevelValid = false; uint64_t u64Key = 0; std::pair<KAI_TABLE::iterator, bool> RetPair; piAITabFile = g_OpenTabFile(szFilePath); if (!piAITabFile) { KGLogPrintf(KGLOG_ERR, "[AI] Unable to open table file \"%s\"\n", szFilePath); goto Exit0; } nHeight = piAITabFile->GetHeight(); KGLOG_PROCESS_ERROR(nHeight > 1); m_nMaxAILevel = 0; for (int nRow = 2; nRow <= nHeight; nRow++) { int nAIType = 0; DWORD dwScriptID = 0; char szScriptPath[MAX_PATH]; KAIInfo AIInfo; nRetCode = piAITabFile->GetInteger(nRow, "AIType", 0, &nAIType); KGLOG_PROCESS_ERROR(nRetCode); KGLOG_PROCESS_ERROR(nAIType >= 0); nRetCode = piAITabFile->GetString(nRow, "ScriptFile", "", szScriptPath, sizeof(szScriptPath)); KGLOG_PROCESS_ERROR(nRetCode); dwScriptID = g_FileNameHash(szScriptPath); KGLOG_PROCESS_ERROR(dwScriptID); AIInfo.dwScriptID = dwScriptID; for (int i = 0; i < countof(AIInfo.nAIOP); ++i) { sprintf(szColumnName, "AIOperation%02d", i + 1); nRetCode = piAITabFile->GetInteger(nRow, szColumnName, 0, &AIInfo.nAIOP[i]); KGLOG_PROCESS_ERROR(nRetCode); } nRetCode = piAITabFile->GetInteger(nRow, "AILevelValid", 0, &bAILevelValid); KGLOG_PROCESS_ERROR(nRetCode > 0); AIInfo.bAILevelValid = bAILevelValid; AIInfo.nAILevel = 0; AIInfo.dwAIGroupID = 0; if (bAILevelValid) { nRetCode = piAITabFile->GetInteger(nRow, "AILevel", 0, &AIInfo.nAILevel); KGLOG_PROCESS_ERROR(nRetCode > 0); nRetCode = piAITabFile->GetInteger(nRow, "AIGroupID", DEFAULT_AI_GROUP_ID, (int*)&AIInfo.dwAIGroupID); KGLOG_PROCESS_ERROR(nRetCode); u64Key = MAKE_INT64(AIInfo.dwAIGroupID, AIInfo.nAILevel); m_AILevelTable[u64Key].push_back(nAIType); if (AIInfo.nAILevel > m_nMaxAILevel) m_nMaxAILevel = AIInfo.nAILevel; } AIInfo.pLogic = NULL; m_AITable[nAIType] = AIInfo; } { int nCount = (int)m_AITable.size(); int nIndex = 1; for (KAI_TABLE::iterator it = m_AITable.begin();it != m_AITable.end(); ++it, ++nIndex) { #if (defined(_MSC_VER) || defined(__ICL)) //WINDOWS cprintf("******************************>: Setup AI scripts : %d/%d\r", nIndex, nCount); #endif it->second.pLogic = CreateAI(it->first, it->second.dwScriptID); } #if (defined(_MSC_VER) || defined(__ICL)) //WINDOWS cprintf("\n"); #endif } bResult = true; Exit0: if (!bResult) { m_AITable.clear(); } KG_COM_RELEASE(piAITabFile); return bResult; }
//need to differentiate between ships and projectiles void CEntitiesFactory::AddComponents(CEntity * const entity, const SEntityParams * params) { if(params->GetType() == EET_SHIP) { /* JSON COMPONENTS */ const rapidjson::Value &ship = m_doc[static_cast<const SShipParams *> (params)->GetShipName().c_str()]; const rapidjson::Value ¶meters = ship.FindMember("parameters")->value; //ship params int16 linearSpeed = static_cast<int16>(parameters["linearSpeed"].GetInt()); int16 angularSpeed = static_cast<int16>(parameters["angularSpeed"].GetInt()); uint16 energy = static_cast<uint16>(parameters["energy"].GetInt()); float energyChargeRate = static_cast<float>(parameters["energyChargeRate"].GetFloat()); int16 hitpoints = static_cast<int16>(parameters["hitpoints"].GetInt()); CCompShipParams * shipParamsComp = new CCompShipParams(entity, linearSpeed, angularSpeed, energy, energyChargeRate, hitpoints); entity->AddComponent(shipParamsComp); //components const rapidjson::Value &components = ship.FindMember("components")->value; for (rapidjson::Value::ConstMemberIterator itr = components.MemberBegin(); itr != components.MemberEnd(); ++itr) { if (!strcmp(itr->name.GetString(), "ai") && static_cast<const SShipParams *>(params)->IsAI()) { CComponent * comp = CreateAI(entity, itr->value["name"].GetString()); entity->AddComponent(comp); //entity already has CCompShipParams, so set it's isAI to true SSetAIMsg setAIMsg(true); entity->ReceiveMessage(setAIMsg); } else if(!strcmp(itr->name.GetString(), "primaryWeapon")) { CComponent * comp = CreateWeapon(entity, 0, itr); entity->AddComponent(comp); } else if(!strcmp(itr->name.GetString(), "secondaryWeapon")) { CComponent * comp = CreateWeapon(entity, 1, itr); entity->AddComponent(comp); } else { CComponent * newComp = CreateComponent(entity, itr); if (newComp != nullptr) { entity->AddComponent(newComp); } } } } else if(params->GetType() == EET_PROJECTILE) { const SProjectileParams * projParams = static_cast<const SProjectileParams *>(params); CCompTransform * transformComp = new CCompTransform(entity, 0, 0, 0); entity->AddComponent(transformComp); Sprite * sprt = new Sprite(projParams->GetImage()); CCompRender * renderComp = new CCompRender(entity, sprt); entity->AddComponent(renderComp); SGetLinSpeedMsg getLinSpeedMsg; entity->ReceiveMessage(getLinSpeedMsg); SGetAngSpeedMsg getAngSpeedMsg; entity->ReceiveMessage(getAngSpeedMsg); CCompProjectileMove * projMoveComp = new CCompProjectileMove(entity, getLinSpeedMsg.GetLinSpeed(), getAngSpeedMsg.GetAngSpeed()); entity->AddComponent(projMoveComp); CCompProjParams * projParamsComp = new CCompProjParams(entity, projParams->GetDamage()); entity->AddComponent(projParamsComp); SSetRotMsg setRotMsg(projParams->GetRot()); entity->ReceiveMessage(setRotMsg); SSetPosMsg setPosMsg(projParams->GetX(), projParams->GetY()); entity->ReceiveMessage(setPosMsg); } else if (params->GetType() == EET_EXPLOSION) { const SExplosionParams * explParams = static_cast<const SExplosionParams *>(params); const rapidjson::Value &explosion = m_doc[static_cast<const SExplosionParams *> (params)->GetExplName().c_str()]; //ship params Image * img = ResourceManager::Instance().LoadImage(explosion["image"].GetString(), 4, 4); img->SetMidHandle(); float lifeTime = static_cast<float>(explosion["lifetime"].GetFloat()); int16 fps = static_cast<int16>(explosion["fps"].GetInt()); Sprite * sprt = new Sprite(img); sprt->SetFrameRange(0, 15); CCompRender * renderComp = new CCompRender(entity, sprt); entity->AddComponent(renderComp); entity->AddComponent(new CCompTransform(entity, explParams->GetX(), explParams->GetY(), explParams->GetRot())); entity->AddComponent(new CCompExplParams(entity, fps, lifeTime)); SSetRotMsg setRotMsg(explParams->GetRot()); entity->ReceiveMessage(setRotMsg); SSetPosMsg setPosMsg(explParams->GetX(), explParams->GetY()); entity->ReceiveMessage(setPosMsg); SSetFPSMsg setFPSMsg(fps); entity->ReceiveMessage(setFPSMsg); } else if (params->GetType() == EET_DECOY) { const SDecoyParams * decoyParams = static_cast<const SDecoyParams *>(params); Sprite * sprt = new Sprite(decoyParams->GetImg()); CCompRender * compRender = new CCompRender(entity, sprt); entity->AddComponent(compRender); CCompTransform * transfComp = new CCompTransform(entity, decoyParams->GetX(), decoyParams->GetY(), decoyParams->GetRot()); entity->AddComponent(transfComp); CCompDecoyParams * decoyComp = new CCompDecoyParams(entity, decoyParams->GetLifeTime(), decoyParams->GetDamage(), decoyParams->GetAttractFactor()); entity->AddComponent(decoyComp); CCompTractorDecoy * tractorDecoyComp = new CCompTractorDecoy(entity, decoyParams->GetLifeTime()); entity->AddComponent(tractorDecoyComp); SSetRotMsg setRotMsg(decoyParams->GetRot()); entity->ReceiveMessage(setRotMsg); SSetPosMsg setPosMsg(decoyParams->GetX(), decoyParams->GetY()); entity->ReceiveMessage(setPosMsg); } else if (params->GetType() == EET_BOT) { const SBotParams * botParams = static_cast<const SBotParams *>(params); Sprite * sprt = new Sprite(botParams->GetImg()); CCompRender * compRender = new CCompRender(entity, sprt); entity->AddComponent(compRender); SSetFPSMsg setFpsMsg(20); compRender->ReceiveMessage(setFpsMsg); CCompTransform * transfComp = new CCompTransform(entity, botParams->GetX(), botParams->GetY(), 0.f); entity->AddComponent(transfComp); CCompBotParams * botParamsComp = new CCompBotParams(entity, botParams->GetLifeTime(), botParams->GetDamage(), botParams->GetSpeed()); entity->AddComponent(botParamsComp); CCompAIBot * botAIComp = new CCompAIBot(entity, botParams->GetLifeTime()); entity->AddComponent(botAIComp); SSetRotMsg setRotMsg(0.f); entity->ReceiveMessage(setRotMsg); SSetPosMsg setPosMsg(botParams->GetX(), botParams->GetY()); entity->ReceiveMessage(setPosMsg); } CCompCollision * colComp = new CCompCollision(entity); entity->AddComponent(colComp); }