timesynk/test/rolls.c

165 lines
5.1 KiB
C

#include "../common/data.h"
#include "../common/c_extra.h"
#include <stdio.h>
#define ROLL_FILE "rolls.tsc"
struct TablePair *attacker;
struct TablePair *defender;
struct Table *tile_table;
int roll(int dice, int sides) {
int divisor = RAND_MAX / (sides+1);
int total = 0;
int d = 0;
while(d < dice) {
int s_total = 0;
do {
s_total = rand() / divisor;
} while(s_total > sides);
total += s_total;
d++;
}
return total;
}
void getFighters() {
attacker = NULL;
defender = NULL;
printf("The Gladiators\n");
printf("''''''''''''''\n");
int i = 0;
while (i < tile_table->size) {
struct TablePair *entry = tile_table->pair[i];
while (entry != NULL) {
if (entry->type == T_TABLE) {
printf("%s ", entry->key);
}
entry = entry->next;
}
i++;
}
printf("\n");
char input_buffer[128];
int input_i = 0;
unsigned int c;
while(attacker == NULL) {
printf("Select First Gladiator: ");
input_i = 0;
while((c = getchar()) != '\n') {
input_buffer[input_i++] = c;
}
input_buffer[input_i] = '\0';
attacker = getTablePair(tile_table, input_buffer);
if (attacker == NULL) {
printf("ERROR, no such tile exists!\n");
}
}
while(defender == NULL) {
printf("Select Second Gladiator: ");
input_i = 0;
while((c = getchar()) != '\n') {
input_buffer[input_i++] = c;
}
input_buffer[input_i] = '\0';
defender = getTablePair(tile_table, input_buffer);
if (defender == NULL) {
printf("ERROR, no such tile exists!\n");
}
}
}
void fight(struct TablePair *attacker, struct TablePair *defender) {
printf("-- %s vs %s --\n", attacker->key, defender->key);
printf("%s's starting HP: %d\n", defender->key, getTablePairValueInt(defender->value, "hp"));
printf("\nHit return to start first round!");
getchar();
int round = 1;
while(round) {
printf("Round %d\n", round);
printf("````````\n");
printf("%s swings at %s with %s!\n", attacker->key, defender->key, getTablePairValueString(getTablePairValueTable((struct Table*)attacker->value, "weapon"), "name"));
struct Table *a_stats = getTablePairValueTable(attacker->value, "stats");
struct Table *a_skills = getTablePairValueTable(attacker->value, "skills");
struct Table *d_stats = getTablePairValueTable(defender->value, "stats");
int a_to_hit = getTablePairValueInt(a_stats, "finesse") + roll(1, getTablePairValueInt(a_stats, "luck"));
int d_to_def = getTablePairValueInt(d_stats, "finesse") + roll(1, getTablePairValueInt(d_stats, "luck"));
int to_hit = a_to_hit - d_to_def;
printf("\tTo hit for %d (%d - %d)!\n", to_hit, a_to_hit, d_to_def);
if (to_hit > 0) {
printf("Hit!\n");
struct Table *a_weapon = getTablePairValueTable(attacker->value, "weapon");
struct Table *a_weapon_damage = getTablePairValueTable(a_weapon, "damage");
struct Table *d_armour = getTablePairValueTable(attacker->value, "armor");
struct Table *d_armour_resist = getTablePairValueTable(d_armour, "resist");
// physical damage
int a_phys = getTablePairValueInt(a_stats, "kinetic")
+ getTablePairValueInt(a_weapon_damage, "physical")
+ getTablePairValueInt(a_skills, getTablePairValueString(a_weapon, "skill"))
+ to_hit
;
// physical resist
int d_phys = getTablePairValueInt(d_armour_resist, "physical");
int damage = a_phys - d_phys;
if (damage > 0) {
printf("\tphysical %d (%d - %d)!\n", damage, a_phys, d_phys);
addTablePairInt(defender->value, "hp", getTablePairValueInt(defender->value, "hp") - damage);
}
// bonus/elemental damage!
int i = 0;
while(i < a_weapon_damage->size) {
struct TablePair *entry = a_weapon_damage->pair[i];
while (entry != NULL) {
if (strcmp(entry->key, "physical") != 0) {
damage = *(int*)entry->value - getTablePairValueInt(d_armour_resist, entry->key);
printf("\t%s %d (%d - %d)!\n", entry->key, damage, *(int*)entry->value, getTablePairValueInt(d_armour_resist, entry->key));
addTablePairInt(defender->value, "hp", getTablePairValueInt(defender->value, "hp") - damage);
}
entry = entry->next;
}
i++;
}
} else {
printf("Miss!\n");
}
if (getTablePairValueInt(defender->value, "hp") <= 0) {
printf("%s is dead!\n", defender->key);
round = 0;
} else {
printf("%s has %d remaining\n", defender->key, getTablePairValueInt(defender->value, "hp"));
round++;
printf("Hit return for round %d!\n", round);
getchar();
}
}
}
int main(int argc, char **argv) {
int buffer_size = 0;
char *memory;
if ((buffer_size = fileToMemory(&memory, ROLL_FILE)) >= 0) {
tile_table = newTable(32);
int *offset;
offset = 0;
loadConfig_r(tile_table, memory, buffer_size, &offset);
printf(",,,,,,,,,,,,,\n");
printf("Xibalba Arena\n");
printf("`````````````\n");
getFighters();
fight(attacker, defender);
} else {
printf("ERR: couldn't open %s for reading\n", ROLL_FILE);
}
}