30ce772a3262e6e0a2d5e12bd507a6172b31dbeb
[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  * $Id: netris.h,v 1.28 1999/05/16 06:56:29 mhw Exp $
20  */
21
22 #ifndef NETRIS_H
23 #define NETRIS_H
24
25 #include "config.h"
26 #include <sys/time.h>
27 #include <assert.h>
28 #include <stdio.h>
29 #include <signal.h>
30
31 #define version_string          "0.5.810"
32
33 #define ExtFunc         /* Marks functions that need prototypes */
34
35 #ifdef NOEXT    //prevent re-declaration
36 # define EXT
37 #else
38 # define EXT extern
39 #endif
40
41 #ifdef HAS_SIGPROCMASK
42 typedef sigset_t MySigSet;
43 #else
44 typedef int MySigSet;
45 #endif
46
47 /*
48  * The following definitions are to ensure network compatibility even if
49  * the sizes of ints and shorts are different.  I'm not sure exactly how
50  * to deal with this problem, so I've added an abstraction layer.
51  */
52
53 typedef short netint2;
54 typedef long netint4;
55
56 #define hton2(x) htons(x)
57 #define hton4(x) htonl(x)
58 #define ntoh2(x) ntohs(x)
59 #define ntoh4(x) ntohl(x)
60
61 /* Protocol versions */
62 #define MAJOR_VERSION           1       
63 #define PROTOCOL_VERSION        4
64 #define ROBOT_VERSION           1
65
66 #define DEFAULT_PORT 9284       /* Very arbitrary */
67
68 #define CONFIG_FILE "netris.conf"
69
70 //#define DEFAULT_KEYS "hlkj mfp^lq"
71 //#define DEFAULT_KEYS "4685 2fp^lq"
72 #define DEFAULT_KEYS "dcaf b^fp^lq"
73
74 #define MAX_BOARD_WIDTH         32
75 #define MAX_BOARD_HEIGHT        64
76 #define MAX_SCREENS                     9 //8 players
77
78 /* Event masks */
79 #define EM_alarm                        000001
80 #define EM_key                          000002
81 #define EM_net                          000004
82 #define EM_robot                        000010
83 #define EM_any                          000777
84
85 typedef enum _GameType { GT_onePlayer, GT_classicTwo, GT_len } GameType;
86 typedef enum _BlockTypeA {
87         BT_none, BT_white, BT_blue, BT_magenta, BT_cyan, BT_yellow, BT_green,
88         BT_red, BT_wall, BT_len
89 } BlockTypeA;
90 typedef enum _Dir { D_down, D_right, D_up, D_left } Dir;
91 typedef enum _Cmd { C_end, C_forw, C_back, C_left, C_right, C_plot } Cmd;
92 typedef enum _FDType { FT_read, FT_write, FT_except, FT_len } FDType;
93 typedef enum _MyEventType {
94         E_none, E_alarm, E_key, E_net, E_lostConn, E_robot, E_lostRobot
95 } MyEventType;
96 typedef enum _NetPacketType {
97         NP_endConn, NP_byeBye,
98         NP_error, NP_hello, NP_gamedata, NP_newPlayer, NP_goAhead,
99         NP_pause, NP_argghhh, NP_giveJunk, NP_newPiece, NP_down, NP_left, NP_right,
100         NP_rotright, NP_rotleft, NP_drop, NP_clear, NP_insertJunk
101 } NetPacketType;
102
103 typedef signed char BlockType;
104
105 typedef struct _MyEvent {
106         MyEventType type;
107         union {
108                 char key;
109                 struct {
110                         short sender, uid;
111                         NetPacketType type;
112                         int size;
113                         void *data;
114                 } net;
115                 struct {
116                         int size;
117                         char *data;
118                 } robot;
119         } u;
120 } MyEvent;
121
122 struct _EventGenRec;
123 typedef MyEventType (*EventGenFunc)(struct _EventGenRec *gen, MyEvent *event);
124
125 typedef struct _EventGenRec {
126         struct _EventGenRec *next;
127         int ready;
128         FDType fdType;
129         int fd;
130         EventGenFunc func;
131         int mask;
132         short player;
133         char buf[512];
134         int bufSize, bufGoal;
135 } EventGenRec;
136 EXT EventGenRec netGen[MAX_SCREENS];
137
138 MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event);
139
140 typedef struct _Shape {
141         struct _Shape *rotateTo, *rotateFrom;
142         int initY, initX, mirrored;
143         Dir initDir;
144         BlockType type;
145         Cmd *cmds;
146 } Shape;
147
148 typedef struct _ShapeOption {
149         float weight;
150         Shape *shape;
151 } ShapeOption;
152
153 typedef int (*ShapeDrawFunc)(int scr, int y, int x,
154                                         BlockType type, void *data);
155
156 /* NP_startConn flags */
157 #define SCF_usingRobot          000001
158 #define SCF_fairRobot           000002
159 #define SCF_paused                      000004
160
161 EXT int totalPlayers;
162
163 typedef struct _Player {
164         int alive;
165         char name[16];
166         int flags;
167         int team;
168         int dropmode;
169         int boardHeight, boardWidth, boardVisible;
170         int curX, curY;
171         Shape *curShape, *nextShape;
172         struct _Score {
173                 short level;
174                 long score;
175                 int drops, lines, adds;
176         } score;
177         char host[256]; //last-1
178         int spy;                //last
179 } Player;
180 EXT Player Players[MAX_SCREENS];
181 EXT short me;
182
183 #define DEFAULT_INTERVAL        1000000 /* Step-down interval in microseconds */
184 #define SPEEDINC                        1.2
185 #define SPEEDMINIMUM            40000
186
187 typedef struct __Game {
188         long seed;              //1st
189         int initspeed;  //2nd
190         int speed;
191         int standout, color, ascii;
192 } _Game;
193 EXT _Game Game;
194
195 EXT GameType game;
196 EXT int robotEnable, robotVersion, fairRobot;
197 EXT int protocolVersion;
198
199 EXT int initConn;
200 EXT short port;
201
202 EXT char scratch[1024];
203
204 extern ShapeOption stdOptions[];
205
206 #include "proto.h"
207
208 #endif /* NETRIS_H */
209
210 /*
211  * vi: ts=4 ai
212  * vim: noai si
213  */