Skip to content

Commit

Permalink
Fix #31 Chat Queue
Browse files Browse the repository at this point in the history
Rate limit chat messages to avoid screen spam.
  • Loading branch information
SMUnlimited committed Nov 5, 2023
1 parent 141b94c commit 14850d6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- DEMON HUNTERS will can now sometimes learn Immolation.
- Shredders now replace 10 peons (instead of 8)
- Improvements to worker logic at game start. (jzy-chitong56)
- To reduce chat spam for players, chat messages from the AI are now rate limited to reduce screen spam especially in large player maps.

### Removed
- (DevTools) Old non-working installer has now been removed now we have new version in place.
Expand Down Expand Up @@ -69,6 +70,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fixed harass groups to use a more reasonable number of entries to prevent high processing and memory usage.
- Fixed lead ally being determined to the incorrect player in some cases.
- Fixed night elf worpal blades upgrade had the wrong code (jzy-chitong56)
- Fixed an issue where the AMAI cache was not saving any data (jzy-chitong56)

## [3.2.2] - 2022-10-05

Expand Down
1 change: 1 addition & 0 deletions Jobs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Job id Frequency Function call Condition for initial start time Initial Start ti
BUY_NEUTRAL 10 BuyNeutral(par) false 0 0
BUY_NEUTRAL_HERO 10 BuyNeutralHero(par) false 0 0
TIMED_CHAT 2 Chat(par) false 0 0
CHAT_QUEUE 30 FireChat() true 3 5
EXCHANGE 5 ExchangeJob() force_number > 1 75 90
UPDATE_STRENGTH 10 UpdateStrengthJob() true 50 65
REVEAL_ENEMY 1 RevealEnemy() true 120 135
Expand Down
35 changes: 35 additions & 0 deletions Jobs/CHAT_QUEUE.eai
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#IFDEF GLOBAL
boolean i_locked_chat = false
#ELSE
function FireChat takes nothing returns nothing
local integer lockplayer = -1
if HaveStoredInteger(amaiCache, Int2Str(CHAT_LOCK), "lock") then
set lockplayer = GetStoredInteger(amaiCache, Int2Str(CHAT_LOCK), "lock")
endif
//call Trace("Chat Queue: Player Lock: " + Int2Str(lockplayer) + " AI Player:" + Int2Str(GetAiPlayer()))
if chat_queue_size >= 0 then
call Trace("Chat Queue: Want to chat")
if lockplayer <= -1 then
call Trace("Chat Queue: Taking Lock")
call StoreInteger(amaiCache, Int2Str(CHAT_LOCK), "lock", GetAiPlayer())
call TQAddJob(1 + GetRandomInt(0, 2), CHAT_QUEUE, 0)
return
elseif lockplayer == GetAiPlayer() then
set i_locked_chat = true
call DisplayToPlayer(chat_queue[chat_queue_size] , chat_queue_player[chat_queue_size], chat_queue_importance[chat_queue_size])
set chat_queue_size = chat_queue_size - 1
endif
elseif lockplayer == GetAiPlayer() then
set i_locked_chat = false
call Trace("Chat Queue: Reset Lock")
call FlushStoredInteger(amaiCache, Int2Str(CHAT_LOCK), "lock")
call TQAddJob(16 + GetRandomInt(0, 4), CHAT_QUEUE, 0)
return
endif
if i_locked_chat then
call TQAddJob(5 + GetRandomInt(0, 5), CHAT_QUEUE, 0)
else
call TQAddJob(16 + GetRandomInt(0, 4), CHAT_QUEUE, 0)
endif
endfunction
#ENDIF
39 changes: 31 additions & 8 deletions common.eai
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ globals
real comp_chosen_target_rate = 0.5

boolean balancing = false
texttag debugstrattag = CreateTextTag()
texttag debugcountertag = CreateTextTag()

//==============================================================
// (AMAI) Healing System Globals
Expand Down Expand Up @@ -818,6 +820,10 @@ globals
integer chat_list_length = 0
string chat_race = ""
integer chat_eq = 0
string array chat_queue
boolean array chat_queue_importance
player array chat_queue_player
integer chat_queue_size = -1
constant integer C_STRATEGY = 0

