@@ -205,22 +205,17 @@ void Engine::broadcast(unsigned int index, VirtualMachine *sourceScript, bool wa
205205void Engine::broadcastByPtr (Broadcast *broadcast, VirtualMachine *sourceScript, bool wait)
206206{
207207 const std::vector<Script *> &scripts = m_broadcastMap[broadcast];
208- auto &runningBroadcasts = m_runningBroadcastMap[broadcast];
209208
210- for (auto script : scripts) {
211- for (auto &pair : runningBroadcasts) {
212- if (pair.second ->script () == script)
213- pair.first = sourceScript;
214- }
209+ for (const auto &[target, targetScripts] : m_runningScripts) {
210+ for (auto vm : targetScripts) {
211+ auto it = std::find_if (scripts.begin (), scripts.end (), [vm](Script *script) { return vm->script () == script; });
215212
216- if (script == sourceScript->script ())
217- sourceScript->stop (false , !wait); // source script is the broadcast script
213+ if (it != scripts.end () && *it == sourceScript->script ())
214+ sourceScript->stop (false , !wait); // source script is the broadcast script
215+ }
218216 }
219217
220- std::vector<VirtualMachine *> startedScripts = startHats (scripts);
221-
222- for (VirtualMachine *vm : startedScripts)
223- runningBroadcasts.push_back ({ sourceScript, vm });
218+ startHats (scripts);
224219}
225220
226221void Engine::stopScript (VirtualMachine *vm)
@@ -448,20 +443,6 @@ void Engine::runScripts(const TargetScriptMap &scriptMap, TargetScriptMap &globa
448443 break ;
449444 }
450445 }
451-
452- // Remove from m_runningBroadcastMap
453- for (auto &[broadcast, pairs] : m_runningBroadcastMap) {
454- size_t index = 0 ;
455-
456- for (const auto &pair : pairs) {
457- if (pair.second == script) {
458- pairs.erase (pairs.begin () + index);
459- break ;
460- }
461-
462- index++;
463- }
464- }
465446 }
466447 m_scriptsToRemove.clear ();
467448}
@@ -642,16 +623,16 @@ bool Engine::broadcastRunning(unsigned int index, VirtualMachine *sourceScript)
642623
643624bool Engine::broadcastByPtrRunning (Broadcast *broadcast, VirtualMachine *sourceScript)
644625{
645- auto it = m_runningBroadcastMap.find (broadcast);
626+ assert (m_broadcastMap.find (broadcast) != m_broadcastMap.cend ());
627+ const auto &scripts = m_broadcastMap[broadcast];
646628
647- if (it == m_runningBroadcastMap.cend ())
648- return false ;
629+ for (const auto &[target, targetScripts] : m_runningScripts) {
630+ for (auto vm : targetScripts) {
631+ auto it = std::find_if (scripts.begin (), scripts.end (), [vm](Script *script) { return vm->script () == script; });
649632
650- const auto &scripts = it->second ;
651-
652- for (const auto &pair : scripts) {
653- if (pair.first == sourceScript)
654- return true ;
633+ if (it != scripts.end ())
634+ return true ;
635+ }
655636 }
656637
657638 return false ;
@@ -790,13 +771,8 @@ void Engine::addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, Broadc
790771 std::vector<Script *> &scripts = m_broadcastMap[broadcast];
791772 // TODO: Do not allow adding existing scripts
792773 scripts.push_back (m_scripts[whenReceivedBlock].get ());
793- } else {
774+ } else
794775 m_broadcastMap[broadcast] = { m_scripts[whenReceivedBlock].get () };
795-
796- // Create a vector of running scripts for this broadcast
797- // so we don't need to check if it's there
798- m_runningBroadcastMap[broadcast] = {};
799- }
800776}
801777
802778void Engine::addCloneInitScript (std::shared_ptr<Block> hatBlock)
@@ -1238,7 +1214,7 @@ std::vector<VirtualMachine *> Engine::startHats(const std::vector<Script *> &scr
12381214 for (VirtualMachine *vm : runningScripts) {
12391215 vm->reset ();
12401216
1241- // Remove the script from scripts to remove because it's going to run again
1217+ // Remove the script from scripts to remove and running broadcast map because it's going to run again
12421218 m_scriptsToRemove.erase (std::remove (m_scriptsToRemove.begin (), m_scriptsToRemove.end (), vm), m_scriptsToRemove.end ());
12431219 assert (std::find (m_scriptsToRemove.begin (), m_scriptsToRemove.end (), vm) == m_scriptsToRemove.end ());
12441220 }
0 commit comments