1 //Copyright Paul Reiche, Fred Ford. 1992-2002
2 
3 /*
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifndef UQM_STATE_H_
20 #define UQM_STATE_H_
21 
22 #include "port.h"
23 #include "libs/compiler.h"
24 #include <assert.h>
25 
26 #if defined(__cplusplus)
27 extern "C" {
28 #endif
29 
30 extern void InitPlanetInfo (void);
31 extern void UninitPlanetInfo (void);
32 extern void GetPlanetInfo (void);
33 extern void PutPlanetInfo (void);
34 
35 extern void InitGroupInfo (BOOLEAN FirstTime);
36 extern void UninitGroupInfo (void);
37 extern BOOLEAN GetGroupInfo (DWORD offset, BYTE which_group);
38 extern DWORD PutGroupInfo (DWORD offset, BYTE which_group);
39 #define GROUPS_RANDOM  ((DWORD)(0L))
40 #define GROUPS_ADD_NEW ((DWORD)(~0L))
41 #define GROUP_LIST     ((BYTE)0)
42 #define GROUP_INIT_IP  ((BYTE)~0)
43 		// Initialize IP group list (ip_group_q) from the actual groups
44 		// (not GROUP_LIST) in one of the state files
45 #define GROUP_LOAD_IP  GROUP_LIST
46 		// Read IP group list into ip_group_q from the list entry
47 		// (GROUP_LIST) in one of the state files
48 #define GROUP_SAVE_IP  ((BYTE)~0)
49 		// Write IP group list from ip_group_q to the list entry
50 		// (GROUP_LIST) in one of the state files
51 extern void BuildGroups (void);
52 
53 typedef struct GAME_STATE_FILE GAME_STATE_FILE;
54 
55 #define STARINFO_FILE 0
56 	//"starinfo.dat"
57 #define STAR_BUFSIZE (NUM_SOLAR_SYSTEMS * sizeof (DWORD) \
58 		+ 3800 * (3 * sizeof (DWORD)))
59 #define RANDGRPINFO_FILE 1
60 	//"randgrp.dat"
61 #define RAND_BUFSIZE (4 * 1024)
62 #define DEFGRPINFO_FILE 2
63 	//"defgrp.dat"
64 #define DEF_BUFSIZE (10 * 1024)
65 
66 typedef enum
67 {
68 	STARINFO,
69 	RANDGRPINFO,
70 	DEFGRPINFO
71 } INFO_TYPE;
72 
73 GAME_STATE_FILE* OpenStateFile (int stateFile, const char *mode);
74 void CloseStateFile (GAME_STATE_FILE *fp);
75 void DeleteStateFile (int stateFile);
76 DWORD LengthStateFile (GAME_STATE_FILE *fp);
77 int ReadStateFile (void *lpBuf, COUNT size, COUNT count, GAME_STATE_FILE *fp);
78 int WriteStateFile (const void *lpBuf, COUNT size, COUNT count, GAME_STATE_FILE *fp);
79 int SeekStateFile (GAME_STATE_FILE *fp, long offset, int whence);
80 
81 static inline COUNT
sread_8(GAME_STATE_FILE * fp,BYTE * v)82 sread_8 (GAME_STATE_FILE *fp, BYTE *v)
83 {
84 	BYTE t;
85 	if (!v) /* read value ignored */
86 		v = &t;
87 	return ReadStateFile (v, 1, 1, fp);
88 }
89 
90 static inline COUNT
sread_16(GAME_STATE_FILE * fp,UWORD * v)91 sread_16 (GAME_STATE_FILE *fp, UWORD *v)
92 {
93 	UWORD t;
94 	if (!v) /* read value ignored */
95 		v = &t;
96 	return ReadStateFile (v, 2, 1, fp);
97 }
98 
99 static inline COUNT
sread_16s(GAME_STATE_FILE * fp,SWORD * v)100 sread_16s (GAME_STATE_FILE *fp, SWORD *v)
101 {
102 	UWORD t;
103 	COUNT ret;
104 	ret = sread_16 (fp, &t);
105 	// unsigned to signed conversion
106 	if (v)
107 		*v = t;
108 	return ret;
109 }
110 
111 static inline COUNT
sread_32(GAME_STATE_FILE * fp,DWORD * v)112 sread_32 (GAME_STATE_FILE *fp, DWORD *v)
113 {
114 	DWORD t;
115 	if (!v) /* read value ignored */
116 		v = &t;
117 	return ReadStateFile (v, 4, 1, fp);
118 }
119 
120 static inline COUNT
sread_a32(GAME_STATE_FILE * fp,DWORD * ar,COUNT count)121 sread_a32 (GAME_STATE_FILE *fp, DWORD *ar, COUNT count)
122 {
123 	assert (ar != NULL);
124 
125 	for ( ; count > 0; --count, ++ar)
126 	{
127 		if (sread_32 (fp, ar) != 1)
128 			return 0;
129 	}
130 	return 1;
131 }
132 
133 static inline COUNT
swrite_8(GAME_STATE_FILE * fp,BYTE v)134 swrite_8 (GAME_STATE_FILE *fp, BYTE v)
135 {
136 	return WriteStateFile (&v, 1, 1, fp);
137 }
138 
139 static inline COUNT
swrite_16(GAME_STATE_FILE * fp,UWORD v)140 swrite_16 (GAME_STATE_FILE *fp, UWORD v)
141 {
142 	return WriteStateFile (&v, 2, 1, fp);
143 }
144 
145 static inline COUNT
swrite_32(GAME_STATE_FILE * fp,DWORD v)146 swrite_32 (GAME_STATE_FILE *fp, DWORD v)
147 {
148 	return WriteStateFile (&v, 4, 1, fp);
149 }
150 
151 static inline COUNT
swrite_a32(GAME_STATE_FILE * fp,const DWORD * ar,COUNT count)152 swrite_a32 (GAME_STATE_FILE *fp, const DWORD *ar, COUNT count)
153 {
154 	for ( ; count > 0; --count, ++ar)
155 	{
156 		if (swrite_32 (fp, *ar) != 1)
157 			return 0;
158 	}
159 	return 1;
160 }
161 
162 #if defined(__cplusplus)
163 }
164 #endif
165 
166 #endif /* UQM_STATE_H_ */
167