#INCLUDETABLE <ChatEvents.txt> #EFR
Expand Down Expand Up @@ -861,6 +867,7 @@ globals
constant integer RACE_PREF = 4
constant integer USE_SPECIAL_RACES = 5
constant integer PROFILE_SELECTION = 6
constant integer CHAT_LOCK = 7

gamecache amaiCache = InitGameCache("AMAI_AI.w3v")

Expand Down Expand Up @@ -1114,7 +1121,7 @@ endfunction
// Properly clear the game caches to make sure they are empty on use
function InitAMAIGameCache takes nothing returns nothing
//call FlushGameCache(InitGameCache("AMAI_TM.w3v"))
call FlushGameCache(amaiCache)
//call FlushGameCache(amaiCache) Do not flush as breaks further use of the cache
//call FlushGameCache(InitGameCache("AMAI_Com.w3v"))
//call FlushGameCache(InitGameCache("AMAI_Set.w3v"))
//call FlushGameCache(InitGameCache("AMAI_CVM.w3v"))
Expand Down Expand Up @@ -1208,6 +1215,12 @@ function SetDebugTagColor takes texttag tt returns nothing
set c = null
endfunction

function UpdateDebugTextTag takes texttag tt, string s, real fontsize, real posx, real posy returns nothing
call SetTextTagText(tt, s, ( fontsize * 0.023 / 10)) // *0.023/10 creates the correct font size from given real
call SetTextTagPos(tt, posx, posy, 0)
call SetDebugTagColor(tt)
endfunction

function CreateDebugTagLoc takes string s, real fontsize, real posx, real posy, real lifespan, real fadespan returns nothing
local texttag tt = null

Expand Down Expand Up @@ -3015,6 +3028,15 @@ endfunction
//============================================================================
// (AMAI) Chat function
//============================================================================
function QueueChat takes string text, player p, boolean important returns nothing
if (p == GetLocalPlayer() and chat_queue_size <= 100) then
set chat_queue_size = chat_queue_size + 1
set chat_queue[chat_queue_size] = text
set chat_queue_player[chat_queue_size] = p
set chat_queue_importance[chat_queue_size] = important
endif
endfunction

function DisplayChat takes boolean ally, boolean enemy, boolean obs, boolean important returns nothing
local integer i = 0
local integer std_rand = 0
Expand All @@ -3035,7 +3057,7 @@ function DisplayChat takes boolean ally, boolean enemy, boolean obs, boolean imp
set rand = GetRandomInt(0, chat_list_length - 1)
endloop
call SetChatVarsPlayer(p)
call DisplayToPlayer(ApplyChatVars(chat_list[rand]), p, important)
call QueueChat(ApplyChatVars(chat_list[rand]), p, important)
endif
set i = i + 1
endloop
Expand All @@ -3050,8 +3072,11 @@ function Chat takes integer c returns nothing
endif

