~ubuntu-branches/debian/stretch/opentyrian/stretch

« back to all changes in this revision

Viewing changes to src/starlib.c

  • Committer: Package Import Robot
  • Author(s): Etienne Millon
  • Date: 2015-03-31 08:48:54 UTC
  • Revision ID: package-import@ubuntu.com-20150331084854-f5a4uoz7uv3vopk6
Tags: upstream-2.1.20130907+dfsg
ImportĀ upstreamĀ versionĀ 2.1.20130907+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "opentyr.h"
 
20
#include "starlib.h"
 
21
 
 
22
#include "keyboard.h"
 
23
#include "mtrand.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
 
 
63
void JE_starlib_main( void )
 
64
{
 
65
        int off;
 
66
        JE_word i;
 
67
        JE_integer tempZ;
 
68
        JE_byte tempCol;
 
69
        struct JE_StarType *stars;
 
70
        Uint8 *surf;
 
71
 
 
72
        JE_wackyCol();
 
73
 
 
74
        grayB = false;
 
75
 
 
76
        starlib_speed += speedChange;
 
77
 
 
78
 
 
79
        for(stars = star, i = starlib_MAX_STARS; i > 0; stars++, i--)
 
80
        {
 
81
                /* Make a pointer to the screen... */
 
82
                surf = VGAScreen->pixels;
 
83
 
 
84
                /* Calculate the offset to where we wish to draw */
 
85
                off = (stars->lastX)+(stars->lastY)*320;
 
86
 
 
87
 
 
88
                /* We don't want trails in our star field.  Erase the old graphic */
 
89
                if (off >= 640 && off < (320*200)-640)
 
90
                {
 
91
                        surf[off] = 0; /* Shade Level 0 */
 
92
 
 
93
                        surf[off-1] = 0; /* Shade Level 1, 2 */
 
94
                        surf[off+1] = 0;
 
95
                        surf[off-2] = 0;
 
96
                        surf[off+2] = 0;
 
97
 
 
98
                        surf[off-320] = 0;
 
99
                        surf[off+320] = 0;
 
100
                        surf[off-640] = 0;
 
101
                        surf[off+640] = 0;
 
102
                }
 
103
 
 
104
                /* Move star */
 
105
                tempZ = stars->spZ;
 
106
                tempX = (stars->spX / tempZ) + 160;
 
107
                tempY = (stars->spY / tempZ) + 100;
 
108
                tempZ -=  starlib_speed;
 
109
 
 
110
 
 
111
                /* If star is out of range, make a new one */
 
112
                if (tempZ <=  0 ||
 
113
                    tempY ==  0 || tempY > 198 ||
 
114
                    tempX > 318 || tempX <   1)
 
115
                {
 
116
                        stars->spZ = 500;
 
117
 
 
118
                        JE_newStar();
 
119
 
 
120
                        stars->spX = tempX;
 
121
                        stars->spY = tempY;
 
122
                }
 
123
                else /* Otherwise, update & draw it */
 
124
                {
 
125
                        stars->lastX = tempX;
 
126
                        stars->lastY = tempY;
 
127
                        stars->spZ = tempZ;
 
128
 
 
129
                        off = tempX+tempY*320;
 
130
 
 
131
                        if (grayB)
 
132
                        {
 
133
                                tempCol = tempZ >> 1;
 
134
                        } else {
 
135
                                tempCol = pColor+((tempZ >> 4) & 31);
 
136
                        }
 
137
 
 
138
                        /* Draw the pixel! */
 
139
                        if (off >= 640 && off < (320*200)-640)
 
140
                        {
 
141
                                surf[off] = tempCol;
 
142
 
 
143
                                tempCol += 72;
 
144
                                surf[off-1] = tempCol;
 
145
                                surf[off+1] = tempCol;
 
146
                                surf[off-320] = tempCol;
 
147
                                surf[off+320] = tempCol;
 
148
 
 
149
                                tempCol += 72;
 
150
                                surf[off-2] = tempCol;
 
151
                                surf[off+2] = tempCol;
 
152
                                surf[off-640] = tempCol;
 
153
                                surf[off+640] = tempCol;
 
154
                        }
 
155
                }
 
156
        }
 
157
 
 
158
        if (newkey)
 
159
        {
 
160
                switch (toupper(lastkey_char))
 
161
                {
 
162
                        case '+':
 
163
                                starlib_speed++;
 
164
                                speedChange = 0;
 
165
                                break;
 
166
                        case '-':
 
167
                                starlib_speed--;
 
168
                                speedChange = 0;
 
169
                                break;
 
170
                        case '1':
 
171
                                JE_changeSetup(1);
 
172
                                break;
 
173
                        case '2':
 
174
                                JE_changeSetup(2);
 
175
                                break;
 
176
                        case '3':
 
177
                                JE_changeSetup(3);
 
178
                                break;
 
179
                        case '4':
 
180
                                JE_changeSetup(4);
 
181
                                break;
 
182
                        case '5':
 
183
                                JE_changeSetup(5);
 
184
                                break;
 
185
                        case '6':
 
186
                                JE_changeSetup(6);
 
187
                                break;
 
188
                        case '7':
 
189
                                JE_changeSetup(7);
 
190
                                break;
 
191
                        case '8':
 
192
                                JE_changeSetup(8);
 
193
                                break;
 
194
                        case '9':
 
195
                                JE_changeSetup(9);
 
196
                                break;
 
197
                        case '0':
 
198
                                JE_changeSetup(10);
 
199
                                break;
 
200
                        case '!':
 
201
                                JE_changeSetup(11);
 
202
                                break;
 
203
                        case '@':
 
204
                                JE_changeSetup(12);
 
205
                                break;
 
206
                        case '#':
 
207
                                JE_changeSetup(13);
 
208
                                break;
 
209
                        case '$':
 
210
                                JE_changeSetup(14);
 
211
                                break;
 
212
 
 
213
                        case 'C':
 
214
                                JE_resetValues();
 
215
                                break;
 
216
                        case 'S':
 
217
                                nspVarVarInc = mt_rand_1() * 0.01f - 0.005f;
 
218
                                break;
 
219
                        case 'X':
 
220
                        case 27:
 
221
                                run = false;
 
222
                                break;
 
223
                        case '[':
 
224
                                pColor--;
 
225
                                break;
 
226
                        case ']':
 
227
                                pColor++;
 
228
                                break;
 
229
                        case '{':
 
230
                                pColor -= 72;
 
231
                                break;
 
232
                        case '}':
 
233
                                pColor += 72;
 
234
                                break;
 
235
                        case '`': /* ` */
 
236
                                doChange = !doChange;
 
237
                                break;
 
238
                        case 'P':
 
239
                                wait_noinput(true, false, false);
 
240
                                wait_input(true, false, false);
 
241
                                break;
 
242
                        default:
 
243
                                break;
 
244
                }
 
245
        }
 
246
 
 
247
        if (doChange)
 
248
        {
 
249
                stepCounter++;
 
250
                if (stepCounter > changeTime)
 
251
                {
 
252
                        JE_changeSetup(0);
 
253
                }
 
254
        }
 
255
 
 
256
        if ((mt_rand() % 1000) == 1)
 
257
        {
 
258
                nspVarVarInc = mt_rand_1() * 0.01f - 0.005f;
 
259
        }
 
260
 
 
261
        nspVarInc += nspVarVarInc;
 
262
}
 
