code duplication
[netris.git] / netris.h
1 /*
2  * Netris -- A free networked version of T*tris
3  * Copyright (C) 1994-1996,1999  Mark H. Weaver <mhw@netris.org>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #ifndef __NETRIS_H
21 #define __NETRIS_H
22
23 #include "config.h"
24
25 #include <sys/time.h>
26 #include <assert.h>
27 #include <stdio.h>
28 #include <signal.h>
29
30 #define version_string "0.8"
31
32 #ifdef NOEXT  //prevent re-declaration
33 # define EXT
34 #else
35 # define EXT extern
36 #endif
37
38 #ifdef HAS_SIGPROCMASK
39 typedef sigset_t MySigSet;
40 #else
41 typedef int MySigSet;
42 #endif
43
44 /*
45  * The following definitions are to ensure network compatibility even if
46  * the sizes of ints and shorts are different.  I'm not sure exactly how
47  * to deal with this problem, so I've added an abstraction layer.
48  */
49
50 typedef short netint2;
51 typedef long netint4;
52
53 #define hton2(x) htons(x)
54 #define hton4(x) htonl(x)
55 #define ntoh2(x) ntohs(x)
56 #define ntoh4(x) ntohl(x)
57
58 /* Protocol versions */
59 #define MAJOR_VERSION     1
60 #define PROTOCOL_VERSION  6
61
62 #define DEFAULT_PORT 9284  /* Very arbitrary */
63
64 #define CONFIG_FILE "netris.conf"
65
66 //#define DEFAULT_KEYS "hlkj mnfp^ltq"
67 //#define DEFAULT_KEYS "4685 02fp^l^mq"
68 #define DEFAULT_KEYS "dcaf xb^fp^l^mq"
69
70 #define MAX_BOARD_WIDTH    32
71 #define MAX_BOARD_HEIGHT   64
72 #define MAX_SCREENS         9 //8 players
73
74 /* Event masks */
75 #define EM_alarm       000001
76 #define EM_key         000002
77 #define EM_net         000004
78 #define EM_connect     000020
79 #define EM_any         000777
80
81 typedef enum _BlockTypeA {
82         BT_shadow, BT_none,
83         BT_S, BT_L, BT_I, BT_O, BT_Z, BT_J, BT_T,
84         BT_wall, BT_len
85 } BlockTypeA;
86 typedef enum _FDType { FT_read, FT_write, FT_except, FT_len } FDType;
87 typedef enum _MyEventType {
88         E_none, E_alarm, E_key, E_connect, E_net, E_lostConn
89 } MyEventType;
90 typedef enum _NetPacketType {
91         NP_endConn,     //client/server quits
92         NP_byeBye,      //unused atm
93         NP_error,       //handshake error
94         NP_hello,       //check versions
95         NP_gamedata,    //game options
96
97         NP_start,       //game ok to start
98         NP_pause,       //player (un)pauses
99         NP_stop,        //game ended
100         NP_newPlayer,   //add new player
101         NP_team,        //player switched teams
102         NP_argghhh,     //player died
103         NP_part,        //player left
104
105         NP_msg,         //chat message
106
107         NP_newPiece,    //new piece info
108         NP_rotright,    //rotate piece clockwise
109         NP_rotleft,     //rotate piece counterclockwise
110         NP_left,        //move piece left
111         NP_right,       //move piece right
112         NP_down,        //move piece one down
113         NP_drop,        //drop piece to bottom
114         NP_clear,       //line cleared
115         NP_insertJunk,  //player added junk
116
117         NP_giveJunk     //player has to add junk
118 } NetPacketType;
119
120 typedef struct {
121         short sender, uid;
122         NetPacketType type;
123         int size;
124         void *data;
125 } _netEvent;
126 typedef struct _MyEvent {
127         MyEventType type;
128         union {
129                 char key;
130                 _netEvent net;
131         } u;
132 } MyEvent;
133
134 struct _EventGenRec;
135 typedef MyEventType (*EventGenFunc)(struct _EventGenRec *gen, MyEvent *event);
136
137 typedef struct _EventGenRec {
138         struct _EventGenRec *next;
139         int ready;
140         FDType fdType;
141         int fd;
142         EventGenFunc func;
143         int mask;
144         short player;
145         char buf[512];
146         int bufSize, bufGoal;
147 } EventGenRec;
148
149 /* NP_startConn flags */
150 #define SCF_paused  1
151
152 typedef struct {
153         int alive;
154         char name[16];
155         int flags;
156         int team;
157         int boardHeight, boardWidth, boardVisible;
158         int curX, curY;
159         char curShape, nextShape;
160         struct score_t {
161                 short level;
162                 long score;
163                 int pieces, lines, adds;
164         } score;
165         char host[256];  //last
166 } player_t;
167 EXT player_t Players[MAX_SCREENS];
168 EXT short me;
169 EXT short maxPlayer;
170 EXT int spied; //in player.flags
171
172 #define DEFAULT_INTERVAL  1000000  /* Step-down interval in microseconds */
173 #define SPEEDINC          1.2
174 #define SPEEDMINIMUM      40000
175
176 typedef enum _GameType { GT_onePlayer, GT_classicTwo, GT_len } GameType;
177 typedef struct {
178         GameType type;
179         int gravity;     //1
180         int started;     //2
181         int continuous;  //3
182         long seed;       //4
183         float shapes[7];
184         int initspeed;   //5
185         int speed;
186 } game_t;
187 EXT game_t Game;
188
189 #define MSG_WIDTH 128
190
191 EXT short port; // => just in client.c, parameter to inet connect
192
193 static const char *teamname[] = {
194         "", "Green", "Cyan", "Blue", "Purple",
195         "Red", "Grey", "White", "*Orange"
196 };
197
198 #endif //__NETRIS_H
199