From 10e94e0beea8eb0b157c40cd51ad1d1fc13d1b4c Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 2 Jul 2019 09:33:39 +0300 Subject: [PATCH] Fixed GCC Errors. --- include/corelib.h | 4 +-- source/chatlib.cpp | 68 ++++++++++++++++++++++----------------------- source/manager.cpp | 5 +++- source/waypoint.cpp | 11 ++++---- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/include/corelib.h b/include/corelib.h index 1ef7b27..a1a9a92 100644 --- a/include/corelib.h +++ b/include/corelib.h @@ -1246,7 +1246,7 @@ public: template void assign (const char *fmt, Args ...args) { const size_t size = snprintf (nullptr, 0, fmt, args...); - reserve (size + 1); + reserve (size + 2); // for null-terminator resize (size); snprintf (&m_data[0], size + 1, fmt, args...); @@ -1256,7 +1256,7 @@ public: const size_t size = snprintf (nullptr, 0, fmt, args...) + m_length; const size_t len = m_length; - reserve (size + 1); + reserve (size + 2); // for null-terminator resize (size); snprintf (&m_data[len], size + 1, fmt, args...); diff --git a/source/chatlib.cpp b/source/chatlib.cpp index 48b611d..1a7e189 100644 --- a/source/chatlib.cpp +++ b/source/chatlib.cpp @@ -81,13 +81,13 @@ bool BotUtils::checkKeywords (const String &line, String &reply) { } for (auto &factory : conf.getReplies ()) { - for (auto &keyword : factory.keywords) { + for (const auto &keyword : factory.keywords) { // check is keyword has occurred in message if (line.find (keyword, 0) != String::INVALID_INDEX) { StringArray &usedReplies = factory.usedReplies; - if (usedReplies.length () >= factory.replies.length () / 2) { + if (usedReplies.length () >= factory.replies.length () / 4) { usedReplies.clear (); } @@ -107,7 +107,6 @@ bool BotUtils::checkKeywords (const String &line, String &reply) { if (!replyUsed) { reply.assign (choosenReply); // update final buffer usedReplies.push (choosenReply); // add to ignore list - return true; } } @@ -182,10 +181,10 @@ void Bot::prepareChatMessage (const String &message) { // get roundtime auto getRoundTime = [] (void) -> String { - int roundTimeSecs = static_cast (bots.getRoundEndTime () - game.timebase ()); + auto roundTimeSecs = static_cast (bots.getRoundEndTime () - game.timebase ()); String roundTime; - roundTime.assign ("%02d:%02d", roundTimeSecs / 60, cr::abs (roundTimeSecs) % 60); + roundTime.assign ("%02d:%02d", cr::clamp (roundTimeSecs / 60, 0, 59), cr::clamp (cr::abs (roundTimeSecs % 60), 0, 59)); return cr::move (roundTime); }; @@ -332,12 +331,13 @@ bool Bot::isReplyingToChat (void) { // this function sends reply to a player if (m_sayTextBuffer.entityIndex != -1 && !m_sayTextBuffer.sayText.empty ()) { - String message; // check is time to chat is good if (m_sayTextBuffer.timeNextChat < game.timebase ()) { - if (rng.chance (m_sayTextBuffer.chatProbability + rng.getInt (25, 45)) && checkChatKeywords (message)) { - prepareChatMessage (message); + String replyText; + + if (rng.chance (m_sayTextBuffer.chatProbability + rng.getInt (25, 45)) && checkChatKeywords (replyText)) { + prepareChatMessage (replyText); pushMsgQueue (GAME_MSG_SAY_CMD); m_sayTextBuffer.entityIndex = -1; @@ -355,42 +355,42 @@ bool Bot::isReplyingToChat (void) { void Bot::checkForChat (void) { + // say a text every now and then + if (rng.chance (35) || m_notKilled || !yb_chat.boolean ()) { + return; + } // bot chatting turned on? - if (!m_notKilled && yb_chat.boolean () && m_lastChatTime + 10.0 < game.timebase () && bots.getLastChatTimestamp () + 5.0f < game.timebase () && !isReplyingToChat ()) { + if (m_lastChatTime + 10.0 < game.timebase () && bots.getLastChatTimestamp () + 5.0f < game.timebase () && !isReplyingToChat ()) { auto &chat = conf.getChat (); - // say a text every now and then - if (rng.chance (50)) { - m_lastChatTime = game.timebase (); - bots.setLastChatTimestamp (game.timebase ()); + if (!chat[CHAT_DEAD].empty ()) { + const String &phrase = chat[CHAT_DEAD].random (); + bool sayBufferExists = false; - if (!chat[CHAT_DEAD].empty ()) { - const String &phrase = chat[CHAT_DEAD].random (); - bool sayBufferExists = false; - - // search for last messages, sayed - for (auto &sentence : m_sayTextBuffer.lastUsedSentences) { - if (strncmp (sentence.chars (), phrase.chars (), sentence.length ()) == 0) { - sayBufferExists = true; - break; - } - } - - if (!sayBufferExists) { - prepareChatMessage (phrase); - pushMsgQueue (GAME_MSG_SAY_CMD); - - // add to ignore list - m_sayTextBuffer.lastUsedSentences.push (phrase); + // search for last messages, sayed + for (auto &sentence : m_sayTextBuffer.lastUsedSentences) { + if (strncmp (sentence.chars (), phrase.chars (), sentence.length ()) == 0) { + sayBufferExists = true; + break; } } + if (!sayBufferExists) { + prepareChatMessage (phrase); + pushMsgQueue (GAME_MSG_SAY_CMD); + + m_lastChatTime = game.timebase (); + bots.setLastChatTimestamp (game.timebase ()); - // clear the used line buffer every now and then - if (static_cast (m_sayTextBuffer.lastUsedSentences.length ()) > rng.getInt (4, 6)) { - m_sayTextBuffer.lastUsedSentences.clear (); + // add to ignore list + m_sayTextBuffer.lastUsedSentences.push (phrase); } } + + // clear the used line buffer every now and then + if (static_cast (m_sayTextBuffer.lastUsedSentences.length ()) > rng.getInt (4, 6)) { + m_sayTextBuffer.lastUsedSentences.clear (); + } } } diff --git a/source/manager.cpp b/source/manager.cpp index 810780f..dfc3168 100644 --- a/source/manager.cpp +++ b/source/manager.cpp @@ -460,6 +460,9 @@ void BotManager::reset (void) { m_quotaMaintainTime = 0.0f; m_grenadeUpdateTime = 0.0f; m_entityUpdateTime = 0.0f; + m_plantSearchUpdateTime = 0.0f; + m_lastChatTime = 0.0f; + m_timeBombPlanted = 0.0f; m_intrestingEntities.clear (); m_activeGrenades.clear (); @@ -905,7 +908,7 @@ Bot::Bot (edict_t *bot, int difficulty, int personality, int team, int member, c // assign how talkative this bot will be m_sayTextBuffer.chatDelay = rng.getFloat (3.8f, 10.0f); - m_sayTextBuffer.chatProbability = rng.getInt (1, 100); + m_sayTextBuffer.chatProbability = rng.getInt (10, 100); m_notKilled = false; m_weaponBurstMode = BURST_OFF; diff --git a/source/waypoint.cpp b/source/waypoint.cpp index 8ff2224..df0e389 100644 --- a/source/waypoint.cpp +++ b/source/waypoint.cpp @@ -1,3 +1,4 @@ +// // Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd"). // Copyright (c) YaPB Development Team. // @@ -1275,7 +1276,7 @@ bool Waypoint::saveExtFile (const char *ext, const char *type, const char *magic header.fileVersion = version; header.uncompressed = size; - strncpy (header.header, magic, strlen (magic)); + strncpy (header.header, magic, cr::bufsize (header.header)); auto compressed = new uint8[header.uncompressed]; int compressedLength = lz.compress (reinterpret_cast (data), header.uncompressed, compressed); @@ -1294,9 +1295,8 @@ bool Waypoint::saveExtFile (const char *ext, const char *type, const char *magic dataSaved = true; } else { - util.logEntry (true, LL_ERROR, "Couldn't save %s data (unable to write the file)", type); + util.logEntry (true, LL_ERROR, "Couldn't save %s data (unable to write the file \"%s\")", type, util.format ("%slearned/%s.%s", getDataDirectory (), game.getMapName (), ext)); dataSaved = false; - } } else { @@ -1324,7 +1324,7 @@ bool Waypoint::loadExtFile (const char *ext, const char *type, const char *magic return false; } - if (!!strncmp (header.header, magic, strlen (magic))) { + if (!!strncmp (header.header, magic, cr::bufsize (header.header))) { util.logEntry (true, LL_ERROR, "%s data damaged (bad header '%s')", type, header.header); fp.close (); @@ -1459,7 +1459,7 @@ bool Waypoint::load (void) { return throwError ("%s.pwf - damaged waypoint file (unable to read header)", map); } - if (strncmp (header.header, FH_WAYPOINT, strlen (FH_WAYPOINT)) == 0) { + if (strncmp (header.header, FH_WAYPOINT, cr::bufsize (FH_WAYPOINT)) == 0) { if (header.fileVersion != FV_WAYPOINT) { return throwError ("%s.pwf - incorrect waypoint file version (expected '%d' found '%ld')", map, FV_WAYPOINT, header.fileVersion); } @@ -2555,6 +2555,7 @@ void Waypoint::eraseFromDisk (void) { const char *Waypoint::getDataDirectory (bool isMemoryFile) { static String buffer; + buffer.clear (); if (isMemoryFile) { buffer.assign ("addons/yapb/data/");