135 lines
3.7 KiB
C
135 lines
3.7 KiB
C
#include "ktkStructure.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <time.h> // for randomizeSeed
|
|
|
|
unsigned long ktk_RANDOM_SEED = 1;
|
|
|
|
unsigned long ktk_randomizeSeed() {
|
|
ktk_RANDOM_SEED = (unsigned long)time(0);
|
|
srandom(ktk_RANDOM_SEED);
|
|
return ktk_RANDOM_SEED;
|
|
}
|
|
|
|
int ktk_getRandom(int min, int max) {
|
|
int num;
|
|
if (min == 0 && max == 0) return 0;
|
|
if (min == max) return min;
|
|
// include max as a possible number (+1)
|
|
int range = (max+1) - min;
|
|
if (range >= RAND_MAX) {
|
|
printf("ktk_getRandom(%d, %d): ERROR, range beyond RAND_MAX!\n", min, max);
|
|
return 0;
|
|
} else if (range == 0) {
|
|
return min;
|
|
}
|
|
int buckets = RAND_MAX / range;
|
|
int limit = buckets * range;
|
|
for (num = random(); num >= limit; num = random());
|
|
return min + (num / buckets);
|
|
}
|
|
|
|
int ktk_rollNumber(struct ktkNumber number) {
|
|
int numb = ktk_getRandom(number.a, number.b);
|
|
return (ktk_getRandom(number.a, number.b));
|
|
if (number.type == ktk_PERCENT) {
|
|
|
|
} else if (number.type == ktk_DIGIT) {
|
|
|
|
}
|
|
}
|
|
int ktk_rollNumberSet(struct ktkNumberSet set) {
|
|
if (set.count <= 0) return 0;
|
|
return ktk_rollNumber(set.sets[ktk_getRandom(0, set.count-1)]);
|
|
}
|
|
int ktk_deleteNumberSet(struct ktkNumberSet *set) {
|
|
if (set->sets != NULL) {
|
|
free(set->sets);
|
|
set->sets = NULL;
|
|
set->count = NULL;
|
|
}
|
|
return 0;
|
|
}
|
|
int ktk_copyNumberSet(struct ktkNumberSet *set_1, struct ktkNumberSet *set_2) {
|
|
ktk_deleteNumberSet(set_2);
|
|
if (set_1->count > 0) {
|
|
set_2->sets = realloc(set_2->sets, set_1->count*sizeof(struct ktkNumber));
|
|
memcpy(set_2->sets, set_1->sets, set_1->count*sizeof(struct ktkNumber));
|
|
set_2->count = set_1->count;
|
|
} else {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
/*ktkField_t ktkStructureTable[] = {
|
|
{FIELD(flags)},
|
|
{FIELD(x)}, {FIELD(y)},
|
|
{FIELD(size_x)}, {FIELD(size_y)},
|
|
{FIELD(id_1)}, {FIELD(id_2)},
|
|
{FIELD(data)},
|
|
{FIELD(relations)}, {FIELD(relation_count)}
|
|
};
|
|
*/
|
|
//int ktk_setField(ktkField_t *table, size_t table_size,
|
|
|
|
const struct ktkStructure ktk_STRUCTURE_DEFAULT = {
|
|
0,
|
|
NULL,
|
|
{ 0, 0, 0}, { 0, 0, 0}, // x, y
|
|
{ 0, 0, 0}, { 0, 0, 0}, // size_x, size_y
|
|
{ NULL, 0}, // id_1
|
|
{ NULL, 0}, // id_2
|
|
{ NULL, 0}, // id_1 replace rules
|
|
{ NULL, 0}, // id_2 replace rules
|
|
NULL,
|
|
NULL, 0, // relations
|
|
NULL, 0 // sub structures
|
|
};
|
|
void ktk_dumpStructure(struct ktkStructure *structure) {
|
|
printf("flags: %d\n", structure->flags);
|
|
printf("name: %s\n", structure->name);
|
|
printf("x: %dx%d\n", structure->x.a, structure->x.b);
|
|
printf("y: %dx%d\n", structure->y.a, structure->y.b);
|
|
printf("size_x: %dx%d\n", structure->size_x.a, structure->size_x.b);
|
|
printf("size_y: %dx%d\n", structure->size_y.a, structure->size_y.b);
|
|
}
|
|
|
|
const struct ktkRelation ktk_RELATION_DEFAULT = {
|
|
0,
|
|
NULL,
|
|
{ 0, 0, 0}, { 0, 0, 0},
|
|
{ 0, 0, 0}, { 0, 0, 0},
|
|
{ 1, 1, ktk_DIGIT},
|
|
NULL,
|
|
NULL,
|
|
0,
|
|
NULL,
|
|
NULL,
|
|
0,
|
|
NULL,
|
|
NULL,
|
|
0,
|
|
};
|
|
|
|
int ktk_explodeString(const char *string, char delim, int depth, char ***results) {
|
|
int words = 0, word_size = 0, word_i = 0;
|
|
int i = 0;
|
|
int len = strlen(string)+1;
|
|
// TODO: resize in 8-byte chunks or so
|
|
*results = malloc(1*sizeof(char*));
|
|
(*results)[0] = malloc(1*sizeof(char));
|
|
for (i = 0; i < len; i++) {
|
|
if ((string[i] == delim && words < depth) || string[i] == '\0') {
|
|
(*results)[words][word_i] = '\0';
|
|
*results = realloc(*results, ++words*sizeof(char*));
|
|
(*results)[words] = malloc(1*sizeof(char));
|
|
word_i = word_size = 0;
|
|
} else {
|
|
(*results)[words] = realloc((*results)[words], ++word_size*sizeof(char));
|
|
(*results)[words][word_i++] = string[i];
|
|
}
|
|
}
|
|
return words;
|
|
}
|