Subversion Repositories Kolibri OS

Rev

Rev 5243 | Rev 5260 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5243 Rev 5251
1
#include "rsgame.h"
1
#include "rsgame.h"
2
 
2
 
3
#include "rsgametext.h"
3
#include "rsgametext.h"
4
 
4
 
5
#include "rsgamemenu.h"
5
#include "rsgamemenu.h"
6
 
6
 
7
#include "rsgamedraw.h"
7
#include "rsgamedraw.h"
8
 
8
 
9
#include "rskos.h"
9
#include "rskos.h"
10
 
10
 
11
#include "rsgentex.h"
11
#include "rsgentex.h"
12
#include "rssoundgen.h"
12
#include "rssoundgen.h"
13
#include "rsnoise.h"
13
#include "rsnoise.h"
14
 
14
 
15
#include "rs/rsplatform.h"
15
#include "rs/rsplatform.h"
16
 
16
 
17
 
17
 
18
 
18
 
19
 
19
 
20
 
20
 
21
rs_game_t game;
21
rs_game_t game;
22
 
22
 
23
 
23
 
24
void texture_init(rs_texture_t *tex, int w, int h) {
24
void texture_init(rs_texture_t *tex, int w, int h) {
25
    tex->status = 1;
25
    tex->status = 1;
26
    tex->w = w;
26
    tex->w = w;
27
    tex->h = h;
27
    tex->h = h;
28
    tex->data = malloc(w*h*4); // BGRA BGRA
28
    tex->data = malloc(w*h*4); // BGRA BGRA
29
};
29
};
30
 
30
 
31
void texture_free(rs_texture_t *tex) {
31
void texture_free(rs_texture_t *tex) {
32
    free(tex->data);
32
    free(tex->data);
33
    tex->status = 0;
33
    tex->status = 0;
34
};
34
};
35
 
35
 
36
void texture_clear(rs_texture_t *tex, unsigned int color) {
36
void texture_clear(rs_texture_t *tex, unsigned int color) {
37
    int i;
37
    int i;
38
    for (i = 0; i < tex->w * tex->h; i++) {
38
    for (i = 0; i < tex->w * tex->h; i++) {
39
        *((unsigned int*)(&tex->data[i*4])) = color;
39
        *((unsigned int*)(&tex->data[i*4])) = color;
40
    };
40
    };
41
};
41
};
42
 
42
 
43
void texture_draw(rs_texture_t *dest, rs_texture_t *src, int x, int y, int mode) {
43
void texture_draw(rs_texture_t *dest, rs_texture_t *src, int x, int y, int mode) {
44
    
44
    
45
    int i; // y
45
    int i; // y
46
    int j; // x
46
    int j; // x
47
    int k; // color component
47
    int k; // color component
48
    
48
    
49
    int istart = (y < 0) ? -y : 0;
49
    int istart = (y < 0) ? -y : 0;
50
    int iend = src->h - (( (y + src->h) > dest->h) ? (y + src->h - dest->h) : 0);
50
    int iend = src->h - (( (y + src->h) > dest->h) ? (y + src->h - dest->h) : 0);
51
    
51
    
52
    int jstart = (x < 0) ? -x : 0;
52
    int jstart = (x < 0) ? -x : 0;
53
    int jend = src->w - (( (x + src->w) > dest->w) ? (x + src->w - dest->w) : 0);
53
    int jend = src->w - (( (x + src->w) > dest->w) ? (x + src->w - dest->w) : 0);
54
    
54
    
55
    int ishift = 0;
55
    int ishift = 0;
56
    int jshift = 0;
56
    int jshift = 0;
57
    
57
    
58
    float a; // alpha value
58
    float a; // alpha value
59
    
59
    
60
    if (mode & DRAW_TILED_FLAG) {
60
    if (mode & DRAW_TILED_FLAG) {
61
        jshift = x;
61
        jshift = x;
62
        ishift = y;
62
        ishift = y;
63
        x = y = istart = jstart = 0;
63
        x = y = istart = jstart = 0;
64
        iend = dest->h;
64
        iend = dest->h;
65
        jend = dest->w;
65
        jend = dest->w;
66
    };
66
    };
67
    
67
    
68
    mode = mode & DRAW_MODE_MASK;
68
    mode = mode & DRAW_MODE_MASK;
69
    
69
    
70
    int modvalue = (src->w*src->h*4); 
70
    int modvalue = (src->w*src->h*4); 
71
    
71
    
72
    if (mode == DRAW_MODE_REPLACE) {
72
    if (mode == DRAW_MODE_REPLACE) {
73
        for (i = istart; i < iend; i++) {
73
        for (i = istart; i < iend; i++) {
74
            for (j = jstart; j < jend; j++) {
74
            for (j = jstart; j < jend; j++) {
75
                for (k = 0; k < 4; k++) {
75
                for (k = 0; k < 4; k++) {
76
                    dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] = src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue];
76
                    dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] = src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue];
77
                };
77
                };
78
            };
78
            };
79
        };
79
        };
80
    }
80
    }
81
    else if (mode == DRAW_MODE_ADDITIVE) {
81
    else if (mode == DRAW_MODE_ADDITIVE) {
82
        for (i = istart; i < iend; i++) {
82
        for (i = istart; i < iend; i++) {
83
            for (j = jstart; j < jend; j++) {
83
            for (j = jstart; j < jend; j++) {
84
                for (k = 0; k < 3; k++) { // Alpha channel is not added
84
                for (k = 0; k < 3; k++) { // Alpha channel is not added
85
                    dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] = 
85
                    dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] = 
86
                        clamp_byte( dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] + src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue] );
86
                        clamp_byte( dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] + src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue] );
87
                };
87
                };
88
            };
88
            };
89
        };
89
        };
90
    }
90
    }
91
    else if (mode == DRAW_MODE_MULT) {
91
    else if (mode == DRAW_MODE_MULT) {
92
        for (i = istart; i < iend; i++) {
92
        for (i = istart; i < iend; i++) {
93
            for (j = jstart; j < jend; j++) {
93
            for (j = jstart; j < jend; j++) {
94
                for (k = 0; k < 3; k++) { // Alpha channel is not added
94
                for (k = 0; k < 3; k++) { // Alpha channel is not added
95
                    dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] = 
95
                    dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] = 
96
                        clamp_byte( dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] * src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue] / 255 );
96
                        clamp_byte( dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] * src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue] / 255 );
97
                };
97
                };
98
            };
98
            };
99
        };
99
        };
100
    }
100
    }
