Codebase list brainparty / fresh-snapshots/main jeweljam.cpp
fresh-snapshots/main

Tree @fresh-snapshots/main (Download .tar.gz)

jeweljam.cpp @fresh-snapshots/mainraw · history · blame

  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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Brain Party
// Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)

// Brain Party 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 3
// of the License, or (at your option) any later version.

// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "jeweljam.h"
#include "Minigame.h"
	
BPMiniGame_JewelJam::BPMiniGame_JewelJam(BPGame* game) : BPMiniGame(game) {
	sfcBackground = TheGame->LoadBitmap("jeweljam", 320, 416);
	
	SelectedRow = NULL;
	Locked = false; // when true disallow movement
	TimeStarted = 0;
	SuccessTime = -1;
	
	sfcClock = sfcScoreStr = NULL;
	
	DisappearTime = 350;
	
	Score = 0;
	SetScore();
		
	GameTitle = "Jewel Jam";
	GameHelp = "Drag the rows of gems left and right, matching three or more of the same type scores points and more gems will fall. The more you match in one move, the more you score. But hurry - you have only two minutes!";
	GameHelp2 = "The key in this game is that you can only slide left and right, so you need to start looking for vertical columns of gems to match. Generally it's best to work near the bottom of the screen, because that way falling gems are more likely to have a chain reaction and earn you more points.";
	
	MiniGameType = PUZZLE;
	
	ColoursLo.Add(TheGame->LoadBitmap("gem1_40", 40, 40));
	ColoursLo.Add(TheGame->LoadBitmap("gem2_40", 40, 40));
	ColoursLo.Add(TheGame->LoadBitmap("gem3_40", 40, 40));
	ColoursLo.Add(TheGame->LoadBitmap("gem4_40", 40, 40));
	ColoursLo.Add(TheGame->LoadBitmap("gem5_40", 40, 40));
	ColoursLo.Add(TheGame->LoadBitmap("gem6_40", 40, 40));
	ColoursLo.Add(TheGame->LoadBitmap("gem7_40", 40, 40));
	ColoursLo.Add(TheGame->LoadBitmap("gem8_40", 40, 40));
	
	ColoursHi.Add(TheGame->LoadBitmap("gem1_hi_40", 40, 40));
	ColoursHi.Add(TheGame->LoadBitmap("gem2_hi_40", 40, 40));
	ColoursHi.Add(TheGame->LoadBitmap("gem3_hi_40", 40, 40));
	ColoursHi.Add(TheGame->LoadBitmap("gem4_hi_40", 40, 40));
	ColoursHi.Add(TheGame->LoadBitmap("gem5_hi_40", 40, 40));
	ColoursHi.Add(TheGame->LoadBitmap("gem6_hi_40", 40, 40));
	ColoursHi.Add(TheGame->LoadBitmap("gem7_hi_40", 40, 40));
	ColoursHi.Add(TheGame->LoadBitmap("gem8_hi_40", 40, 40));
	
	for (int j = 0; j < 9; ++j) {
		vector<BPMiniGame_JewelJam_Box*>* row = new vector<BPMiniGame_JewelJam_Box*>();
		
		for (int i = 0; i < 8; ++i) {
			BPMiniGame_JewelJam_Box* box = new BPMiniGame_JewelJam_Box();
			box->X = i * BoxSize;
			box->Y = -((9 - j) * BoxSize * i);
			box->DestY = 77 + (j * BoxSize);
			box->Colour = TheGame->RandomRange(0, ColoursLo.Count - 1);
			row->push_back(box);
		}
		
		Boxes.push_back(row);
	}
}

BPMiniGame_JewelJam::~BPMiniGame_JewelJam() {
	SAFE_DELETE(sfcBackground);
	
	BPMiniGame_JewelJam_Box* box;
	
	for (int i = 0; i < Boxes.size(); ++i) {
		vector<BPMiniGame_JewelJam_Box*>* row = Boxes[i];
		
		for (int i = row->size() - 1; i >= 0; --i) {
			box = (*row)[i];
			SAFE_DELETE(box);
		}
		
		row->clear();
		SAFE_DELETE(row);
	}
	
	Boxes.clear();
	
	ColoursLo.Clear();
	ColoursHi.Clear();
	
	if (SelectedRow != NULL) {
		// shouldn't have to do this, because it's just pointers to things that have already been deleted
		//SelectedRow->clear();
		SAFE_DELETE(SelectedRow);
	}
	
	SAFE_DELETE(sfcScoreStr);
	SAFE_DELETE(sfcClock);
}

