Debian Codegraph @

src/network.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "network.h"
20
21 #include "episodes.h"
22 #include "fonthand.h"
23 #include "helptext.h"
24 #include "joystick.h"
25 #include "keyboard.h"
26 #include "mainint.h"
27 #include "nortvars.h"
28 #include "opentyr.h"
29 #include "picload.h"
30 #include "sprite.h"
31 #include "varz.h"
32 #include "video.h"
33
34 #include <assert.h>
35
36 /* HERE BE DRAGONS!
37 *
38 * When I wrote this code I thought it was wonderful... that thought was very
39 * wrong. It works, but good luck understanding how... I don't anymore.
40 *
41 * Hopefully it'll be rewritten some day.
42 */
43
44 #define NET_VERSION 2 // increment whenever networking changes might create incompatibility
45 #define NET_PORT 1333 // UDP
46
47 #define NET_PACKET_SIZE 256
48 #define NET_PACKET_QUEUE 16
49
50 #define NET_RETRY 640 // ticks to wait for packet acknowledgment before resending
51 #define NET_RESEND 320 // ticks to wait before requesting unreceived game packet
52 #define NET_KEEP_ALIVE 1600 // ticks to wait between keep-alive packets
53 #define NET_TIME_OUT 16000 // ticks to wait before considering connection dead
54
55 bool isNetworkGame = false;
56 int network_delay = 1 + 1; // minimum is 1 + 0
57
58 char *network_opponent_host = NULL;
59
60 Uint16 network_player_port = NET_PORT,
61 network_opponent_port = NET_PORT;
62
63 static char empty_string[] = "";
64 char *network_player_name = empty_string,
65 *network_opponent_name = empty_string;
66
67 #ifdef WITH_NETWORK
68 static UDPsocket socket;
69 static IPaddress ip;
70
71 UDPpacket *packet_out_temp;
72 static UDPpacket *packet_temp;
73
74 UDPpacket *packet_in[NET_PACKET_QUEUE] = { NULL },
75 *packet_out[NET_PACKET_QUEUE] = { NULL };
76
77 static Uint16 last_out_sync = 0, queue_in_sync = 0, queue_out_sync = 0, last_ack_sync = 0;
78 static Uint32 last_in_tick = 0, last_out_tick = 0;
79
80 UDPpacket *packet_state_in[NET_PACKET_QUEUE] = { NULL };
81 static UDPpacket *packet_state_in_xor[NET_PACKET_QUEUE] = { NULL };
82 UDPpacket *packet_state_out[NET_PACKET_QUEUE] = { NULL };
83
84 static Uint16 last_state_in_sync = 0, last_state_out_sync = 0;
85 static Uint32 last_state_in_tick = 0;
86
87 static bool net_initialized = false;
88 static bool connected = false, quit = false;
89 #endif
90
91 uint thisPlayerNum = 0; /* Player number on this PC (1 or 2) */
92
93 JE_boolean haltGame = false;
94
95 JE_boolean moveOk;
96
97 /* Special Requests */
98 JE_boolean pauseRequest, skipLevelRequest, helpRequest, nortShipRequest;
99 JE_boolean yourInGameMenuRequest, inGameMenuRequest;
100
101 #ifdef WITH_NETWORK
102 static void packet_copy(UDPpacket *dst, UDPpacket *src)
103 {
104 void *temp = dst->data;
105 memcpy(dst, src, sizeof(*dst));
106 dst->data = temp;
107 memcpy(dst->data, src->data, src->len);
108 }
109
110 static void packets_shift_up(UDPpacket **packet, int max_packets)
111 {
112 if (packet[0])
113 {
114 SDLNet_FreePacket(packet[0]);
115 }
116 for (int i = 0; i < max_packets - 1; i++)
117 {
118 packet[i] = packet[i + 1];
119 }
120 packet[max_packets - 1] = NULL;
121 }
122
123 static void packets_shift_down(UDPpacket **packet, int max_packets)
124 {
125 if (packet[max_packets - 1])
126 {
127 SDLNet_FreePacket(packet[max_packets - 1]);
128 }
129 for (int i = max_packets - 1; i > 0; i--)
130 {
131 packet[i] = packet[i - 1];
132 }
133 packet[0] = NULL;
134 }
135
136 // prepare new packet for sending
137 void network_prepare(Uint16 type)
138 {
139 SDLNet_Write16(type, &packet_out_temp->data[0]);
140 SDLNet_Write16(last_out_sync, &packet_out_temp->data[2]);
141 }
142
143 // send packet but don't expect acknowledgment of delivery
144 static bool network_send_no_ack(int len)
145 {
146 packet_out_temp->len = len;
147
148 if (!SDLNet_UDP_Send(socket, 0, packet_out_temp))
149 {
150 printf("SDLNet_UDP_Send: %s\n", SDL_GetError());
151 return false;
152 }
153
154 return true;
155 }
156
157 // send packet and place it in queue to be acknowledged
158 bool network_send(int len)
159 {
160 bool temp = network_send_no_ack(len);
161
162 Uint16 i = last_out_sync - queue_out_sync;
163 if (i < NET_PACKET_QUEUE)
164 {
165 packet_out[i] = SDLNet_AllocPacket(NET_PACKET_SIZE);
166 packet_copy(packet_out[i], packet_out_temp);
167 }
168 else
169 {
170 // connection is probably bad now
171 fprintf(stderr, "warning: outbound packet queue overflow\n");
172 return false;
173 }
174
175 last_out_sync++;
176
177 if (network_is_sync())
178 last_out_tick = SDL_GetTicks();
179
180 return temp;
181 }
182
183 // send acknowledgment packet
184 static int network_acknowledge(Uint16 sync)
185 {
186 SDLNet_Write16(PACKET_ACKNOWLEDGE, &packet_out_temp->data[0]);
187 SDLNet_Write16(sync, &packet_out_temp->data[2]);
188 network_send_no_ack(4);
189
190 return 0;
191 }
192
193 // activity lately?
194 static bool network_is_alive(void)
195 {
196 return (SDL_GetTicks() - last_in_tick < NET_TIME_OUT || SDL_GetTicks() - last_state_in_tick < NET_TIME_OUT);
197 }
198
199 // poll for new packets received, check that connection is alive, resend queued packets if necessary
200 int network_check(void)
201 {
202 if (!net_initialized)
203 return -1;
204
205 if (connected)
206 {
207 // timeout
208 if (!network_is_alive())
209 {
210 if (!quit)
211 network_tyrian_halt(2, false);
212 }
213
214 // keep-alive
215 static Uint32 keep_alive_tick = 0;
216 if (SDL_GetTicks() - keep_alive_tick > NET_KEEP_ALIVE)
217 {
218 network_prepare(PACKET_KEEP_ALIVE);
219 network_send_no_ack(4);
220
221 keep_alive_tick = SDL_GetTicks();
222 }
223 }
224
225 // retry
226 if (packet_out[0] && SDL_GetTicks() - last_out_tick > NET_RETRY)
227 {
228 if (!SDLNet_UDP_Send(socket, 0, packet_out[0]))
229 {
230 printf("SDLNet_UDP_Send: %s\n", SDL_GetError());
231 return -1;
232 }
233
234 last_out_tick = SDL_GetTicks();
235 }
236
237 switch (SDLNet_UDP_Recv(socket, packet_temp))
238 {
239 case -1:
240 printf("SDLNet_UDP_Recv: %s\n", SDL_GetError());
241 return -1;
242 break;
243 case 0:
244 break;
245 default:
246 if (packet_temp->channel == 0 && packet_temp->len >= 4)
247 {
248 switch (SDLNet_Read16(&packet_temp->data[0]))
249 {
250 case PACKET_ACKNOWLEDGE:
251 if ((Uint16)(SDLNet_Read16(&packet_temp->data[2]) - last_ack_sync) < NET_PACKET_QUEUE)
252 {
253 last_ack_sync = SDLNet_Read16(&packet_temp->data[2]);
254 }
255
256 {
257 Uint16 i = SDLNet_Read16(&packet_temp->data[2]) - queue_out_sync;
258 if (i < NET_PACKET_QUEUE)
259 {
260 if (packet_out[i])
261 {
262 SDLNet_FreePacket(packet_out[i]);
263 packet_out[i] = NULL;
264 }
265 }
266 }
267
268 // remove acknowledged packets from queue
269 while (packet_out[0] == NULL && (Uint16)(last_ack_sync - queue_out_sync) < NET_PACKET_QUEUE)
270 {
271 packets_shift_up(packet_out, NET_PACKET_QUEUE);
272
273 queue_out_sync++;
274 }
275
276 last_in_tick = SDL_GetTicks();
277 break;
278
279 case PACKET_CONNECT:
280 queue_in_sync = SDLNet_Read16(&packet_temp->data[2]);
281
282 for (int i = 0; i < NET_PACKET_QUEUE; i++)
283 {
284 if (packet_in[i])
285 {
286 SDLNet_FreePacket(packet_in[i]);
287 packet_in[i] = NULL;
288 }
289 }
290 // fall through
291
292 case PACKET_DETAILS:
293 case PACKET_WAITING:
294 case PACKET_BUSY:
295 case PACKET_GAME_QUIT:
296 case PACKET_GAME_PAUSE:
297 case PACKET_GAME_MENU:
298 {
299 Uint16 i = SDLNet_Read16(&packet_temp->data[2]) - queue_in_sync;
300 if (i < NET_PACKET_QUEUE)
301 {
302 if (packet_in[i] == NULL)
303 packet_in[i] = SDLNet_AllocPacket(NET_PACKET_SIZE);
304 packet_copy(packet_in[i], packet_temp);
305 }
306 else
307 {
308 // inbound packet queue overflow/underflow
309 // under normal circumstances, this is okay
310 }
311 }
312
313 network_acknowledge(SDLNet_Read16(&packet_temp->data[2]));
314 // fall through
315
316 case PACKET_KEEP_ALIVE:
317 last_in_tick = SDL_GetTicks();
318 break;
319
320 case PACKET_QUIT:
321 if (!quit)
322 {
323 network_prepare(PACKET_QUIT);
324 network_send(4); // PACKET_QUIT
325 }
326
327 network_acknowledge(SDLNet_Read16(&packet_temp->data[2]));
328
329 if (!quit)
330 network_tyrian_halt(1, true);
331 break;
332
333 case PACKET_STATE:
334 // place packet in queue if within limits
335 {
336 Uint16 i = SDLNet_Read16(&packet_temp->data[2]) - last_state_in_sync + 1;
337 if (i < NET_PACKET_QUEUE)
338 {
339 if (packet_state_in[i] == NULL)
340 packet_state_in[i] = SDLNet_AllocPacket(NET_PACKET_SIZE);
341 packet_copy(packet_state_in[i], packet_temp);
342 }
343 }
344 break;
345
346 case PACKET_STATE_XOR:
347 // place packet in queue if within limits
348 {
349 Uint16 i = SDLNet_Read16(&packet_temp->data[2]) - last_state_in_sync + 1;
350 if (i < NET_PACKET_QUEUE)
351 {
352 if (packet_state_in_xor[i] == NULL)
353 {
354 packet_state_in_xor[i] = SDLNet_AllocPacket(NET_PACKET_SIZE);
355 packet_copy(packet_state_in_xor[i], packet_temp);
356 }
357 else if (SDLNet_Read16(&packet_state_in_xor[i]->data[0]) != PACKET_STATE_XOR)
358 {
359 for (int j = 4; j < packet_state_in_xor[i]->len; j++)
360 packet_state_in_xor[i]->data[j] ^= packet_temp->data[j];
361 SDLNet_Write16(PACKET_STATE_XOR, &packet_state_in_xor[i]->data[0]);
362 }
363 }
364 }
365 break;
366
367 case PACKET_STATE_RESEND:
368 // resend requested state packet if still available
369 {
370 Uint16 i = last_state_out_sync - SDLNet_Read16(&packet_temp->data[2]);
371 if (i > 0 && i < NET_PACKET_QUEUE)
372 {
373 if (packet_state_out[i])
374 {
375 if (!SDLNet_UDP_Send(socket, 0, packet_state_out[i]))
376 {
377 printf("SDLNet_UDP_Send: %s\n", SDL_GetError());
378 return -1;
379 }
380 }
381 }
382 }
383 break;
384
385 default:
386 fprintf(stderr, "warning: bad packet %d received\n", SDLNet_Read16(&packet_temp->data[0]));
387 return 0;
388 break;
389 }
390
391 return 1;
392 }
393 break;
394 }
395
396 return 0;
397 }
398
399 // discard working packet, now processing next packet in queue
400 bool network_update(void)
401 {
402 if (packet_in[0])
403 {
404 packets_shift_up(packet_in, NET_PACKET_QUEUE);
405
406 queue_in_sync++;
407
408 return true;
409 }
410
411 return false;
412 }
413
414 // has opponent gotten all the packets we've sent?
415 bool network_is_sync(void)
416 {
417 return (queue_out_sync - last_ack_sync == 1);
418 }
419
420 // prepare new state for sending
421 void network_state_prepare(void)
422 {
423 if (packet_state_out[0])
424 {
425 fprintf(stderr, "warning: state packet overwritten (previous packet remains unsent)\n");
426 }
427 else
428 {
429 packet_state_out[0] = SDLNet_AllocPacket(NET_PACKET_SIZE);
430 packet_state_out[0]->len = 28;
431 }
432
433 SDLNet_Write16(PACKET_STATE, &packet_state_out[0]->data[0]);
434 SDLNet_Write16(last_state_out_sync, &packet_state_out[0]->data[2]);
435 memset(&packet_state_out[0]->data[4], 0, 28 - 4);
436 }
437
438 // send state packet, xor packet if applicable
439 int network_state_send(void)
440 {
441 if (!SDLNet_UDP_Send(socket, 0, packet_state_out[0]))
442 {
443 printf("SDLNet_UDP_Send: %s\n", SDL_GetError());
444 return -1;
445 }
446
447 // send xor of last network_delay packets
448 if (network_delay > 1 && (last_state_out_sync + 1) % network_delay == 0 && packet_state_out[network_delay - 1] != NULL)
449 {
450 packet_copy(packet_temp, packet_state_out[0]);
451 SDLNet_Write16(PACKET_STATE_XOR, &packet_temp->data[0]);
452 for (int i = 1; i < network_delay; i++)
453 for (int j = 4; j < packet_temp->len; j++)
454 packet_temp->data[j] ^= packet_state_out[i]->data[j];
455
456 if (!SDLNet_UDP_Send(socket, 0, packet_temp))
457 {
458 printf("SDLNet_UDP_Send: %s\n", SDL_GetError());
459 return -1;
460 }
461 }
462
463 packets_shift_down(packet_state_out, NET_PACKET_QUEUE);
464
465 last_state_out_sync++;
466
467 return 0;
468 }
469
470 // receive state packet, wait until received
471 bool network_state_update(void)
472 {
473 if (network_state_is_reset())
474 {
475 return 0;
476 }
477 else
478 {
479 packets_shift_up(packet_state_in, NET_PACKET_QUEUE);
480
481 packets_shift_up(packet_state_in_xor, NET_PACKET_QUEUE);
482
483 last_state_in_sync++;
484
485 // current xor packet index
486 int x = network_delay - (last_state_in_sync - 1) % network_delay - 1;
487
488 // loop until needed packet is available
489 while (!packet_state_in[0])
490 {
491 // xor the packet from thin air, if possible
492 if (packet_state_in_xor[x] && SDLNet_Read16(&packet_state_in_xor[x]->data[0]) == PACKET_STATE_XOR)
493 {
494 // check for all other required packets
495 bool okay = true;
496 for (int i = 1; i <= x; i++)
497 {
498 if (packet_state_in[i] == NULL)
499 {
500 okay = false;
501 break;
502 }
503 }
504 if (okay)
505 {
506 packet_state_in[0] = SDLNet_AllocPacket(NET_PACKET_SIZE);
507 packet_copy(packet_state_in[0], packet_state_in_xor[x]);
508 for (int i = 1; i <= x; i++)
509 for (int j = 4; j < packet_state_in[0]->len; j++)
510 packet_state_in[0]->data[j] ^= packet_state_in[i]->data[j];
511 break;
512 }
513 }
514
515 static Uint32 resend_tick = 0;
516 if (SDL_GetTicks() - last_state_in_tick > NET_RESEND && SDL_GetTicks() - resend_tick > NET_RESEND)
517 {
518 SDLNet_Write16(PACKET_STATE_RESEND, &packet_out_temp->data[0]);
519 SDLNet_Write16(last_state_in_sync - 1, &packet_out_temp->data[2]);
520 network_send_no_ack(4); // PACKET_RESEND
521
522 resend_tick = SDL_GetTicks();
523 }
524
525 if (network_check() == 0)
526 SDL_Delay(1);
527 }
528
529 if (network_delay > 1)
530 {
531 // process the current in packet against the xor queue
532 if (packet_state_in_xor[x] == NULL)
533 {
534 packet_state_in_xor[x] = SDLNet_AllocPacket(NET_PACKET_SIZE);
535 packet_copy(packet_state_in_xor[x], packet_state_in[0]);
536 packet_state_in_xor[x]->status = 0;
537 }
538 else
539 {
540 for (int j = 4; j < packet_state_in_xor[x]->len; j++)
541 packet_state_in_xor[x]->data[j] ^= packet_state_in[0]->data[j];
542 }
543 }
544
545 last_state_in_tick = SDL_GetTicks();
546 }
547
548 return 1;
549 }
550
551 // ignore first network_delay states of level
552 bool network_state_is_reset(void)
553 {
554 return (last_state_out_sync < network_delay);
555 }
556
557 // reset queues for new level
558 void network_state_reset(void)
559 {
560 last_state_in_sync = last_state_out_sync = 0;
561
562 for (int i = 0; i < NET_PACKET_QUEUE; i++)
563 {
564 if (packet_state_in[i])
565 {
566 SDLNet_FreePacket(packet_state_in[i]);
567 packet_state_in[i] = NULL;
568 }
569 }
570 for (int i = 0; i < NET_PACKET_QUEUE; i++)
571 {
572 if (packet_state_in_xor[i])
573 {
574 SDLNet_FreePacket(packet_state_in_xor[i]);
575 packet_state_in_xor[i] = NULL;
576 }
577 }
578 for (int i = 0; i < NET_PACKET_QUEUE; i++)
579 {
580 if (packet_state_out[i])
581 {
582 SDLNet_FreePacket(packet_state_out[i]);
583 packet_state_out[i] = NULL;
584 }
585 }
586
587 last_state_in_tick = SDL_GetTicks();
588 }
589
590 // attempt to punch through firewall by firing off UDP packets at the opponent
591 // exchange game information
592 int network_connect(void)
593 {
594 SDLNet_ResolveHost(&ip, network_opponent_host, network_opponent_port);
595
596 SDLNet_UDP_Bind(socket, 0, &ip);
597
598 Uint16 episodes = 0, episodes_local = 0;
599 assert(EPISODE_MAX <= 16);
600 for (int i = EPISODE_MAX - 1; i >= 0; i--)
601 {
602 episodes <<= 1;
603 episodes |= (episodeAvail[i] != 0);
604 }
605 episodes_local = episodes;
606
607 assert(NET_PACKET_SIZE - 12 >= 20 + 1);
608 if (strlen(network_player_name) > 20)
609 network_player_name[20] = '\0';
610
611 connect_reset:
612 network_prepare(PACKET_CONNECT);
613 SDLNet_Write16(NET_VERSION, &packet_out_temp->data[4]);
614 SDLNet_Write16(network_delay, &packet_out_temp->data[6]);
615 SDLNet_Write16(episodes_local, &packet_out_temp->data[8]);
616 SDLNet_Write16(thisPlayerNum, &packet_out_temp->data[10]);
617 strcpy((char *)&packet_out_temp->data[12], network_player_name);
618 network_send(12 + strlen(network_player_name) + 1); // PACKET_CONNECT
619
620 // until opponent sends connect packet
621 while (true)
622 {
623 push_joysticks_as_keyboard();
624 service_SDL_events(false);
625
626 if (newkey && lastkey_scan == SDL_SCANCODE_ESCAPE)
627 network_tyrian_halt(0, false);
628
629 // never timeout
630 last_in_tick = SDL_GetTicks();
631
632 if (packet_in[0] && SDLNet_Read16(&packet_in[0]->data[0]) == PACKET_CONNECT)
633 break;
634
635 network_update();
636 network_check();
637
638 SDL_Delay(16);
639 }
640
641 connect_again:
642 if (SDLNet_Read16(&packet_in[0]->data[4]) != NET_VERSION)
643 {
644 fprintf(stderr, "error: network version did not match opponent's\n");
645 network_tyrian_halt(4, true);
646 }
647 if (SDLNet_Read16(&packet_in[0]->data[6]) != network_delay)
648 {
649 fprintf(stderr, "error: network delay did not match opponent's\n");
650 network_tyrian_halt(5, true);
651 }
652 if (SDLNet_Read16(&packet_in[0]->data[10]) == thisPlayerNum)
653 {
654 fprintf(stderr, "error: player number conflicts with opponent's\n");
655 network_tyrian_halt(6, true);
656 }
657
658 episodes = SDLNet_Read16(&packet_in[0]->data[8]);
659 for (int i = 0; i < EPISODE_MAX; i++) {
660 episodeAvail[i] &= (episodes & 1);
661 episodes >>= 1;
662 }
663
664 network_opponent_name = malloc(packet_in[0]->len - 12 + 1);
665 strcpy(network_opponent_name, (char *)&packet_in[0]->data[12]);
666
667 network_update();
668
669 // until opponent has acknowledged
670 while (!network_is_sync())
671 {
672 service_SDL_events(false);
673
674 // got a duplicate packet; process it again (but why?)
675 if (packet_in[0] && SDLNet_Read16(&packet_in[0]->data[0]) == PACKET_CONNECT)
676 goto connect_again;
677
678 network_check();
679
680 // maybe opponent didn't get our packet
681 if (SDL_GetTicks() - last_out_tick > NET_RETRY)
682 goto connect_reset;
683
684 SDL_Delay(16);
685 }
686
687 // send another packet since sometimes the network syncs without both connect packets exchanged
688 // there should be a better way to handle this
689 network_prepare(PACKET_CONNECT);
690 SDLNet_Write16(NET_VERSION, &packet_out_temp->data[4]);
691 SDLNet_Write16(network_delay, &packet_out_temp->data[6]);
692 SDLNet_Write16(episodes_local, &packet_out_temp->data[8]);
693 SDLNet_Write16(thisPlayerNum, &packet_out_temp->data[10]);
694 strcpy((char *)&packet_out_temp->data[12], network_player_name);
695 network_send(12 + strlen(network_player_name) + 1); // PACKET_CONNECT
696
697 connected = true;
698
699 return 0;
700 }
701
702 // something has gone wrong :(
703 void network_tyrian_halt(unsigned int err, bool attempt_sync)
704 {
705 const char *const err_msg[] = {
706 "Quitting...",
707 "Other player quit the game.",
708 "Network connection was lost.",
709 "Network connection failed.",
710 "Network version mismatch.",
711 "Network delay mismatch.",
712 "Network player number conflict.",
713 };
714
715 quit = true;
716
717 if (err >= COUNTOF(err_msg))
718 err = 0;
719
720 fade_black(10);
721
722 VGAScreen = VGAScreenSeg;
723
724 JE_loadPic(VGAScreen, 2, false);
725 JE_dString(VGAScreen, JE_fontCenter(err_msg[err], SMALL_FONT_SHAPES), 140, err_msg[err], SMALL_FONT_SHAPES);
726
727 JE_showVGA();
728 fade_palette(colors, 10, 0, 255);
729
730 if (attempt_sync)
731 {
732 while (!network_is_sync() && network_is_alive())
733 {
734 service_SDL_events(false);
735
736 network_check();
737 SDL_Delay(16);
738 }
739 }
740
741 if (err)
742 {
743 while (!JE_anyButton())
744 SDL_Delay(16);
745 }
746
747 fade_black(10);
748
749 SDLNet_Quit();
750
751 JE_tyrianHalt(5);
752 }
753
754 int network_init(void)
755 {
756 printf("Initializing network...\n");
757
758 if (network_delay * 2 > NET_PACKET_QUEUE - 2)
759 {
760 fprintf(stderr, "error: network delay would overflow packet queue\n");
761 return -4;
762 }
763
764 if (SDLNet_Init() == -1)
765 {
766 fprintf(stderr, "error: SDLNet_Init: %s\n", SDLNet_GetError());
767 return -1;
768 }
769
770 socket = SDLNet_UDP_Open(network_player_port);
771 if (!socket)
772 {
773 fprintf(stderr, "error: SDLNet_UDP_Open: %s\n", SDLNet_GetError());
774 return -2;
775 }
776
777 packet_temp = SDLNet_AllocPacket(NET_PACKET_SIZE);
778 packet_out_temp = SDLNet_AllocPacket(NET_PACKET_SIZE);
779
780 if (!packet_temp || !packet_out_temp)
781 {
782 printf("SDLNet_AllocPacket: %s\n", SDLNet_GetError());
783 return -3;
784 }
785
786 net_initialized = true;
787
788 return 0;
789 }
790
791 #endif
792
793 void JE_clearSpecialRequests(void)
794 {
795 pauseRequest = false;
796 inGameMenuRequest = false;
797 skipLevelRequest = false;
798 helpRequest = false;
799 nortShipRequest = false;
800 }