File: netserv.c

package info (click to toggle)
netmaze 0.81+jpg0.82-14.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,208 kB
  • ctags: 915
  • sloc: ansic: 8,277; tcl: 1,223; makefile: 66; sh: 26
file content (2087 lines) | stat: -rw-r--r-- 49,969 bytes parent folder | download | duplicates (5)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
/* This is the Netserv V0.36-alpha. (March 1994)
 * ------------------------------------------------
 * written & copyrights (c) by M.Hipp
 *  It's hard to understand the code, (also for me)
 *  because the server is grown enormous.
 *
 * A lot of machines are having problems if more than one application wants
 * to use the itimer. In this case the signal is delivered delayed
 * and the game slows down extremly.
 *
 * Subserv: I have removed all of the subserv code, because the concept
 *          was braindead.
 *
 * still in progress:
 *   The rewriting of the whole servercode!
 *
 */

#include "netmaze.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>

#include <stddef.h>
#include <unistd.h>
#include <sys/types.h>

#ifdef RS6000
 #include <sys/select.h>
#endif

#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <memory.h>

#include "netcmds.h"
#include "network.h"
#include "netserv.h"

struct cqueue *cfirst;
struct gqueue *gfirst;

volatile unsigned int timerticks=0;
unsigned int lasttick=0,starttick=0;

int acc_socket;
int menu_in=0,menu_out=1,use_exmenu=FALSE;
char *exmenu_name;
int nowait=FALSE;

/* extern: maze.c */
extern int create_maze(MAZE *mazeadd);
extern int load_maze(char *name,MAZE *mazeadd);
extern int random_maze(MAZE*,int,int);

void init_slots(int first,int *,struct gqueue *);

void send_message(char *str,struct cqueue *);
void send_names(struct gqueue *);
void send_joydata(struct gqueue *);
void send_maze(struct gqueue *);
void send_start(struct gqueue *);
void start_game(struct gqueue *,int *);
void end_game(struct gqueue *);
void send_comments(struct gqueue *);
void send_comment(struct gqueue *g,struct pqueue *p);
void send_inactive(struct gqueue *g,struct pqueue *p);
void send_mes(char*,struct pqueue*,int type,struct pqueue*,struct gqueue*);

void send_command(struct cqueue *q,int mask,char *data,int len);
void send_group(struct gqueue *g,int mask,char *data,int len);

void usage(void);
void print_menu(void);
void print_timerinfo(unsigned int);
void list_connections(void);

void handle_input(void);
void do_command(struct gqueue *g,struct pqueue *p,char *line);
char *get_hostname(struct cqueue *,char *name);
void setup_sigchild(void);
void inter(int);
void work_input(unsigned char *buf,int len,struct cqueue *);
void do_timer(int);
void start_signal(void);
void io_cntl(void);
void handle_sigchild(int s);
int read_pipe(int socket, char *buf, int maxlen);
int read_buffer(int fd, char *buf, int count);

void accept_socket(void);
void close_timeout_sockets(struct gqueue *);
void remove_player(struct pqueue *p,struct cqueue *c);
struct cqueue *close_socket(struct cqueue *);
struct pqueue *add_player(struct cqueue *c,int,int,char*);
struct gqueue *new_group(int no);
int leave_group(struct pqueue *p);
int join_group(int no,struct pqueue *p);
struct pqueue *find_player_by_cn(struct cqueue *c,int cn);
struct pqueue *find_player(char*);


#ifdef HAVE_FDSET
  fd_set readmask;
#else
  struct fd_mask readmask;
#endif

#ifdef USE_SIGVEC
  struct sigvec vec,vec1;
#else
  struct sigaction vec,vec1;
#endif

#ifdef ITIMERVAL
  struct itimerval value,ovalue;
#else
  struct itimerstruct value,ovalue;
#endif

int use_tclmenu = 0; /* JG HACK */
int suppressXnag = 0;
char tclcommand[200];

void sendcommand(void);

/**************************************
 * main()
 *
 *  - initialize most of the stuff
 *  - spawn ex-menu
 *  - setup acceptsocket
 */

