-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieClicker.ino
617 lines (471 loc) · 13.8 KB
/
CookieClicker.ino
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
CookieClicker game for arduino
It's a ripoff of a classic clicker game for phones
It's really nothing new
I tested this on atmega 328p and 32u4 micro controllers,
but it should work on others as well
the only problem might be the low power library
I used this one: https://github.com/rocketscream/Low-Power
That library works on these MCUs:
ATMega88
ATMega168
ATMega168P
ATMega328P
ATMega32U4
ATMega644P
ATMega1284P
ATMega2560
ATMega256RFR2
ATSAMD21G18A
the problem with some of these might be lack of memory
Adafruit SSD1306 library uses a lot of memory, so look out for that
I made this game a bit more interesting with added levels.
You have 4 auto clicker uogrades and 4 click upgrades.
To level up, you have to collect 10 of each.
Levels get progressively harder and harder.
I had to make my own big number notation
1aa = 1
1ab = 1000
1ac = 10000000
...
1jj = 10¹⁰⁰
when you get to that point, you've beaten the game
It will take you like 70000 years, but that's fine.
Low Power
After 30seconds of not doing anything, it goes into deep sleep.
You get about 1% of the revenue for 2 hours while in sleep mode.
the first screen also has some power saving measures, so if you want to
reprogram your MCU, you'll have to do it from the upgrade screens and
not from the home screen (the MCU turns off 30 times per second, which disables
all usb functionality). If you want to decrease power conumption, desolder the led
that is always on. Those pesky little things add 2-3mA to the total consumption.
Even when the MCU is sleeping, so if you want it to be battery friendly, you really have to do it.
Hookup guide
I used pins 0 and 1.
They work on 32u4. If you use it on other MCUs, have a look at this page to see
which pins you can use: https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
I'm handling all the clicks with interrupts, so the pins have to support them as well.
Display
This game is meant for SSD1306 based 128x32 OLED displays.
You can change it. I'm using the I2C flavor.
EEPROM
The game is being backed up to EEPROM every few minutes and everytime it goes to sleep and when you level up.
https://github.com/andraz213/CookieClickerArduino
*/
//define your pins
#define CLICKPIN 1
#define CONTEXTPIN 0
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <LowPower.h>
Adafruit_SSD1306 display(128, 32, &Wire, -1);
#include <EEPROM.h>
double currentCPS = 0;
double displayCPS = 0;
double cookies = 0;
double previousLvlCPS = 0;
double previousLvlClickWorth = 0;
double clickWorth = 1;
double decimalPower = 1;
int level = 1;
int upgrades [2][4];
double prices [2][4];
int lastBack = 0;
double clicks = 0.0;
double clicksMult = 1.0;
int clickPower = 7;
int secondPower = 10;
int clickCost = 10;
int secondCost = 7;
int context = 0;
long prevClick = 0;
long prevContx = 0;
long prevUpd = 0;
long loopLen = 0;
/////////////////////////////// EEPROM /////////////////////////////////////////////////
struct backupObject {
double previousLvlCPS;
double decimalPower;
int level;
int upgrades [2][4];
double cookies;
};
void backupToEEPROM() {
backupObject bck;
bck.previousLvlCPS = previousLvlCPS;
bck.decimalPower = decimalPower;
bck.level = level;
for (int i = 0; i < 4; i++) {
bck.upgrades[0][i] = upgrades[0][i];
bck.upgrades[1][i] = upgrades[1][i];
}
bck.cookies = cookies;
EEPROM.put(100, bck);
}
void retrieveFromEEPROM() {
backupObject rtrv;
EEPROM.get(100, rtrv);
previousLvlCPS = rtrv.previousLvlCPS;
decimalPower = rtrv.decimalPower;
level = rtrv.level;
for (int i = 0; i < 4; i++) {
upgrades[0][i] = rtrv.upgrades[0][i];
upgrades[1][i] = rtrv.upgrades[1][i];
}
cookies = rtrv.cookies;
}
///////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////// DISPLAYING AND FORMATTING ///////////////////////////////////////
// This handles my weird number notation
String getMultString(int offset) {
String res = "";
int prva = (int) ( (int)(((int)decimalPower + offset) / 3) / 10);
switch (prva) {
case 0:
res += "a";
break;
case 1:
res += "b";
break;
case 2:
res += "c";
break;
case 3:
res += "d";
break;
case 4:
res += "e";
break;
case 5:
res += "f";
break;
case 6:
res += "g";
break;
case 7:
res += "h";
break;
case 8:
res += "i";
break;
default:
res += "j";
break;
}
int druga = (int)((int)(((int)decimalPower + offset) / 3) % 10);
switch (druga) {
case 0:
res += "a";
break;
case 1:
res += "b";
break;
case 2:
res += "c";
break;
case 3:
res += "d";
break;
case 4:
res += "e";
break;
case 5:
res += "f";
break;
case 6:
res += "g";
break;
case 7:
res += "h";
break;
case 8:
res += "i";
break;
default:
res += "j";
break;
}
return res;
}
// This makes numbers fit on the screen
// it just does this: 2443.34aa = 2.44ab
String getNumberDisplay(double num) {
int offset = 0;
//Serial.println(num);
//Serial.println(" sdfds ");
while (num > 1000) {
num /= 1000;
offset += 3;
}
String res = String(num);
res += getMultString(offset);
return res;
}
void sleepDisplay(Adafruit_SSD1306* display) {
display->ssd1306_command(SSD1306_DISPLAYOFF);
}
void wakeDisplay(Adafruit_SSD1306* display) {
display->ssd1306_command(SSD1306_DISPLAYON);
}
//this handles displaying upgrade screens
// UI is always messy, so don't beat me over this one lol
void displayContext() {
int item = (context - 1) % 4;
int category = 0;
if (context - 1 > 3) category++;
while (upgrades[category][item] > 9) {
context++;
context = context % 9;
item = (context - 1) % 4;
if (context - 1 > 3) category = 1;
}
if (item == 2 && upgrades[category][0] < 10) context += 2;
if ((item == 3 && upgrades[category][1] < 10)) context ++;
context = context % 9;
item = (context - 1) % 4;
if (context - 1 > 3) category = 1;
if (context != 0) {
String itemname = "Auto clicker";
if (category) {
itemname = "Click Booster";
}
updatePrices();
int having = upgrades[category][item];
int performance = 10 * pow(secondPower, item);
if (category) performance = 10 * pow(clickPower, item);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(itemname);
display.print(" ");
display.print(item + 1);
display.print(" x");
display.println(having);
display.print("+");
display.print(getNumberDisplay(performance));
if (!category)
display.println(" CPS");
if (category)
display.println("/click");
display.print("Cost: ");
display.println(getNumberDisplay(prices[category][item]));
display.print("Cookies ");
display.println(getNumberDisplay(cookies));
display.display();
}
}
// this is displaying the home screen. Nothing special really
void displayHome() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Cookies ");
display.println(getNumberDisplay(cookies));
display.print("CPS ");
display.println(getNumberDisplay(displayCPS));
display.print("Level ");
display.println(level);
//display.print(clicksMult);
display.display();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////// GAMEPLAY AND LEVEL UP STUFF ///////////////////////////////
/////////////// HOW MUCH EARNINGS
// This calculates click worth
// click worth at the end of previous level + all the upgrades from this level
void updateClickWorth() {
double worth = previousLvlClickWorth;
for (int i = 0; i < 4; i++) {
worth += 10 * upgrades[1][i] * pow(clickPower, i);
}
clickWorth = worth;
if (worth == 0) {
clickWorth = 1;
}
}
// This figures out how much earnings you get per second it's the same formula as for clicks really
double figureOutCPS() {
double res = previousLvlCPS;
for (int i = 0; i < 4; i++) {
res += 10 * upgrades[0][i] * pow(secondPower, i);
}
return res;
}
//////////// PRICES AND BUYING
// I'm doing some fancy stuff here to make the game interesting
void updatePrices() {
double multiplier = level * 0.005 + 0.10;
for (int i = 0; i < 4; i++) {
prices[0][i] = 40 * level * pow(secondCost, i) * pow((multiplier + 1), upgrades[0][i] * 2.7);
}
for (int i = 0; i < 4; i++) {
prices[1][i] = 40 * level * pow(clickCost, i) * pow((multiplier + 1), upgrades[1][i] * 2);
}
}
// if you have enough money, it buys what you're looking at
// it also updates all the values that change with buying an item
void buy() {
updatePrices();
int item = (context - 1) % 4;
int category = 0;
if (context - 1 > 3) category++;
if (prices[category][item] <= cookies && upgrades[category][item] < 10) {
cookies -= prices[category][item];
upgrades[category][item] ++;
currentCPS = figureOutCPS();
updateClickWorth();
amLevelUp();
}
}
//////LEVELS/////////////
// it checks if you've bought enough upgrades to level up
boolean amLevelUp() {
for (int i = 0; i < 4; i++) {
if (upgrades[0][i] < 10 || upgrades[1][i] < 10) return false;
}
levelUp();
context = 0;
return true;
}
// just levels you up
// I'm doing some weird stuff with my weird bug number notation
int levelUp() {
previousLvlCPS = figureOutCPS();
previousLvlClickWorth = clickWorth;
double chg = previousLvlCPS;
int offset = 0;
while (chg > 1000) {
chg /= 1000;
offset += 3;
cookies /= 1000;
previousLvlClickWorth /= 1000;
}
previousLvlCPS = chg;
decimalPower += offset;
level ++;
for (int i = 0; i < 4; i++) {
upgrades[0][i] = 0;
upgrades[1][i] = 0;
}
updatePrices();
updateClickWorth();
currentCPS = figureOutCPS();
backupToEEPROM();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////// HOUSEKEEPING AND LOW POWER AND INTERRUPTS AND TIMED OPERATIONS ///////
void timedStuff() {
// millis resets back to zero from time to time
// this makes sure that the game doesn't get stuck
if (millis() < 1500) {
prevClick = 0;
prevContx = 0;
prevUpd = 0;
loopLen = 0;
}
// this is updating the averageCPS that gets displayed and number of clicks for click multiplier
//it's doing that every half a second
if (millis() - loopLen > 500) {
displayCPS -= (displayCPS - currentCPS) * (millis() - loopLen) / 1000;
clicks -= clicks * (millis() - loopLen) / 10000;
loopLen = millis();
}
//This adds up your per second earnings to you bank account
// it also keeps track if when the backups are supposed to happen
if (millis() - prevUpd >= 1000) {
cookies += currentCPS;
prevUpd = millis();
lastBack--;
if (lastBack < 1) {
backupToEEPROM();
lastBack = 600;
}
}
}
void lowPowerStuff() {
// if you're on the home screen, it goes to sleep for 30ms to save power
if (context == 0) {
LowPower.powerDown(SLEEP_30MS, ADC_OFF, BOD_OFF);
loopLen -= 30;
prevUpd -= 30;
}
// if you haven't done anything in 30s, it goes into deep sleep
if (millis() - prevContx > 30000 && millis() - prevClick > 30000) {
sleepDisplay(&display);
backupToEEPROM();
// it wakes up every second to update your earnings for two hours
int i = 0;
while (millis() - prevContx > 30000 && millis() - prevClick > 30000 && i < 7200) {
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
cookies += clickWorth * clicksMult * (0.01 + 0.1 * level);
i++;
}
while (millis() - prevContx > 30000 && millis() - prevClick > 30000) {
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
}
wakeDisplay(&display);
}
}
// interrupt services for the clicks
void contextSwitch() {
if (millis() - prevContx > 250) {
context += 1;
context = context % 9;
prevContx = millis();
}
}
void clickISR() {
if (millis() - prevClick > 70) {
if (context == 0) {
clicks++;
clicksMult = 1 + clicks * 9.0 / 35.0;
displayCPS += clickWorth * clicksMult;
cookies += clickWorth * clicksMult;
}
else {
buy();
}
prevClick = millis();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////7
void setup() {
pinMode(CONTEXTPIN, INPUT_PULLUP);
pinMode(CLICKPIN, INPUT_PULLUP);
//holding down both buttons before it boots will restart the game
// IMPORTANT you should also do that on the first boot to wipe the EEPROM
// if you don't do that, you will get weird values for the ammout of cookies you have
if (digitalRead(CONTEXTPIN) == LOW && digitalRead(CLICKPIN) == LOW) {
backupToEEPROM();
}
attachInterrupt(digitalPinToInterrupt(CONTEXTPIN), contextSwitch, FALLING);
attachInterrupt(digitalPinToInterrupt(CLICKPIN), clickISR, FALLING);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Cookie ");
display.println("Clicker ");
display.display();
delay(1000);
retrieveFromEEPROM();
currentCPS = figureOutCPS();
updateClickWorth();
prevUpd = millis();
loopLen = millis();
}
void loop() {
if (context == 0) {
displayHome();
}
else {
displayContext();
}
timedStuff();
lowPowerStuff();
}