263
 
 
264
void JE_wackyCol( void )
 
265
{
 
266
        /* YKS: Does nothing */
 
267
}
 
268
 
 
269
void JE_starlib_init( void )
 
270
{
 
271
        static JE_boolean initialized = false;
 
272
 
 
273
        if (!initialized)
 
274
        {
 
275
                initialized = true;
 
276
 
 
277
                JE_resetValues();
 
278
                JE_changeSetup(2);
 
279
                doChange = true;
 
280
 
 
281
                /* RANDOMIZE; */
 
282
                for (int x = 0; x < starlib_MAX_STARS; x++)
 
283
                {
 
284
                        star[x].spX = (mt_rand() % 64000) - 32000;
 
285
                        star[x].spY = (mt_rand() % 40000) - 20000;
 
286
                        star[x].spZ = x+1;
 
287
                }
 
288
        }
 
289
}
 
290
 
 
291
void JE_resetValues( void )
 
292
{
 
293
        nsp2 = 1;
 
294
        nspVar2Inc = 1;
 
295
        nspVarInc = 0.1f;
 
296
        nspVarVarInc = 0.0001f;
 
297
        nsp = 0;
 
298
        pColor = 32;
 
299
        starlib_speed = 2;
 
300
        speedChange = 0;
 
301
}
 
302
 
 
303
void JE_changeSetup( JE_byte setupType )
 
304
{
 
305
        stepCounter = 0;
 
306
        changeTime = (mt_rand() % 1000);
 
307
 
 
308
        if (setupType > 0)
 
309
        {
 
310
                setup = setupType;
 
311
        } else {
 
312
                setup = mt_rand() % (MAX_TYPES + 1);
 
313
        }
 
314
 
 
315
        if (setup == 1)
 
316
        {
 
317
                nspVarInc = 0.1f;
 
318
        }
 
319
        if (nspVarInc > 2.2f)
 
320
        {
 
321
                nspVarInc = 0.1f;
 
322
        }
 
323
}
 
324
 
 
325
void JE_newStar( void )
 