101
    else if (mode == DRAW_MODE_ALPHA) {
101
    else if (mode == DRAW_MODE_ALPHA) {
102
        for (i = istart; i < iend; i++) {
102
        for (i = istart; i < iend; i++) {
103
            for (j = jstart; j < jend; j++) {
103
            for (j = jstart; j < jend; j++) {
104
                for (k = 0; k < 3; k++) {
104
                for (k = 0; k < 3; k++) {
105
                    a = (1.0 * src->data[ (4*(i*src->w + j) + 3) % modvalue ] / 255.0);
105
                    a = (1.0 * src->data[ (4*(i*src->w + j) + 3) % modvalue ] / 255.0);
106
                    dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] = 
106
                    dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] = 
107
                         (unsigned char) ( (1.0-a) * dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] 
107
                         (unsigned char) ( (1.0-a) * dest->data[ 4 * ( (y+i)*dest->w + (x+j) ) + k ] 
108
                                          + a*src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue] );
108
                                          + a*src->data[ (4*((i+ishift)*src->w + j + jshift) + k) % modvalue] );
109
                };
109
                };
110
            };
110
            };
111
        };
111
        };
112
    };
112
    };
113
    
113
    
114
};
114
};
115
 
115
 
116
 
116
 
117
 
117
 
118
void texture_set_pixel(rs_texture_t *tex, int x, int y, unsigned int color) {
118
void texture_set_pixel(rs_texture_t *tex, int x, int y, unsigned int color) {
119
    *((unsigned int*) &tex->data[ 4 * ( (y)*tex->w + (x) ) + 0 ]) = color;
119
    *((unsigned int*) &tex->data[ 4 * ( (y)*tex->w + (x) ) + 0 ]) = color;
120
};
120
};
121
 
121
 
122
void texture_draw_vline(rs_texture_t *tex, int x, int y, int l, unsigned int color) {
122
void texture_draw_vline(rs_texture_t *tex, int x, int y, int l, unsigned int color) {
123
    int i;
123
    int i;
124
    if (y+l >= tex->h) {
124
    if (y+l >= tex->h) {
125
        l = tex->h - y;
125
        l = tex->h - y;
126
    };
126
    };
127
    for (i = 0; i < l; i++) {
127
    for (i = 0; i < l; i++) {
128
        *((unsigned int*) &tex->data[ 4 * ( (y+i)*tex->w + (x) ) + 0 ]) = color;
128
        *((unsigned int*) &tex->data[ 4 * ( (y+i)*tex->w + (x) ) + 0 ]) = color;
129
    };    
129
    };    
130
};
130
};
131
 
131
 
132
void texture_draw_hline(rs_texture_t *tex, int x, int y, int l, unsigned int color) {
132
void texture_draw_hline(rs_texture_t *tex, int x, int y, int l, unsigned int color) {
133
    int i;
133
    int i;
134
    if (x+l >= tex->w) {
134
    if (x+l >= tex->w) {
135
        l = tex->w - x;
135
        l = tex->w - x;
136
    };
136
    };
137
    for (i = 0; i < l; i++) {
137
    for (i = 0; i < l; i++) {
138
        *((unsigned int*) &tex->data[ 4 * ( (y)*tex->w + (x+i) ) + 0 ]) = color;
138
        *((unsigned int*) &tex->data[ 4 * ( (y)*tex->w + (x+i) ) + 0 ]) = color;
139
    };    
139
    };    
140
};
140
};
141
 
141
 
142
 
142
 
143
 
143
 
144
void soundbuf_init(rs_soundbuf_t *snd, int length_samples) {
144
void soundbuf_init(rs_soundbuf_t *snd, int length_samples) {
145
    snd->status = 1;
145
    snd->status = 1;
146
    snd->length_samples = length_samples;
146
    snd->length_samples = length_samples;
147
    snd->data = malloc(length_samples*2);
147
    snd->data = malloc(length_samples*2);
148
    rskos_snd_create_buffer(&snd->hbuf, snd->data, length_samples);
148
    rskos_snd_create_buffer(&snd->hbuf, snd->data, length_samples);
149
};
149
};
-
 
150
 
-
 
151
void soundbuf_update(rs_soundbuf_t *snd) {
-
 
152
    rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
-
 
153
};
150
 
154
 
151
void soundbuf_free(rs_soundbuf_t *snd) {
155
void soundbuf_free(rs_soundbuf_t *snd) {
152
    snd->status = 0;
156
    snd->status = 0;
153
    free(snd->data);
157
    free(snd->data);
154
};
158
};
155
 
159
 
156
void soundbuf_fill(rs_soundbuf_t *snd, int amp, int freq_div) {
160
void soundbuf_fill(rs_soundbuf_t *snd, int amp, int freq_div) {
157
    int i;
161
    int i;
158
    for (i = 0; i < snd->length_samples; i++) {
162
    for (i = 0; i < snd->length_samples; i++) {
159
		snd->data[i] = -amp/2 + amp/2*( ( (i % freq_div) > freq_div/2 ) ? 1 : 0 );
163
		snd->data[i] = -amp/2 + amp/2*( ( (i % freq_div) > freq_div/2 ) ? 1 : 0 );
160
	};
164
	};
161
	rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
165
	rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
162
};
166
};
163
 
167
 
164
void soundbuf_sin(rs_soundbuf_t *snd, float freq) {
168
void soundbuf_sin(rs_soundbuf_t *snd, float freq) {
165
    int i;
169
    int i;
166
    int amp = 29000;
170
    int amp = 29000;
167
    for (i = 0; i < snd->length_samples; i++) {
171
    for (i = 0; i < snd->length_samples; i++) {
168
		snd->data[i] = ( 1.0 - 1.0*i/snd->length_samples ) * sin(freq*i) * amp;
172
		snd->data[i] = ( 1.0 - 1.0*i/snd->length_samples ) * sin(freq*i) * amp;
169
	};
173
	};
170
	rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
174
	rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
171
};
175
};
172
 
176
 
173
void soundbuf_sin_fade(rs_soundbuf_t *snd, float freq) {
177
void soundbuf_sin_fade(rs_soundbuf_t *snd, float freq) {
174
    int i;
178
    int i;
175
    int amp = 29000;
179
    int amp = 29000;
176
    for (i = 0; i < snd->length_samples; i++) {
180
    for (i = 0; i < snd->length_samples; i++) {
177
		snd->data[i] = ( 1.0 - 1.0*i/snd->length_samples ) * sin( ( (1.0 - 0.48*i/snd->length_samples) * freq ) *i) * amp;
181
		snd->data[i] = ( 1.0 - 1.0*i/snd->length_samples ) * sin( ( (1.0 - 0.48*i/snd->length_samples) * freq ) *i) * amp;
178
	};
182
	};
179
	
183
	
180
	
184
	
181
	/*
185
	/*
182
	
186
	
183
	// ok
187
	// ok
184
	
188
	
185
	rs_sgen_init(2, snd->length_samples);
189
	rs_sgen_init(2, snd->length_samples);
186
	rs_sgen_func_pm(1, 880.0, 21.0, 0.3, 110.0, 0.3);
190
	rs_sgen_func_pm(1, 880.0, 21.0, 0.3, 110.0, 0.3);
187
	rs_sgen_func_normalize(1, 1.0);
191
	rs_sgen_func_normalize(1, 1.0);
188
	rs_sgen_func_lowpass(0, 1, 1.0, 0.0, 1.0);
192
	rs_sgen_func_lowpass(0, 1, 1.0, 0.0, 1.0);
189
	rs_sgen_wave_out(0);
193
	rs_sgen_wave_out(0);
190
	
194
	
191
	memcpy(snd->data, rs_sgen_reg.wave_out, snd->length_samples*2 );
195
	memcpy(snd->data, rs_sgen_reg.wave_out, snd->length_samples*2 );
192
	
196
	
193
	rs_sgen_term();
197
	rs_sgen_term();
194
	
198
	
195
	*/
199
	*/
196
	
200
	
197
	rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
201
	rskos_snd_update_buffer(&snd->hbuf, snd->data, snd->length_samples);
198
};
202
};
199
 
