File: springshot.cpp

package info (click to toggle)
stax 1.37-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,768 kB
  • ctags: 809
  • sloc: cpp: 8,151; makefile: 30
file content (297 lines) | stat: -rw-r--r-- 10,963 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright 1999-2006 Trent Gamblin
 *
 * This file is part of Stax.
 *
 * Stax is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Stax is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Stax; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "stax.h"

bool SpringShot(void)
{
	input1 = CreateInput(1);
	input1->SetRepeatTimes(150, 150, 300, 400, 150);
	input2 = CreateInput(2);
	if (input2)
		input2->SetRepeatTimes(150, 150, 300, 400, 150);

	if (input2 == 0)
		panel1 = new Panel(SINGLE_PLAYER_PANEL_START_X, PANEL_START_Y, configuration.initial_height, RISE_START);
	else {
		panel1 = new Panel(PANEL1_START_X, PANEL_START_Y, configuration.initial_height, RISE_START);
		panel2 = new Panel(PANEL2_START_X, PANEL_START_Y, configuration.initial_height, RISE_START);
	}

	int player1_column = PANEL_WIDTH/2;
	int player2_column = PANEL_WIDTH/2;
	int player1_block = GetRandomBlock();
	int player2_block = GetRandomBlock();
	int player1_next = GetRandomBlock();
	int player2_next = GetRandomBlock();
	int player1_power = 0;
	int player2_power = 0;
	int player1_charges = START_CHARGES;
	int player2_charges = START_CHARGES;
	int free_charge_count = 0;
	int player1_spring_delay = 0;
	int player2_spring_delay = 0;
	
	int winner = -1;

	int delay_per_block = (int)((float)BLOCK_SIZE / BLAST_INCREMENT);

	bool done = false;
	int last_tick = currentTimeMillis();

	bool esc_pressed = false;

	for (;;) {
		long current_time = currentTimeMillis();
		int steps =  MIN(100, current_time - last_tick);
		last_tick = current_time;
		for (int step = MIN(2, steps); steps > 0; steps -= 2, step = MIN(2, steps)) {
			UpdateFallingBlocks(step);
			if (key[KEY_BACKSPACE])
				SaveScreenshot();
			if (panel1->MoveBlocks(step)) {
				// game over
				if (input2 && panel2->MoveBlocks(step))
					winner = 3;
				else
					winner = 2;
				done = true;
				break;
			}
			if (input2 != 0) {
				if (panel2->MoveBlocks(step)) {
					// game over
					winner = 1;
					done = true;
					break;
				}
			}
			input1->Update();
			if (input1->LeftPressed() && player1_column)
				player1_column--;
			else if (input1->RightPressed() && player1_column < PANEL_WIDTH-1)
				player1_column++;
			else if (!player1_spring_delay && input1->DownPressed()) {
				Block* block = new Block;
				block->type = player1_block;
				block->popping = false;
				block->pop_count = 0;
				block->y = -BLOCK_SIZE-(BLOCK_SIZE*player1_power);
				block->fall_increment = BLAST_INCREMENT;
				block->fall_count = 0.0;
				block->pop_power = player1_power;
				if (player1_power) {
					play_sample(blast_sample, 255, 0, 1000, 0);
					panel1->AddFallingBlock(player1_column, block, true);
					player1_spring_delay = player1_power * delay_per_block;
					if (!player1_spring_delay) {
						player1_block = player1_next;
						player1_next = GetRandomBlock();
					}
					player1_power = 0;
				}
				else {
					if (!panel1->AddFallingBlock(player1_column, block, false)) {
						play_sample(nocharge_sample, 255, 0, 1000, 0);
						delete block;
					}
					else {
						play_sample(blast_sample, 255, 0, 1000, 0);
						player1_block = player1_next;
						player1_next = GetRandomBlock();
					}
				}
			}
			if (!player1_spring_delay && input1->UpPressed() && player1_power < 3 && player1_charges) {
				play_sample(charge_sample, 255, 0, 1000, 0);
				player1_power++;
				player1_charges--;
			}
			else if (input1->UpPressed())
					play_sample(nocharge_sample, 255, 0, 1000, 0);
			if (player1_spring_delay) {
				player1_spring_delay--;
				if (player1_spring_delay <= 0) {
					player1_block = player1_next;
					player1_next = GetRandomBlock();
				}
				else
					player1_power = player1_spring_delay / delay_per_block;
			}
			if (input2 != 0) {
				input2->Update();
				if (input2->LeftPressed() && player2_column)
					player2_column--;
				else if (input2->RightPressed() && player2_column < PANEL_WIDTH-1)
					player2_column++;
				else if (!player2_spring_delay && input2->DownPressed()) {
					Block* block = new Block;
					block->type = player2_block;
					block->popping = false;
					block->pop_count = 0;
					block->y = -BLOCK_SIZE-(BLOCK_SIZE*player2_power);
					block->fall_increment = BLAST_INCREMENT;
					block->fall_count = 0.0;
					block->pop_power = player2_power;
					if (player2_power) {
						play_sample(blast_sample, 255, 0, 1000, 0);
						panel2->AddFallingBlock(player2_column, block, true);
						player2_spring_delay = player2_power * delay_per_block;
						if (!player2_spring_delay) {
							player2_block = player2_next;
							player2_next = GetRandomBlock();
						}
						player2_power = 0;
					}
					else {
						if (!panel2->AddFallingBlock(player2_column, block, false)) {
							play_sample(nocharge_sample, 255, 0, 1000, 0);
							delete block;
						}
						else {
							play_sample(blast_sample, 255, 0, 1000, 0);
							player2_block = player2_next;
							player2_next = GetRandomBlock();
						}
					}
				}
				if (!player2_spring_delay && input2->UpPressed() && player2_power < 3 && player2_charges) {
					play_sample(charge_sample, 255, 0, 1000, 0);
					player2_power++;
					player2_charges--;
				}
				else if (input1->UpPressed())
						play_sample(nocharge_sample, 255, 0, 1000, 0);
				if (player2_spring_delay) {
					player2_spring_delay--;
					if (player2_spring_delay <= 0) {
						player2_block = player2_next;
						player2_next = GetRandomBlock();
					}
					else
						player2_power = player2_spring_delay / delay_per_block;
				}
			}
			free_charge_count++;
			if (free_charge_count >= FREE_CHARGE_DELAY) {
				free_charge_count = 0;
				player1_charges++;
				player2_charges++;
			}
			if (key[KEY_ESC]) {
				WaitForRelease();
				if (GUI_Prompt("|000255000Q|000255255u|240000240i|255000000t |255255000t|000255000h|000255255i|240000240s |255000000g|255255000a|000255000m|000255255e|240000240?", "Yes", "No")) {
					esc_pressed = true;
					goto end;
				}
			}
		}
		blit(background_bitmap, buffer, 0, 0, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);
		int player1_spring_x;
		int score = panel1->GetScore();
		if (input2 == 0) {
			player1_spring_x = SINGLE_PLAYER_PANEL_START_X;
			DrawSunkenRectangle(SINGLE_PLAYER_PANEL_START_X-1, PANEL_START_Y-1, SINGLE_PLAYER_PANEL_START_X+(PANEL_WIDTH*BLOCK_SIZE), PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE));
			DrawText(SINGLE_PLAYER_PANEL_START_X, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12, false, "Score: %d", score);
			DrawText(buffer->w/2, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12, true, "%d", player1_charges);
			DrawText(SINGLE_PLAYER_PANEL_START_X+(PANEL_WIDTH*BLOCK_SIZE)-65, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12, false, "Next:");
			draw_sprite(buffer, block_bitmaps[player1_next], SINGLE_PLAYER_PANEL_START_X+(PANEL_WIDTH*BLOCK_SIZE)-BLOCK_SIZE, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12);
		}
		else {
			player1_spring_x = PANEL1_START_X;
			DrawSunkenRectangle(PANEL1_START_X-1, PANEL_START_Y-1, PANEL1_START_X+(PANEL_WIDTH*BLOCK_SIZE), PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE));
			DrawText(PANEL1_START_X, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12, false, "Score: %d", score);
			DrawText(PANEL1_START_X+((PANEL_WIDTH*BLOCK_SIZE)/2), PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12, true, "%d", player1_charges);
			DrawText(PANEL1_START_X+(PANEL_WIDTH*BLOCK_SIZE)-65, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12, false, "Next:");
			draw_sprite(buffer, block_bitmaps[player1_next], PANEL1_START_X+(PANEL_WIDTH*BLOCK_SIZE)-BLOCK_SIZE, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12);
		}
		draw_sprite(buffer, spring_bitmaps[player1_power], player1_spring_x+(player1_column*BLOCK_SIZE), PANEL_START_Y-spring_bitmaps[0]->h-BLOCK_SIZE);
		if (!player1_spring_delay)
			draw_sprite(buffer, block_bitmaps[player1_block], player1_spring_x+(player1_column*BLOCK_SIZE), PANEL_START_Y-BLOCK_SIZE-(BLOCK_SIZE*player1_power));
		panel1->Draw();
		if (input2 != 0) {
			DrawSunkenRectangle(PANEL2_START_X-1, PANEL_START_Y-1, PANEL2_START_X+(PANEL_WIDTH*BLOCK_SIZE), PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE));
			score = panel2->GetScore();
			DrawText(PANEL2_START_X, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12, false, "Score: %d", score);
			DrawText(PANEL2_START_X+((PANEL_WIDTH*BLOCK_SIZE)/2), PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12, true, "%d", player2_charges);
			DrawText(PANEL2_START_X+(PANEL_WIDTH*BLOCK_SIZE)-65, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12, false, "Next:");
			draw_sprite(buffer, block_bitmaps[player2_next], PANEL2_START_X+(PANEL_WIDTH*BLOCK_SIZE)-BLOCK_SIZE, PANEL_START_Y+(PANEL_HEIGHT*BLOCK_SIZE)+12);
			draw_sprite(buffer, spring_bitmaps[player2_power], PANEL2_START_X+(player2_column*BLOCK_SIZE), PANEL_START_Y-spring_bitmaps[0]->h-BLOCK_SIZE);
			if (!player2_spring_delay)
				draw_sprite(buffer, block_bitmaps[player2_block], PANEL2_START_X+(player2_column*BLOCK_SIZE), PANEL_START_Y-BLOCK_SIZE-(BLOCK_SIZE*player2_power));
			panel2->Draw();
		}
		BlitToScreen();
		int duration = currentTimeMillis() - last_tick;
		if (duration < TIMESTEP)
			rest(TIMESTEP-duration);
		if (done)
			break;
	}
