summaryrefslogtreecommitdiff
path: root/src/uqm/state.h
blob: e469cba04f90d8cc13dd643eee83b3ca413ffe81 (plain)
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
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef UQM_STATE_H_
#define UQM_STATE_H_

#include "port.h"
#include "libs/compiler.h"
#include <assert.h>

#if defined(__cplusplus)
extern "C" {
#endif

extern void InitPlanetInfo (void);
extern void UninitPlanetInfo (void);
extern void GetPlanetInfo (void);
extern void PutPlanetInfo (void);

extern void InitGroupInfo (BOOLEAN FirstTime);
extern void UninitGroupInfo (void);
extern BOOLEAN GetGroupInfo (DWORD offset, BYTE which_group);
extern DWORD PutGroupInfo (DWORD offset, BYTE which_group);
#define GROUPS_RANDOM  ((DWORD)(0L))
#define GROUPS_ADD_NEW ((DWORD)(~0L))
#define GROUP_LIST     ((BYTE)0)
#define GROUP_INIT_IP  ((BYTE)~0)
		// Initialize IP group list (ip_group_q) from the actual groups
		// (not GROUP_LIST) in one of the state files
#define GROUP_LOAD_IP  GROUP_LIST
		// Read IP group list into ip_group_q from the list entry
		// (GROUP_LIST) in one of the state files
#define GROUP_SAVE_IP  ((BYTE)~0)
		// Write IP group list from ip_group_q to the list entry
		// (GROUP_LIST) in one of the state files
extern void BuildGroups (void);

typedef struct GAME_STATE_FILE GAME_STATE_FILE;

#define STARINFO_FILE 0
	//"starinfo.dat"
#define STAR_BUFSIZE (NUM_SOLAR_SYSTEMS * sizeof (DWORD) \
		+ 3800 * (3 * sizeof (DWORD)))
#define RANDGRPINFO_FILE 1
	//"randgrp.dat"
#define RAND_BUFSIZE (4 * 1024)
#define DEFGRPINFO_FILE 2
	//"defgrp.dat"
#define DEF_BUFSIZE (10 * 1024)

typedef enum
{
	STARINFO,
	RANDGRPINFO,
	DEFGRPINFO
} INFO_TYPE;

GAME_STATE_FILE* OpenStateFile (int stateFile, const char *mode);
void CloseStateFile (GAME_STATE_FILE *fp);
void DeleteStateFile (int stateFile);
DWORD LengthStateFile (GAME_STATE_FILE *fp);
int ReadStateFile (void *lpBuf, COUNT size, COUNT count, GAME_STATE_FILE *fp);
int WriteStateFile (const void *lpBuf, COUNT size, COUNT count, GAME_STATE_FILE *fp);
int SeekStateFile (GAME_STATE_FILE *fp, long offset, int whence);

static inline COUNT
sread_8 (GAME_STATE_FILE *fp, BYTE *v)
{
	BYTE t;
	if (!v) /* read value ignored */
		v = &t;
	return ReadStateFile (v, 1, 1, fp);
}

static inline COUNT
sread_16 (GAME_STATE_FILE *fp, UWORD *v)
{
	UWORD t;
	if (!v) /* read value ignored */
		v = &t;
	return ReadStateFile (v, 2, 1, fp);
}

static inline COUNT
sread_16s (GAME_STATE_FILE *fp, SWORD *v)
{
	UWORD t;
	COUNT ret;
	ret = sread_16 (fp, &t);
	// unsigned to signed conversion
	if (v)
		*v = t;
	return ret;
}

static inline COUNT
sread_32 (GAME_STATE_FILE *fp, DWORD *v)
{
	DWORD t;
	if (!v) /* read value ignored */
		v = &t;
	return ReadStateFile (v, 4, 1, fp);
}

static inline COUNT
sread_a32 (GAME_STATE_FILE *fp, DWORD *ar, COUNT count)
{
	assert (ar != NULL);

	for ( ; count > 0; --count, ++ar)
	{
		if (sread_32 (fp, ar) != 1)
			return 0;
	}
	return 1;
}

static inline COUNT
swrite_8 (GAME_STATE_FILE *fp, BYTE v)
{
	return WriteStateFile (&v, 1, 1, fp);
}

static inline COUNT
swrite_16 (GAME_STATE_FILE *fp, UWORD v)
{
	return WriteStateFile (&v, 2, 1, fp);
}

static inline COUNT
swrite_32 (GAME_STATE_FILE *fp, DWORD v)
{
	return WriteStateFile (&v, 4, 1, fp);
}

static inline COUNT
swrite_a32 (GAME_STATE_FILE *fp, const DWORD *ar, COUNT count)
{
	for ( ; count > 0; --count, ++ar)
	{
		if (swrite_32 (fp, *ar) != 1)
			return 0;
	}
	return 1;
}

#if defined(__cplusplus)
}
#endif

#endif /* UQM_STATE_H_ */