Hooks
static bool BeforeCvGame_GetDiploResponse(CvGame* self, const char* szLeader, const char* szResponse, const Localization::String& strOptionalKey1, const Localization::String& strOptionalKey2, Localization::String& result);static void AfterCvGame_GetDiploResponse(CvGame* self, const char* szLeader, const char* szResponse, const Localization::String& strOptionalKey1, const Localization::String& strOptionalKey2, Localization::String& result);static Localization::String ReplaceCvGame_GetDiploResponse(Localization::String (*original)(CvGame*, const char*, const char*, const Localization::String&, const Localization::String&), CvGame* self, const char* szLeader, const char* szResponse, const Localization::String& strOptionalKey1, const Localization::String& strOptionalKey2);CivRegisterBefore<CivHooks::CvGame_GetDiploResponse>(host, &BeforeCvGame_GetDiploResponse);CivRegisterAfter<CivHooks::CvGame_GetDiploResponse>(host, &AfterCvGame_GetDiploResponse);CivRegisterReplace<CivHooks::CvGame_GetDiploResponse>(host, &ReplaceCvGame_GetDiploResponse);
Code
5433Localization::String CvGame::GetDiploResponse(const char* szLeader, const char* szResponse, const Localization::String& strOptionalKey1, const Localization::String& strOptionalKey2)5434{5435 //cvStopWatch stopWatch("GetDiploResponse");54365437 Localization::String response;54385439 if(m_pDiploResponseQuery == NULL)5440 {5441 //Directly reference Language_en_US is safe here since we're just looking for the tag5442 //and not the actual text.5443 const char* szSQL = "select Tag, Bias from Diplomacy_Responses, Language_en_US where (LeaderType = ? or LeaderType = 'GENERIC') and ResponseType = ? and Tag like Response";5444 m_pDiploResponseQuery = new Database::Results();5445 if(!GC.GetGameDatabase()->Execute(*m_pDiploResponseQuery, szSQL, strlen(szSQL)))5446 {5447 CvAssertMsg(false, "Failed to generate diplo response query.");5448 }5449 }54505451 //This is not the fastest thing out there...5452 //The original design was to select a random text key (of uniform probability) from the set generated by the above SQL statement5453 //(with the random selection actually part of the SQL statement)5454 //5455 //Jon had later decided that he wanted to specify a bias for each diplomacy response such that certain ones would5456 //appear more frequently than others.5457 //To implement this quickly, I convert the discrete distribution into a uniform distribution and select from that.5458 //This implementation generates a ton of strings to store the text keys though and would benefit greatly from a "stack_string"5459 //implementation. For now though, it works, and the code is called so infrequently that it shouldn't be noticeable.5460 //NOTE: Profiled on my machine to take 0.006965 seconds on average to complete.5461 std::vector<string> probabilities;5462 probabilities.reserve(512);54635464 m_pDiploResponseQuery->Bind(1, szLeader);5465 m_pDiploResponseQuery->Bind(2, szResponse);54665467 while(m_pDiploResponseQuery->Step())5468 {5469 const char* szTag = m_pDiploResponseQuery->GetText(0);5470 int bias = m_pDiploResponseQuery->GetInt(1);5471 for(int i = 0; i < bias; i++)5472 {5473 probabilities.push_back(szTag);5474 }5475 }54765477 m_pDiploResponseQuery->Reset();54785479 if(!probabilities.empty())5480 {5481 response = Localization::Lookup(probabilities[getAsyncRandNum(probabilities.size(), "Diplomacy Rand")].c_str());5482 response << strOptionalKey1 << strOptionalKey2;5483 }54845485 if(response.IsEmpty())5486 {5487 char szMessage[256];5488 sprintf_s(szMessage, "Please send Jon this with your last 5 autosaves and what changelist # you're playing. Could not find diplomacy response. Leader - %s, Response - %s", szLeader, szResponse);5489 CvAssertMsg(false, szMessage);5490 }54915492 return response;5493}