1 #include <unistd.h>
2 #include <GL/glut.h>
3 
4 
5 #include "globals.h"
6 #include "sizes.h"
7 #include "sound.h"
8 #include "network.h"
9 #include "netdata.h"
10 #include "key.h"
11 #include "menu.h"
12 #include "thread.h"
13 #include "score.h"
14 #include "configuration.h"
15 #include "serverconfig.h"
16 #include "object.h"
17 #include "screen.h"
18 #include "netvariables.h"
19 #include "ship.h"
20 
21 
22 void KeyDown(unsigned char key, int x, int y);
23 void KeyUp(unsigned char key, int x, int y);
24 void MainMenuSpecialKey(int key, int x, int y);
25 void ConfMenuSpecialKey(int key, int x, int y);
26 void ScoreMenuSpecialKey(int key, int x, int y);
27 void MainMenuKey(unsigned char key, int x, int y);
28 void ScoreMenuKey(unsigned char key, int x, int y);
29 void SetKeyValueKey(unsigned char key, int x, int y);
30 void TextEntryKey(unsigned char key, int x, int y);
31 void ScoreEntryKey(unsigned char key, int x, int y);
32 void IntTextEntryKey(unsigned char key, int x, int y);
33 
34 
35 ControlStruct controls;
36 
37 
38 // Function to handle key presses while game is running
KeyDown(unsigned char key,int x,int y)39 void KeyDown(unsigned char key, int x, int y)
40 {
41   int rc;
42 
43   if (!Ships[myShip]) {
44     return;
45   }
46 
47   if (key==controls.Fire) {
48     if (Ships[myShip]->Lives) {
49       TellServer(FIRE_KEY, 1);
50     } else {
51       TellServer(ABORT_KEY, 1);
52       GameStop=1;
53 
54       CleanupSounds();
55 
56       rc=pthread_join(ServerThread, NULL);
57       if (!rc) {
58         ReadServerConfig(ServerConfigFileName);
59       }
60       pthread_mutex_unlock(&ClientLock);
61 
62       if (CheckForHighScore()) {
63         // If a high score is obtained, ask player to enter name for list
64         playsound(HighScoreSound);
65         *HighScoreName=0;
66         TextValue=HighScoreName;
67         MaxTextLength=MAX_PLAYERNAME_LENGTH-1;
68         InitMatrixMode();
69         glutKeyboardFunc(ScoreEntryKey);
70         glutKeyboardUpFunc(NULL);
71         glutSpecialFunc(NULL);
72         glutIdleFunc(NULL);
73         glutDisplayFunc(ScoreEntry);
74         ScoreEntry();
75       } else {
76         InitMatrixMode();
77         SetupMainMenu();
78       }
79     }
80   }
81 
82   /*
83     if (key==controls.xForward) {
84     KeyValues.xForwardKey=1;
85     TellServer(X_FORWARD_KEY, 1);
86     } else if (key==controls.xBackward) {
87     KeyValues.xBackwardKey=1;
88     TellServer(X_BACKWARD_KEY, 1);
89     }
90     if (key==controls.yForward) {
91     KeyValues.yForwardKey=1;
92     TellServer(Y_FORWARD_KEY, 1);
93     } else if (key==controls.yBackward) {
94     KeyValues.yBackwardKey=1;
95     TellServer(Y_BACKWARD_KEY, 1);
96     }
97   */
98   if (key==controls.zForward) {
99     Ships[myShip]->Thrusting=1;
100     KeyValues.zForwardKey=1;
101     TellServer(Z_FORWARD_KEY, 1);
102     if (Ships[myShip]) {
103       if (Ships[myShip]->Alive) {
104         if (ThrustSoundNum==-1) {
105           ThrustSoundNum=playsoundinloop(ThrustSound);
106         }
107       }
108     }
109   } else if (key==controls.zBackward) {
110     KeyValues.zBackwardKey=1;
111     TellServer(Z_BACKWARD_KEY, 1);
112   }
113 
114   if (key==controls.xFRotate) {
115     KeyValues.xFRotateKey=1;
116     TellServer(X_FROTATE_KEY, 1);
117   } else if (key==controls.xBRotate) {
118     KeyValues.xBRotateKey=1;
119     TellServer(X_BROTATE_KEY, 1);
120   }
121   if (key==controls.yFRotate) {
122     KeyValues.yFRotateKey=1;
123     TellServer(Y_FROTATE_KEY, 1);
124   } else if (key==controls.yBRotate) {
125     KeyValues.yBRotateKey=1;
126     TellServer(Y_BROTATE_KEY, 1);
127   }
128   if (key==controls.zFRotate) {
129     KeyValues.zFRotateKey=1;
130     TellServer(Z_FROTATE_KEY, 1);
131   } else if (key==controls.zBRotate) {
132     KeyValues.zBRotateKey=1;
133     TellServer(Z_BROTATE_KEY, 1);
134   }
135 
136   if (key==controls.Shield) {
137     if (Ships[myShip]) {
138       if (!Ships[myShip]->Shield && Ships[myShip]->Alive) {
139         playsound(NoShieldSound);
140       }
141       if (Ships[myShip]->Alive && Ships[myShip]->Shield) {
142         if (ShieldSoundNum==-1) {
143           ShieldSoundNum=playsoundinloop(ShieldOnSound);
144         }
145       }
146     }
147     KeyValues.ShieldKey=1;
148     TellServer(SHIELD_KEY, 1);
149   }
150 
151   if (key==controls.Warp) {
152     KeyValues.WarpKey=1;
153     TellServer(WARP_KEY, 1);
154   }
155 
156   if (key==controls.Nuke) {
157     KeyValues.NukeKey=1;
158     TellServer(NUKE_KEY, 1);
159   }
160 
161   if (key==controls.Score) {
162     ShowScore=1;
163   }
164 
165   if (key==controls.rView) {
166     ReverseView=1;
167   }
168 
169   if (key==controls.Quit) {
170     AbortGame();
171   }
172 }
173 
174 
175 // Function to handle game releases while game is running
KeyUp(unsigned char key,int x,int y)176 void KeyUp(unsigned char key, int x, int y)
177 {
178   /*
179     if (key==controls.xForward) {
180     KeyValues.xForwardKey=0;
181     TellServer(X_FORWARD_KEY, 0);
182     } else if (key==controls.xBackward) {
183     KeyValues.xBackwardKey=0;
184     TellServer(X_BACKWARD_KEY, 0);
185     }
186     if (key==controls.yForward) {
187     KeyValues.yForwardKey=0;
188     TellServer(Y_FORWARD_KEY, 0);
189     } else if (key==controls.yBackward) {
190     KeyValues.yBackwardKey=0;
191     TellServer(Y_BACKWARD_KEY, 0);
192     }
193   */
194   if (key==controls.zForward) {
195     Ships[myShip]->Thrusting=0;
196     KeyValues.zForwardKey=0;
197     TellServer(Z_FORWARD_KEY, 0);
198     if (ThrustSoundNum!=-1) {
199       stopsoundinloop(ThrustSoundNum);
200       ThrustSoundNum=-1;
201     }
202   } else if (key==controls.zBackward) {
203     KeyValues.zBackwardKey=0;
204     TellServer(Z_BACKWARD_KEY, 0);
205   }
206 
207   if (key==controls.xFRotate) {
208     KeyValues.xFRotateKey=0;
209     TellServer(X_FROTATE_KEY, 0);
210   } else if (key==controls.xBRotate) {
211     KeyValues.xBRotateKey=0;
212     TellServer(X_BROTATE_KEY, 0);
213   }
214   if (key==controls.yFRotate) {
215     KeyValues.yFRotateKey=0;
216     TellServer(Y_FROTATE_KEY, 0);
217   } else if (key==controls.yBRotate) {
218     KeyValues.yBRotateKey=0;
219     TellServer(Y_BROTATE_KEY, 0);
220   }
221   if (key==controls.zFRotate) {
222     KeyValues.zFRotateKey=0;
223     TellServer(Z_FROTATE_KEY, 0);
224   } else if (key==controls.zBRotate) {
225     KeyValues.zBRotateKey=0;
226     TellServer(Z_BROTATE_KEY, 0);
227   }
228 
229   if (key==controls.Shield) {
230     KeyValues.ShieldKey=0;
231     TellServer(SHIELD_KEY, 0);
232     if (ShieldSoundNum!=-1) {
233       stopsoundinloop(ShieldSoundNum);
234       ShieldSoundNum=-1;
235     }
236   }
237 
238   if (key==controls.Score) {
239     ShowScore=0;
240   }
241 
242   if (key==controls.rView) {
243     ReverseView=0;
244   }
245 }
246 
247 
MainMenuSpecialKey(int key,int x,int y)248 void MainMenuSpecialKey(int key, int x, int y)
249 {
250   switch (key) {
251   case GLUT_KEY_UP:
252     playsound(BonkSound);
253     MainMenuItemHighlighted--;
254     if (MainMenuItemHighlighted < 0) {
255       MainMenuItemHighlighted=NumMainMenuItems-1;
256     }
257     break;
258   case GLUT_KEY_DOWN:
259     playsound(BonkSound);
260     MainMenuItemHighlighted++;
261     if (MainMenuItemHighlighted >= NumMainMenuItems) {
262       MainMenuItemHighlighted=0;
263     }
264     break;
265   }
266   MainMenu();
267 }
268 
269 
ConfMenuSpecialKey(int key,int x,int y)270 void ConfMenuSpecialKey(int key, int x, int y)
271 {
272   switch (key) {
273   case GLUT_KEY_UP:
274     playsound(BonkSound);
275     ConfMenuItemHighlighted--;
276     if (ConfMenuItemHighlighted < 0) {
277       ConfMenuItemHighlighted=NumConfMenuItems-1;
278     }
279     break;
280   case GLUT_KEY_DOWN:
281     playsound(BonkSound);
282     ConfMenuItemHighlighted++;
283     if (ConfMenuItemHighlighted >= NumConfMenuItems) {
284       ConfMenuItemHighlighted=0;
285     }
286     break;
287   }
288   ConfMenu();
289 }
290 
291 
ScoreMenuSpecialKey(int key,int x,int y)292 void ScoreMenuSpecialKey(int key, int x, int y)
293 {
294   switch (key) {
295   case GLUT_KEY_UP:
296     playsound(BonkSound);
297     ScoreMenuItemHighlighted--;
298     if (ScoreMenuItemHighlighted < 0) {
299       ScoreMenuItemHighlighted=NumScoreMenuItems-1;
300     }
301     break;
302   case GLUT_KEY_DOWN:
303     playsound(BonkSound);
304     ScoreMenuItemHighlighted++;
305     if (ScoreMenuItemHighlighted >= NumScoreMenuItems) {
306       ScoreMenuItemHighlighted=0;
307     }
308     break;
309   }
310   ScoreMenu();
311 }
312 
313 
MainMenuKey(unsigned char key,int x,int y)314 void MainMenuKey(unsigned char key, int x, int y)
315 {
316   if (key=='\r') {
317     MainMenuItemSelected();
318   }
319 }
320 
321 
ConfMenuKey(unsigned char key,int x,int y)322 void ConfMenuKey(unsigned char key, int x, int y)
323 {
324   if (key=='\r') {
325     ConfMenuItemSelected();
326   }
327 }
328 
329 
ScoreMenuKey(unsigned char key,int x,int y)330 void ScoreMenuKey(unsigned char key, int x, int y)
331 {
332   if (key=='\r') {
333     ScoreMenuItemSelected();
334   }
335 }
336 
337 
SetKeyValueKey(unsigned char key,int x,int y)338 void SetKeyValueKey(unsigned char key, int x, int y)
339 {
340   *KeyValue=key;
341 
342   glutKeyboardFunc(ConfMenuKey);
343   glutSpecialFunc(ConfMenuSpecialKey);
344   glutIdleFunc(NULL);
345   glutDisplayFunc(ConfMenu);
346   ConfMenu();
347 }
348 
349 
TextEntryKey(unsigned char key,int x,int y)350 void TextEntryKey(unsigned char key, int x, int y)
351 {
352   int i;
353 
354   if (key=='\b') {
355     i=strlen(TextValue);
356     *(TextValue+i-1)='\0';
357     TextEntry();
358   } else if (key=='\r') {
359     glutKeyboardFunc(ConfMenuKey);
360     glutSpecialFunc(ConfMenuSpecialKey);
361     glutIdleFunc(NULL);
362     glutDisplayFunc(ConfMenu);
363     ConfMenu();
364   } else {
365     i=strlen(TextValue);
366     if (i<MaxTextLength) {
367       *(TextValue+i)=key;
368       *(TextValue+i+1)='\0';
369     }
370     TextEntry();
371   }
372 }
373 
374 
ScoreEntryKey(unsigned char key,int x,int y)375 void ScoreEntryKey(unsigned char key, int x, int y)
376 {
377   int i;
378 
379   if (key=='\b') {
380     i=strlen(TextValue);
381     *(TextValue+i-1)='\0';
382     ScoreEntry();
383   } else if (key=='\r') {
384     playsound(LuckySound);
385     AddHighScore(TextValue);
386     SetupScoreMenu();
387   } else {
388     i=strlen(TextValue);
389     if (i<MaxTextLength) {
390       *(TextValue+i)=key;
391       *(TextValue+i+1)='\0';
392     }
393     ScoreEntry();
394   }
395 }
396 
397 
IntTextEntryKey(unsigned char key,int x,int y)398 void IntTextEntryKey(unsigned char key, int x, int y)
399 {
400   int i;
401   char Value[80];
402 
403   sprintf(Value, "%d", *IntValue);
404 
405   if (key=='\b') {
406     i=strlen(Value);
407     *(Value+i-1)='\0';
408     *IntValue=atoi(Value);
409     IntTextEntry();
410   } else if (key=='\r') {
411     glutKeyboardFunc(ConfMenuKey);
412     glutSpecialFunc(ConfMenuSpecialKey);
413     glutIdleFunc(NULL);
414     glutDisplayFunc(ConfMenu);
415     *IntValue=atoi(Value);
416     if (NewScreenWidth!=ScreenWidth || NewScreenHeight!=ScreenHeight) {
417       ScreenWidth=NewScreenWidth;
418       ScreenHeight=NewScreenHeight;
419       glutReshapeWindow(ScreenWidth, ScreenHeight);
420       InitMainMenu();
421       InitConfMenu();
422     }
423     if (newvolume!=volume) {
424       volume=newvolume;
425       setvolume();
426     }
427     ConfMenu();
428   } else if (key>='0' && key <='9') {
429     i=strlen(Value);
430     if (i<MaxTextLength) {
431       *(Value+i)=key;
432       *(Value+i+1)='\0';
433       *IntValue=atoi(Value);
434     } else if (i==1 && *IntValue==0) {
435       *(Value)=key;
436       *(Value+1)='\0';
437       *IntValue=atoi(Value);
438     }
439     IntTextEntry();
440   }
441 }
442