aboutsummaryrefslogtreecommitdiff
path: root/games/gameclicker.c
blob: 4dff0b6d50f6340480612673a44127226b117367 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <ncurses.h>
#include "../rat.h"

/*
i lowkey stole the art of the little rat and the cheese from the internet lol
https://asciiart.website/art/752
*/

int running;

static void stop() {
    clear();
    refresh();
    endwin();
    running = 0; 
}

static char *cheeseart = {
" ()()         ____\n (..)        /|o  |\n /\\/\\       /o|  o|\nc\\db/o...  /o_|_o_|"
};
//i hate this 

/*
    //i need to make a system based on intergers to represent non-int numbers like 0.5 or  five trillion as 5T instead of 500000000000 or whatever  
    //how do i tell a computer that 1000 = 1k and 10000 = 10k and 1000000 = 1M and that etc
    //i'll use powers of 10 
    //1^10x0 = 1 1^1(10x1) = 10 1^1(10x2) = 100 1^1(10x3) = 1k 
    //  x*10^y <-- this is what i store 
    //1 becomes X. x can range from 0 to 999, when it reaches 999 it adds 1 to y and x goes back to 0 
    //is this memory safe????? yeah sure i don't see why it wouldnt
    //x is a float or a double i think a double yeah a double
    //
    //and then print that as it should so 2.3^10x3 = 2.3k 
    //and also other functions to add or substract from this using regular numbers
    
    //wait i could just do powers of 1000 
    //x is still 0.0 - 999.999 but it's x*1000^y 
    //55.3* 1000^2 = 55300000
    //
    //once x reaches 999.999 it wraps back to 0 and adds 1 to y 
    //then check y so that y = 0 is units, y = 1 is k, y = 2 is M, y = 3 is B, y = 4 is T, etc
*/

/*
//a bignum is just a scientific notation number
// x ^ 1000^y 
// 55300 = 55.3*1000^1
// double x = 55.3 
// int y = 1 
*/
typedef struct bignum{
    double x;
    int y;
}bignum; 

static char *digit[] = {
    "", "K", "M", "B", "T",
    "Qa", "Qi", "Sx", "Sp", 
    "Oc", "No", "Dc"  
    // up to y=11 (10^36) there's no way someone gets farther than this
};

static bignum normalize(double regnum){
    bignum b;
    double val = regnum;
    int y = 0;

    while (fabs(val) >= 1000) {
        val /= 1000.0;
        y++;
    }
    while (val != 0.0 && fabs(val) < 1) {
        val *= 1000.0;
        y--;
    }

    b.x = val;
    b.y = y;
    return b;
}

static bignum normalizebignum(bignum b){
    double val = b.x;
    int y = b.y;
 
    while (fabs(val) >= 1000) {
        val /= 1000.0;
        y++;
    }
    while (val != 0.0 && fabs(val) < 1) {
        val *= 1000.0;
        y--;
    }
 
    b.x = val;
    b.y = y;
    return b;
}
 
static bignum addbignum(bignum a, bignum b){
    bignum result;
    if(a.y == b.y){
        result.x = a.x + b.x;
        result.y = a.y;
    } else if(a.y > b.y){
        double bx = b.x;
        for(int i = a.y - b.y; i > 0; i--) bx /= 1000.0;
        result.x = a.x + bx;
        result.y = a.y;
    } else {
        double ax = a.x;
        for(int i = b.y - a.y; i > 0; i--) ax /= 1000.0;
        result.x = ax + b.x;
        result.y = b.y;
    }
    return normalizebignum(result);
}
 
static bignum subbignum(bignum a, bignum b){
    bignum result;
    if(a.y == b.y){
        result.x = a.x - b.x;
        result.y = a.y;
    } else if(a.y > b.y){
        double bx = b.x;
        for(int i = a.y - b.y; i > 0; i--) bx /= 1000.0;
        result.x = a.x - bx;
        result.y = a.y;
    } else {
        double ax = a.x;
        for(int i = b.y - a.y; i > 0; i--) ax /= 1000.0;
        result.x = ax - b.x;
        result.y = b.y;
    }
    return normalizebignum(result);
}
 
static bignum mulbignum(bignum a, bignum b){
    bignum result;
    result.x = a.x * b.x;
    result.y = a.y + b.y;
    return normalizebignum(result);
}
 
static bignum divbignum(bignum a, bignum b){
    bignum result;
    result.x = a.x / b.x;
    result.y = a.y - b.y;
    return normalizebignum(result);
}
 