if c == C_STRATEGY then
call DisplayToAlliesImportant( GetCurrentStrategyReport() + GetCurrentDynamicReport() )
call DisplayToObserversImportant( GetCurrentStrategyReport() + GetCurrentDynamicReport() )
if IsPlayerAlly(ai_player,GetLocalPlayer()) or IsPlayerObserver(GetLocalPlayer()) and chatting then
call QueueChat(GetCurrentStrategyReport() + GetCurrentDynamicReport(), GetLocalPlayer(), true)
endif
//call DisplayToAlliesImportant( GetCurrentStrategyReport() + GetCurrentDynamicReport() )
//call DisplayToObserversImportant( GetCurrentStrategyReport() + GetCurrentDynamicReport() )
else
if not (chatting or chat_important[c]) then
return
Expand Down Expand Up @@ -7024,9 +7049,7 @@ function TQLoop takes nothing returns nothing
local texttag t = null
if debugging then
set t = CreateTextTag()
call SetTextTagText(t, null, 0.1)
call SetTextTagPos(t, GetLocationX(home_location) + 300, GetLocationY(home_location) + 300, 0)
call SetDebugTagColor(t)
call UpdateDebugTextTag(t, null, 24, GetLocationX(home_location) + 300, GetLocationY(home_location) + 300)
call SetTextTagPermanent(t, true)
endif
set tq_time[0] = 0
Expand All @@ -7039,7 +7062,7 @@ function TQLoop takes nothing returns nothing
if tq_length > 0 then
if not TQHandleOnce() then
if debugging then
call SetTextTagText(t, Int2Str(tq_jid[1]), 0.1)
call UpdateDebugTextTag(t, Int2Str(tq_jid[1]), 24, GetLocationX(home_location) + 300, GetLocationY(home_location) + 300)
endif
set i = tq_time[1] - TimerGetElapsed(tq_timer) + 0.05
if i > 0 then
Expand Down
26 changes: 24 additions & 2 deletions races.eai
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,14 @@ function build_sequence_all takes nothing returns nothing
//call Trace("BUILD SEQUENCE: init_strategy")
call init_strategy()
endif
if debugging then
call UpdateDebugTextTag(debugstrattag, "Strat:" + GetCurrentStrategyName(), 18, GetLocationX(home_location) - 300, GetLocationY(home_location) - 300)
call SetTextTagPermanent(debugstrattag, true)
endif

if (x > counter_timer and counter_timer < 0) or reset_counter then
set chosen_counter = DetermineCounterForce()

if last_counter != chosen_counter and (debugging or GetRandomInt(1,5) == 1) then // Only randomly report counter changes to allies not every single time unless debug mode is on
set reportStrategy = true
endif
Expand All @@ -800,11 +805,18 @@ function build_sequence_all takes nothing returns nothing
set counter_timer = 2 // Once fired don't change again until strategy changes
endif
endif
if debugging then
call UpdateDebugTextTag(debugcountertag, "Counter:" + strengthtext[chosen_counter], 18, GetLocationX(home_location) - 600, GetLocationY(home_location) - 600)
call SetTextTagPermanent(debugcountertag, true)
endif

if reportStrategy then
set reportStrategy = false
call DisplayToAlliesImportant( GetCurrentStrategyReport() + GetCurrentDynamicReport())
call DisplayToObserversImportant( GetCurrentStrategyReport() + GetCurrentDynamicReport())
if (IsPlayerObserver(GetLocalPlayer()) or IsPlayerAlly(GetLocalPlayer(), ai_player)) and chatting then
call QueueChat(GetCurrentStrategyReport() + GetCurrentDynamicReport(), GetLocalPlayer(), true)
endif
//call DisplayToAlliesImportant( GetCurrentStrategyReport() + GetCurrentDynamicReport())
//call DisplayToObserversImportant( GetCurrentStrategyReport() + GetCurrentDynamicReport())
endif

set new_tier = GetTier()
Expand Down Expand Up @@ -873,10 +885,20 @@ function main takes nothing returns nothing
call removeNonBeginningStrats()
call display_rp_strat()
set chosen_counter = DetermineCounterForce()
if debugging then
//set debugcountertag = CreateTextTag()
call UpdateDebugTextTag(debugcountertag, "Counter:" + strengthtext[chosen_counter], 18, GetLocationX(home_location) - 600, GetLocationY(home_location) - 600)
call SetTextTagPermanent(debugcountertag, true)
endif
set chosen_strategy = choose_strategy()

set strategy = chosen_strategy
call SetChatVar("Strategy", GetCurrentStrategyName())
if debugging then
//set debugstrattag = CreateTextTag()
call UpdateDebugTextTag(debugstrattag, "Strat:" + GetCurrentStrategyName(), 18, GetLocationX(home_location) - 300, GetLocationY(home_location) - 300)
call SetTextTagPermanent(debugstrattag, true)
endif
call Trace("Strategy chosen")
call AMAI_PickMeleeHero()
if race_towerrush_available then
Expand Down

0 comments on commit 14850d6

Please sign in to comment.