203
 
200
void soundbuf_play(rs_soundbuf_t *snd) {
204
void soundbuf_play(rs_soundbuf_t *snd) {
201
    rskos_snd_play(&snd->hbuf, 0);
205
    rskos_snd_play(&snd->hbuf, 0);
202
};
206
};
203
 
207
 
204
void soundbuf_stop(rs_soundbuf_t *snd) {
208
void soundbuf_stop(rs_soundbuf_t *snd) {
205
    rskos_snd_stop(&snd->hbuf);
209
    rskos_snd_stop(&snd->hbuf);
206
};
210
};
207
 
211
 
208
 
212
 
209
 
213
 
210
unsigned char clamp_byte(int value) {
214
unsigned char clamp_byte(int value) {
211
    value = value * (1 - (value >> 31)); // negative to zero
215
    value = value * (1 - (value >> 31)); // negative to zero
212
    return (value > 255) ? 255 : value;
216
    return (value > 255) ? 255 : value;
213
};
217
};
214
 
218
 
215
 
219
 
216
void game_reg_init() {
220
void game_reg_init() {
217
    
221
    
218
    game.loader_counter = 0;
222
    game.loader_counter = 0;
-
 
223
    
-
 
224
    game.menu_replay_timeout = 0;
219
    
225
    
-
 
226
    game.process_timer = 0;
-
 
227
    
220
    game.process_timer = 0;
228
    game.sound_index = 0;
221
    
229
    
222
    game.need_redraw = 1;
230
    game.need_redraw = 1;
223
    
231
    
224
    game.score = 0;
232
    game.score = 0;
225
    game.time = 0;
233
    game.time = 0;
-
 
234
    
-
 
235
//    game.game_mode = GAME_MODE_RAMPAGE;
226
 
236
 
227
    game.selected = 0;
237
    game.selected = 0;
228
    game.selected_x = game.selected_y = 0;
238
    game.selected_x = game.selected_y = 0;
229
    
239
    
230
    game.explosions_count = 0;
240
    game.explosions_count = 0;
231
    
241
    
232
    game.tx = 0;
242
//    game.tx = 0;
233
    game.ty = 0;
243
//    game.ty = 0;
234
    game.tz = 0;
244
//    game.tz = 0;
235
 
245
 
236
//    int i;
246
//    int i;
237
//    for (i = 0; i < BULLETS_COUNT; i++) {
247
//    for (i = 0; i < BULLETS_COUNT; i++) {
238
//        game.bullet_x[i] = 0;
248
//        game.bullet_x[i] = 0;
239
//        game.bullet_y[i] = 0;
249
//        game.bullet_y[i] = 0;
240
//    };
250
//    };
241
//    game.bullet_index = 0;
251
//    game.bullet_index = 0;
242
    
252
    
243
    game.status = STATUS_LOADING;
253
    game.status = STATUS_LOADING;
244
 
254
 
245
//    game.window_scale = 1;
255
//    game.window_scale = 1;
246
    
256
    
247
//    game.window_scale = 2;
257
//    game.window_scale = 2;
248
//    #ifndef RS_KOS
258
//    #ifndef RS_KOS
249
//        game.window_scale = 3;
259
//        game.window_scale = 3;
250
//        window_scale_str[3] = '3'; 
260
//        window_scale_str[3] = '3'; 
251
//    #endif
261
//    #endif
252
    
262
    
253
    game.keyboard_state = 0;
263
    game.keyboard_state = 0;
254
    
264
    
255
    game.menu_index = 0;
265
//    game.menu_index = 0;
256
    game.menu_item_index = 0;
266
//    game.menu_item_index = 0;
257
};
267
};
258
 
268
 
259
 
269
 
260
int is_key_pressed(int mask) {
270
int is_key_pressed(int mask) {
261
    return IS_BIT_SET(game.keyboard_state, mask) ? 1 : 0;
271
    return IS_BIT_SET(game.keyboard_state, mask) ? 1 : 0;
262
};
272
};
263
 
273
 
