aboutsummaryrefslogtreecommitdiff
path: root/petratc.c
blob: 872ff2b3fe947a16f657abffa6fbef43f8319566 (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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include "rat.h"
#include "defs.h"

#ifdef __APPLE__
  #define ratpath  "%s/Library/Application Support/rat"
  #define ratsave "%s/Library/Application Support/rat/rat.txt"
#else
  #define ratpath  "%s/.local/share/rat"
  #define ratsave "%s/.local/share/rat/rat.txt"
#endif

  char ratart[] = 
        "     .---.\n  (\\./)     \\.......-\n  >' '<  (__.'\"\"\"\"\"\"\n  \" ` \" \"_";

  char *messages[] = {
    "squeak!",
    "squeak! !",
    "squeak",
    "squeak...",
    "SQUEAK",
    "SQUEAK!!",
    "SQUEAAAKKKKK",
    };

void addstat(float *guy){

    if(*guy < 98.5){
        *guy += 1.5;
    } else{*guy += 0.05;}     
}

Rat rat;

void save(void) {
    char path[256];
    snprintf(path, sizeof(path), ratpath, getenv("HOME"));
    mkdir(path, 0755);
    snprintf(path, sizeof(path), ratsave, getenv("HOME"));

    FILE *f = fopen(path, "w");
    if (!f) return;
    fprintf(f, "%f\n%f\n%f\n%f\n%f\n%d\n",
        rat.hunger, rat.love, rat.fun, rat.clean, rat.health, rat.age);
    fclose(f);
}

void load(void) {
    char path[256];
    snprintf(path, sizeof(path), ratsave, getenv("HOME"));

    FILE *f = fopen(path, "r");
    if (!f) {
        rat.hunger = 80.0f;
        rat.love   = 80.0f;
        rat.fun    = 80.0f;
        rat.clean  = 80.0f;
        rat.health = 80.0f;
        rat.age    = time(NULL);
        return;
    }
    fscanf(f, "%f\n%f\n%f\n%f\n%f\n%d\n",
        &rat.hunger, &rat.love, &rat.fun, &rat.clean, &rat.health, &rat.age);
    fclose(f);
}

float decay(float current, int timepassed, float rate) {
    float result = current - (timepassed * rate);
    return result < 0.0f ? 0.0f : result;
}

void setstat(){
    int timenow = time(NULL);
    int newage = timenow - rat.age;  

    rat.hunger = decay(rat.hunger, newage, 0.005f);
    rat.love   = decay(rat.love, newage, 0.002f);
    rat.fun    = decay(rat.fun, newage, 0.006f);
    rat.clean  = decay(rat.clean, newage, 0.0002f);
    rat.health = decay(rat.health, newage, 0.00002f);
    

    rat.age = timenow; //thinking about this makes my cortisol spike so i'll just ignore it even more
}   
    

void printrat(){

    printf("%s\n", ratart);
    int howmany = sizeof(messages) / sizeof(messages[0]);
    printf("%s\n", messages[rand() % howmany]);
    printf("rat hunger: %.2f, rat love: %.2f, rat fun: %.2f, rat clean: %.2f, rat health: %.2f\n",
            rat.hunger, rat.love, rat.fun, rat.clean, rat.health);
 
}


int main(){
srand(time(NULL));
int choice;
int truing = 1;

    load();
    setstat();
    printrat();

    while(truing){
    printf("1: play a game! (fun up), 2: go down to the maze... (hunger up), 4: take a shower! (clean up), 9: print me!, 0: exit...\n");
    
    if(scanf("%d", &choice) != 1){
        while(getchar() != '\n');  //flush the bad input if you're a bad boy and put q instead of 1 
        continue;
    }    
    switch(choice){

        case 1: gameguess(); break;
        case 2: gamemaze(); break; 
        case 4: gamecatch(); break; 
        case 9: printrat(); break;
        case 0: truing = 0; break; 
        }
    }


    save();
    printf("bye bye~~\n");
    return(0);
}