forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplay.cpp
2010 lines (1703 loc) · 49.7 KB
/
multiplay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Multiplay.c
*
* Alex Lee, Sep97, Pumpkin Studios
*
* Contains the day to day networking stuff, and received message handler.
*/
#include <string.h>
#include "lib/framework/frame.h"
#include "lib/framework/input.h"
#include "lib/framework/strres.h"
#include "map.h"
#include "stats.h" // for templates.
#include "game.h" // for loading maps
#include "hci.h"
#include <time.h> // for recording ping times.
#include "research.h"
#include "display3d.h" // for changing the viewpoint
#include "console.h" // for screen messages
#include "power.h"
#include "cmddroid.h" // for commanddroidupdatekills
#include "wrappers.h" // for game over
#include "component.h"
#include "frontend.h"
#include "lib/sound/audio.h"
#include "lib/sound/audio_id.h"
#include "levels.h"
#include "selection.h"
#include "research.h"
#include "init.h"
#include "warcam.h" // these 4 for fireworks
#include "mission.h"
#include "effects.h"
#include "lib/gamelib/gtime.h"
#include "keybind.h"
#include "qtscript.h"
#include "lib/script/script.h" //Because of "ScriptTabs.h"
#include "scripttabs.h" //because of CALL_AI_MSG
#include "scriptcb.h" //for console callback
#include "scriptfuncs.h"
#include "template.h"
#include "lib/netplay/netplay.h" // the netplay library.
#include "multiplay.h" // warzone net stuff.
#include "multijoin.h" // player management stuff.
#include "multirecv.h" // incoming messages stuff
#include "multistat.h"
#include "multigifts.h" // gifts and alliances.
#include "multiint.h"
#include "keymap.h"
#include "cheat.h"
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// globals.
bool bMultiPlayer = false; // true when more than 1 player.
bool bMultiMessages = false; // == bMultiPlayer unless multimessages are disabled
bool openchannels[MAX_PLAYERS]={true};
UBYTE bDisplayMultiJoiningStatus;
MULTIPLAYERGAME game; //info to describe game.
MULTIPLAYERINGAME ingame;
char beaconReceiveMsg[MAX_PLAYERS][MAX_CONSOLE_STRING_LENGTH]; //beacon msg for each player
char playerName[MAX_PLAYERS][MAX_STR_LENGTH]; //Array to store all player names (humans and AIs)
/////////////////////////////////////
/* multiplayer message stack stuff */
/////////////////////////////////////
#define MAX_MSG_STACK 100 // must be *at least* 64
static char msgStr[MAX_MSG_STACK][MAX_STR_LENGTH];
static SDWORD msgPlFrom[MAX_MSG_STACK];
static SDWORD msgPlTo[MAX_MSG_STACK];
static SDWORD callbackType[MAX_MSG_STACK];
static SDWORD locx[MAX_MSG_STACK];
static SDWORD locy[MAX_MSG_STACK];
static DROID *msgDroid[MAX_MSG_STACK];
static SDWORD msgStackPos = -1; //top element pointer
// ////////////////////////////////////////////////////////////////////////////
// Local Prototypes
static bool recvBeacon(NETQUEUE queue);
static bool recvDestroyTemplate(NETQUEUE queue);
static bool recvResearch(NETQUEUE queue);
bool multiplayPlayersReady (bool bNotifyStatus);
void startMultiplayerGame (void);
// ////////////////////////////////////////////////////////////////////////////
// temporarily disable multiplayer mode.
void turnOffMultiMsg(bool bDoit)
{
if (!bMultiPlayer)
{
return;
}
bMultiMessages = !bDoit;
return;
}
// ////////////////////////////////////////////////////////////////////////////
// throw a party when you win!
bool multiplayerWinSequence(bool firstCall)
{
static Position pos;
Position pos2;
static UDWORD last=0;
float rotAmount;
STRUCTURE *psStruct;
if(firstCall)
{
pos = cameraToHome(selectedPlayer,true); // pan the camera to home if not already doing so
last =0;
// stop all research
CancelAllResearch(selectedPlayer);
// stop all manufacture.
for(psStruct=apsStructLists[selectedPlayer];psStruct;psStruct = psStruct->psNext)
{
if (StructIsFactory(psStruct))
{
if (((FACTORY *)psStruct->pFunctionality)->psSubject)//check if active
{
cancelProduction(psStruct, ModeQueue);
}
}
}
}
// rotate world
if (MissionResUp && !getWarCamStatus())
{
rotAmount = graphicsTimeAdjustedIncrement(MAP_SPIN_RATE / 12);
player.r.y += rotAmount;
}
if(last > gameTime)last= 0;
if((gameTime-last) < 500 ) // only if not done recently.
{
return true;
}
last = gameTime;
if(rand()%3 == 0)
{
pos2=pos;
pos2.x += (rand() % world_coord(8)) - world_coord(4);
pos2.z += (rand() % world_coord(8)) - world_coord(4);
if (pos2.x < 0)
pos2.x = 128;
if ((unsigned)pos2.x > world_coord(mapWidth))
pos2.x = world_coord(mapWidth);
if (pos2.z < 0)
pos2.z = 128;
if ((unsigned)pos2.z > world_coord(mapHeight))
pos2.z = world_coord(mapHeight);
addEffect(&pos2,EFFECT_FIREWORK,FIREWORK_TYPE_LAUNCHER,false,NULL,0); // throw up some fire works.
}
// show the score..
return true;
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// MultiPlayer main game loop code.
bool multiPlayerLoop(void)
{
UDWORD i;
UBYTE joinCount;
joinCount =0;
for(i=0;i<MAX_PLAYERS;i++)
{
if(isHumanPlayer(i) && ingame.JoiningInProgress[i] )
{
joinCount++;
}
}
if(joinCount)
{
setWidgetsStatus(false);
bDisplayMultiJoiningStatus = joinCount; // someone is still joining! say So
// deselect anything selected.
selDroidDeselect(selectedPlayer);
if(keyPressed(KEY_ESC) )// check for cancel
{
bDisplayMultiJoiningStatus = 0;
setWidgetsStatus(true);
setPlayerHasLost(true);
}
}
else //everyone is in the game now!
{
if(bDisplayMultiJoiningStatus)
{
bDisplayMultiJoiningStatus = 0;
setWidgetsStatus(true);
}
if (!ingame.TimeEveryoneIsInGame)
{
ingame.TimeEveryoneIsInGame = gameTime;
debug(LOG_NET, "I have entered the game @ %d", ingame.TimeEveryoneIsInGame );
if (!NetPlay.isHost)
{
debug(LOG_NET, "=== Sending hash to host ===");
sendDataCheck();
}
}
if (NetPlay.bComms)
{
sendPing();
}
// Only have to do this on a true MP game
if (NetPlay.isHost && !ingame.isAllPlayersDataOK && NetPlay.bComms)
{
if (gameTime - ingame.TimeEveryoneIsInGame > GAME_TICKS_PER_SEC * 60)
{
// we waited 60 secs to make sure people didn't bypass the data integrity checks
int index;
for (index=0; index < MAX_PLAYERS; index++)
{
if (ingame.DataIntegrity[index] == false && isHumanPlayer(index) && index != NET_HOST_ONLY)
{
char msg[256] = {'\0'};
sprintf(msg, _("Kicking player %s, because they tried to bypass data integrity check!"), getPlayerName(index));
sendTextMessage(msg, true);
addConsoleMessage(msg, LEFT_JUSTIFY, NOTIFY_MESSAGE);
NETlogEntry(msg, SYNC_FLAG, index);
#ifndef DEBUG
kickPlayer(index, "invalid data!", ERROR_INVALID);
#endif
debug(LOG_WARNING, "Kicking Player %s (%u), they tried to bypass data integrity check!", getPlayerName(index), index);
}
}
ingame.isAllPlayersDataOK = true;
}
}
}
// if player has won then process the win effects...
if(testPlayerHasWon())
{
multiplayerWinSequence(false);
}
return true;
}
// ////////////////////////////////////////////////////////////////////////////
// quikie functions.
// to get droids ...
DROID *IdToDroid(UDWORD id, UDWORD player)
{
if (player == ANYPLAYER)
{
for (int i = 0; i < MAX_PLAYERS; i++)
{
for (DROID *d = apsDroidLists[i]; d; d = d->psNext)
{
if (d->id == id)
{
return d;
}
}
}
}
else if (player < MAX_PLAYERS)
{
for (DROID *d = apsDroidLists[player]; d; d = d->psNext)
{
if (d->id == id)
{
return d;
}
}
}
return NULL;
}
// ////////////////////////////////////////////////////////////////////////////
// find a structure
STRUCTURE *IdToStruct(UDWORD id, UDWORD player)
{
int beginPlayer = 0, endPlayer = MAX_PLAYERS;
if (player != ANYPLAYER)
{
beginPlayer = player;
endPlayer = std::min<int>(player + 1, MAX_PLAYERS);
}
STRUCTURE **lists[2] = {apsStructLists, mission.apsStructLists};
for (int j = 0; j < 2; ++j)
{
for (int i = beginPlayer; i < endPlayer; ++i)
{
for (STRUCTURE *d = lists[j][i]; d; d = d->psNext)
{
if (d->id == id)
{
return d;
}
}
}
}
return NULL;
}
// ////////////////////////////////////////////////////////////////////////////
// find a feature
FEATURE *IdToFeature(UDWORD id, UDWORD player)
{
(void)player; // unused, all features go into player 0
for (FEATURE *d = apsFeatureLists[0]; d; d = d->psNext)
{
if (d->id == id)
{
return d;
}
}
return NULL;
}
// ////////////////////////////////////////////////////////////////////////////
DROID_TEMPLATE *IdToTemplate(UDWORD tempId, UDWORD player)
{
DROID_TEMPLATE *psTempl = NULL;
UDWORD i;
// Check if we know which player this is from, in that case, assume it is a player template
// FIXME: nuke the ANYPLAYER hack
if (player != ANYPLAYER && player < MAX_PLAYERS )
{
for (psTempl = apsDroidTemplates[player]; psTempl && (psTempl->multiPlayerID != tempId); psTempl = psTempl->psNext)
{} // follow templates
if (psTempl)
{
return psTempl;
}
else
{
return NULL;
}
}
// It could be a AI template...or that of another player
for (i = 0; i < MAX_PLAYERS; i++)
{
for (psTempl = apsDroidTemplates[i]; psTempl && psTempl->multiPlayerID != tempId; psTempl = psTempl->psNext)
{} // follow templates
if (psTempl)
{
debug(LOG_NEVER, "Found template ID %d, for player %d, but found it in player's %d list?",tempId, player, i);
return psTempl;
}
}
// no error, since it is possible that we don't have this template defined yet.
return NULL;
}
/////////////////////////////////////////////////////////////////////////////////
// Returns a pointer to base object, given an id and optionally a player.
BASE_OBJECT *IdToPointer(UDWORD id,UDWORD player)
{
DROID *pD;
STRUCTURE *pS;
FEATURE *pF;
// droids.
pD = IdToDroid(id, player);
if (pD)
{
return (BASE_OBJECT*)pD;
}
// structures
pS = IdToStruct(id,player);
if(pS)
{
return (BASE_OBJECT*)pS;
}
// features
pF = IdToFeature(id,player);
if(pF)
{
return (BASE_OBJECT*)pF;
}
return NULL;
}
// ////////////////////////////////////////////////////////////////////////////
// return a players name.
const char* getPlayerName(int player)
{
ASSERT_OR_RETURN(NULL, player < MAX_PLAYERS , "Wrong player index: %u", player);
if (game.type != CAMPAIGN)
{
if (strcmp(playerName[player], "") != 0)
{
return (char*)&playerName[player];
}
}
if (strlen(NetPlay.players[player].name) == 0)
{
// make up a name for this player.
return getPlayerColourName(player);
}
else if (NetPlay.players[player].ai >= 0 && !NetPlay.players[player].allocated)
{
static char names[MAX_PLAYERS][StringSize]; // Must be static, since the getPlayerName() return value is used in tool tips... Long live the widget system.
// Add colour to player name.
sstrcpy(names[player], getPlayerColourName(player));
sstrcat(names[player], "-");
sstrcat(names[player], NetPlay.players[player].name);
return names[player];
}
return NetPlay.players[player].name;
}
bool setPlayerName(int player, const char *sName)
{
ASSERT_OR_RETURN(false, player < MAX_PLAYERS && player >= 0, "Player index (%u) out of range", player);
sstrcpy(playerName[player], sName);
return true;
}
// ////////////////////////////////////////////////////////////////////////////
// to determine human/computer players and responsibilities of each..
bool isHumanPlayer(int player)
{
if (player >= MAX_PLAYERS || player < 0)
{
return false; // obvious, really
}
return NetPlay.players[player].allocated;
}
// returns player responsible for 'player'
int whosResponsible(int player)
{
if (isHumanPlayer(player))
{
return player; // Responsible for him or her self
}
else if (player == selectedPlayer)
{
return player; // We are responsibly for ourselves
}
else
{
return NET_HOST_ONLY; // host responsible for all AIs
}
}
//returns true if selected player is responsible for 'player'
bool myResponsibility(int player)
{
return (whosResponsible(player) == selectedPlayer || whosResponsible(player) == realSelectedPlayer);
}
//returns true if 'player' is responsible for 'playerinquestion'
bool responsibleFor(int player, int playerinquestion)
{
return whosResponsible(playerinquestion) == player;
}
bool canGiveOrdersFor(int player, int playerInQuestion)
{
return playerInQuestion >= 0 && playerInQuestion < MAX_PLAYERS &&
(player == playerInQuestion || responsibleFor(player, playerInQuestion) || getDebugMappingStatus());
}
int scavengerSlot()
{
// Scavengers used to always be in position 7, when scavengers were only supported in less than 8 player maps.
// Scavengers should be in position N in N-player maps, where N ≥ 8.
return MAX(game.maxPlayers, 7);
}
int scavengerPlayer()
{
return game.scavengers? scavengerSlot() : -1;
}
// ////////////////////////////////////////////////////////////////////////////
// probably temporary. Places the camera on the players 1st droid or struct.
Vector3i cameraToHome(UDWORD player,bool scroll)
{
Vector3i res;
UDWORD x,y;
STRUCTURE *psBuilding;
for (psBuilding = apsStructLists[player]; psBuilding && (psBuilding->pStructureType->type != REF_HQ); psBuilding= psBuilding->psNext) {}
if(psBuilding)
{
x= map_coord(psBuilding->pos.x);
y= map_coord(psBuilding->pos.y);
}
else if (apsDroidLists[player]) // or first droid
{
x= map_coord(apsDroidLists[player]->pos.x);
y= map_coord(apsDroidLists[player]->pos.y);
}
else if (apsStructLists[player]) // center on first struct
{
x= map_coord(apsStructLists[player]->pos.x);
y= map_coord(apsStructLists[player]->pos.y);
}
else //or map center.
{
x= mapWidth/2;
y= mapHeight/2;
}
if(scroll)
{
requestRadarTrack(world_coord(x), world_coord(y));
}
else
{
setViewPos(x,y,true);
}
res.x = world_coord(x);
res.y = map_TileHeight(x,y);
res.z = world_coord(y);
return res;
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// Recv Messages. Get a message and dispatch to relevant function.
bool recvMessage(void)
{
NETQUEUE queue;
uint8_t type;
while (NETrecvNet(&queue, &type) || NETrecvGame(&queue, &type)) // for all incoming messages.
{
bool processedMessage1 = false;
bool processedMessage2 = false;
if (queue.queueType == QUEUE_GAME)
{
syncDebug("Processing player %d, message %s", queue.index, messageTypeToString(type));
}
// messages only in game.
if(!ingame.localJoiningInProgress)
{
processedMessage1 = true;
switch(type)
{
case GAME_DROIDINFO: //droid update info
recvDroidInfo(queue);
break;
case NET_TEXTMSG: // simple text message
recvTextMessage(queue);
break;
case NET_DATA_CHECK:
recvDataCheck(queue);
break;
case NET_AITEXTMSG: //multiplayer AI text message
recvTextMessageAI(queue);
break;
case NET_BEACONMSG: //beacon (blip) message
recvBeacon(queue);
break;
case GAME_DROIDDISEMBARK:
recvDroidDisEmbark(queue); //droid has disembarked from a Transporter
break;
case GAME_GIFT: // an alliance gift from one player to another.
recvGift(queue);
break;
case GAME_LASSAT:
recvLasSat(queue);
break;
case GAME_DEBUG_MODE:
recvProcessDebugMappings(queue);
break;
case GAME_DEBUG_ADD_DROID:
recvDroid(queue);
break;
case GAME_DEBUG_ADD_STRUCTURE:
recvBuildFinished(queue);
break;
case GAME_DEBUG_ADD_FEATURE:
recvMultiPlayerFeature(queue);
break;
case GAME_DEBUG_REMOVE_DROID:
recvDestroyDroid(queue);
break;
case GAME_DEBUG_REMOVE_STRUCTURE:
recvDestroyStructure(queue);
break;
case GAME_DEBUG_REMOVE_FEATURE:
recvDestroyFeature(queue);
break;
case GAME_DEBUG_FINISH_RESEARCH:
recvResearch(queue);
break;
default:
processedMessage1 = false;
break;
}
}
// messages usable all the time
processedMessage2 = true;
switch(type)
{
case GAME_TEMPLATE: // new template
recvTemplate(queue);
break;
case GAME_TEMPLATEDEST: // template destroy
recvDestroyTemplate(queue);
break;
case NET_PING: // diagnostic ping msg.
recvPing(queue);
break;
case NET_OPTIONS:
recvOptions(queue);
break;
case NET_PLAYER_DROPPED: // remote player got disconnected
{
uint32_t player_id;
NETbeginDecode(queue, NET_PLAYER_DROPPED);
{
NETuint32_t(&player_id);
}
NETend();
if (whosResponsible(player_id) != queue.index && queue.index != NET_HOST_ONLY)
{
HandleBadParam("NET_PLAYER_DROPPED given incorrect params.", player_id, queue.index);
break;
}
debug(LOG_INFO,"** player %u has dropped!", player_id);
if (NetPlay.players[player_id].allocated)
{
MultiPlayerLeave(player_id); // get rid of their stuff
NET_InitPlayer(player_id, false);
}
NETsetPlayerConnectionStatus(CONNECTIONSTATUS_PLAYER_DROPPED, player_id);
break;
}
case NET_PLAYERRESPONDING: // remote player is now playing
{
uint32_t player_id;
resetReadyStatus(false);
NETbeginDecode(queue, NET_PLAYERRESPONDING);
// the player that has just responded
NETuint32_t(&player_id);
NETend();
if (player_id >= MAX_PLAYERS)
{
debug(LOG_ERROR, "Bad NET_PLAYERRESPONDING received, ID is %d", (int)player_id);
break;
}
// This player is now with us!
ingame.JoiningInProgress[player_id] = false;
break;
}
// FIXME: the next 4 cases might not belong here --check (we got two loops for this)
case NET_COLOURREQUEST:
recvColourRequest(queue);
break;
case NET_POSITIONREQUEST:
recvPositionRequest(queue);
break;
case NET_TEAMREQUEST:
recvTeamRequest(queue);
break;
case NET_READY_REQUEST:
recvReadyRequest(queue);
// if hosting try to start the game if everyone is ready
if(NetPlay.isHost && multiplayPlayersReady(false))
{
startMultiplayerGame();
}
break;
case GAME_ALLIANCE:
recvAlliance(queue, true);
break;
case NET_KICK: // in-game kick message
{
uint32_t player_id;
char reason[MAX_KICK_REASON];
LOBBY_ERROR_TYPES KICK_TYPE = ERROR_NOERROR;
NETbeginDecode(queue, NET_KICK);
NETuint32_t(&player_id);
NETstring(reason, MAX_KICK_REASON);
NETenum(&KICK_TYPE);
NETend();
if (player_id == NET_HOST_ONLY)
{
char buf[250]= {'\0'};
ssprintf(buf, "Player %d (%s : %s) tried to kick %u", (int) queue.index, NetPlay.players[queue.index].name, NetPlay.players[queue.index].IPtextAddress, player_id);
NETlogEntry(buf, SYNC_FLAG, 0);
debug(LOG_ERROR, "%s", buf);
if (NetPlay.isHost)
{
NETplayerKicked((unsigned int) queue.index);
}
break;
}
else if (selectedPlayer == player_id) // we've been told to leave.
{
debug(LOG_ERROR, "You were kicked because %s", reason);
setPlayerHasLost(true);
}
else
{
debug(LOG_NET, "Player %d was kicked: %s", player_id, reason);
NETplayerKicked(player_id);
}
break;
}
case GAME_RESEARCHSTATUS:
recvResearchStatus(queue);
break;
case GAME_STRUCTUREINFO:
recvStructureInfo(queue);
break;
case NET_PLAYER_STATS:
recvMultiStats(queue);
break;
case GAME_PLAYER_LEFT:
recvPlayerLeft(queue);
break;
default:
processedMessage2 = false;
break;
}
if (processedMessage1 && processedMessage2)
{
debug(LOG_ERROR, "Processed %s message twice!", messageTypeToString(type));
}
if (!processedMessage1 && !processedMessage2)
{
debug(LOG_ERROR, "Didn't handle %s message!", messageTypeToString(type));
}
NETpop(queue);
}
return true;
}
void HandleBadParam(const char *msg, const int from, const int actual)
{
char buf[255];
LOBBY_ERROR_TYPES KICK_TYPE = ERROR_INVALID;
ssprintf(buf, "!!>Msg: %s, Actual: %d, Bad: %d", msg, actual, from);
NETlogEntry(buf, SYNC_FLAG, actual);
if (NetPlay.isHost)
{
ssprintf(buf, "Auto kicking player %s, invalid command received.", NetPlay.players[actual].name);
sendTextMessage(buf, true);
kickPlayer(actual, buf, KICK_TYPE);
}
}
// ////////////////////////////////////////////////////////////////////////////
// Research Stuff. Nat games only send the result of research procedures.
bool SendResearch(uint8_t player, uint32_t index, bool trigger)
{
// Send the player that is researching the topic and the topic itself
NETbeginEncode(NETgameQueue(selectedPlayer), GAME_DEBUG_FINISH_RESEARCH);
NETuint8_t(&player);
NETuint32_t(&index);
NETend();
return true;
}
// recv a research topic that is now complete.
static bool recvResearch(NETQUEUE queue)
{
uint8_t player;
uint32_t index;
int i;
PLAYER_RESEARCH *pPlayerRes;
NETbeginDecode(queue, GAME_DEBUG_FINISH_RESEARCH);
NETuint8_t(&player);
NETuint32_t(&index);
NETend();
if (!getDebugMappingStatus() && bMultiPlayer)
{
debug(LOG_WARNING, "Failed to finish research for player %u.", NetPlay.players[queue.index].position);
return false;
}
syncDebug("player%d, index%u", player, index);
if (player >= MAX_PLAYERS || index >= asResearch.size())
{
debug(LOG_ERROR, "Bad GAME_DEBUG_FINISH_RESEARCH received, player is %d, index is %u", (int)player, index);
return false;
}
pPlayerRes = &asPlayerResList[player][index];
syncDebug("research status = %d", pPlayerRes->ResearchStatus & RESBITS);
if (!IsResearchCompleted(pPlayerRes))
{
MakeResearchCompleted(pPlayerRes);
researchResult(index, player, false, NULL, true);
}
// Update allies research accordingly
if (game.type == SKIRMISH)
{
for (i = 0; i < MAX_PLAYERS; i++)
{
if (alliances[i][player] == ALLIANCE_FORMED)
{
pPlayerRes = &asPlayerResList[i][index];
if (!IsResearchCompleted(pPlayerRes))
{
// Do the research for that player
MakeResearchCompleted(pPlayerRes);
researchResult(index, i, false, NULL, true);
}
}
}
}
return true;
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////
// New research stuff, so you can see what others are up to!
// inform others that I'm researching this.
bool sendResearchStatus(STRUCTURE *psBuilding, uint32_t index, uint8_t player, bool bStart)
{
if (!myResponsibility(player) || gameTime < 5)
{
return true;
}
NETbeginEncode(NETgameQueue(selectedPlayer), GAME_RESEARCHSTATUS);
NETuint8_t(&player);
NETbool(&bStart);
// If we know the building researching it then send its ID
if (psBuilding)
{
NETuint32_t(&psBuilding->id);
}
else
{
uint32_t zero = 0;
NETuint32_t(&zero);
}
// Finally the topic in question
NETuint32_t(&index);
NETend();
// Tell UI to remove from the list of available research.
MakeResearchStartedPending(&asPlayerResList[player][index]);
return true;
}
bool recvResearchStatus(NETQUEUE queue)
{
STRUCTURE *psBuilding;
PLAYER_RESEARCH *pPlayerRes;
RESEARCH_FACILITY *psResFacilty;
RESEARCH *pResearch;
uint8_t player;
bool bStart;
uint32_t index, structRef;
NETbeginDecode(queue, GAME_RESEARCHSTATUS);
NETuint8_t(&player);
NETbool(&bStart);
NETuint32_t(&structRef);
NETuint32_t(&index);
NETend();
syncDebug("player%d, bStart%d, structRef%u, index%u", player, bStart, structRef, index);
if (player >= MAX_PLAYERS || index >= asResearch.size())
{
debug(LOG_ERROR, "Bad GAME_RESEARCHSTATUS received, player is %d, index is %u", (int)player, index);
return false;
}
if (!canGiveOrdersFor(queue.index, player))
{
debug(LOG_WARNING, "Droid order for wrong player.");
syncDebug("Wrong player.");
return false;
}
int prevResearchState = 0;
if (aiCheckAlliances(selectedPlayer, player))
{
prevResearchState = intGetResearchState();
}
pPlayerRes = &asPlayerResList[player][index];
// psBuilding may be null if finishing
if (bStart) // Starting research
{
psBuilding = IdToStruct(structRef, player);
// Set that facility to research
if (psBuilding && psBuilding->pFunctionality)
{
psResFacilty = (RESEARCH_FACILITY *) psBuilding->pFunctionality;
popStatusPending(*psResFacilty); // Research is no longer pending, as it's actually starting now.
if (psResFacilty->psSubject)
{
cancelResearch(psBuilding, ModeImmediate);
}
// Set the subject up
pResearch = &asResearch[index];
psResFacilty->psSubject = pResearch;
// Start the research
MakeResearchStarted(pPlayerRes);
psResFacilty->timeStartHold = 0;
}
}
// Finished/cancelled research
else
{
// If they completed the research, we're done