void BPMiniGame_JewelJam::OnMouseMove() {
	if (Locked) return;
	if (SelectedRow == NULL) return;
	
	Locked = true;
	
	BPMiniGame_JewelJam_Box* box;
	
	if (LastPos != TheGame->EmptyPoint && LastPos != TouchEvent) {
		if (TouchEvent.X < LastPos.X) {
			// move left
			
			for (int i = SelectedRow->size() - 1; i >= 0; --i) {
				box = (*SelectedRow)[i];
				
				box->X += TouchEvent.X - LastPos.X;
				
				if (box->X <= -BoxSize) {
					box->X = MiniGameWidth + box->X;
					SelectedRow->erase(SelectedRow->begin() + i);
					SelectedRow->push_back(box);
				}
			}
			
		} else {
			// move right
			
			for (int i = 0; i < SelectedRow->size(); ++i) {
				box = (*SelectedRow)[i];
				
				box->X += TouchEvent.X - LastPos.X;
				
				if (box->X >= MiniGameWidth) {
					box->X -= MiniGameWidth;
					SelectedRow->erase(SelectedRow->begin() + i);
					SelectedRow->insert(SelectedRow->begin() + 0, box);
				}
			}
		}
	}
	
	LastPos = TouchEvent;
	
	Locked = false;
}

void BPMiniGame_JewelJam::OnMouseDown() {
	if (Locked) return;
	Locked = true;
	
	LastPos = TouchEvent;
	
	for (int i = 0; i < Boxes.size(); ++i) {
		if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 0, 57 + (i * BoxSize), 320, BoxSize)) {
			// move this row
			SelectedRow = Boxes[i];
			break;
		}
	}
	
	Locked = false;
}

void BPMiniGame_JewelJam::OnMouseUp() {
	if (Locked) return;
	if (SelectedRow == NULL) return;
	
	BPMiniGame_JewelJam_Box* box;
	
	Locked = true;
	
	// we've let go of a row - snap it into place!
	int xoffset = (*SelectedRow)[0]->X;
	
	int remainder = xoffset % BoxSize;
	if (remainder != 0) {
		int adjust = 0;
		
		if (remainder > HalfBoxSize || (remainder < 0 && remainder > -HalfBoxSize)) {
			// snap to the right
			if (remainder > 0) {
				adjust = BoxSize - remainder;
			} else {
				adjust = abs(remainder);
			}
			
			for (int i = 0; i < SelectedRow->size(); ++i) {
				box = (*SelectedRow)[i];
				
				box->X += adjust;
				
				if (box->X >= MiniGameWidth) {
					box->X -= MiniGameWidth;
					SelectedRow->erase(SelectedRow->begin() + i);
					SelectedRow->insert(SelectedRow->begin() + 0, box);
				}
			}
		} else {
			// snap to the left
			if (remainder < 0) {
				adjust = abs(remainder) - BoxSize;
			} else {
				adjust = -remainder;
			}
			
			for (int i = SelectedRow->size() - 1; i >= 0; --i) {
				box = (*SelectedRow)[i];
				
				box->X += adjust;
				
				if (box->X <= -BoxSize) {
					box->X = MiniGameWidth + box->X;
					SelectedRow->erase(SelectedRow->begin() + i);
					SelectedRow->push_back(box);
				}
			}
			
		}
	}
	
	SelectedRow = NULL;
	
	CheckForMatches();
	
	// subtract one point for each move made
	if (Score > 0) {
		--Score;
		SetScore();
	}
}

void BPMiniGame_JewelJam::Start() {
	TimeStarted = TheGame->TickCount;
}

int BPMiniGame_JewelJam::GetWeight() {
	return MinMax(round(Score / 6));
}