int main(int argc,char **argv)
{
  int i;
  struct sockaddr_in myaddr; 	/* local socket address */

  if(argc != 1)
  {
    for(i=1;i<argc;i++)
    {
      if((strcmp(argv[i],"-help") == 0) || (strcmp(argv[i],"-h") == 0))
        usage();
      else if(strcmp(argv[i],"-exmenu") == 0)
      {
        i++;
        use_exmenu = TRUE;
        exmenu_name = argv[i];
      }
      else if(strcmp(argv[i],"-nowait") == 0)
        nowait = TRUE;
      else if(strcmp(argv[i],"-suppressXnag") == 0)
        suppressXnag = 1;
      else if(strcmp(argv[i],"-tclmenu") == 0)
        use_tclmenu = TRUE;
      else
        usage();
    }
  }

  if(use_exmenu)
  {
    int f,fd1[2],fd2[2];
    char *argv1[] = { "NetservExternalMenu" , NULL };

    pipe(fd1);
    pipe(fd2);
    menu_in = fd1[0];
    menu_out = fd2[1];
    setup_sigchild();

    if((f=fork()) == 0)
    {
      close(0); close(1);
      dup(fd2[0]); dup(fd1[1]);
      close(fd1[0]); close(fd1[1]); close(fd2[0]); close(fd2[1]);
      if(execv(exmenu_name,argv1) < 0)
      {
        fprintf(stderr,"Can't spawn external menu.\n");
        exit(1);
      }
    }
    if(f < 0)
    {
      fprintf(stderr,"Can't fork external menu!\n");
      use_exmenu = FALSE;
    }
  }

  cfirst = NULL;
  new_group(1); /* for now we have only one group */
  if(create_maze(&(gfirst->maze)))
    gfirst->nomaze=FALSE;

  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = INADDR_ANY;
  myaddr.sin_port = htons(NETMAZEPORT);
  if( (acc_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror(argv[0]); exit(1); }

  FD_ZERO(&readmask);
  FD_SET(acc_socket, &readmask);
  if(!use_exmenu)
    FD_SET(0,&readmask); /* stdin */
  else
    FD_SET(menu_in,&readmask); /* external menu */

  if (bind(acc_socket,(struct sockaddr *) &myaddr, sizeof(struct sockaddr)) == -1)
  {
    perror("bind"); exit(1);
  }

  if (listen(acc_socket, 5) == -1 ) { perror(argv[0]); exit(1); }

  start_signal();
  printf(" Netmaze Server 0.36alpha/March 1994 by MH; ");
  printf(" Modified by John Goerzen Oct, 1996 <jgoerzen@complete.org> (.81)\n");
  printf(" Using Port: %d\n",NETMAZEPORT);
  if (!suppressXnag) {
    printf(">>>>>>>>>>>>\a\a CHECK OUT THE NEW xnetserv PROGRAM!\a\n");
    printf(">>>>>>>>>>>> The all new interface to the Netmaze server.\n");
  }
  print_menu();

  for(;;)
    io_cntl();
}

/**********************************
 * handle external menu input
 */

void handle_exinput(void)
{
  int len;
  static char line[256];
  static int cmdlen,bytesleft=0;
  struct gqueue *g=gfirst; /* test */

  if(!bytesleft) /* read a new command */
  {
      if((len = read(menu_in,line,1)) != 1)
      {
        fprintf(stderr,"ExMenuReadError! -> EXIT\n");
        exit(1);
      }
      cmdlen = 1; bytesleft = 0;
      switch((int) line[0])
      {
        case MENU_LOADMAZE:
          bytesleft = read(menu_in,line+1,1);
          cmdlen = bytesleft + 1;
          break;
        case MENU_RUNGAME:
          break;
        case MENU_RUNTEAMGAME:
          break;
        case MENU_STOPGAME:
          break;
        case MENU_LIST:
          break;
        case MENU_DISCONNECT:
          bytesleft = 1;
          cmdlen = 2;
          break;
        case MENU_SETMODE:
          bytesleft = 4;
          cmdlen = 5;
          break;
        case MENU_SETDIVIDER:
          bytesleft = 1;
          cmdlen = 2;
          break;
        case MENU_SETTEAMS:
          bytesleft = 32;
          cmdlen = 33;
          break;
      }
  }
  else
  {
    len = read(menu_in,line+cmdlen-bytesleft,bytesleft);
    bytesleft -= len;
  }

  if(bytesleft == 0) /* execute the command */
    do_command(g,NULL,line);
}

/**********************************
 * execute a command
 */

void print_player(char *m,struct pqueue *p)
{
  if(p == NULL)
    printf("%s",m);
  else
    send_mes(m,NULL,MSG_PLAYER,p,NULL);
}

void do_command(struct gqueue *g,struct pqueue *pl,char *line)
{
  int i;
  struct cqueue *q;
  struct pqueue *p;
  char d[256];

    switch((int) line[0])
    {
      case MENU_LOADMAZE:
        break;
      case MENU_RUNGAME:
        if(g->playing) return;
        if(g->nomaze == TRUE)
        {
          print_player("Load/Reinit a Maze first!\n",pl);
          return;
        }
        if(g->numplayers == 0)
        {
          print_player("No player yet!\nTry again later\n",pl);
          return;
        }
        g->numteams = 0;
        start_game(g,NULL);
        break;
      case MENU_RUNTEAMGAME:
        if(g->playing) return;
        if(g->nomaze == TRUE)
        {
          print_player("Load/Reinit a Maze first!\n",pl);
          return;
        }
        if(g->numplayers == 0)
        {
          print_player("No players yet!\nTry again later\n",pl);
          return;
        }
        g->numteams = 0;
        for(p=g->first;p!=NULL;p=p->gnext)
        {
          if(p->team > g->numteams) g->numteams = p->team;
        }
        g->numteams++;
        start_game(g,NULL);
        break;
      case MENU_STOPGAME:
        end_game(g);
        break;
      case MENU_LIST:
        if(pl == NULL)
          list_connections();
        else
        {
          for(p=g->first,i=0;p!=NULL;p=p->gnext,i++)
          {
            sprintf(d,"%3d | %3d | %16s | %s\n",i,p->team,p->name,p->connection->hostname);
            print_player(d,pl);
          }
        }
        break;
      case MENU_DISCONNECT:
        close(acc_socket);
        if(pl != NULL)
          break;
        i = (int) line[1];
/*        for(p=g->first,i=0;p!=NULL;p=p->gnext,i++)  
	for(p=g->first;p!=NULL;p=p->gnext,i--)
	{
	  if(!i)
	  {
	    break;
	  }
	}
	i = (int) line[1]; */
        for(q=cfirst;q!=NULL;q=q->next,i--)
        {
          if(!i)
          {
            close_socket(q);
            break;
          }
        }
        break;
      case MENU_SETMODE:
        if(line[1] == 2)
          g->gamemode = GM_REFLECTINGSHOTS|GM_DECAYINGSHOTS|GM_MULTIPLESHOTS|
                        GM_WEAKINGSHOTS | GM_ALLOWHIDE | GM_ALLOWRADAR;
        else if(line[1] == 3)
          g->gamemode = GM_REFLECTINGSHOTS|GM_DECAYINGSHOTS|GM_MULTIPLESHOTS |
                        GM_FASTWALKING | GM_FASTRECHARGE | GM_ALLOWRADAR;
        else
          g->gamemode = 0;
        break;
      case MENU_SETDIVIDER:
        g->divider = line[1] - 1;
        if((g->divider < 0 ) || (g->divider > 2)) g->divider = 0;
        break;
      case MENU_SETTEAMS:
        for(p=g->first,i=0;p!=NULL;p=p->gnext,i++)
	
        {
          if((p->team < 0) || (p->team > MAXPLAYERS))
            p->team = 0;
          else
            p->team = (int) line[i+1];   
            
          if((q->players->team < 0) || (q->players->team > MAXPLAYERS))
            q->players->team = 0;
          else
            q->players->team = (int) line[i+1];
        }
        break;
    }
}

/*********************************+
 * handle input (ugly)
 */

void handle_input(void)
{
  char line[256],name[100];
  int len,c=0,a,i,j;
  int teams[MAXPLAYERS];
  char *num;
  struct cqueue *q,*cq;
  struct gqueue *g=gfirst;
  struct pqueue *p;

  memset(line,0,256);
  if (use_exmenu && use_tclmenu) {
    len = read_pipe(menu_in, line, 255);
  } else {
     len = read(0,line,255);
  }
  		/* JG HACK */

  if(len == 0)
  {
    fprintf(stderr,"OOPS: end-of-file??\n");
    exit(255);				/* JG added this line */
    return;
  }

  a = sscanf(line,"%d%s",&c,name);
  if(a == 0)
  {
    printf("Unknown command\n");
    sprintf(tclcommand, "MESSAGE\nUnknown command\n");
    sendcommand();
    print_menu();
    return;
  }
  switch(c)
  {
    case 1:
      if(g->playing > 0) return;
      if(a == 1)
      {
        printf("OK .. re-initalize builtin-maze\n");
        sprintf(tclcommand, "MESSAGE\nOK... re-initialize built-in maze\n");
        sendcommand();
        if(create_maze(&(g->maze)))
          g->nomaze=FALSE;
        else
        {
          printf("Can't init maze\n");
          sprintf(tclcommand, "MESSAGE\nCan't initialize maze.\n");
          sendcommand();
          g->nomaze=TRUE;
        }
      }
      else
      {
        if(sscanf(line,"%d%s%d",&c,name,&j) == 2)
        {
          if(load_maze(name,&(g->maze)) == TRUE)
          {
            g->nomaze=FALSE;
            printf("Maze is OK!\n");
            sprintf(tclcommand, "MESSAGE\nMaze is OK!\n");
            sendcommand();
          }
          else
          {
            g->nomaze=TRUE;
            printf("Can't load '%s'\n",name);
            sprintf(tclcommand, "MESSAGE\nCan't load \"%s\"\n", name);
            sendcommand();
          }
        }
        else
        {
          sscanf(line,"%d%d%d",&c,&i,&j);
          if(random_maze(&(g->maze),i,j)) {
            printf("Randommaze is ok!\n");
            sprintf(tclcommand, "MESSAGE\nRandom maze is ok.\n");
            sendcommand();
          }
          else {
            printf("Error, size too big!\n");
            sprintf(tclcommand, "MESSAGE\nError, size too big.\n");
            sendcommand();
          }
        }
      }
      break;
    case 2:
      if(g->playing) return;
      if(g->nomaze == TRUE)
      {
        printf("Load/Reinit a Maze first!\n");
        sprintf(tclcommand, "MESSAGE\nLoad/reinit a maze first!\n");
        sendcommand();
        return;
      }
      if(g->numplayers == 0)
      {
        printf("No players yet!\nTry again later\n");
        sprintf(tclcommand, "MESSAGE\nNo players net; try again later.\n");
        sendcommand();
        return;
      }
      if(a == 1)
      {
        g->numteams = 0;
        start_game(g,NULL);
      }
      else
      {
        g->numteams = 0;
        for(i=0;i<MAXPLAYERS;i++) teams[i] = 0;
        num = strtok(line,"\n\t ");
        for(i=0;;i++)
        {
          num = strtok(NULL,"\n\t ");
          if(num == NULL) break;
          sscanf(num,"%d",&teams[i]);
          if((teams[i] < 0) || (teams[i] > MAXPLAYERS))
          {
            i = 0;
            break;
          }
        }
        if(i > 0)
        {
          for(g->numteams=0,j=0;j<i;j++)
            if(teams[j] > g->numteams) g->numteams = teams[j];
          g->numteams++;

          if(g->numplayers == 0) {
            printf("No player yet!\nTry again later\n");
            sprintf(tclcommand, "MESSAGE\nNo player yet; try again later.\n");
            sendcommand();
          }
          else
          {
            start_game(g,teams);
          }
        }
        else {
          printf("Illegal team-selection!\n");
          sprintf(tclcommand, "MESSAGE\nIllegal team selection.\n");
          sendcommand();
        }
      }
      break;
    case 3:
      end_game(g);
      break;
    case 4:
      list_connections();
      break;
    case 5:
      if(a == 2)
      {
        sscanf(line,"%d %d",&j,&i);
        for(q=cfirst;q!=NULL;q=q->next,i--)
        {
          if(!i)
          {
            close_socket(q);
            break;
          }
        }
      }
      else {
        printf("Too few parameters.\n");
        sprintf(tclcommand, "MESSAGE\nToo few parameters.\n");
        sendcommand();
      }
      break;

/* JG HACK.... */
      case 21: g->gamemode = (g->gamemode & GM_REFLECTINGSHOTS) ? 
               g->gamemode - GM_REFLECTINGSHOTS : g->gamemode | GM_REFLECTINGSHOTS;
               printf("Reflect (bounce) toggled.\n");
               break;
      case 22: g->gamemode = (g->gamemode & GM_DECAYINGSHOTS) ? 
               g->gamemode - GM_DECAYINGSHOTS : g->gamemode | GM_DECAYINGSHOTS;
               printf("Decay (shots lose power) toggled.\n");
               break;
      case 23: g->gamemode = (g->gamemode & GM_MULTIPLESHOTS) ? 
               g->gamemode - GM_MULTIPLESHOTS : g->gamemode | GM_MULTIPLESHOTS;
               printf("Multishots toggled.\n");
               break;
      case 24: g->gamemode = (g->gamemode & GM_WEAKINGSHOTS) ? 
               g->gamemode - GM_WEAKINGSHOTS : g->gamemode | GM_WEAKINGSHOTS;
               printf("Hurts to shoot toggled..\n");
               break;
      case 25: g->gamemode = (g->gamemode & GM_REPOWERONKILL) ? 
               g->gamemode - GM_REPOWERONKILL : g->gamemode | GM_REPOWERONKILL;
               printf("Full power after killing someone toggled.\n");
               break;
      case 26: g->gamemode = (g->gamemode & GM_FASTRECHARGE) ? 
               g->gamemode - GM_FASTRECHARGE : g->gamemode | GM_FASTRECHARGE;
               printf("Fast recharge toggled.\n");
               break;
      case 27: g->gamemode = (g->gamemode & GM_FASTWALKING) ? 
               g->gamemode - GM_FASTWALKING : g->gamemode | GM_FASTWALKING;
               printf("Fast walking toggled.\n");
               break;
/*      case 28: g->gamemode = (g->gamemode & GM_SHOWGHOST) ? 
               g->gamemode - GM_SHOWGHOST : g->gamemode | GM_SHOWGHOST;
               printf("Show ghost (ghostmode) toggled.\n");
               break;  */
      case 29: g->gamemode = (g->gamemode & GM_ALLOWHIDE) ? 
               g->gamemode - GM_ALLOWHIDE : g->gamemode | GM_ALLOWHIDE;
               printf("Allow hide (cloak) toggled.\n");
               break;
      case 30: g->gamemode = (g->gamemode & GM_ALLOWRADAR) ? 
               g->gamemode - GM_ALLOWRADAR : g->gamemode | GM_ALLOWRADAR;
               printf("Radar toggled.\n");
               break;
/*      case 31: g->gamemode = (g->gamemode & GM_DESTRUCTSHOTS) ? 
               g->gamemode - GM_DESTRUCTSHOTS : g->gamemode | GM_DESTRUCTSHOTS;
               printf("Destruct shots on kill toggled.\n");
               break;  */
      case 32: g->gamemode = (g->gamemode & GM_DECSCORE) ? 
               g->gamemode - GM_DECSCORE : g->gamemode | GM_DECSCORE;
               printf("Decrease score of killed player toggled.\n");
               break;
/*      case 33: g->gamemode = (g->gamemode & GM_RANKSCORE) ? 
               g->gamemode - GM_RANKSCORE : g->gamemode | GM_RANKSCORE;
               printf("Rankingdepend score-bonus toggled.\n");
               break;  */
        case 34: g->gamemode = (g->gamemode & GM_TEAMSHOTHURT) ? 
               g->gamemode - GM_TEAMSHOTHURT : g->gamemode | GM_TEAMSHOTHURT;
               printf("Shots from team members hurt toggled.\n");
               break;
/*    case 6:
      if(a == 1)
      {
        if(g->gamemode == 0)
        {
          g->gamemode = GM_REFLECTINGSHOTS|GM_DECAYINGSHOTS|GM_MULTIPLESHOTS |
                        GM_WEAKINGSHOTS | GM_ALLOWHIDE | GM_ALLOWRADAR;
          printf(" Enhanced gamemode selected!\n");
        }
        else if(!(g->gamemode & GM_FASTWALKING))
        {
          g->gamemode = GM_REFLECTINGSHOTS|GM_DECAYINGSHOTS | GM_MULTIPLESHOTS |
                        GM_FASTWALKING | GM_FASTRECHARGE | GM_ALLOWRADAR;
          printf(" Enhanced just-for-fun gamemode selected!\n");
        }
        else
        {
          g->gamemode = 0;
          printf(" Classic gamemode selected!\n");
        }
      }
      break;
*/
    case 7:
      g->divider++;
      if(g->divider == 3)
        g->divider = 0;
      switch(g->divider)
      {
        case 0:
          printf(" OK. Now, the divider is: 1.\n");
          break;
        case 1:
          printf(" OK. Now, the divider is: 2.\n");
          break;
        case 2:
          printf(" OK. Now, the divider is: 4.\n");
          break;
      }
      break;
    case 9:
      if(g->playing > 0) return;
      for(q=cfirst;q!=NULL;)
      {
        q = close_socket(q);
      }
      exit(0);
      break;
    case 96: /* JG HACK */   /* Will refresh the X interface's playerlist */
            list_connections();
            break;
    case 97: /* JG HACK */
             printf("Sending message: %s\n", (char *)(line+3));
             for(q=cfirst;q!=NULL;q=q->next,i--)
               send_message((char *)(line+3), q);
             break;
    case 98: g->gamemode = 0;
             printf("All options off.\n");
             g->divider = 0;
             printf("Beat divider reset to 1.\n");
             break;
    case 99:
      printf("******** INFO: *********\n\n");
      printf("numplayers: %d response: %d playing: %d gameflag: %d mode: %d.\n",g->numplayers,g->response,g->playing,g->gameflag,g->mode);
      for(p=gfirst->first,i=0;p!=NULL;p=p->gnext,i++)
      {
        printf("G[%d] %s pl: %d joy: %d cn: %d num: %d gno: %d team: %d ch: %d.\n",i,p->name,p->playing,p->joydata,p->cnumber,p->number,p->groupnumber,p->team,p->checked);
      }
      for(cq=cfirst,i=0;cq!=NULL;cq=cq->next,i++)
      {
        if(cq->players != NULL)
          printf("C[%d] %s plnum: %d mode: %d socket: %d response: %d.\n",i,cq->players->name,cq->plnum,cq->mode,cq->socket,cq->response);
        else
          printf("C[%d] %s plnum: %d mode: %d socket: %d response: %d.\n",i,"UNK",cq->plnum,cq->mode,cq->socket,cq->response);
      }
      break;
    default:
      printf("Unknown command\n");
      sprintf(tclcommand, "MESSAGE\nUnknown command\n");
      sendcommand();
      print_menu();
      break;
  }
}

/***********************/
/* Install Mastertimer */
/***********************/

void start_signal(void)
{
  value.it_interval.tv_sec = 0;
  value.it_value.tv_sec =  1;
#ifdef ITIMERVAL
  value.it_value.tv_usec = 0;
  value.it_interval.tv_usec = DRAWTIME;
#else
  value.it_value.tv_nsec = 0;
  value.it_interval.tv_nsec = DRAWTIME;
#endif

  setitimer(ITIMER_REAL,&value,&ovalue);

#ifdef USE_SIGVEC
  vec1.sv_handler = (void (*)(int)) inter;
  vec1.sv_mask = 0;
  vec1.sv_flags = 0;
  if (sigvector(SIGALRM, &vec1, (struct sigvec *) 0) == -1) perror("SIGALRM\n");
#else
  vec1.sa_handler = (void (*)(int)) inter;
 #ifdef RS6000 /* ibm rs/6000 */
   sigemptyset(&vec1.sa_mask);
 #else
  vec1.sa_mask = 0;
 #endif
  vec1.sa_flags = 0;
  if ( sigaction(SIGALRM, &vec1, (struct sigaction *) 0) == -1) perror("SIGALRM\n");
#endif
}

void setup_sigchild(void) /* for external menu */
{
#ifdef USE_SIGVEC
  struct sigvec svec1;

  svec1.sv_handler = (void (*)(int)) handle_sigchild;
  svec1.sv_mask = 0;
  svec1.sv_flags = 0;
  if(sigvector(SIGCHLD, &svec1, (struct sigvec *) 0) == -1) perror("SIGCHLD\n");
#else
  struct sigaction svec1;

  svec1.sa_handler = (void (*)(int)) handle_sigchild;
 #ifdef RS6000 /* ibm rs/6000 */
   sigemptyset(&svec1.sa_mask);
 #else
  svec1.sa_mask = 0;
 #endif
  svec1.sa_flags = 0;
  if(sigaction(SIGCHLD,&svec1,(struct sigaction *)0) == -1) perror("SIGCHLD\n");
#endif
}

void handle_sigchild(int s)
{
  int stat,cpid;
  cpid = wait(&stat);
  fprintf(stderr,"Child (exmenu?): %d died.\n",cpid);
  exit(1); /* ok, that's the hard way */
}

/************************/
/* Timer-Signal-Handler */
/************************/

void inter(int sig)
{
  timerticks++; /* enough for about 6 years :-) */

  /* the timertick is for synchronisation */
  /* the main timer-function is called by the main loop */
  /* so we don't have to care about (very tricky) interrupt conditions. */
}

/********************************
 * the main timerfunction
 */

void do_timer(int nowaitgroup)
{
  struct gqueue *g;
  struct pqueue *p;
  unsigned int t;
  static unsigned int lasttick;
  int d;

  t = timerticks;
  if(!(t-lasttick) && !nowaitgroup) return;

  if((t - lasttick) > 0x1000)
  {
    fprintf(stderr,"Timerproblems?\n");
    lasttick = timerticks;
    return;
  }

  t -= lasttick;
  lasttick += t;

  for(g=gfirst;g!=NULL;g=g->next) /* all groups */
  {
    if(g->gameflag)
    {
      d = 1<<g->divider;
      if(g->dividercnt > d) /* sure is sure */
        g->dividercnt = d;
      else
        g->dividercnt -= t;
      if(g->dividercnt <= 0)
      {
        if(g->dividercnt > -d)
          g->dividercnt += d;
        else
          g->dividercnt = d;
      }
      else
        if(!(nowaitgroup == g->groupno)) continue;

      if(!g->response || nowait)
      {
        print_timerinfo(timerticks &(~(d-1)) );
        g->response = g->numwait;
        send_joydata(g);
        g->timeout = 0;
      }
      else
      {
        g->timeout+=d;
        if((g->timeout & 0x1f) == 0x1f)
        {
          for(p=g->first;p!=NULL;p=p->gnext)
          {
            if(p->playing && (p->mode == PLAYERMODE) && !p->connection->response)
            {
              fprintf(stderr,"Connection of Player %d doesn't response!\n",p->number);
              sprintf(tclcommand, "MESSAGE\nConnection of player %d\nMESSAGECAT\ndoesn't respond\n",
                      p->number);
              sendcommand();
            }
          }
        }
        if(g->timeout > 100)
          close_timeout_sockets(g);
      }
    }
  }

  for(g=gfirst;g!=NULL;g=g->next) /* all groups */
  {
    if( (g->playing == 1) && !g->gameflag)
    {
      if(!g->response)
      {
        g->playing++;
        g->gameflag = TRUE;
        printf("ok ... ACTION!!! ...[(3) stop game] \n");
      }
      else
      {
        if(timerticks - starttick > 100)
        {
          close_timeout_sockets(g);
        }
      }
    }
  }

}

/***********************************
 * I/O-Signal-Handler
 */

void io_cntl(void)
{
#ifdef HAVE_FDSET
  fd_set readmask1;
#else
  struct fd_mask readmask1;
#endif

  int count,len;
  int numfds;
  char buf[280];
  struct cqueue *q;

  for(;;)
  {
    readmask1 = readmask;

    /* we need max. 3+32 (+cams) filedescriptors
     * if select returns with a value < 0 we expect a SIGNAL not an error
     */
    if ((numfds = select(36,&readmask1,NULL,NULL,NULL)) < 0)
    {
      do_timer(FALSE);
      continue;
    }
    do_timer(FALSE);

    if(numfds == 0) break;

    /*
     * slave-sockets
     */
    for(q=cfirst;q!=NULL;)
    {
      if(FD_ISSET(q->socket,&readmask1))
      {
        if((count = recv(q->socket,buf,1,0)) != 1) /* nicht optimal */
        {
          q = close_socket(q);
          continue;
        }
        if((len = (int) nm_field[(int) *buf]) > 0) /* fixed length command */
        {
          if(len > 1)
          {
            count = recv(q->socket,buf+1,len-1,0);
            if(count != len-1)
            {
              fprintf(stderr,"IO: wrong length\n");
              return;
            }
          }
          work_input((unsigned char*)buf,len,q);
        }
        else
        {  /* getting length */
          if((count = recv(q->socket,buf+1,1,0)) != 1) /* nicht optimal */
          {
            fprintf(stderr,"IO: error reading blklen\n");
            return;
          }
          len = (int) buf[1];
          if(len > 2)
          {
            if((count = recv(q->socket,buf+2,len-2,0)) != (len-2) )
            {
              fprintf(stderr,"IO-1: wrong length\n");
              return;
            }
          }
          work_input((unsigned char*)buf,len,q);
        }
      }
      q=q->next;
    }

    /*
     * accept_socket
     */
    if (FD_ISSET(acc_socket, &readmask1))
    {
      accept_socket();
    }

    if(use_exmenu)
    {
      if(FD_ISSET(menu_in,&readmask1))
/* JG HACK        handle_exinput();    */
        handle_input();
    }
    else /* keyboard (stdin) */
    {
      if(FD_ISSET(0,&readmask1))
        handle_input();
    }

  }
} /* end io_handler */


/*************************************
 * (try to) accept a new socket
 */

void accept_socket(void)
{
  int addrlen = sizeof(struct sockaddr);
  struct cqueue *q;

  q = calloc(1,sizeof(struct cqueue)); /* alloc a new node */

  q->response = FALSE;
  q->socket   = accept(acc_socket,&(q->remoteaddr),&addrlen);
  if(q->socket == -1 )
  {
    perror("accept call failed"); exit(1);
  }

  q->next = cfirst;
  q->mode = 0;
  cfirst = q;

  get_hostname(q,q->hostname);

  printf("\n accepted a connection request from [%s].\n",q->hostname);
  sprintf(tclcommand, "MESSAGE\nNew connection request from\nMESSAGECAT\n%s\n",
          q->hostname);
  sendcommand();

  FD_SET(q->socket,&readmask);
}

/************************
 * resolve hostname
 */

char *get_hostname(struct cqueue *q,char *name)
{
  struct hostent *hp;
  char *saddr = (char *) &(((struct sockaddr_in *) &(q->remoteaddr))->sin_addr.s_addr);
  hp = gethostbyaddr(saddr,4,AF_INET);

  if(hp && hp->h_name && (strlen(hp->h_name) > 0))
    strcpy(name,hp->h_name);
  else
    sprintf(name,"%ud.%ud.%ud.%ud",(unsigned int) saddr[0],(unsigned int) saddr[1],(unsigned int) saddr[2],(unsigned int) saddr[3]);
  return name;
}

/**********************************
 * close a connection
 */

struct cqueue *close_socket(struct cqueue *q)
{
  struct cqueue *q2,*q1,*qr;
  struct pqueue *p,*p1;

  qr = q->next;

  FD_CLR(q->socket,&readmask); /* shutdown ? */
  close(q->socket);
  q->socket = -1;

  for(q2=cfirst,q1=NULL;q2 != NULL;q1=q2,q2=q2->next)
    if(q2 == q)
      break;

  if(q1 == NULL)
    cfirst = q->next;
  else
    q1->next = q->next;

  fprintf(stdout,"I've lost a connection ...\n");

  for(p=q->players;p!=NULL;p=p1)
  {
    p1=p->cnext;
    leave_group(p);
    remove_player(p,q);
  }
  free(q);

  return qr;
}

/*********************************
 * close not responding sockets
 */

void close_timeout_sockets(struct gqueue *g)
{
  struct pqueue *p;

  for(p=g->first;p!=NULL;)
  {
    if(p->playing)
    {
      if(!p->connection->response)
      {
        fprintf(stderr,"Connection of Player %d doesn't response .. -> shutdown\n",p->number);
        close_socket(p->connection);
        p=g->first;
        continue;
      }
    }
    p=p->gnext;
  }
}

/*******************************
 * handle an incoming packet
 * works only for SINGLE(PLAYER/CAMERA)-connections
 */

void work_input(unsigned char *buf,int len,struct cqueue *q)
{
  unsigned int lval;
  struct pqueue *pl;
  int cn;

/* ident?? */
  if( !(q->mode & (SINGLEPLAYER|SINGLECAMERA)) && (*buf != NM_SETMODE))
  {
    fprintf(stderr,"error: Junk on an unchecked connection.\n");
    return;
  }

  switch(*buf)
  {
    case NM_OWNDATA:
      if(q->mode & SINGLEPLAYER)
      {
        if(q->players == NULL) 
          break;

        q->players->joydata = (int) buf[1];
        if(q->players->playing && (q->players->mode == PLAYERMODE) )
        {
          q->players->group->response--;
          q->response = TRUE;
          if(q->players->group->timeout && !q->players->group->response
                                        && !nowait)
            do_timer(q->players->groupnumber);
        }
      }
      else
      {

      }
      break;
    case NM_MESSAGE:
        if(q->players == NULL)
          break;
        buf[(int) buf[1]] = 0;
        switch(buf[2])
        {
          case MSG_SERVER:
            printf("%s\n",buf+6); /* message to server */
            break;
          case MSG_ALL:
            break; /* shout all */
          case MSG_GROUP:
            send_mes((char*)buf+6,q->players,MSG_GROUP,NULL,q->players->group);
            break; /* group-message */
          case MSG_TEAM:
            break; /* team-message */
          case MSG_TO_NAME:
            if((pl=find_player((char*)buf+6)) != NULL)
              print_player((char*)buf+7+strlen((char*)buf),pl);
        }
	break;
    case NM_OWNNAME:
        fprintf(stderr,"CLIENT sent an old command.\n");
        send_message("Sorry, your running an old client-version.\n",q);
        close_socket(q);
	break;
    case NM_READY:
        if(q->players == NULL) 
          break;
        if(q->players->playing && (q->players->mode == PLAYERMODE))
        {
          q->players->group->response--;
          q->response = TRUE;
        }
	break;
    case NM_END:
        if(q->players == NULL) 
          break;
        fprintf(stderr,"Received END-Command by: %s.\n",q->players->name);
        if(q->players->group->gameflag && q->players->playing)
          if(q->players->master)
          {
            fprintf(stderr,"Accepted END-Command by: %s.\n",q->players->name);
            end_game(q->players->group);
          }
        break;
    case NM_SETMODE:
        if(!(q->mode & (SINGLEPLAYER)) )
        {
          lval = 0;
          lval |= ((unsigned int) buf[1]) << 24;
          lval |= ((unsigned int) buf[2]) << 16;
          lval |= ((unsigned int) buf[3]) << 8;
          lval |= ((unsigned int) buf[4]);

          switch(lval)
          {
            case PLAYERMAGIC:
              q->mode = SINGLEPLAYER;
              printf("It's a SINGPLEPLAYER connection!\n");
              break;
            case CAMMAGIC:
              q->mode = SINGLECAMERA;
              printf("It's a CAMERA connection! (not debugged!)\n");
              break;
            default:
              printf("Unknown Magic: Do you use different netprotocolls?\n");
              close_socket(q);
              break;
          }
        }
        break;
      case NM_OWNCOMMENT:
        if(q->players == NULL) 
          break;
        buf[(int)buf[1]] = 0;
        if(strlen((char*)buf+2) > MAXCOMMENT)
          buf[2+MAXCOMMENT] = 0;
        strcpy(q->players->comment,(char*)buf+2);
        if(q->players->group->playing)
          send_comment(q->players->group,q->players);
        break;
      case NM_DOCOMMAND:
        if(q->players == NULL) 
          break;
        fprintf(stderr,"-EXCOMMAND-\n");
        if(q->players->master)
          do_command(q->players->group,q->players,(char*)buf+2);
        break;
      case NM_JOIN:
        cn = ((int)buf[1]<<8)+buf[2];
        if((pl=find_player_by_cn(q,cn)) != NULL)
          join_group((int)buf[3],pl);
        else
          fprintf(stderr,"Join-Error!\n");
        break;
      case NM_ADDPLAYER:
        buf[(int)buf[1]] = 0; /* add \0 to playernamename */
        if(strlen((char*)buf+4) > MAXNAME)
          buf[4+MAXNAME] = 0;
        if(q->mode & SINGLEPLAYER)

          add_player(q,((int)buf[2]<<8)+buf[3],PLAYERMODE,(char*)buf+4);
        else if(q->mode & SINGLECAMERA)
          add_player(q,((int)buf[2]<<8)+buf[3],CAMMODE,(char*)buf+4);

/*          add_player(q,q->socket,PLAYERMODE,(char*)buf+4);
        else if(q->mode & SINGLECAMERA)
          add_player(q,q->socket,CAMMODE,(char*)buf+4); */
        break;
      case NM_REMOVEPLAYER:
        break;
      default:
        fprintf(stderr,"error: Unknown NET-Command!!\n");
        break;
  }
}

/***********************************/
/* Start Game  (master)            */
/***********************************/

void start_game(struct gqueue *g,int *teams)
{
  sprintf(tclcommand, "STARTGAME\n");
  sendcommand();

  g->playing = 1;
  g->numgamers = g->numplayers;

  init_slots(0,teams,g);

  send_names(g);
  send_comments(g);
  send_maze(g);
  send_start(g);
}

/**************************/
/* init_slots             */
/**************************/

void init_slots(int first,int *teams,struct gqueue *g)
{
  int j;
  struct pqueue *p;

  g->numwait=0;

  for(p=g->first,j=first;p!=NULL;p=p->gnext)
  {
    p->playing = 1;

    if(p->mode != PLAYERMODE)
      continue;

    p->joydata = 0;
    p->connection->response = FALSE;
    p->number = j;

    if(teams != NULL)
      p->team = teams[j];
    j++;
    g->numwait++;
  }
}

/***********************/
/* Spielende           */
/***********************/

void end_game(struct gqueue *g)
{

  
  char data[2];
  struct pqueue *p;

  sprintf(tclcommand, "STOPGAME\n");
  sendcommand();

  g->playing = 0;
  g->gameflag = FALSE;

  data[0] = NM_STOPGAME;
  send_group(g,0,data,1);

  for(p=g->first;p != NULL;p=p->gnext)
  {
    p->playing = FALSE;
  }
  fprintf(stdout,"Gameover in Group %s.\n",g->groupname);
}

/**********************************/
/* Sende Routinen                 */
/**********************************/

void send_maze(struct gqueue *g)
{
  int (*hfeld)[MAZEDIMENSION],(*vfeld)[MAZEDIMENSION];
  int dim,i,j;
  char buf[MAZEDIMENSION+10];

  hfeld = g->maze.hwalls;
  vfeld = g->maze.vwalls;
  dim = g->maze.xdim;

  for(i=0;i<=dim;i++)
  {
    for(j=0;j<dim;j++)
      buf[j+4] = (char) hfeld[i][j];
    buf[0] = NM_MAZEH;
    buf[1] = dim+4;
    buf[2] = (char) dim;
    buf[3] = (char) i;

    send_group(g,PLAYING,buf,dim+4);
  }

  for(i=0;i<=dim;i++)
  {
    for(j=0;j<dim;j++)
      buf[j+4] = (char) vfeld[j][i];
    buf[0] = NM_MAZEV;
    buf[1] = dim+4;
    buf[2] = (char) dim;
    buf[3] = (char) i;

    send_group(g,PLAYING,buf,dim+4);
  }
}

/*********************/
/* Send Names        */
/*********************/

void send_names(struct gqueue *g)
{
  char data[256];
  struct pqueue *p;

  for(p=g->first;p!=NULL;p=p->gnext)
  {
    if(p->playing && (p->mode == PLAYERMODE) )
    {
      data[0] = NM_ALLNAMES;
      data[1] = 3 + strlen(p->name);
      data[2] = (char) p->number;
      strcpy(data+3,p->name);
      send_group(g,PLAYING,data,data[1]);
    }
  }
}

/*********************/
/* Send Comments     */
/*********************/

void send_comments(struct gqueue *g)
{
  struct pqueue *p;

  for(p=g->first;p!=NULL;p=p->gnext)
    send_comment(g,p);
}

void send_comment(struct gqueue *g,struct pqueue *p)
{
  char data[256];

  if(p->playing && (p->mode == PLAYERMODE) )
  {
    data[0] = NM_ALLCOMMENTS;
    data[1] = 3 + strlen(p->comment);
    data[2] = (char) p->number;
    strcpy(data+3,p->comment);
    send_group(g,PLAYING,data,data[1]);
  }
}


/****************************************************
 * send start (init) data block
 * including: num. of players & playernumbers
 */

void send_start(struct gqueue *g)
{
  char data[MAXPLAYERS+10];
  int rndwert;
  struct pqueue *p;

  g->response = g->numwait;
  g->numjoy = g->numgamers;

  printf("Starting game with %d player(s) in group %s,\n",g->numgamers,g->groupname);

  data[0] = NM_STARTGAME;
  data[1] = g->numgamers + 16;
  data[2] = (char) g->numgamers;

  rndwert = rand();
  data[4] = rndwert >> 8;
  data[5] = rndwert & 0xff;

  if(g->numteams == 0)
    data[6] = (char) g->numgamers;
  else
    data[6] = g->numteams;

  data[7] = (unsigned char) (g->gamemode & 0xff);
  data[8] = (unsigned char) (g->gamemode>>8);
  data[9] = (unsigned char) g->divider;

  /* data 10 - 15 free for future changes */

  for(p=g->first;p!=NULL;p=p->gnext)
  {
    if(p->playing && (p->mode == PLAYERMODE))
    {
      if(g->numteams)
        data[16+p->number] = p->team;
      else
        data[16+p->number] = p->number;
    }
  }

  for(p=g->first;p!=NULL;p=p->gnext)
  {
    if(p->playing)
    {
      p->connection->response = FALSE; /* warning! */
      if(p->mode == PLAYERMODE)
        data[3] = (char) p->number;
      else
        data[3] = 0;
      send(p->connection->socket,data,data[1],0);
    }
  }
  starttick = timerticks;
}

/*************************************
 * send a (new) message
 */

void send_mes(char *m,struct pqueue *from,int type,struct pqueue *p,struct gqueue *g)
{
  unsigned char data[256];
  char mes[256];

  if(from != NULL)
  {
    if(from->checked)
      strcpy(mes,"<");
    else
      strcpy(mes,"!<");
    strcat(mes,from->name);
    strcat(mes,"> ");
  }
  else
    strcpy(mes,"[Server] ");

  strcat(mes,m);      

  data[0] = NM_MESSAGE;
  data[1] = 7 + strlen(mes);
  data[2] = type;
  data[3] = 0;
  data[4] = 0;
  data[5] = 0;
  strcpy((char *) data+6,mes);

  switch(type)
  {
    case MSG_ALL:
      send_command(cfirst,0,(char*)data,data[1]);
      break;
    case MSG_GROUP:
      data[4] = g->groupno;
      send_group(g,0,(char*)data,data[1]);
      break;
    case MSG_PLAYER:
      data[4] = (p->cnumber>>8) & 0xff;
      data[5] = p->cnumber & 0xff;
      send(p->connection->socket,(char *) data,(int) data[1],0);
      break;
    case MSG_TEAM:
      for(p=g->first;p!=NULL;p=p->gnext)
        if( (from->team == p->team) && (from != p) )
        {
          data[4] = (p->cnumber>>8) & 0xff;
          data[5] = p->cnumber & 0xff;
          send(p->connection->socket,(char *) data,(int) data[1],0);
        }
      break;
  }
}

/********************/
/* Send Message     */
/********************/

void send_message(char *str,struct cqueue *q)
{
  char data[256];

  data[0] = NM_MESSAGE;
  data[1] = strlen(str)+7;
  data[2] = 0;
  data[3] = 0;
  data[4] = 0;
  data[5] = 0;
  strcpy(data+6,str);
  send(q->socket,data,data[1],0);
}

/*************************
 * Send joystick data
 */

void send_joydata(struct gqueue *g)
{
  char data[MAXPLAYERS+2];
  struct pqueue *p;

  for(p=g->first;p!=NULL;p=p->gnext)
  {
    if(p->playing && (p->mode == PLAYERMODE))
    {
      data[p->number+1] = (char) p->joydata;
      p->connection->response = FALSE;
    }
  }

  data[0] = NM_ALLDATA;
  send_group(g,PLAYING,data,g->numjoy+1);
}

void send_inactive(struct gqueue *g,struct pqueue *p)
{
  char data[3];

  data[0] = NM_INACTIVATE;
  data[1] = g->groupno;
  data[2] = p->number;
  send_group(g,PLAYING,data,3);
}

/**************************************************************************
 *
 *   NEW STUFF:
 *
 */

/************************************
 * Send a command (new)
 */

void send_command(struct cqueue *q,int mask,char *data,int len)
{
  while(q != NULL)
  {
    if(q->mode & mask)
      send(q->socket,data,len,0);
    q = q->next;
  }
}

/************************************
 * send a command to all players of a group
 */

void send_group(struct gqueue *g,int mask,char *data,int len)
{
  struct pqueue *p=g->first;

  while(p != NULL)
  {
    if(!mask || ((mask == PLAYING) && p->playing))
      send(p->connection->socket,data,len,0);
    p = p->gnext;
  }
}

/************************************
 * add player (or camera)
 */

struct pqueue *add_player(struct cqueue *c,int cn,int mode,char *name)
{
  struct pqueue *p,*p1,*p2=NULL;

  p = calloc(1,sizeof(struct pqueue));

  if(find_player(name) == NULL)
  {
    p->checked = TRUE;
  }

  for(p1=c->players;p1!=NULL;p1=p1->cnext)
  {
    if(p1->cnumber == cn)
      fprintf(stderr,"Duplicated connection-numbers.\n");
    p2=p1;
  }
  if(p2 == NULL)
  {
    c->players = p;
  }
  else
  {
    p2->cnext = p;
    p->clast = p2;
  }

  p->connection = c;
  p->mode = mode;
  p->cnumber = cn;

  if(name != NULL)
  {
    if(strlen(name)>MAXNAME)
      name[MAXNAME] = 0;
    strcpy(p->name,name);
  }
  strcpy(p->comment,"Gotcha!!!!");

  if(p->checked)
    send_mes("OK, player added with a valid name.",NULL,MSG_PLAYER,p,NULL);
  else
    send_mes("OK, player added with an invalid name.",NULL,MSG_PLAYER,p,NULL);

  fprintf(stderr,"Added player %s : %d.\n",p->name,p->cnumber);
  list_connections();
  sprintf(tclcommand, "MESSAGECAT\nNew player: %s\n", p->name);
  sendcommand();

  return p;
}

void remove_player(struct pqueue *p,struct cqueue *c)
{
  list_connections();
  sprintf(tclcommand, "MESSAGE\nLost player: %s\n", p->name);
  sendcommand();
  free(p);
  /* if(c != NULL) -> silent on this connection */

}

/************************************
 * leave group
 */

int leave_group(struct pqueue *p)
{
  struct gqueue *g;

  g = p->group;

  if(!p->groupnumber || (g == NULL) )
    return 0;

  if(p->glast == NULL)
    p->group->first = p->gnext;
  else
    p->glast->gnext = p->gnext;

  if(p->gnext != NULL)
  {
    p->gnext->glast = p->glast;
  }

  p->gnext = NULL;
  p->glast = NULL;
  p->group = NULL;
  p->groupnumber = 0;

  if(p->mode == PLAYERMODE)
    g->numplayers--;

  if(p->master)
    if(g->first != NULL)
      g->first->master = TRUE;

  if(p->playing == TRUE)
  {
    p->playing = FALSE;
    if(p->mode == PLAYERMODE)
    {
      g->numgamers--;
      g->numwait--;
      if(!p->connection->response)
        g->response--;
    }

    if((g->numgamers == 0) || (g->numwait == 0)) /* double-check */
    {
      fprintf(stdout,"Last active player has left the game.\n");
      end_game(g);
    }
    else
      send_inactive(g,p);
  }

  return 1;
}

/************************************
 * join group
 */

int join_group(int no,struct pqueue *p)
{
  struct gqueue *g;
  struct pqueue *p1,*p2;

  if(p==NULL)
    return FALSE;

  if(p->groupnumber)
    leave_group(p);

  for(g=gfirst;g!=NULL;g=g->next)
    if(g->groupno == no)
      break;
  if(g == NULL)
    return 0;

  for(p1=g->first,p2=NULL;p1!=NULL;p1=p1->gnext) p2=p1;

  if(p2 == NULL)
  {
    g->first = p;
    p->glast = NULL;
    p->master = TRUE;
  }
  else
  {
    p2->gnext = p;
    p->glast = p2;
    p->master = FALSE;
  }
  p->gnext = NULL;
  p->groupnumber = no;
  p->group = g;
  p->playing = FALSE;

  if(p->mode == PLAYERMODE)
    g->numplayers++;

  send_mes("OK, here I am!",p,MSG_GROUP,NULL,g);
  if(g->playing)
    send_mes("Please wait .. another game is running!",NULL,MSG_PLAYER,p,NULL);
  else
    send_mes("OK, joined group!",NULL,MSG_PLAYER,p,NULL);

  return 1;
}

/***************************************
 * find a group (by name)
 */

struct gqueue *find_group(char *name)
{
  struct gqueue *g;

  for(g=gfirst;g!=NULL;g=g->next)
  {
    if(strcmp(g->groupname,name) == 0)
      return g;
  }
  return NULL;
}

/***************************************
 * find a player (by name) (later: hashtables)
 */

struct pqueue *find_player(char *name)
{
  struct cqueue *c;
  struct pqueue *p;

  if(name == NULL) return NULL;

  for(c=cfirst;c!=NULL;c=c->next)
    for(p=c->players;p!=NULL;p=p->cnext)
    {
      if(p->checked) /* only checked players */
        if(strcmp(name,p->name) == 0) return p;
    }

  return NULL;
}

/***************************************
 * find a player (by connectionnumber)
 */

struct pqueue *find_player_by_cn(struct cqueue *c,int cn)
{
  struct pqueue *p;

  if(c == NULL) return NULL;

  for(p=c->players;p!=NULL;p=p->cnext)
  {
    if(p->cnumber == cn)
      return p;
  }

  return NULL;
}

/***************************************
 * Create a new group
 */

struct gqueue *new_group(int no)
{
  struct gqueue *g,*g1,*g2=NULL;

  g = calloc(1,sizeof(struct gqueue));

  for(g1=gfirst;g1!=NULL;g1=g1->next) g2=g1;
  if(g2 == NULL)
    gfirst = g;
  else
    g2->next = g;

  g->groupno = no;
  strcpy(g->groupname,"NETMAZE");

  return g;
}

/*******************************
 * List connections
 */

void list_connections(void)
{
  struct cqueue *q;
  struct pqueue *p;
  struct gqueue *g=gfirst;
  int i;

  sprintf(tclcommand, "CONNECTIONLIST\n");
  sendcommand();  
  
  printf("No.: |     Player-Name: | hostname\n");
  printf("-----+------------------+----------------\n");

/*  for(p=g->first,i=0;p!=NULL;p=p->gnext,i++)  */
  for(q=cfirst,i=0;q!=NULL;q=q->next,i++) 
  {
    if(q->players->mode == PLAYERMODE) {
      printf("%3d  | %16s | %s\n",i,q->players->name,q->hostname);
      sprintf(tclcommand, "PLAYER\n%d\n%s\n%s\n", i,
              q->players->name, q->hostname);
      sendcommand();
    }
    else
      printf("%3d  | %16s | %s\n",i,"A Camera?!?",q->hostname);
  }
  printf("-----+------------------+----------------\n");
  sprintf(tclcommand, "ENDCONNECTIONLIST\n");
  sendcommand();
}

/***************************/
/* little help for newbies */
/***************************/

void usage(void)
{
  printf("Usage: netserv [-h|-help] [-exmenu <external-menu-program>] [-nowait]\n");
  printf("\t-h|-help: this message\n");
  printf("\t-exmenu: Control with an external menu\n");
  printf("\t-nowait: server shouldn't wait for clients (for very slow lines)\n");
  printf("\t-tclmenu: Uses tcl menu\n");
  exit(0);
}

void print_timerinfo(unsigned int t)
{
  switch(t & 0x3f)
  {
    case 0:
      fprintf(stderr,"\010|");
      break;
    case 0x10:
      fprintf(stderr,"\010/");
      break;
    case 0x20:
      fprintf(stderr,"\010-");
      break;
    case 0x30:
      fprintf(stderr,"\010\\");
      break;
  }
}

void print_menu(void)
{
  if(use_exmenu) return;

  printf("\t****************** MENU ******************\n");
  printf("\t1 [mazename] => Reinit/Load a maze ( rndmaze: 1 <size> <maxlen> )\n");
  printf("\t2 [teamlist] => Start game [with teams]\n");
  printf("\t3            => Stop a running game\n");
  printf("\t4            => List connections\n");
  printf("\t5 <No.>      => Shutdown a connection <No.>\n");
  switch(gfirst->divider)
  {
    case 0:
      printf("\t7            => Change 'beat' divider (current: 1)\n");
      break;
    case 1:
      printf("\t7            => Change 'beat' divider (current: 2)\n");
      break;
    case 2:
      printf("\t7            => Change 'beat' divider (current: 4)\n");
      break;
  }
  printf("\t21: %s, 22: %s, 23: %s, 24: %s\n",
         (gfirst->gamemode & GM_REFLECTINGSHOTS) ? "BOUNCE" : "bounce",
         (gfirst->gamemode & GM_DECAYINGSHOTS) ? "DECAY" : "decay",
         (gfirst->gamemode & GM_MULTIPLESHOTS) ? "MULTISHOT" : "multishot",
         (gfirst->gamemode & GM_WEAKINGSHOTS) ? "HURTS2SHOOT" : "hurts2shoot");
  printf("\t25: %s, 26:%s, 27: %s\n",
         (gfirst->gamemode & GM_REPOWERONKILL) ? "REPOWERONKILL" : "repoweronkill",
         (gfirst->gamemode & GM_FASTRECHARGE) ? "FASTHEAL" : "fastheal",
         (gfirst->gamemode & GM_FASTWALKING) ? "FASTWALK" : "fastwalk");
/*         (gfirst->gamemode & GM_SHOWGHOST) ? "GHOSTMODE" : "ghostmode"); */
  printf("\t29: %s, 30: %s, 32: %s\n",
         (gfirst->gamemode & GM_ALLOWHIDE) ? "CLOAK" : "cloak",
         (gfirst->gamemode & GM_ALLOWRADAR) ? "RADAR" : "radar",
/*         (gfirst->gamemode & GM_DESTRUCTSHOTS) ? "DESTRUCTSHOTS" : "destructshots", */
         (gfirst->gamemode & GM_DECSCORE) ? "DECSCORE" : "decscore");
  printf("\t34: %s\n",
/*         (gfirst->gamemode & GM_RANKSCORE) ? "RANKSCORE" : "rankscore"); */
         (gfirst->gamemode & GM_TEAMSHOTHURT) ? "TEAMSHOTHURT" : "teamshothurt");
     
  printf("\t------------------------------------------\n");
  printf("\t9            => Quit\n\n");
}

int reaper() {
  while(wait3(NULL, WNOHANG, NULL) > 0) {}
}

void sendcommand(void)
{
int len;
char *pts = tclcommand;
int status = 0, n, count;

  if (!use_tclmenu) return;
/* Sends the string in tclcommand to the external menu. */

  count = len = strlen(tclcommand);


  if (count < 0) exit(255);

   while (status != count) { 
        n = write(menu_out, pts+status, count-status); 
        if (n < 0) exit(255);
        status += n; 
    } 
    return;
} 

int read_buffer(int fd, char *buf, int count)
{
    char *pts = buf;
    int  status = 0, n; 

    if (count < 0) return (-1);

    while (status != count) { 
        n = read(fd, pts+status, count-status); 
        if (n < 0) return n;
        status += n; 
    }
    return (status);
}


int read_pipe(int socket, char *buf, int maxlen)
{
int status;
int count = 0;

  while (count <= maxlen) {
    if ((status = read_buffer(socket, buf+count, 1)) < 1) {
      printf("Error reading in function read_pipe\n");
      return 0;
    }
    if ((buf[count] == '\n') || buf[count] == '\r') {
      buf[count] = 0;
      return count;
    }
    count++;
  }
  buf[count] = 0;
  return count;
}