-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokecalc.c
118 lines (115 loc) · 2.41 KB
/
pokecalc.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MELOETTA 648
#include "stat.h"
#include "nature.h"
#include "structs.h"
#include "sqlite_helper.h"
#include "tidy.h"
int calcStat(struct pokemon* poke, int statID) {
int nid = poke->nid;
pokeinfo* pkinf = getInfByNID(nid);
if (pkinf == NULL) {
fprintf(stderr, "ERROR: Invalid species!\n");
return -1;
}
int formID = poke->formID;
int base = pkinf->forms[formID].stats[statID];
int iv = poke->ivs[statID];
int ev = poke->evs[statID];
int level = poke->level;
float nature = natureFactor(poke->nature, statID);
int stat = 0;
if (statID == Hp) {
stat = ((2*base+iv+(ev/4))*level)/100 + level + 10;
} else {
stat = ((2*base+iv+(ev/4))*level)/100 + 5;
float fstat = stat*nature;
stat = (int) fstat;
}
return stat;
}
void bruteForceIV(struct pokemon* poke, int statID, int realStat, int* minIV, int* maxIV) {
int loc = -1;
*minIV = -1;
*maxIV = -1;
for (int i = 0; i < 32; i++) {
poke->ivs[statID] = i;
int stat = calcStat(poke, statID);
if (stat == realStat) {
if (loc == -1) {
loc = 0;
*minIV = i;
*maxIV = i;
} else if (loc == 0) {
*maxIV = i;
}
} else if (loc == 0) {
break;
}
}
}
int promptNature() {
int read;
do {
printf("Nature: ");
char *line = NULL;
size_t sz;
read = getline(&line, &sz, stdin);
int id = getNatureID(line);
if (id != -1) {
return NAT_MAP[id];
}
//free line?
} while (read != -1);
//TODO error
return 0;
}
int promptPokemon() {
int read;
do {
printf("Pokemon: ");
char *line = NULL;
size_t sz;
read = getline(&line, &sz, stdin);
pokeinfo *pkinf = getInfByName(line);
if (pkinf != NULL) {
int ret = pkinf->nid;
free(pkinf);
return ret;
}
//free line?
} while (read != -1);
//TODO error
return 0;
}
int main() {
init_db();
pokemon poke;
memset(&poke, 0, sizeof(struct pokemon));
poke.nid = promptPokemon();
poke.nature = promptNature();
poke.level = 0;
int read = 0;
printf("Level: ");
scanf("%d", &read);
poke.level = read;
int stats[6];
for (int statID = 0; statID < 6; statID++) {
printf("%s: ", STAT_STRINGS[statID]);
scanf("%d", stats+statID);
}
printf("\n");
for (int statID = 0; statID < 6; statID++) {
int minIV;
int maxIV;
bruteForceIV(&poke, statID, stats[statID], &minIV, &maxIV);
printf("%s: %d", STAT_STRINGS[statID], minIV);
if (minIV != maxIV) {
printf("-%d", maxIV);
}
printf("\n");
}
close_db(0);
}