void BPMiniGame_JewelJam::Render() {
	TheGame->DrawImage(sfcBackground, 0, 0);
	
	if (!MarathonMode) {
		int TimePassed = TheGame->TickCount - TimeStarted;
		TimePassed = 120000 - TimePassed;
		
		if (TimePassed <= 0) {
			if (SuccessTime == -1) {
				SuccessTime = TheGame->TickCount;
				Success();
			}
		}
		
		if (sfcClock == NULL || RedrawClock()) {
			TheGame->AllocString(&sfcClock, TheGame->TicksToTime(TimePassed)->c_str(), LARGE, 160, 50, RIGHT);
		}
		
		TheGame->DrawString(sfcClock, WHITE, 150, 4);
	}
	
	TheGame->DrawString(sfcScoreStr, WHITE, 6, 4);
	
	BPMiniGame_JewelJam_Box* box;
	
	for (int i = 0; i < Boxes.size(); ++i) {
		vector<BPMiniGame_JewelJam_Box*>* row = Boxes[i];

		for (int i = row->size() - 1; i >= 0; --i) {
			box = (*row)[i];
			
			// HACK: There's an unknown bug in this minigame that occasionally puts boxes in the wrong X pos.
			// To reproduce, just keep matching stuff while dragging around on the screen manically.
			// This force-resets the X position for each box to the correct value. What a waste of CPU time!
			if (SelectedRow != row) box->X = i * BoxSize;
			
			if (box->MatchTime == -1) {
				TheGame->DrawImage(ColoursLo[box->Colour], box->X + HalfBoxSize, box->Y, 0.0f, 1.0f, (*TheGame->White));
				
				if (box->X < 0) {
					TheGame->DrawImage(ColoursLo[box->Colour], MiniGameWidth + box->X + HalfBoxSize, box->Y, 0.0f, 1.0f, (*TheGame->White));
				} else if (box->X > MiniGameWidth - BoxSize) {
					TheGame->DrawImage(ColoursLo[box->Colour],  HalfBoxSize + box->X - MiniGameWidth, box->Y, 0.0f, 1.0f, (*TheGame->White));
				}
			} else {
				float diff = TheGame->TickCount - box->MatchTime;
				
				if (diff <= DisappearTime) {
					float step = diff / DisappearTime; // get a value between 0 and 1
					Colour col = Colour(1.0f, 1.0f, 1.0f, 1 - step);
					
					TheGame->DrawImage(ColoursHi[box->Colour], box->X + HalfBoxSize, box->Y, 0.0f, TheGame->SmoothStep(1.0f, 3.0f, step), col);
					
					if (box->X < 0) {
						TheGame->DrawImage(ColoursHi[box->Colour], MiniGameWidth + box->X + HalfBoxSize, box->Y, 0.0f, TheGame->SmoothStep(1.0f, 3.0f, step), (*TheGame->White));
					} else if (box->X > MiniGameWidth - BoxSize) {
						TheGame->DrawImage(ColoursHi[box->Colour],  HalfBoxSize + box->X - MiniGameWidth, box->Y, 0.0f, TheGame->SmoothStep(1.0f, 3.0f, step), (*TheGame->White));
					}
				}
			}
		}
	}
}

void BPMiniGame_JewelJam::Tick() {
	vector<BPMiniGame_JewelJam_Box*>* row;
	BPMiniGame_JewelJam_Box* box;
	BPMiniGame_JewelJam_Box* copybox;
	
	int MatchTimeout = TheGame->TickCount - 150;
	
	bool AllStill = true;
	
	for (int i = Boxes.size() - 1; i >= 0; --i) {
		row = Boxes[i];
		
		for (int j = row->size() - 1; j >= 0; --j) {
			box = (*row)[j];
			
			if (box->MatchTime != -1) AllStill = false;
			
			if (box->Y != box->DestY) {
				AllStill = false;
				
				box->YSpeed += 2.0f;
				box->Y += box->YSpeed;
				if (box->Y > box->DestY) {
					box->Y = box->DestY;
					box->YSpeed = 0;
				}
			}
			
			if (box->MatchTime != -1 && box->MatchTime < MatchTimeout) {
				SAFE_DELETE(box);
				
				row->erase(row->begin() + j);
				// move all boxes down above me
				
				for (int k = i - 1; k >= 0; --k) {
					copybox = (*Boxes[k])[j];
					Boxes[k]->erase(Boxes[k]->begin() + j);
					Boxes[k + 1]->insert(Boxes[k + 1]->begin() + j, copybox);
					copybox->DestY = 77 + ((k + 1) * BoxSize);
				}
				
				BPMiniGame_JewelJam_Box* newbox = new BPMiniGame_JewelJam_Box();
				newbox->X = j * BoxSize;
				newbox->Y = -((9 - j) * BoxSize);
				newbox->DestY = 77;
				newbox->Colour = TheGame->RandomRange(0, ColoursLo.Count - 1);
				Boxes[0]->insert(Boxes[0]->begin() + j, newbox);
			}
		}
	}
	
	if (AllStill) {
		if (Locked) {
			CheckForMatches();
		}
	} else {
		Locked = true;
	}
}