264
int seed = 0;
274
int seed = 0;
265
int get_random_crystal() {
275
int get_random_crystal() {
266
    seed += 2;
276
    seed += 2;
267
    return ( (seed + (get_time() & 0xFFFF) ) % CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
277
    return ( (seed + (get_time() & 0xFFFF) ) % CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
268
};
278
};
269
 
279
 
270
int game_check_and_explode() {
280
int game_check_and_explode() {
271
    int x, y, i, item, match_count, found;
281
    int x, y, i, item, match_count, found;
272
    found = 0;
282
    found = 0;
273
    
283
    
274
    // vertical lines
284
    // vertical lines
275
    for (x = 0; x < FIELD_WIDTH; x++) {
285
    for (x = 0; x < FIELD_WIDTH; x++) {
276
        match_count = 0;
286
        match_count = 0;
277
        item = 0xFF;
287
        item = 0xFF;
278
        for (y = FIELD_HEIGHT-1; y >= 0; y--) {
288
        for (y = FIELD_HEIGHT-1; y >= 0; y--) {
279
                
289
                
280
            if ( IS_BIT_SET( FIELD_ITEM(x,y), CRYSTAL_MOVING_BIT ) || IS_BIT_CLEARED( FIELD_ITEM(x,y), CRYSTAL_VISIBLE_BIT) ) {
290
            if ( IS_BIT_SET( FIELD_ITEM(x,y), CRYSTAL_MOVING_BIT ) || IS_BIT_CLEARED( FIELD_ITEM(x,y), CRYSTAL_VISIBLE_BIT) ) {
281
                item = 0xFF;
291
                item = 0xFF;
282
                match_count = 0;
292
                match_count = 0;
283
                continue;
293
                continue;
284
            };
294
            };
285
            
295
            
286
            if ( (FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK) == item) {
296
            if ( (FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK) == item) {
287
                match_count++;
297
                match_count++;
288
            }
298
            }
289
            else {
299
            else {
290
                if (match_count >= 2) {
300
                if (match_count >= 2) {
291
                    found = 1;
301
                    found = 1;
292
                    for (i = y+1; i < y+1+match_count+1; i++) {
302
                    for (i = y+1; i < y+1+match_count+1; i++) {
293
                        BIT_SET( FIELD_ITEM(x, i), CRYSTAL_EXPLODED_BIT );
303
                        BIT_SET( FIELD_ITEM(x, i), CRYSTAL_EXPLODED_BIT );
294
                    };
304
                    };
295
                }
305
                }
296
                item = FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK;
306
                item = FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK;
297
                match_count = 0;
307
                match_count = 0;
298
            };
308
            };
299
        };
309
        };
300
        if (match_count >= 2) { // last
310
        if (match_count >= 2) { // last
301
            found = 1;
311
            found = 1;
302
            for (i = y+1; i < y+1+match_count+1; i++) {
312
            for (i = y+1; i < y+1+match_count+1; i++) {
303
                BIT_SET( FIELD_ITEM(x, i), CRYSTAL_EXPLODED_BIT );
313
                BIT_SET( FIELD_ITEM(x, i), CRYSTAL_EXPLODED_BIT );
304
            };
314
            };
305
        };
315
        };
306
    };
316
    };
307
    
317
    
308
    // horizontal lines
318
    // horizontal lines
309
    for (y = 0; y < FIELD_HEIGHT; y++) {
319
    for (y = 0; y < FIELD_HEIGHT; y++) {
310
            
320
            
311
        match_count = 0;
321
        match_count = 0;
312
        item = 0xFF;
322
        item = 0xFF;
313
        for (x = FIELD_WIDTH-1; x >= 0; x--) {
323
        for (x = FIELD_WIDTH-1; x >= 0; x--) {
314
 
324
 
315
            if ( IS_BIT_SET( FIELD_ITEM(x,y), CRYSTAL_MOVING_BIT ) || IS_BIT_CLEARED( FIELD_ITEM(x,y), CRYSTAL_VISIBLE_BIT) ) {
325
            if ( IS_BIT_SET( FIELD_ITEM(x,y), CRYSTAL_MOVING_BIT ) || IS_BIT_CLEARED( FIELD_ITEM(x,y), CRYSTAL_VISIBLE_BIT) ) {
316
                item = 0xFF;
326
                item = 0xFF;
317
                match_count = 0;
327
                match_count = 0;
318
                continue;
328
                continue;
319
            };
329
            };
320
 
330
 
321
            if ( (FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK) == item) {
331
            if ( (FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK) == item) {
322
                match_count++;
332
                match_count++;
323
            }
333
            }
324
            else {
334
            else {
325
                if (match_count >= 2) {
335
                if (match_count >= 2) {
326
                    found = 1;
336
                    found = 1;
327
                    for (i = x+1; i < x+1+match_count+1; i++) {
337
                    for (i = x+1; i < x+1+match_count+1; i++) {
328
                        BIT_SET( FIELD_ITEM(i, y), CRYSTAL_EXPLODED_BIT );
338
                        BIT_SET( FIELD_ITEM(i, y), CRYSTAL_EXPLODED_BIT );
329
                    };
339
                    };
330
                }
340
                }
331
                item = FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK;
341
                item = FIELD_ITEM(x,y) & CRYSTAL_INDEX_MASK;
332
                match_count = 0;
342
                match_count = 0;
333
            };
343
            };
334
        };
344
        };
335
        if (match_count >= 2) { // last
345
        if (match_count >= 2) { // last
336
            found = 1;
346
            found = 1;
337
            for (i = x+1; i < x+1+match_count+1; i++) {
347
            for (i = x+1; i < x+1+match_count+1; i++) {
338
                BIT_SET( FIELD_ITEM(i, y), CRYSTAL_EXPLODED_BIT );
348
                BIT_SET( FIELD_ITEM(i, y), CRYSTAL_EXPLODED_BIT );
339
            };
349
            };
340
        };
350
        };
341
    };
351
    };
342
 
352
 
343
    for (i = 0; i < FIELD_LENGTH; i++) {
353
    for (i = 0; i < FIELD_LENGTH; i++) {
344
        if (IS_BIT_SET(game.field[i], CRYSTAL_EXPLODED_BIT)) {
354
        if (IS_BIT_SET(game.field[i], CRYSTAL_EXPLODED_BIT)) {
345
            game.field[i] = 0;
355
            game.field[i] = 0;
346
            game.score++;
356
            game.score++;
347
            if (game.explosions_count < EXPLOSIONS_MAX_COUNT-1) {
357
            if (game.explosions_count < EXPLOSIONS_MAX_COUNT-1) {
348
                game.explosions[game.explosions_count] = ( i % FIELD_WIDTH ) | ( (i / FIELD_WIDTH) << 8);
358
                game.explosions[game.explosions_count] = ( i % FIELD_WIDTH ) | ( (i / FIELD_WIDTH) << 8);
349
                game.explosions_count++;
359
                game.explosions_count++;
350
            };
360
            };
351
        };
361
        };
352
    };
362
    };
353
    
363
    
354
    if (game.score > 99) {
364
//    if (game.score > 99) {
355
        game.status = STATUS_MENU;
365
//        game.status = STATUS_MENU;
356
        game.need_redraw = 1;
366
//        game.need_redraw = 1;
357
    };
367
//    };
358
    
368
    
359
    if (found) {
369
    if (found) {
360
        game.need_redraw = 1;
370
        game.need_redraw = 1;
-
 
371
        soundbuf_play(&game.sound_explosion[ game.sound_index ]);
-
 
372
        game.sound_index = (game.sound_index+1) % SOUND_EXPLOSION_COUNT;
361
    };
373
    };
362
    
374
    
363
    return found;
375
    return found;
364
};
376
};
-
 
377
 
-
 
378
 
-
 
379
//void game_check_and_explode_rampage() {
-
 
380
//    
-
 
381
//    
-
 
382
//    
-
 
383
//};
-
 
384
 
365
 
385
 
366
void game_fall() {
386
void game_fall() {
367
    int x, y, fall;
387
    int x, y, fall;
368
    for (x = 0; x < FIELD_WIDTH; x++) {
388
    for (x = 0; x < FIELD_WIDTH; x++) {
369
        fall = 0;
389
        fall = 0;
370
        for (y = FIELD_HEIGHT-1; y > 0; y--) {
390
        for (y = FIELD_HEIGHT-1; y > 0; y--) {
371
            fall |= !(FIELD_ITEM(x, y) & CRYSTAL_VISIBLE_BIT);
391
            fall |= !(FIELD_ITEM(x, y) & CRYSTAL_VISIBLE_BIT);
372
            if (fall) {
392
            if (fall) {
373
                FIELD_ITEM(x, y) = FIELD_ITEM(x, y-1) | CRYSTAL_MOVING_BIT;
393
                FIELD_ITEM(x, y) = FIELD_ITEM(x, y-1) | CRYSTAL_MOVING_BIT;
374
                game.need_redraw = 1;
394
                game.need_redraw = 1;
375
            }
395
            }
376
            else {
396
            else {
377
                BIT_CLEAR( FIELD_ITEM(x, y), CRYSTAL_MOVING_BIT );
397
                BIT_CLEAR( FIELD_ITEM(x, y), CRYSTAL_MOVING_BIT );
378
            };
398
            };
379
        };
399
        };
380
        if ( (fall) || ( IS_BIT_CLEARED(FIELD_ITEM(x,0), CRYSTAL_VISIBLE_BIT) ) ) {
400
        if ( (fall) || ( IS_BIT_CLEARED(FIELD_ITEM(x,0), CRYSTAL_VISIBLE_BIT) ) ) {
381
            FIELD_ITEM(x, 0) = get_random_crystal();
401
            FIELD_ITEM(x, 0) = get_random_crystal();
382
        };
402
        };
383
    };
403
    };
384
};
404
};
385
 
405
 
386
 
406
 
387
 
407
 
388
void GameProcess() {
408
void GameProcess() {
389
    
409
    
390
    if (game.status == STATUS_LOADING) {
410
    if (game.status == STATUS_LOADING) {
391
        game.loader_counter++;
411
        game.loader_counter++;
392
        if (game.loader_counter == 2) {
412
        if (game.loader_counter == 2) {
393
                
413
                
394
            game_textures_init_stage2();
414
            game_textures_init_stage2();
395
               
415
            
-
 
416
            
-
 
417
            // Sounds...
-
 
418
            
-
 
419
            int soundlen = 40555;
-
 
420
            
-
 
421
            int freqs[SOUND_EXPLOSION_COUNT] = { 440, 523, 587, 698, 783, 880, 1046, 1174 };
-
 
422
            
-
 
423
            int i;
-
 
424
            for (i = 0; i < SOUND_EXPLOSION_COUNT; i++) {
-
 
425
 
-
 
426
                soundbuf_init(&game.sound_explosion[i], soundlen);
-
 
427
 
-
 
428
 
-
 
429
                rs_sgen_init(3, soundlen);
-
 
430
 
-
 
431
                rs_sgen_func_noise(2, 1000);
-
 
432
                rs_sgen_func_phaser(0, 2, 0.9, 15.2 + 1.0*i/SOUND_EXPLOSION_COUNT, 6.0, 3.0, 2000.0, 1.73); 
-
 
433
                rs_sgen_func_normalize(0, 1.0);
-
 
434
 
-
 
435
 
-
 
436
                rs_sgen_func_sin(1, freqs[i], 110.3);
-
 
437
                rs_sgen_func_sin(2, 1.5*freqs[i], 110.2);
-
 
438
 
-
 
439
                rs_sgen_func_add(0, 0, 1, 1.0, 0.2);
-
 
440
                rs_sgen_func_add(0, 0, 2, 1.0, 0.2);
-
 
441
 
-
 
442
                rs_sgen_func_lowpass(2, 0, 1.0, 0.0, 20.0);
-
 
443
                rs_sgen_func_normalize(2, 1.0);
-
 
444
 
-
 
445
                rs_sgen_wave_out(2);
-
 
446
 
-
 
447
                memcpy(game.sound_explosion[i].data, (unsigned char*) rs_sgen_reg.wave_out, soundlen*2);
-
 
448
                soundbuf_update(&game.sound_explosion[i]);
-
 
449
 
-
 
450
                rs_sgen_term();
-
 
451
            
-
 
452
            };
-
 
453
            
-
 
454
 
-
 
455
            soundlen = 1024;
-
 
456
            
-
 
457
            soundbuf_init(&game.sound_tick, soundlen);
-
 
458
        //    soundbuf_init(&game.sound_tack, soundlen);
-
 
459
            
-
 
460
            rs_sgen_init(2, soundlen);
-
 
461
            rs_sgen_func_noise(1, 1000);
-
 
462
            
-
 
463
            rs_sgen_func_highpass(0, 1, 0.05, 0.0, 16.0);
-
 
464
            rs_sgen_func_normalize(0, 1.0);
-
 
465
            rs_sgen_wave_out(0);
-
 
466
            memcpy(game.sound_tick.data, (unsigned char*) rs_sgen_reg.wave_out, soundlen*2);
-
 
467
            soundbuf_update(&game.sound_tick);
-
 
468
 
-
 
469
        //    rs_sgen_func_lowpass(0, 1, 0.5, 0.0, 22.0);
-
 
470
        //    rs_sgen_func_normalize(0, 1.0);
-
 
471
        //    rs_sgen_wave_out(0);
-
 
472
        //    memcpy(game.sound_tack.data, (unsigned char*) rs_sgen_reg.wave_out, soundlen*2);
-
 
473
        //    soundbuf_update(&game.sound_tack);
-
 
474
 
-
 
475
            rs_sgen_term();
-
 
476
            
-
 
477
            soundlen = 123000; 
-
 
478
            
-
 
479
            soundbuf_init(&game.sound_bang, soundlen);
-
 
480
            rs_sgen_init(2, soundlen);
-
 
481
            rs_sgen_func_pm(1, 220.0, 1.2, 1.0, 880.0*4, 1.0 );
-
 
482
            rs_sgen_func_lowpass(0, 1, 1.0, 0.0, 15.0);
-
 
483
            rs_sgen_func_normalize(0, 1.0);
-
 
484
            rs_sgen_wave_out(0);
-
 
485
            memcpy(game.sound_bang.data, (unsigned char*) rs_sgen_reg.wave_out, soundlen*2);
-
 
486
            soundbuf_update(&game.sound_bang);
-
 
487
            
-
 
488
            
-
 
489
            soundbuf_init(&game.sound_click, 1024);
-
 
490
            soundbuf_sin(&game.sound_click, 0.25);
-
 
491
            
-
 
492
              
396
            
493
            soundbuf_play(&game.sound_bang); // <-- Loaded. Bang!
397
        
494
        
398
            game.status = STATUS_MENU;
495
            game.status = STATUS_MENU;
399
            game.need_redraw = 1;
496
            game.need_redraw = 1;
400
 
497
 
401
        };
498
        };
402
    }
499
    }
403
    else if (game.status == STATUS_PLAYING) {
500
    else if (game.status == STATUS_PLAYING) {
404
            
501
            
405
        game.process_timer++;
502
        game.process_timer++;
406
 
503
 
407
        if (game.process_timer > ANIMATION_PROCESS_TIMER_LIMIT) {
504
        if (game.process_timer > ANIMATION_PROCESS_TIMER_LIMIT) {
408
            game_check_and_explode();
505
            game_check_and_explode();
409
            game_fall();
506
            game_fall();
410
            game.process_timer = 0;
507
            game.process_timer = 0;
411
        };
508
        };
412
        
509
        
413
        int i;
510
        int i;
414
        for (i = 0; i < game.explosions_count; i++) {
511
        for (i = 0; i < game.explosions_count; i++) {
415
            game.need_redraw = 1;
512
            game.need_redraw = 1;
416
            game.explosions[i] = (game.explosions[i] & 0xFFFF) | ( ((game.explosions[i]>>16)+1) << 16 );
513
            game.explosions[i] = (game.explosions[i] & 0xFFFF) | ( ((game.explosions[i]>>16)+1) << 16 );
417
            if ( (game.explosions[i] >> 16) >= EXPLOSION_FRAMES_COUNT ) {
514
            if ( (game.explosions[i] >> 16) >= EXPLOSION_FRAMES_COUNT ) {
418
                game.explosions[i] = game.explosions[game.explosions_count-1];
515
                game.explosions[i] = game.explosions[game.explosions_count-1];
419
                game.explosions_count--;
516
                game.explosions_count--;
420
                i--;
517
                i--;
421
            };
518
            };
422
        };
519
        };
423
        
520
        
424
        if ((game.time+1)/25 != game.time/25) {
521
        if ((game.time-1)/25 != game.time/25) {
-
 
522
            game.need_redraw = 1;
-
 
523
            
-
 
524
            if (game.time < 10*25) {
425
            game.need_redraw = 1;
525
                soundbuf_play(&game.sound_tick);
426
        };
526
            };
-
 
527
            
-
 
528
        };
-
 
529
        
-
 
530
        game.time--;
-
 
531
        
-
 
532
        if (game.time < 1) {
-
 
533
            // Game Over
-
 
534
            game.status = STATUS_MENU;
-
 
535
            game.need_redraw = 1;
-
 
536
            soundbuf_play(&game.sound_bang);
-
 
537
            
-
 
538
            game.menu_replay_timeout = 40;
-
 
539
        };
-
 
540
        
-
 
541
        
-
 
542
 
-
 
543
    }
-
 
544
    else if (game.status == STATUS_MENU) {
-
 
545
        
-
 
546
        if (game.menu_replay_timeout > 0) {
427
        
547
            game.menu_replay_timeout--;
428
        game.time++;
548
            if (game.menu_replay_timeout == 1) {
429
        
549
                game.menu_replay_timeout = 0;
430
        
550
                game.need_redraw = 1;
431
        
551
            };
432
 
552
        };
433
    };
553
    };
434
 
554
 
435
    game_draw();
555
    game_draw();
436
 
556
 
437
}
557
}
438
 
558
 
439
 
559
 
440
 
560
 
441
 
561
 
442
 
562
 
443
void GameInit() {
563
void GameInit() {
444
 
564
 
445
    game_reg_init();
565
    game_reg_init();
446
    
566
    
447
    game.field = malloc( FIELD_LENGTH );
567
    game.field = malloc( FIELD_LENGTH );
448
    int i;
568
    int i;
449
    for (i = 0; i < FIELD_LENGTH; i++) {
569
    for (i = 0; i < FIELD_LENGTH; i++) {
450
        game.field[i] = (unsigned char) (0.99 * fabs(rs_noise(i, 10)) * CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
570
        game.field[i] = (unsigned char) (0.99 * fabs(rs_noise(i, 10)) * CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
451
    };
571
    };
452
//    memset( game.field, 0, FIELD_LENGTH );
572
//    memset( game.field, 0, FIELD_LENGTH );
453
    
573
    
454
    game.bgr_framebuffer = malloc(GAME_WIDTH * GAME_HEIGHT * 3);
574
    game.bgr_framebuffer = malloc(GAME_WIDTH * GAME_HEIGHT * 3);
455
 
575
 
456
    game_font_init();
576
    game_font_init();
457
    
577
    
458
    game_textures_init_stage1();
578
    game_textures_init_stage1();
459
    
579
    
460
    
580
    
461
 
581
 
462
    #ifndef RS_KOS
582
    #ifndef RS_KOS
463
        rs_audio_init(RS_AUDIO_FMT_MONO16, RS_AUDIO_FREQ_16000, 0); 
583
        rs_audio_init(RS_AUDIO_FMT_MONO16, RS_AUDIO_FREQ_16000, 0); 
464
    #endif
584
    #endif
465
 
-
 
466
    soundbuf_init(&game.sound_test1, 2048);
-
 
467
//    soundbuf_fill(&game.sound_test1, 2, 50);
-
 
468
    soundbuf_sin_fade(&game.sound_test1, 0.7);
585
 
469
 
-
 
470
    soundbuf_init(&game.sound_test2, 1024);
-
 
471
    //soundbuf_fill(&game.sound_test2, 8, 40);
-
 
472
    soundbuf_sin(&game.sound_test2, 0.48);
586
 
473
 
-
 
474
    soundbuf_init(&game.sound_test3, 1024);
-
 
475
    //soundbuf_fill(&game.sound_test3, 12, 60);
-
 
476
    soundbuf_sin(&game.sound_test3, 0.24);
587
    
477
 
588
 
478
 
589
 
479
};
590
};
480
 
591
 
481
 
592
 
482
void GameTerm() {
593
void GameTerm() {
483
 
594
 
484
 
595
 
485
    DEBUG10("--- Game Term ---");
596
    DEBUG10("--- Game Term ---");
486
    
597
    
487
    free(game.field);
598
    free(game.field);
488
 
599
 
489
    #ifndef RS_KOS
600
    #ifndef RS_KOS
490
        rs_audio_term();
601
        rs_audio_term();
491
    #endif
602
    #endif
492
 
603
 
493
    game_font_term();
604
    game_font_term();
494
    
605
    
495
    game_textures_free();
606
    game_textures_free();
496
    
607
    
-
 
608
    
-
 
609
    int i;
-
 
610
 
-
 
611
    for (i = 0; i < SOUND_EXPLOSION_COUNT; i++) {
497
    
-
 
498
    
612
        soundbuf_free(&game.sound_explosion[i]);
499
 
613
    };
-
 
614
    
500
    
615
    soundbuf_free(&game.sound_click);
501
    soundbuf_free(&game.sound_test1);
616
    soundbuf_free(&game.sound_tick);
502
    soundbuf_free(&game.sound_test2);
617
//    soundbuf_free(&game.sound_tack);
503
    soundbuf_free(&game.sound_test3);
618
    soundbuf_free(&game.sound_bang);
504
    
619
    
505
 
620
 
506
};
621
};
507
 
622
 
508
// ------------ #Event Functions ------------
623
// ------------ #Event Functions ------------
509
 
624
 
510
 
625
 
511
 
626
 
512
 
627
 
513
 
628
 
514
 
629
 
515
void GameKeyDown(int key, int first) {
630
void GameKeyDown(int key, int first) {
516
    
631
    
517
    if (key == RS_KEY_A) {
632
    if (key == RS_KEY_A) {
518
    
633
    
519
        game.need_redraw = 1;
634
        game.need_redraw = 1;
520
        
635
        
521
    };    
636
    };    
522
    
637
    
523
    switch (key) {
638
    switch (key) {
524
        case RS_KEY_LEFT:
639
        case RS_KEY_LEFT:
525
            BIT_SET(game.keyboard_state, RS_ARROW_LEFT_MASK);
640
            BIT_SET(game.keyboard_state, RS_ARROW_LEFT_MASK);
526
            break;
641
            break;
527
        case RS_KEY_RIGHT:
642
        case RS_KEY_RIGHT:
528
            BIT_SET(game.keyboard_state, RS_ARROW_RIGHT_MASK);
643
            BIT_SET(game.keyboard_state, RS_ARROW_RIGHT_MASK);
529
            break;
644
            break;
530
        case RS_KEY_UP:
645
        case RS_KEY_UP:
531
            BIT_SET(game.keyboard_state, RS_ARROW_UP_MASK);
646
            BIT_SET(game.keyboard_state, RS_ARROW_UP_MASK);
532
            break;
647
            break;
533
        case RS_KEY_DOWN:
648
        case RS_KEY_DOWN:
534
            BIT_SET(game.keyboard_state, RS_ARROW_DOWN_MASK);
649
            BIT_SET(game.keyboard_state, RS_ARROW_DOWN_MASK);
535
            break;
650
            break;
536
        case RS_KEY_A:
651
        case RS_KEY_A:
537
            BIT_SET(game.keyboard_state, RS_ATTACK_KEY_MASK);
652
            BIT_SET(game.keyboard_state, RS_ATTACK_KEY_MASK);
538
//            game.shoot_keypressed = 1;
653
//            game.shoot_keypressed = 1;
539
            break;
654
            break;
540
    };
655
    };
541
    
656
    
542
    
657
    
543
    if (game.status == STATUS_MENU) {
658
//    if (game.status == STATUS_MENU) {
544
    
659
//    
545
        switch (key) {
660
//        switch (key) {
546
            case RS_KEY_LEFT:
661
//            case RS_KEY_LEFT:
547
                BIT_SET(game.keyboard_state, RS_ARROW_LEFT_MASK);
662
//                BIT_SET(game.keyboard_state, RS_ARROW_LEFT_MASK);
548
                //PlayBuffer(hBuff, 0);
663
//                //PlayBuffer(hBuff, 0);
549
                break;
664
//                break;
550
            case RS_KEY_RIGHT:
665
//            case RS_KEY_RIGHT:
551
                BIT_SET(game.keyboard_state, RS_ARROW_RIGHT_MASK);
666
//                BIT_SET(game.keyboard_state, RS_ARROW_RIGHT_MASK);
552
                //StopBuffer(hBuff);
667
//                //StopBuffer(hBuff);
553
                break;
668
//                break;
554
            case RS_KEY_UP:
669
//            case RS_KEY_UP:
555
                BIT_SET(game.keyboard_state, RS_ARROW_UP_MASK);
670
//                BIT_SET(game.keyboard_state, RS_ARROW_UP_MASK);
556
                menu_cursor_up();
671
//                menu_cursor_up();
557
                //ResetBuffer(hBuff, 0);
672
//                //ResetBuffer(hBuff, 0);
558
                break;
673
//                break;
559
            case RS_KEY_DOWN:
674
//            case RS_KEY_DOWN:
560
                BIT_SET(game.keyboard_state, RS_ARROW_DOWN_MASK);
675
//                BIT_SET(game.keyboard_state, RS_ARROW_DOWN_MASK);
561
                menu_cursor_down();
676
//                menu_cursor_down();
562
                break;
677
//                break;
563
            case RS_KEY_RETURN:
678
//            case RS_KEY_RETURN:
564
                menu_cursor_click();
679
//                menu_cursor_click();
565
                break;
680
//                break;
566
            case RS_KEY_ESCAPE:
681
//            case RS_KEY_ESCAPE:
567
                menu_open(0);
682
//                menu_open(0);
568
                break;
683
//                break;
569
        };
684
//        };
570
 
685
//
571
    };
686
//    };
572
    
687
    
573
    if (game.status == STATUS_PLAYING) {
688
    if (game.status == STATUS_PLAYING) {
574
        
689
        
575
 
690
 
576
        #ifndef RS_KOS
691
        #ifndef RS_KOS
577
            if (key == RS_KEY_SPACE) {
692
            if (key == RS_KEY_SPACE) {
578
                game.score = 101;
693
                game.score = 101;
579
            };
694
            };
-
 
695
            
580
        #endif
696
        #endif
581
 
697
 
582
        if (key == RS_KEY_ESCAPE) {
698
        if (key == RS_KEY_ESCAPE) {
583
            game.time = 0;
699
            game.time = 0;
584
            game.score = 0;
700
            game.score = 0;
585
            game.status = STATUS_MENU;
701
            game.status = STATUS_MENU;
586
            game.need_redraw = 1;
702
            game.need_redraw = 1;
587
        };
703
        };
-
 
704
        
-
 
705
        if (key == RS_KEY_A) {
-
 
706
            soundbuf_play(&game.sound_bang);
-
 
707
        };
-
 
708
//        if (key == RS_KEY_Z) {
-
 
709
//            soundbuf_play(&game.sound_tack);
-
 
710
//        };
588
        
711
        
589
    };
712
    };
590
 
713
 
591
};
714
};
592
 
715
 
593
void GameKeyUp(int key) {
716
void GameKeyUp(int key) {
594
 
717
 
595
    switch (key) {
718
    switch (key) {
596
        case RS_KEY_LEFT:
719
        case RS_KEY_LEFT:
597
            BIT_CLEAR(game.keyboard_state, RS_ARROW_LEFT_MASK);
720
            BIT_CLEAR(game.keyboard_state, RS_ARROW_LEFT_MASK);
598
            break;
721
            break;
599
        case RS_KEY_RIGHT:
722
        case RS_KEY_RIGHT:
600
            BIT_CLEAR(game.keyboard_state, RS_ARROW_RIGHT_MASK);
723
            BIT_CLEAR(game.keyboard_state, RS_ARROW_RIGHT_MASK);
601
            break;
724
            break;
602
        case RS_KEY_UP:
725
        case RS_KEY_UP:
603
            BIT_CLEAR(game.keyboard_state, RS_ARROW_UP_MASK);
726
            BIT_CLEAR(game.keyboard_state, RS_ARROW_UP_MASK);
604
            break;
727
            break;
605
        case RS_KEY_DOWN:
728
        case RS_KEY_DOWN:
606
            BIT_CLEAR(game.keyboard_state, RS_ARROW_DOWN_MASK);
729
            BIT_CLEAR(game.keyboard_state, RS_ARROW_DOWN_MASK);
607
            break;
730
            break;
608
        case RS_KEY_A:
731
        case RS_KEY_A:
609
            BIT_CLEAR(game.keyboard_state, RS_ATTACK_KEY_MASK);
732
            BIT_CLEAR(game.keyboard_state, RS_ATTACK_KEY_MASK);
610
            break;
733
            break;
611
    };
734
    };
612
 
735
 
613
};
736
};
614
 
737
 
615
typedef struct {
738
typedef struct {
616
    int a;
739
    int a;
617
    int b;
740
    int b;
618
    unsigned short c;
741
    unsigned short c;
619
    unsigned short d;
742
    unsigned short d;
620
} cc_t;
743
} cc_t;
621
 
744
 
622
void GameMouseDown(int x, int y) {
745
void GameMouseDown(int x, int y) {
-
 
746
    
-
 
747
    x = (signed short) x;
623
    
748
    y = (signed short) y;
624
    
749
    
625
    game.need_redraw = 1;
750
    game.need_redraw = 1;
626
    
751
    
627
    game.tx = x;
752
//    game.tx = x;
628
    game.ty = y;
753
//    game.ty = y;
629
    
754
    
630
    if (game.status == STATUS_MENU) {
755
    if (game.status == STATUS_MENU) {
-
 
756
            
-
 
757
        if ( (!game.menu_replay_timeout) && (y > 0) ) {
631
            
758
            
632
        int i;
759
            int i;
633
        for (i = 0; i < FIELD_LENGTH; i++) {
760
            for (i = 0; i < FIELD_LENGTH; i++) {
634
            game.field[i] = (unsigned char) (0.99 * fabs(rs_noise(i, seed*7 + 10)) * CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
761
                game.field[i] = (unsigned char) (0.99 * fabs(rs_noise(i, seed*7 + 10)) * CRYSTALS_COUNT) | CRYSTAL_VISIBLE_BIT;
635
        };
762
            };
-
 
763
            
636
        
764
            game.sound_index = 0;
637
        game.selected = 0;
765
            game.selected = 0;
638
        game.time = 0;
766
            game.time = 25*60 - 1;
639
        game.score = 0;
767
            game.score = 0;
-
 
768
            game.status = STATUS_PLAYING;
-
 
769
        
-
 
770
        };
640
        game.status = STATUS_PLAYING;
771
        
641
        return;
772
        return;
642
    };
773
    };
643
    
774
    
644
    if (game.status == STATUS_PLAYING) {
775
    if (game.status == STATUS_PLAYING) {
645
        
776
        
646
        unsigned int field_x = (unsigned int) (x - FIELD_X0) / CRYSTAL_SIZE;
777
        unsigned int field_x = (unsigned int) (x - FIELD_X0) / CRYSTAL_SIZE;
647
        if (field_x != rs_clamp_i(field_x, 0, FIELD_WIDTH-1)) {
778
        if (field_x != rs_clamp_i(field_x, 0, FIELD_WIDTH-1)) {
648
            return;
779
            return;
649
        };
780
        };
650
        
781
        
651
        unsigned int field_y = (unsigned int) (y - FIELD_Y0) / CRYSTAL_SIZE;
782
        unsigned int field_y = (unsigned int) (y - FIELD_Y0) / CRYSTAL_SIZE;
652
        if (field_y != rs_clamp_i(field_y, 0, FIELD_HEIGHT-1)) {
783
        if (field_y != rs_clamp_i(field_y, 0, FIELD_HEIGHT-1)) {
653
            return;
784
            return;
654
        };
785
        };
655
        
786
        
656
        //FIELD_ITEM(field_x, field_y) = 0;
787
        //FIELD_ITEM(field_x, field_y) = 0;
-
 
788
        
-
 
789
        
-
 
790
//        if (game.game_mode == GAME_MODE_MATCH3) {
-
 
791
        
657
        
792
        
658
        if (!game.selected) {
793
            if (!game.selected) {
659
            game.selected = 1;
794
                game.selected = 1;
660
            game.selected_x = field_x;
795
                game.selected_x = field_x;
661
            game.selected_y = field_y;                                                                      
796
                game.selected_y = field_y;                                                                      
662
            
-
 
-
 
797
                soundbuf_play(&game.sound_click);
663
        }
798
            }
664
        else {
799
            else {
665
            
800
                
666
            if ( abs(game.selected_x - field_x) + abs(game.selected_y - field_y) == 1 ) {
801
                if ( abs(game.selected_x - field_x) + abs(game.selected_y - field_y) == 1 ) {
667
                game.selected = 0;
802
                    game.selected = 0;
668
                
803
                    
669
                // Trying to swap
804
                    // Trying to swap
670
                int temp_crystal = FIELD_ITEM(field_x, field_y);
805
                    int temp_crystal = FIELD_ITEM(field_x, field_y);
671
                FIELD_ITEM(field_x, field_y) = FIELD_ITEM(game.selected_x, game.selected_y);
806
                    FIELD_ITEM(field_x, field_y) = FIELD_ITEM(game.selected_x, game.selected_y);
672
                FIELD_ITEM(game.selected_x, game.selected_y) = temp_crystal;
807
                    FIELD_ITEM(game.selected_x, game.selected_y) = temp_crystal;
673
                
808
                    
674
                if ( !game_check_and_explode() ) {
809
                    if ( !game_check_and_explode() ) {
675
                    FIELD_ITEM(game.selected_x, game.selected_y) = FIELD_ITEM(field_x, field_y);
810
                        FIELD_ITEM(game.selected_x, game.selected_y) = FIELD_ITEM(field_x, field_y);
676
                    FIELD_ITEM(field_x, field_y) = temp_crystal;
811
                        FIELD_ITEM(field_x, field_y) = temp_crystal;
677
                }
812
                    }
678
                else {
813
                    else {
679
                    // success
814
                        // success
680
                    game.process_timer = 0;
815
                        game.process_timer = 0;
681
                };
816
                    };
682
                
817
                    
683
            }
818
                }
684
            else {
819
                else {
-
 
820
                    soundbuf_play(&game.sound_click);
685
                if ( (game.selected_x != field_x) && (game.selected_y != field_y) ) {
821
                    if ( (game.selected_x != field_x) && (game.selected_y != field_y) ) {
686
                    game.selected_x = field_x;
822
                        game.selected_x = field_x;
687
                    game.selected_y = field_y;
823
                        game.selected_y = field_y;
688
                }
824
                    }
689
                else {
825
                    else {
690
                    game.selected = 0;
826
                        game.selected = 0;
-
 
827
                        
691
                };
828
                    };
692
            };
829
                };
693
            
830
                
694
        };
831
            };
-
 
832
            
-
 
833
//        } else if (game.mode == GAME_MODE_RAMPAGE) {
695
        
834
//            
696
//        int i;
835
//            game.selected = 1;
697
//        for (i = field_y; i > 0; i--) {
836
//            game.selected_x = field_x;
-
 
837
//            game.selected_y = field_y;   
-
 
838
//            
-
 
839
//            game_check_and_explode_rampage();
698
//            FIELD_ITEM(field_x, i) = FIELD_ITEM(field_x, (i-1) );
840
//            
699
//        };
-
 
700
//        FIELD_ITEM(field_x, 0) = 0;
841
//        };
701
        
842
        
702
    };
843
    };
703
    
844
    
704
};
845
};
705
 
846
 
706
void GameMouseUp(int x, int y) {
847
void GameMouseUp(int x, int y) {
707
    //
848
    //
708
};
849
};
709
 
850
 
710
 
851
 
711
void game_ding(int i) {
852
void game_ding(int i) {
712
    
853
    
713
    switch (i) {
854
    switch (i) {
714
        case 0:
855
        case 0:
715
            soundbuf_play(&game.sound_test2);
856
//            soundbuf_play(&game.sound_test2);
716
            break;
857
            break;
717
        case 1:
858
        case 1:
718
            soundbuf_play(&game.sound_test3);
859
//            soundbuf_play(&game.sound_test3);
719
            break;
860
            break;
720
    };
861
    };
721
    
862
    
722
};
863
};