Skip to content

Commit 62228aa

Browse files
ADD: proper sound handling as requested by PR 12 from zverinapavel
1 parent 999db19 commit 62228aa

File tree

1 file changed

+74
-10
lines changed

1 file changed

+74
-10
lines changed

server/uatomic_server.pas

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@
832832
a: Array Of Integer;
833833
b: Boolean;
834834
Begin
835-
log('TServer.HandleSwitchToMapProperties', llTrace);
835+
log(format('TServer.HandleSwitchToMapProperties (UID=%d)', [UID]), llInfo);
836836
aicnt := 0;
837837
pcnt := 0;
838838
t1 := 0;
@@ -1214,16 +1214,79 @@
12141214
Procedure TServer.HandlePlaySoundEffect(PlayerIndex: integer;
12151215
Effect: TSoundEffect);
12161216
Var
1217-
m: TMemoryStream;
1217+
m, m2: TMemoryStream;
1218+
targetUID: Integer;
1219+
i: Integer;
1220+
playerUID: Integer;
12181221
Begin
12191222
log('TServer.HandlePlaySoundEffect', llTrace);
1223+
// Check if player is valid
1224+
If (PlayerIndex < 0) Or (PlayerIndex > high(fPLayer)) Then Begin
1225+
log('Invalid PlayerIndex: ' + inttostr(PlayerIndex), llError);
1226+
LogLeave;
1227+
exit;
1228+
End;
1229+
1230+
playerUID := fPLayer[PlayerIndex].UID;
1231+
1232+
// Skip inactive slots (UID = NoPlayer = 0)
1233+
If playerUID = NoPlayer Then Begin
1234+
log('Skipping sound for inactive player slot: ' + inttostr(PlayerIndex), llTrace);
1235+
LogLeave;
1236+
exit;
1237+
End;
1238+
12201239
m := TMemoryStream.Create;
12211240
m.Write(Effect, sizeof(Effect));
1222-
If Effect = seOtherPlayerDied Then Begin
1223-
SendChunk(miPlaySoundEffekt, m, -fPLayer[PlayerIndex].UID);
1241+
1242+
// Handle AI players (UID = AIPlayer = -1) - send sounds to all other players
1243+
If playerUID = AIPlayer Then Begin
1244+
// For AI players, send sounds to all connected players (broadcast)
1245+
// Use negative UID to indicate broadcast to all except sender (but sender is AI, so send to all)
1246+
If GetActivePlayerCount() > 0 Then Begin
1247+
// Send to all connected players (UID > 0)
1248+
targetUID := 0; // 0 means broadcast to all connected players
1249+
// Actually, we need to send to each connected player individually
1250+
// because SendChunk with negative UID excludes the sender, but we want to include all
1251+
For i := 0 To high(fPLayer) Do Begin
1252+
If fPLayer[i].UID > 0 Then Begin
1253+
// Create a new stream for each player
1254+
m2 := TMemoryStream.Create;
1255+
m2.Write(Effect, sizeof(Effect));
1256+
SendChunk(miPlaySoundEffekt, m2, fPLayer[i].UID);
1257+
End;
1258+
End;
1259+
m.Free; // Free the original stream
1260+
End
1261+
Else Begin
1262+
log('Skipping AI sound - no connected players', llInfo);
1263+
m.Free;
1264+
End;
1265+
End
1266+
Else If playerUID > 0 Then Begin
1267+
// Normal player (UID > 0)
1268+
If Effect = seOtherPlayerDied Then Begin
1269+
// Send to all players except this one (broadcast with negative UID)
1270+
// Only send if there are at least 2 active players
1271+
If GetActivePlayerCount() > 1 Then Begin
1272+
targetUID := -playerUID;
1273+
SendChunk(miPlaySoundEffekt, m, targetUID);
1274+
End
1275+
Else Begin
1276+
log('Skipping seOtherPlayerDied sound - only one player connected', llInfo);
1277+
m.Free;
1278+
End;
1279+
End
1280+
Else Begin
1281+
// Send to this specific player
1282+
targetUID := playerUID;
1283+
SendChunk(miPlaySoundEffekt, m, targetUID);
1284+
End;
12241285
End
12251286
Else Begin
1226-
SendChunk(miPlaySoundEffekt, m, fPLayer[PlayerIndex].UID);
1287+
// Unknown UID value
1288+
log('Unknown player UID: ' + inttostr(playerUID) + ' for player index: ' + inttostr(PlayerIndex), llWarning);
1289+
m.Free;
12271290
End;
12281291
LogLeave;
12291292
End;
@@ -1372,7 +1435,6 @@
13721435
j, i: Integer;
13731436
k: TKeySet;
13741437
Begin
1375-
log('TServer.RefreshAllPlayerStats', llTrace);
13761438
m := TMemoryStream.Create;
13771439
// Einfügen aller Spielerinformationen, dass diese übernommen werden können (z.B. nach Load Game)
13781440
j := length(fPLayer);
@@ -1389,7 +1451,6 @@
13891451
m.WriteAnsiString(fPLayer[i].UserName);
13901452
End;
13911453
SendChunk(miRefreshPlayerStats, m, UID);
1392-
LogLeave;
13931454
End;
13941455

13951456
Procedure TServer.PlayerLeaves(PlayerUid: integer);
@@ -2012,10 +2073,10 @@
20122073
exit;
20132074
End;
20142075
If AiInit() Then Begin
2015-
log(format('Ai "%s" loaded..', [AiVersion()]), llInfo);
2076+
logshow(format('Ai "%s" loaded successfully!', [AiVersion()]), llInfo);
20162077
End
20172078
Else Begin
2018-
log('Failure on Ai load.', llInfo);
2079+
logshow('Failure on Ai load (AiInit returned false).', llError);
20192080
UnLoadAiLib;
20202081
End;
20212082
If fGameState = gsPlaying Then Begin
@@ -2096,7 +2157,10 @@
20962157
// Egal, welcher Speedup, das Spiel wird mit konstanter Rate Aktualisiert
20972158
If fLastClientUpdateTimestamp + UpdateRate <= n Then Begin
20982159
fLastClientUpdateTimestamp := n; // fLastClientUpdateTimestamp + UpdateRate; Verhindern von oben beschriebener Situation
2099-
UpdateAllClients;
2160+
// Only send updates if there are connected clients
2161+
If GetActivePlayerCount() > 0 Then Begin
2162+
UpdateAllClients;
2163+
End;
21002164
End;
21012165
End;
21022166
End;

0 commit comments

Comments
 (0)