@@ -17,39 +17,59 @@ static sp::ecs::Entity script_active_entity;
1717void CommsSystem::update (float delta)
1818{
1919 for (auto [entity, comms] : sp::ecs::Query<CommsTransmitter>()) {
20- if (comms.open_delay > 0 .0f )
21- comms.open_delay -= delta;
22- if (game_server) {
23- if (comms.state == CommsTransmitter::State::OpeningChannel && comms.open_delay <= 0 .0f ) {
24- if (!comms.target ) {
20+ if (comms.open_delay > 0 .0f ) comms.open_delay -= delta;
21+
22+ if (game_server)
23+ {
24+ // If the channel opening delay is expired, determine whether to
25+ // initialize comms with the target.
26+ if (comms.state == CommsTransmitter::State::OpeningChannel && comms.open_delay <= 0 .0f )
27+ {
28+ // Exit with a broken channel if the target no longer exists.
29+ if (!comms.target )
30+ {
2531 comms.state = CommsTransmitter::State::ChannelBroken;
26- }else {
32+ return ;
33+ }
34+ else
35+ {
2736 comms.script_replies .clear ();
2837 comms.script_replies_dirty = true ;
38+
39+ // If the other target is itself a comms transmitter, hail it.
2940 if (auto other_transmitter = comms.target .getComponent <CommsTransmitter>())
3041 {
3142 comms.open_delay = channel_open_time;
3243
33- if (other_transmitter->state == CommsTransmitter::State::Inactive || other_transmitter->state == CommsTransmitter::State::ChannelFailed || other_transmitter->state == CommsTransmitter::State::ChannelBroken || other_transmitter->state == CommsTransmitter::State::ChannelClosed)
44+ if (other_transmitter->state == CommsTransmitter::State::Inactive
45+ || other_transmitter->state == CommsTransmitter::State::ChannelFailed
46+ || other_transmitter->state == CommsTransmitter::State::ChannelBroken
47+ || other_transmitter->state == CommsTransmitter::State::ChannelClosed)
3448 {
3549 other_transmitter->state = CommsTransmitter::State::BeingHailed;
3650 other_transmitter->target = entity;
51+
3752 if (auto callsign = entity.getComponent <CallSign>())
3853 other_transmitter->target_name = callsign->callsign ;
3954 else
4055 other_transmitter->target_name = " ?" ;
4156 }
42- }else if (gameGlobalInfo->intercept_all_comms_to_gm ) {
57+ }
58+ // If all other comms are intercepted by the GM, open a chat
59+ // with the GM.
60+ else if (gameGlobalInfo->intercept_all_comms_to_gm && comms.state != CommsTransmitter::State::ChannelOpen)
4361 comms.state = CommsTransmitter::State::ChannelOpenGM;
44- }else if (openChannel (entity, comms.target )) {
62+ // Otherwise, open a standard comms channel to the target.
63+ else if (openChannel (entity, comms.target ))
4564 comms.state = CommsTransmitter::State::ChannelOpen;
46- } else {
65+ else
4766 comms.state = CommsTransmitter::State::ChannelFailed;
48- }
4967 }
5068 }
69+
5170 if (comms.state == CommsTransmitter::State::ChannelOpen || comms.state == CommsTransmitter::State::ChannelOpenPlayer)
5271 {
72+ // Exit with a broken channel if the target no longer exists.
5373 if (!comms.target )
5474 comms.state = CommsTransmitter::State::ChannelBroken;
5575 }
@@ -349,23 +369,28 @@ bool CommsSystem::openChannel(sp::ecs::Entity player, sp::ecs::Entity target)
349369 player.removeComponent <CommsTransmitterEnvironment>();
350370 transmitter->incomming_message = " ???" ;
351371
372+ // comms_script (comms from file) is prioritized over
373+ // comms_callback (comms from inline function). Scenario authors must clear
374+ // comms_script on an entity with a script in order to use comms_callback.
352375 if (script_name != " " )
353376 {
354377 auto & env = player.addComponent <CommsTransmitterEnvironment>();
355378 env.script_environment = std::make_unique<sp::script::Environment>(gameGlobalInfo->script_environment_base .get ());
356379 setupSubEnvironment (*env.script_environment .get ());
357- // consider "player" deprecated, but keep it for a long time
380+ // Consider "player" deprecated, but keep it for a long time.
358381 env.script_environment ->setGlobal (" player" , player);
359382 env.script_environment ->setGlobal (" comms_source" , player);
360383 env.script_environment ->setGlobal (" comms_target" , target);
361384 i18n::load (" locale/" + script_name.replace (" .lua" , " ." + PreferencesManager::get (" language" , " en" ) + " .po" ));
362385 LuaConsole::checkResult (env.script_environment ->runFile <void >(script_name));
363- }else if (receiver->callback )
386+ }
387+ else if (receiver->callback )
364388 {
365389 receiver->callback .setGlobal (" comms_source" , player);
366390 receiver->callback .setGlobal (" comms_target" , transmitter->target );
367391 LuaConsole::checkResult (receiver->callback .call <void >(player, target));
368392 }
393+
369394 script_active_entity = {};
370395 return transmitter->incomming_message != " ???" ;
371396}
0 commit comments