1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <dirent.h>
4 #include <signal.h>
5 #include <fcntl.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8
9
10 #include "globals.h"
11 #include "sound.h"
12 #include "sizes.h"
13 #include "status.h"
14 #include "score.h"
15 #include "thread.h"
16 #include "star.h"
17 #include "asteroid.h"
18 #include "texture.h"
19 #include "object.h"
20 #include "ship.h"
21 #include "screen.h"
22 #include "model.h"
23 #include "menu.h"
24 #include "configuration.h"
25 #include "serverconfig.h"
26
27
28 int GameRunning;
29 struct KeyValuesStruct KeyValues;
30 Object *Objects[MAX_OBJECTS];
31 Ship *Ships[MAX_PLAYERS];
32 struct Model *Models[MAX_MODELS];
33 struct Model *AsteroidModels[MAX_OBJECTS];
34 struct Point ZeroPosition;
35 pthread_mutex_t ClientLock;
36 pthread_mutex_t ServerLock;
37
38
39 void LoadSpec(char *dirname, const char *filename);
40 int copyfile(char *ConfDirName, const char *FileName);
41
42
DoInitializations()43 void DoInitializations()
44 {
45 int i;
46 char SpecFileName[1000];
47 char DirName[1000];
48
49 ZeroPosition.x=0.0;
50 ZeroPosition.y=0.0;
51 ZeroPosition.z=0.0;
52
53 for (i=0; i<MAX_MODELS; i++) {
54 Models[i]=NULL;
55 }
56
57 for (i=0; i<MAX_OBJECTS; i++) {
58 AsteroidModels[i]=NULL;
59 }
60
61 InitScreen();
62 InitStars();
63
64 playsound(PrizeAppears);
65
66 // Load the models into memory
67 #ifdef WINDOWS
68 sprintf(DirName, "models/");
69 #else
70 sprintf(DirName, "%s/atr3d/models/", DATADIR);
71 #endif
72
73 LoadSpec(DirName, "ship.spec");
74 LoadSpec(DirName, "shot.spec");
75 LoadSpec(DirName, "a.spec");
76 LoadSpec(DirName, "h.spec");
77 LoadSpec(DirName, "n.spec");
78 LoadSpec(DirName, "s.spec");
79 LoadSpec(DirName, "nuke.spec");
80
81 InvertModel(Models[0], 2);
82 ShrinkModel(Models[0], 1.0/FindFurthestPoint(Models[0]));
83 ShrinkModel(Models[6], 1.0/FindFurthestPoint(Models[6]));
84 ShrinkModel(Models[7], 1.0/FindFurthestPoint(Models[7]));
85 ShrinkModel(Models[8], 1.0/FindFurthestPoint(Models[8]));
86 ShrinkModel(Models[9], 1.0/FindFurthestPoint(Models[9]));
87 ShrinkModel(Models[10], 1.0/FindFurthestPoint(Models[9]));
88
89 for (i=0; i<MAX_OBJECTS; i++) {
90 InitAsteroid(i);
91 }
92
93 InitColors();
94 InitHighScores();
95 ReadHighScores();
96
97 InitMainMenu();
98 InitConfMenu();
99 InitScoreMenu();
100
101 for (i=0; i<MAX_OBJECTS; i++) {
102 Objects[i]=NULL;
103 }
104
105 for (i=0; i<MAX_PLAYERS; i++) {
106 Ships[i]=NULL;
107 }
108
109 GameRunning=0;
110 ShowScore=0;
111 ServerStarted=0;
112
113 pthread_mutex_init(&ClientLock, NULL);
114 pthread_mutex_init(&ServerLock, NULL);
115 }
116
117
LoadSpec(char * dirname,const char * filename)118 void LoadSpec(char *dirname, const char *filename)
119 {
120 FILE *fileptr;
121 char entry[80];
122 char *c;
123 int type;
124 float floatvalue;
125 int i, j, k;
126 char fullfilename[2000];
127
128 sprintf(fullfilename, "%s%s", dirname, filename);
129
130 if (!(fileptr=fopen(fullfilename, "r"))) {
131 printf("Error opening spec file %s\n", filename);
132 exit(1);
133 }
134
135 c=entry;
136 while ((*c=fgetc(fileptr))!=EOF) {
137 if (*c==':' || *c=='\n') {
138 break;
139 }
140 c++;
141 }
142 *c='\0';
143 type=atoi(entry);
144 if (type < MAX_MODELS && type >= 0) {
145 if (Models[type]) {
146 free(Models[type]);
147 }
148 Models[type]=(struct Model *)malloc(sizeof(struct Model));
149 }
150
151 c=entry;
152 while ((*c=fgetc(fileptr))!=EOF) {
153 if (*c==':' || *c=='\n') {
154 break;
155 }
156 c++;
157 }
158 *c='\0';
159 Models[type]->NumSides=atoi(entry);
160 Models[type]->Points=NULL;
161
162 Models[type]->Sides=(struct Triangle *)malloc(sizeof(struct Triangle)*Models[type]->NumSides);
163 for (i=0; i<Models[type]->NumSides; i++) {
164 Models[type]->Sides[i].Vertices[0]=(struct Point *)malloc(sizeof(struct Point));
165 Models[type]->Sides[i].Vertices[1]=(struct Point *)malloc(sizeof(struct Point));
166 Models[type]->Sides[i].Vertices[2]=(struct Point *)malloc(sizeof(struct Point));
167 Models[type]->Sides[i].VertexColors[0]=(struct Color *)malloc(sizeof(struct Color));
168 Models[type]->Sides[i].VertexColors[1]=(struct Color *)malloc(sizeof(struct Color));
169 Models[type]->Sides[i].VertexColors[2]=(struct Color *)malloc(sizeof(struct Color));
170 while (fgetc(fileptr)!='\n');
171
172 for (j=0; j<3; j++) {
173 for (k=0; k<3; k++) {
174 c=entry;
175 while ((*c=fgetc(fileptr))!=EOF) {
176 if (*c==':' || *c=='\n') {
177 break;
178 }
179 c++;
180 }
181 *c='\0';
182
183 if (k==0) {
184 Models[type]->Sides[i].Vertices[j]->x=strtod(entry, NULL);
185 } else if (k==1) {
186 Models[type]->Sides[i].Vertices[j]->y=strtod(entry, NULL);
187 } else if (k==2) {
188 Models[type]->Sides[i].Vertices[j]->z=strtod(entry, NULL);
189 }
190 }
191 for (k=0; k<3; k++) {
192 c=entry;
193 while ((*c=fgetc(fileptr))!=EOF) {
194 if (*c==':' || *c=='\n') {
195 break;
196 }
197 c++;
198 }
199 *c='\0';
200
201 if (k==0) {
202 Models[type]->Sides[i].VertexColors[j]->red=strtod(entry, NULL);
203 } else if (k==1) {
204 Models[type]->Sides[i].VertexColors[j]->green=strtod(entry, NULL);
205 } else if (k==2) {
206 Models[type]->Sides[i].VertexColors[j]->blue=strtod(entry, NULL);
207 }
208 }
209 }
210 }
211 fclose(fileptr);
212 }
213
214
InitKeyValues()215 void InitKeyValues()
216 {
217 KeyValues.FireKey=0;
218 KeyValues.ShieldKey=0;
219 KeyValues.WarpKey=0;
220 /*
221 KeyValues.xForwardKey=0;
222 KeyValues.xBackwardKey=0;
223 KeyValues.yForwardKey=0;
224 KeyValues.yBackwardKey=0;
225 */
226 KeyValues.zForwardKey=0;
227 KeyValues.zBackwardKey=0;
228 KeyValues.xFRotateKey=0;
229 KeyValues.xBRotateKey=0;
230 KeyValues.yFRotateKey=0;
231 KeyValues.yBRotateKey=0;
232 KeyValues.zFRotateKey=0;
233 KeyValues.zBRotateKey=0;
234 }
235
236
SetupConfiguration()237 void SetupConfiguration()
238 {
239 DIR *dir;
240 char ConfDirName[80];
241
242 #ifdef WINDOWS
243 sprintf(ConfDirName, "conf");
244 #else
245 sprintf(ConfDirName, "%s/.atr3d", getenv("HOME"));
246
247 if (!(dir=opendir(ConfDirName))) {
248 if (mkdir(ConfDirName, 0755)) {
249 printf("Error opening atr directory\n");
250 exit(1);
251 }
252 if (!copyfile(ConfDirName, "scores.dat")) {
253 printf("Error creating scores.dat file\n");
254 exit(1);
255 }
256 if (!copyfile(ConfDirName, "ATRConfig")) {
257 printf("Error creating ATRConfig file\n");
258 exit(1);
259 }
260 if (!copyfile(ConfDirName, "ATRServerConfig")) {
261 printf("Error creating ATRServerConfig file\n");
262 exit(1);
263 }
264 if (!copyfile(ConfDirName, "ATRServerSingleConfig")) {
265 printf("Error creating ATRServerSingleConfig file\n");
266 exit(1);
267 }
268 } else {
269 closedir(dir);
270 }
271 #endif
272
273 sprintf(ScoreFileName, "%s/scores.dat", ConfDirName);
274 sprintf(ConfigFileName, "%s/ATRConfig", ConfDirName);
275 sprintf(ServerConfigFileName, "%s/ATRServerConfig", ConfDirName);
276 sprintf(ServerSingleConfigFileName, "%s/ATRServerSingleConfig", ConfDirName);
277 }
278
279
copyfile(char * ConfDirName,const char * FileName)280 int copyfile(char *ConfDirName, const char *FileName)
281 {
282 FILE *oldfd, *newfd;
283 char oldFileName[80], newFileName[80];
284 int c;
285
286 sprintf(oldFileName, "%s/atr3d/conf/%s", DATADIR, FileName);
287 sprintf(newFileName, "%s/%s", ConfDirName, FileName);
288
289 if (!(oldfd=fopen(oldFileName, "r"))) {
290 return(0);
291 }
292 if (!(newfd=fopen(newFileName, "w"))) {
293 fclose(oldfd);
294 return(0);
295 }
296 while ((c=fgetc(oldfd))!=EOF) {
297 fputc(c, newfd);
298 }
299 fclose(oldfd);
300 fclose(newfd);
301
302 return(1);
303 }
304