326
{
 
327
        if (setup == 0)
 
328
        {
 
329
                tempX = (mt_rand() % 64000) - 32000;
 
330
                tempY = (mt_rand() % 40000) - 20000;
 
331
        } else {
 
332
                nsp = nsp + nspVarInc; /* YKS: < lol */
 
333
                switch (setup)
 
334
                {
 
335
                        case 1:
 
336
                                tempX = (int)(sinf(nsp / 30) * 20000);
 
337
                                tempY = (mt_rand() % 40000) - 20000;
 
338
                                break;
 
339
                        case 2:
 
340
                                tempX = (int)(cosf(nsp) * 20000);
 
341
                                tempY = (int)(sinf(nsp) * 20000);
 
342
                                break;
 
343
                        case 3:
 
344
                                tempX = (int)(cosf(nsp * 15) * 100) * ((int)(nsp / 6) % 200);
 
345
                                tempY = (int)(sinf(nsp * 15) * 100) * ((int)(nsp / 6) % 200);
 
346
                                break;
 
347
                        case 4:
 
348
                                tempX = (int)(sinf(nsp / 60) * 20000);
 
349
                                tempY = (int)(cosf(nsp) * (int)(sinf(nsp / 200) * 300) * 100);
 
350
                                break;
 
351
                        case 5:
 
352
                                tempX = (int)(sinf(nsp / 2) * 20000);
 
353
                                tempY = (int)(cosf(nsp) * (int)(sinf(nsp / 200) * 300) * 100);
 
354
                                break;
 
355
                        case 6:
 
356
                                tempX = (int)(sinf(nsp) * 40000);
 
357
                                tempY = (int)(cosf(nsp) * 20000);
 
358
                                break;
 
359
                        case 8:
 
360
                                tempX = (int)(sinf(nsp / 2) * 40000);
 
361
                                tempY = (int)(cosf(nsp) * 20000);
 
362
                                break;
 
363
                        case 7:
 
364
                                tempX = mt_rand() % 65535;
 
365
                                if ((mt_rand() % 2) == 0)
 
366
                                {
 
367
                                        tempY = (int)(cosf(nsp / 80) * 10000) + 15000;
 
368
                                } else {
 
369
                                        tempY = 50000 - (int)(cosf(nsp / 80) * 13000);
 
370
                                }
 
371
                                break;
 
372
                        case 9:
 
373
                                nsp2 += nspVar2Inc;
 
374
                                if ((nsp2 == 65535) || (nsp2 == 0))
 
375
                                {
 
376
                                        nspVar2Inc = -nspVar2Inc;
 
377
                                }
 
378
                                tempX = (int)(cosf(sinf(nsp2 / 10.0f) + (nsp / 500)) * 32000);
 
379
                                tempY = (int)(sinf(cosf(nsp2 / 10.0f) + (nsp / 500)) * 30000);
 
380
                                break;
 
381
                        case 10:
 
382
                                nsp2 += nspVar2Inc;
 
383
                                if ((nsp2 == 65535) || (nsp2 == 0))
 
384
                                {
 
385
                                        nspVar2Inc = -nspVar2Inc;
 
386
                                }
 
387
                                tempX = (int)(cosf(sinf(nsp2 / 5.0f) + (nsp / 100)) * 32000);
 
388
                                tempY = (int)(sinf(cosf(nsp2 / 5.0f) + (nsp / 100)) * 30000);
 
389
                                break;;
 
390
                        case 11:
 
391
                                nsp2 += nspVar2Inc;
 
392
                                if ((nsp2 == 65535) || (nsp2 == 0))
 
393
                                {
 
394
                                        nspVar2Inc = -nspVar2Inc;
 
395
                                }
 
396
                                tempX = (int)(cosf(sinf(nsp2 / 1000.0f) + (nsp / 2)) * 32000);
 
397
                                tempY = (int)(sinf(cosf(nsp2 / 1000.0f) + (nsp / 2)) * 30000);
 
398
                                break;
 
399
                        case 12:
 
400
                                if (nsp != 0)
 
401
                                {
 
402
                                        nsp2 += nspVar2Inc;
 
403
                                        if ((nsp2 == 65535) || (nsp2 == 0))
 
404
                                        {
 
405
                                                nspVar2Inc = -nspVar2Inc;
 
406
                                        }
 
407
                                        tempX = (int)(cosf(sinf(nsp2 / 2.0f) / (sqrtf(fabsf(nsp)) / 10.0f + 1) + (nsp2 / 100.0f)) * 32000);
 
408
                                        tempY = (int)(sinf(cosf(nsp2 / 2.0f) / (sqrtf(fabsf(nsp)) / 10.0f + 1) + (nsp2 / 100.0f)) * 30000);
 
409
                                }
 
410
                                break;
 
411
                        case 13:
 
412
                                if (nsp != 0)
 
413
                                {
 
414
                                        nsp2 += nspVar2Inc;
 
415
                                        if ((nsp2 == 65535) || (nsp2 == 0))
 
416
                                        {
 
417
                                                nspVar2Inc = -nspVar2Inc;
 
418
                                        }
 
419
                                        tempX = (int)(cosf(sinf(nsp2 / 10.0f) / 2 + (nsp / 20)) * 32000);
 
420
                                        tempY = (int)(sinf(sinf(nsp2 / 11.0f) / 2 + (nsp / 20)) * 30000);
 
421
                                }
 
422
                                break;
 
423
                        case 14:
 
424
                                nsp2 += nspVar2Inc;
 
425
                                tempX = (int)((sinf(nsp) + cosf(nsp2 / 1000.0f) * 3) * 12000);
 
426
                                tempY = (int)(cosf(nsp) * 10000) + nsp2;
 
427
                                break;
 
428
                }
 
429
        }
 
430
}
 
431