end:

	if (!esc_pressed) {
		if (input2 && winner >= 1) {
			char *note;
			if (panel1->GetScore() > panel2->GetScore()) {
				if (winner == 1)
					note = "|255255000Player 1 scored higher\n|255255000AND lasted longer!";
				else if (winner == 2)
					note = "|255255000Player 1 scored higher...\n|255255255BUT player 2 lasted longer!";
				else
					note = "|255255000Player 1 scored higher...\n|255255255BUT it was a tie for time!";
			}
			else if (panel1->GetScore() < panel2->GetScore()) {
				if (winner == 1)
					note = "|255255000Player 2 scored higher...\n|255255255BUT player 1 lasted longer!";
				else if (winner == 2)
					note = "|255255000Player 2 scored higher\n|255255000AND lasted longer!";
				else
					note = "|255255000Player 2 scored higher...\n|255255255BUT it was a tie for time!";
			}
			else {
				if (winner == 1)
					note = "|000255000It was a tie for score...\n|255255000BUT player 1 lasted longer!";
				else if (winner == 2)
					note = "|000255000It was a tie for score...\n|255255000BUT player 2 lasted longer!";
				else
					note = "|000255000Wow!\n|000255000A dead-even tie!";
			}
			GUI_WaitMessage(note);
		}
	}

	if (input2 == 0) {
		if (!esc_pressed)
			CheckHighScores(panel1->GetScore());
		delete input1;
		delete panel1;
	}
	else {
		delete input1;
		delete input2;
		delete panel1;
		delete panel2;
	}

	WaitForRelease();

	return esc_pressed;
}