Debian Codegraph @

src/starlib.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "starlib.h"
20
21 #include "keyboard.h"
22 #include "mtrand.h"
23 #include "opentyr.h"
24 #include "video.h"
25
26 #include <ctype.h>
27
28 #define starlib_MAX_STARS 1000
29 #define MAX_TYPES 14
30
31 struct JE_StarType
32 {
33 JE_integer spX, spY, spZ;
34 JE_integer lastX, lastY;
35 };
36
37 static int tempX, tempY;
38 static JE_boolean run;
39 static struct JE_StarType star[starlib_MAX_STARS];
40
41 static JE_byte setup;
42 static JE_word stepCounter;
43
44 static JE_word nsp2;
45 static JE_shortint nspVar2Inc;
46
47 /* JE: new sprite pointer */
48 static JE_real nsp;
49 static JE_real nspVarInc;
50 static JE_real nspVarVarInc;
51
52 static JE_word changeTime;
53 static JE_boolean doChange;
54
55 static JE_boolean grayB;
56
57 static JE_integer starlib_speed;
58 static JE_shortint speedChange;
59
60 static JE_byte pColor;
61
62 void JE_starlib_main(void)
63 {
64 int off;
65 JE_word i;
66 JE_integer tempZ;
67 JE_byte tempCol;
68 struct JE_StarType *stars;
69 Uint8 *surf;
70
71 JE_wackyCol();
72
73 grayB = false;
74
75 starlib_speed += speedChange;
76
77 for (stars = star, i = starlib_MAX_STARS; i > 0; stars++, i--)
78 {
79 /* Make a pointer to the screen... */
80 surf = VGAScreen->pixels;
81
82 /* Calculate the offset to where we wish to draw */
83 off = (stars->lastX)+(stars->lastY)*320;
84
85 /* We don't want trails in our star field. Erase the old graphic */
86 if (off >= 640 && off < (320*200)-640)
87 {
88 surf[off] = 0; /* Shade Level 0 */
89
90 surf[off-1] = 0; /* Shade Level 1, 2 */
91 surf[off+1] = 0;
92 surf[off-2] = 0;
93 surf[off+2] = 0;
94
95 surf[off-320] = 0;
96 surf[off+320] = 0;
97 surf[off-640] = 0;
98 surf[off+640] = 0;
99 }
100
101 /* Move star */
102 tempZ = stars->spZ;
103 tempX = (stars->spX / tempZ) + 160;
104 tempY = (stars->spY / tempZ) + 100;
105 tempZ -= starlib_speed;
106
107 /* If star is out of range, make a new one */
108 if (tempZ <= 0 ||
109 tempY == 0 || tempY > 198 ||
110 tempX > 318 || tempX < 1)
111 {
112 stars->spZ = 500;
113
114 JE_newStar();
115
116 stars->spX = tempX;
117 stars->spY = tempY;
118 }
119 else /* Otherwise, update & draw it */
120 {
121 stars->lastX = tempX;
122 stars->lastY = tempY;
123 stars->spZ = tempZ;
124
125 off = tempX+tempY*320;
126
127 if (grayB)
128 tempCol = tempZ >> 1;
129 else
130 tempCol = pColor+((tempZ >> 4) & 31);
131
132 /* Draw the pixel! */
133 if (off >= 640 && off < (320*200)-640)
134 {
135 surf[off] = tempCol;
136
137 tempCol += 72;
138 surf[off-1] = tempCol;
139 surf[off+1] = tempCol;
140 surf[off-320] = tempCol;
141 surf[off+320] = tempCol;
142
143 tempCol += 72;
144 surf[off-2] = tempCol;
145 surf[off+2] = tempCol;
146 surf[off-640] = tempCol;
147 surf[off+640] = tempCol;
148 }
149 }
150 }
151
152 if (newkey)
153 {
154 char key = 0;
155
156 if ((lastkey_mod & (KMOD_CTRL | KMOD_SHIFT | KMOD_ALT | KMOD_GUI)) == KMOD_NONE)
157 {
158 switch (lastkey_scan)
159 {
160 case SDL_SCANCODE_C: key = 'c'; break;
161 case SDL_SCANCODE_P: key = 'p'; break;
162 case SDL_SCANCODE_S: key = 's'; break;
163 case SDL_SCANCODE_X: key = 'x'; break;
164 case SDL_SCANCODE_1: key = '1'; break;
165 case SDL_SCANCODE_2: key = '2'; break;
166 case SDL_SCANCODE_3: key = '3'; break;
167 case SDL_SCANCODE_4: key = '4'; break;
168 case SDL_SCANCODE_5: key = '5'; break;
169 case SDL_SCANCODE_6: key = '6'; break;
170 case SDL_SCANCODE_7: key = '7'; break;
171 case SDL_SCANCODE_8: key = '8'; break;
172 case SDL_SCANCODE_9: key = '9'; break;
173 case SDL_SCANCODE_0: key = '0'; break;
174 case SDL_SCANCODE_ESCAPE: key = 27; break;
175 case SDL_SCANCODE_MINUS: key = '-'; break;
176 case SDL_SCANCODE_LEFTBRACKET: key = '['; break;
177 case SDL_SCANCODE_RIGHTBRACKET: key = ']'; break;
178 case SDL_SCANCODE_GRAVE: key = '`'; break;
179 case SDL_SCANCODE_KP_MINUS: key = '-'; break;
180 case SDL_SCANCODE_KP_PLUS: key = '+'; break;
181 case SDL_SCANCODE_KP_1: key = '1'; break;
182 case SDL_SCANCODE_KP_2: key = '2'; break;
183 case SDL_SCANCODE_KP_3: key = '3'; break;
184 case SDL_SCANCODE_KP_4: key = '4'; break;
185 case SDL_SCANCODE_KP_5: key = '5'; break;
186 case SDL_SCANCODE_KP_6: key = '6'; break;
187 case SDL_SCANCODE_KP_7: key = '7'; break;
188 case SDL_SCANCODE_KP_8: key = '8'; break;
189 case SDL_SCANCODE_KP_9: key = '9'; break;
190 case SDL_SCANCODE_KP_0: key = '0'; break;
191 default: break;
192 }
193 }
194 else if ((lastkey_mod & KMOD_SHIFT) != KMOD_NONE &&
195 (lastkey_mod & (KMOD_CTRL | KMOD_ALT | KMOD_GUI)) == KMOD_NONE)
196 {
197 switch (lastkey_scan)
198 {
199 case SDL_SCANCODE_C: key = 'C'; break;
200 case SDL_SCANCODE_P: key = 'P'; break;
201 case SDL_SCANCODE_S: key = 'S'; break;
202 case SDL_SCANCODE_X: key = 'X'; break;
203 case SDL_SCANCODE_1: key = '!'; break;
204 case SDL_SCANCODE_2: key = '@'; break;
205 case SDL_SCANCODE_3: key = '#'; break;
206 case SDL_SCANCODE_4: key = '$'; break;
207 case SDL_SCANCODE_EQUALS: key = '+'; break;
208 case SDL_SCANCODE_LEFTBRACKET: key = '{'; break;
209 case SDL_SCANCODE_RIGHTBRACKET: key = '}'; break;
210 default: break;
211 }
212 }
213
214 switch (toupper(key))
215 {
216 case '+':
217 starlib_speed++;
218 speedChange = 0;
219 break;
220 case '-':
221 starlib_speed--;
222 speedChange = 0;
223 break;
224 case '1':
225 JE_changeSetup(1);
226 break;
227 case '2':
228 JE_changeSetup(2);
229 break;
230 case '3':
231 JE_changeSetup(3);
232 break;
233 case '4':
234 JE_changeSetup(4);
235 break;
236 case '5':
237 JE_changeSetup(5);
238 break;
239 case '6':
240 JE_changeSetup(6);
241 break;
242 case '7':
243 JE_changeSetup(7);
244 break;
245 case '8':
246 JE_changeSetup(8);
247 break;
248 case '9':
249 JE_changeSetup(9);
250 break;
251 case '0':
252 JE_changeSetup(10);
253 break;
254 case '!':
255 JE_changeSetup(11);
256 break;
257 case '@':
258 JE_changeSetup(12);
259 break;
260 case '#':
261 JE_changeSetup(13);
262 break;
263 case '$':
264 JE_changeSetup(14);
265 break;
266
267 case 'C':
268 JE_resetValues();
269 break;
270 case 'S':
271 nspVarVarInc = mt_rand_1() * 0.01f - 0.005f;
272 break;
273 case 'X':
274 case 27:
275 run = false;
276 break;
277 case '[':
278 pColor--;
279 break;
280 case ']':
281 pColor++;
282 break;
283 case '{':
284 pColor -= 72;
285 break;
286 case '}':
287 pColor += 72;
288 break;
289 case '`':
290 doChange = !doChange;
291 break;
292 case 'P':
293 wait_noinput(true, false, false);
294 wait_input(true, false, false);
295 break;
296 default:
297 break;
298 }
299 }
300
301 if (doChange)
302 {
303 stepCounter++;
304 if (stepCounter > changeTime)
305 {
306 JE_changeSetup(0);
307 }
308 }
309
310 if ((mt_rand() % 1000) == 1)
311 {
312 nspVarVarInc = mt_rand_1() * 0.01f - 0.005f;
313 }
314
315 nspVarInc += nspVarVarInc;
316 }
317
318 void JE_wackyCol(void)
319 {
320 /* YKS: Does nothing */
321 }
322
323 void JE_starlib_init(void)
324 {
325 static JE_boolean initialized = false;
326
327 if (!initialized)
328 {
329 initialized = true;
330
331 JE_resetValues();
332 JE_changeSetup(2);
333 doChange = true;
334
335 /* RANDOMIZE; */
336 for (int x = 0; x < starlib_MAX_STARS; x++)
337 {
338 star[x].spX = (mt_rand() % 64000) - 32000;
339 star[x].spY = (mt_rand() % 40000) - 20000;
340 star[x].spZ = x+1;
341 }
342 }
343 }
344
345 void JE_resetValues(void)
346 {
347 nsp2 = 1;
348 nspVar2Inc = 1;
349 nspVarInc = 0.1f;
350 nspVarVarInc = 0.0001f;
351 nsp = 0;
352 pColor = 32;
353 starlib_speed = 2;
354 speedChange = 0;
355 }
356
357 void JE_changeSetup(JE_byte setupType)
358 {
359 stepCounter = 0;
360 changeTime = (mt_rand() % 1000);
361
362 if (setupType > 0)
363 setup = setupType;
364 else
365 setup = mt_rand() % (MAX_TYPES + 1);
366
367 if (setup == 1)
368 nspVarInc = 0.1f;
369 if (nspVarInc > 2.2f)
370 nspVarInc = 0.1f;
371 }
372
373 void JE_newStar(void)
374 {
375 if (setup == 0)
376 {
377 tempX = (mt_rand() % 64000) - 32000;
378 tempY = (mt_rand() % 40000) - 20000;
379 }
380 else
381 {
382 nsp = nsp + nspVarInc; /* YKS: < lol */
383 switch (setup)
384 {
385 case 1:
386 tempX = (int)(sinf(nsp / 30) * 20000);
387 tempY = (mt_rand() % 40000) - 20000;
388 break;
389 case 2:
390 tempX = (int)(cosf(nsp) * 20000);
391 tempY = (int)(sinf(nsp) * 20000);
392 break;
393 case 3:
394 tempX = (int)(cosf(nsp * 15) * 100) * ((int)(nsp / 6) % 200);
395 tempY = (int)(sinf(nsp * 15) * 100) * ((int)(nsp / 6) % 200);
396 break;
397 case 4:
398 tempX = (int)(sinf(nsp / 60) * 20000);
399 tempY = (int)(cosf(nsp) * (int)(sinf(nsp / 200) * 300) * 100);
400 break;
401 case 5:
402 tempX = (int)(sinf(nsp / 2) * 20000);
403 tempY = (int)(cosf(nsp) * (int)(sinf(nsp / 200) * 300) * 100);
404 break;
405 case 6:
406 tempX = (int)(sinf(nsp) * 40000);
407 tempY = (int)(cosf(nsp) * 20000);
408 break;
409 case 8:
410 tempX = (int)(sinf(nsp / 2) * 40000);
411 tempY = (int)(cosf(nsp) * 20000);
412 break;
413 case 7:
414 tempX = mt_rand() % 65535;
415 if ((mt_rand() % 2) == 0)
416 tempY = (int)(cosf(nsp / 80) * 10000) + 15000;
417 else
418 tempY = 50000 - (int)(cosf(nsp / 80) * 13000);
419 break;
420 case 9:
421 nsp2 += nspVar2Inc;
422 if ((nsp2 == 65535) || (nsp2 == 0))
423 nspVar2Inc = -nspVar2Inc;
424 tempX = (int)(cosf(sinf(nsp2 / 10.0f) + (nsp / 500)) * 32000);
425 tempY = (int)(sinf(cosf(nsp2 / 10.0f) + (nsp / 500)) * 30000);
426 break;
427 case 10:
428 nsp2 += nspVar2Inc;
429 if ((nsp2 == 65535) || (nsp2 == 0))
430 nspVar2Inc = -nspVar2Inc;
431 tempX = (int)(cosf(sinf(nsp2 / 5.0f) + (nsp / 100)) * 32000);
432 tempY = (int)(sinf(cosf(nsp2 / 5.0f) + (nsp / 100)) * 30000);
433 break;;
434 case 11:
435 nsp2 += nspVar2Inc;
436 if ((nsp2 == 65535) || (nsp2 == 0))
437 nspVar2Inc = -nspVar2Inc;
438 tempX = (int)(cosf(sinf(nsp2 / 1000.0f) + (nsp / 2)) * 32000);
439 tempY = (int)(sinf(cosf(nsp2 / 1000.0f) + (nsp / 2)) * 30000);
440 break;
441 case 12:
442 if (nsp != 0)
443 {
444 nsp2 += nspVar2Inc;
445 if ((nsp2 == 65535) || (nsp2 == 0))
446 nspVar2Inc = -nspVar2Inc;
447 tempX = (int)(cosf(sinf(nsp2 / 2.0f) / (sqrtf(fabsf(nsp)) / 10.0f + 1) + (nsp2 / 100.0f)) * 32000);
448 tempY = (int)(sinf(cosf(nsp2 / 2.0f) / (sqrtf(fabsf(nsp)) / 10.0f + 1) + (nsp2 / 100.0f)) * 30000);
449 }
450 break;
451 case 13:
452 if (nsp != 0)
453 {
454 nsp2 += nspVar2Inc;
455 if ((nsp2 == 65535) || (nsp2 == 0))
456 nspVar2Inc = -nspVar2Inc;
457 tempX = (int)(cosf(sinf(nsp2 / 10.0f) / 2 + (nsp / 20)) * 32000);
458 tempY = (int)(sinf(sinf(nsp2 / 11.0f) / 2 + (nsp / 20)) * 30000);
459 }
460 break;
461 case 14:
462 nsp2 += nspVar2Inc;
463 tempX = (int)((sinf(nsp) + cosf(nsp2 / 1000.0f) * 3) * 12000);
464 tempY = (int)(cosf(nsp) * 10000) + nsp2;
465 break;
466 }
467 }
468 }