static bignum addmixregbig(bignum a, double regnum){
    return addbignum(a, normalize(regnum));
}
 
static bignum submixregbig(bignum a, double regnum){
    return subbignum(a, normalize(regnum));
}
 
static bignum mulmixregbig(bignum a, double regnum){
    return mulbignum(a, normalize(regnum));
}
 
static bignum divmixregbig(bignum a, double regnum){
    return divbignum(a, normalize(regnum));
}

static char *bignumprint(bignum b) {
    //32 bytes is more than enough for 124.24 trillion idk
    char *nice = malloc(32 * sizeof(char)); 
    if(nice == NULL) return NULL;

    if(b.y >= 0 && b.y < 12) {
        snprintf(nice, 32, "%.2lf%s", b.x, digit[b.y]);
    } else{
        snprintf(nice, 32, "%.2lfe3*%d", b.x, b.y);
    }
    return nice; 
}


//wide = art and score side by side
//narrow = art on topand score below
typedef enum { layoutnarrow, layoutwide } layoutmode;

//bounding box of the drawn cheese art in winlocal coordinates for click detection
static int artrow0, artcol0, artrows, artcols;

static layoutmode getlayout(WINDOW *win){
    return (getmaxx(win) >= getmaxy(win) * 2) ? layoutwide : layoutnarrow;
}

static void drawwin(WINDOW *win, bignum cheeses){

    werase(win);
    box(win, 0, 0);

    int winw = getmaxx(win);
    layoutmode mode = getlayout(win);

    char artcopy[2048];
    strncpy(artcopy, cheeseart, sizeof(artcopy));
    artcopy[sizeof(artcopy)-1] = '\0';

    int artcol = 2;
    int artrow = 1;
    int scorecol, scorewrapw;

    if(mode == layoutwide){
        scorecol = winw / 2 + 2;
        scorewrapw = winw - scorecol - 2;
    } else {
        scorecol = 2;
        scorewrapw = winw - 4;
    }

    int row = artrow;
    int widest = 0;
    char *line = strtok(artcopy, "\n");
    while(line){
        mvwprintw(win, row, artcol, "%s", line);
        int len = strlen(line);
        if(len > widest) widest = len;
        row++;
        line = strtok(NULL, "\n");
    }

    //save where the art landed so the click handler knows what to hit-test against
    artrow0 = artrow;
    artcol0 = artcol;
    artrows = row - artrow;
    artcols = widest;

    char *scorestr = bignumprint(cheeses);
    char scoremsg[64];
    snprintf(scoremsg, sizeof(scoremsg), "cheese: %s", scorestr);
    free(scorestr);

    if(mode == layoutwide){
        mvwprintw(win, 1, scorecol, "%s", scoremsg);
        wrapprint(win, 3, scorecol, scorewrapw, "click the cheese!");
    } else {
        int scorerow = artrow0 + artrows + 1;
        wrapprint(win, scorerow, scorecol, scorewrapw, scoremsg);
        wrapprint(win, scorerow+1, scorecol, scorewrapw, "click the cheese!");
    }

    wrefresh(win);
}

void gameclicker(){

    srand(time(NULL));

    WINDOW *win = ratdrawbox();
    mousemask(BUTTON1_PRESSED, NULL);
    mouseinterval(0); //don't wait to resolve single vs double click

    running = 1;

    bignum cheeses = {0.0, 0};
    bignum onecheese = {1.0, 0}; //what a single click is worth

    drawwin(win, cheeses);

    while(running){
        if(!running){stop(); break;}

        int ch;
        while((ch = getch()) != ERR){ //drain the whole queue each frame so rapid clicks/keys aren't ignroed 
            switch (ch) {
                case 'q': case 'Q': stop(); break;
                case 'a': cheeses = addmixregbig(cheeses, 50.0); break;
                case 'b': cheeses = mulmixregbig(cheeses, 235.2); break;
                case KEY_MOUSE: {
                    MEVENT event;
                    if(getmouse(&event) == OK && (event.bstate & BUTTON1_PRESSED)){
                        int y = event.y, x = event.x;
                        //translate the screen click into winlocal coordinates
                        if(wmouse_trafo(win, &y, &x, FALSE)){
                            if (y >= artrow0 && y < artrow0 + artrows &&
                               x >= artcol0 && x < artcol0 + artcols){
                                cheeses = addbignum(cheeses, onecheese);
                            }
                        }
                    }
                    break;
                }
            }
            if(!running) break;
        }

        if(running) drawwin(win, cheeses);

        usleep(16667); //60 fps according to google ai overview lmao
    }

stop();
}