#include <stdlib.h> /*-Azbia-*/
#include <stdio.h>
#include <time.h>
#include <math.h>
void map1 (); /*This is an rpg as I think it*/
void map2 (); /*should be, not a maximum yet*/
void map3 (); /*the texts are rather random*/
void map4 (); /*but it's stable, not a fake*/
struct enemystruct {char name [15]; int hp; int ap;
int wh; int gold; int run;};
enemystruct e [6] = {
{"snake", 50, 5, 10, 40, 1},
{"orc", 100, 0, 40, 142, 0},
{"gnome", 149, 24, 40, 300, 1},
{"troll", 333, 33, 33, 333, 1},
{"centaur", 500, 4, 75, 1000000, 0},
{"zombie", 0, 0, 0, 0, 1}};
struct weaponstruct {char name [15]; int damage;
int price;};
weaponstruct w [3] = {
{"long sword", 200, 400},
{"short sword", 100, 200},
{"dagger", 75, 100}};
typedef void (*maptype) ();
struct placestruct {char name [30]; maptype map;};
placestruct p [4] = {
{"mountain", &map1},
{"forest", &map2},
{"desert", &map3},
{"town", &map4}};
int gold = 0, hp = 100, mhp = 100, armor = 10,
weapon = 25, run, damage, valid, next, ehp, n, i;
char wname [20] = "mace";

void choice (int *n, int max)
{
if (!max) printf ("Where do you want to go?\n");
for (i = 0, *n = 0; i < 4 && !max; i++)
printf ("[%i] %s\n", i + 1, p [i].name);
if (!max) max = 4;
scanf ("%i", n);
fflush (stdin);
if (*n > 0 && *n <= max) valid = 1;
}

void printstat ()
{
printf ("Statistics:\narmor: %i\n", armor);
printf ("weapon name: %s\n", wname);
printf ("weapon damage: %i\n", weapon);
printf ("hp: %i/%i\n", hp, mhp);
printf ("gold: %i\n", gold);
}

void pressenter ()
{
printf ("\n%*s[ Press enter to continue ]", 24, " ");
getchar ();
fflush (stdin);
}

void battle (int m)
{
run = 0;
ehp = e [m].hp;
while (ehp > 0 && hp > 0 && (run < 13 || !e [m].run))
{
for (valid = 0; !valid; choice (&n, 3))
{
system ("cls");
printf ("You are in combat with: %s\n", e [m].name);
printf ("enemy hp:%s%i%syour hp:%s%i/%i\n",
"\t", ehp, "\t", "\t", hp, mhp);
printf ("enemy armor:%s%i%syour armor:"
"%s%i\n", "\t", e [m].ap, "\t", "\t", armor);
printf ("enemy weapon%s%i%syour weapon:%s%i\n\n",
"\t", e [m].wh, "\t", "\t", weapon);
printf ("[1] attack\n[2] run\n[3] stats\n");
}
if (n == 1 && hp > 0)
{
damage = weapon - e [m].ap / 4;
printf ("You hit for %i damage\n\n", damage);
if ((ehp -= damage) < 1)
{
printf ("You have killed the enemy\n");
printf ("You gain: %i gold\n", e [m].gold);
gold += e [m].gold;
hp += 5;
mhp += 5;
}
}
if (n == 1 && ehp > 0)
{
damage = e [m].wh - armor / 4;
printf ("Enemy hits for %i damage\n", damage);
if ((hp -= damage) < 1) printf ("You LOST!\n");
}
if (n == 2 && (run = rand () % 26) > 12 && e [m].run)
printf ("\nYou have sucessfully run away\n");
if (n == 2 && (run < 13 || !e [m].run))
printf ("\nYou failed to run away\n");
if (n == 3) printstat ();
pressenter ();
}
}

void heal (int choice)
{
if (gold < 51) printf ("You don't have enough gold\n");
if (gold > 50) printf ("HP is full: %i\n", hp = mhp);
if (gold > 50) gold -= 50;
if (n == choice) pressenter ();
}

void weaponsstore ()
{
for (valid = 0; !valid; choice (&n, 3))
{
system ("cls");
for (i = 0, printf ("Weapons store\n"); i < 3; i++)
printf ("[%i] %s%s-- Max Damage: %i%s-- "
"Price: %i\n", i + 1, w [i].name, "\t",
w [i].damage, "\t", w [i].price);
printf ("\nWhich one would you like?\n");
}
if (gold < w [--n].price)
printf ("You don't have enough gold\n");
if (gold >= w [n].price)
printf ("You buy the best one the %s\n", w [n].name);
if (gold >= w [n].price) weapon = w [n].damage;
if (gold >= w [n].price) strcpy (wname, w [n].name);
printstat ();
pressenter ();
}

void map1 ()
{
system ("cls");
printf ("A lot of evil ones live on this mountain,\n");
printf ("local people once worshiped them as gods.\n");
pressenter ();
weaponsstore ();
battle (2);
battle (3);
if (hp < 1) return;
system ("cls");
printf ("You met a healer and he prepared you "
"to face your final enemy.\n");
pressenter ();
hp = mhp;
battle (4);
if (hp < 1) return;
system ("cls");
printf ("Evil has been defeated in Azbia, "
"but your work is not finished yet...\n");
pressenter ();
hp = 0;
}

void map2 ()
{
for (valid = 0; !valid; choice (&n, 3))
{
system ("cls");
printf ("It's silent in this forest, there don't "
"seem to be any enemies\n");
printf ("Where do you want to go?\n[1] healer "
"(50 gold)\n[2] weapons\n[3] town\n");
}
if (n == 1) heal (1);
if (n == 2) weaponsstore ();
if (n == 3) next = 4;
if (n == 3) return;
for (valid = 0; !valid; choice (&n, 0)) system ("cls");
next = n;
}

void map3 ()
{
system ("cls");
printf ("There are a lot of little creatures under "
"the sand in this desert,\n");
printf ("the biggest one of these bites you.\n");
pressenter ();
battle (0);
battle (1);
if (hp < 1) return;
for (valid = 0; !valid; choice (&n, 0)) system ("cls");
next = n;
}

void map4 ()
{
for (n = 1; hp > 0 && (n == 1 || n == 3)
{
e [5].hp = rand () % 26 + 30;
e [5].ap = rand () % 26;
e [5].gold = rand () % 26 + 30;
e [5].wh = rand () % 26 + 12;
for (battle (5), valid = 0; !valid; choice (&n, 3))
{
system ("cls");
printf ("Do you want to battle some more?\n");
printf ("[1] yes\n[2] no\n[3] heal\n");
}
if (n == 3) heal (3);
}
if (hp < 1) return;
for (valid = 0; !valid; choice (&n, 0)) system ("cls");
next = n;
}

int main ()
{
srand (time (NULL));
printf ("When you arrive at Azbia, you see a desolate"
" place\n");
printf ("so that even all these supernatural "
"creatures don't seem to be so unnatural...\n\n");
printf ("Welcome to Azbia!\n");
pressenter ();
for (valid = 0; !valid; choice (&n, 0)) system ("cls");
next = n;
while (hp > 0) if (hp > 0) (*(p [next - 1].map)) ();
}