void BPMiniGame_JewelJam::CheckForMatches() {
	Locked = true;
	
	int ThisScore = 0;
	
	// if any three boxes are in a line, match them and any others touching them of the same colour
	bool got_match = false;
	
	vector<BPMiniGame_JewelJam_Box*>* row;
	BPMiniGame_JewelJam_Box* box;
	
	for (int i = Boxes.size() - 1; i >= 0; --i) {
		row = Boxes[i];
		
		for (int j = row->size() - 1; j >= 0; --j) {
			// does this box match?
			box = (*row)[j];
			
			// check two boxes to the left
			if (j > 1) {
				if ((*row)[j - 1]->Colour == box->Colour && (*row)[j - 2]->Colour == box->Colour) {
					got_match = true;
					ThisScore += MatchBox(box, i, j);
					continue;
				}
			}
			
			if (j > 0 && j < 7) {
				// try matching one to the left and one to the right
				if ((*row)[j - 1]->Colour == box->Colour && (*row)[j + 1]->Colour == box->Colour) {
					got_match = true;
					ThisScore += MatchBox(box, i, j);
					continue;
				}
			}
			
			// check two boxes to the right
			if (j < 6) {
				if ((*row)[j + 1]->Colour == box->Colour && (*row)[j + 2]->Colour == box->Colour) {
					got_match = true;
					ThisScore += MatchBox(box, i, j);
					continue;
				}
			}
			
			
			// check two boxes above
			if (i > 1) {
				if ((*Boxes[i - 1])[j]->Colour == box->Colour && (*Boxes[i - 2])[j]->Colour == box->Colour) {
					got_match = true;
					ThisScore += MatchBox(box, i, j);
					continue;
				}
			}
			
			// try matching one box above and one box below
			if (i > 0 && i < 7) {
				if ((*Boxes[i - 1])[j]->Colour == box->Colour && (*Boxes[i + 1])[j]->Colour == box->Colour) {
					got_match = true;
					ThisScore += MatchBox(box, i, j);
					continue;
				}
			}
			
			// check two boxes below
			if (i < 6) {
				if ((*Boxes[i + 1])[j]->Colour == box->Colour && (*Boxes[i + 2])[j]->Colour == box->Colour) {
					got_match = true;
					ThisScore += MatchBox(box, i, j);
					continue;
				}
			}
			
		}
	}
	
	if (ThisScore != 0) {
		Score += min(512, (int)round(pow(2.0f, ThisScore)));
		SetScore();
	}
	
	Locked = got_match;
	
	if (got_match) {
		TheGame->PlaySound("gem_select");
	}
}

int BPMiniGame_JewelJam::MatchBox(BPMiniGame_JewelJam_Box* box, int row, int col) {
	if (box->MatchTime == -1) {
		box->MatchTime = TheGame->TickCount;
		
		int thisscore = 1;
		
		// now match all other identical boxes around it
		if (row > 0) {
			if ((*Boxes[row - 1])[col]->Colour == box->Colour && (*Boxes[row - 1])[col]->MatchTime == -1) {
				thisscore += MatchBox((*Boxes[row - 1])[col], row - 1, col);
			}
		}
		
		if (row < 7) {
			if ((*Boxes[row + 1])[col]->Colour == box->Colour && (*Boxes[row + 1])[col]->MatchTime == -1) {
				thisscore += MatchBox((*Boxes[row + 1])[col], row + 1, col);
			}
		}
		
		if (col > 0) {
			if ((*Boxes[row])[col - 1]->Colour == box->Colour && (*Boxes[row])[col - 1]->MatchTime == -1) {
				thisscore += MatchBox((*Boxes[row])[col - 1], row, col - 1);
			}
		}
		
		if (col < 7) {
			if ((*Boxes[row])[col + 1]->Colour == box->Colour && (*Boxes[row])[col + 1]->MatchTime == -1) {
				thisscore += MatchBox((*Boxes[row])[col + 1], row, col + 1);
			}
		}
		
		return thisscore;
	} else {
		return 0;
	}
}

void BPMiniGame_JewelJam::SetMarathon() {
	MarathonMode = true;
	GameHelp = "Drag rows of jewels left and right, matching three or more to score points; the more you match, the more you score.";
}

void BPMiniGame_JewelJam::SetScore() {
	if (Score > 0) {
		ostringstream score;
		string scorestr = TheGame->SeparateThousands(Score);
		score << "Score: " << scorestr;
		TheGame->AllocString(&sfcScoreStr, score.str().c_str(), LARGE, 270, 50, LEFT);
	} else {
		TheGame->AllocString(&sfcScoreStr, "Score: 0", LARGE, 270, 50, LEFT);
	}
}