#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int hp = 100;
int monsterhp = 100;
int turn = 0;
char name[50];
char flag[50];

void getname() {
	char n[50];
	printf("What is your name, adventurer? ");
	gets(n);
	n[50] = 0;
	strncpy(name, n, 50);
	printf("Well met, %s of Bof!\n", n);
	printf("You are the last hope for taking down the feared Roger of Palormin.\n");
	printf("He has learned the terrific 'flag spell', and is destroying the land with it.\n");
	printf("Are you ready to take him down? (y/n) ");
	gets(n);
	if (n[0]=='n') exit(0);
}

void monsterAction() {
	if (turn%10 == 9) {
		printf("ROP casts the mighty flag spell at you!");
		FILE* f = fopen("flag.txt", "r");
		char flg[50];
		fscanf(f, "%s", flg);
		fclose(f);
		int cutoff = rand() % 4 + 1;
		flg[cutoff] = 0;
		printf("'%s...', he booms. You die before you can hear the rest of the spell.\n", flg);
		hp = 0;
		return;
	}
	printf("ROP swings his greatsword at you, hitting you squarely in the torso.\n");
	printf("You take 10 damage.\n");
	hp -= 10;
}

void hit(int roll) {
	if (roll == 1) {
		printf("You stumble and trip over a rock.\n");
		printf("You take 1 damage.\n");
		hp--;
	} else if (roll == 20) {
		printf("Sure of your strike, you hit a weak point in ROP's armor.\n");
		printf("You deal 2 damage.\n");
		monsterhp -= 2;
	} else if (roll > 15) {
		printf("You swing recklessly, putting all of your weight behind the blade.\n");
		printf("You deal 1 damage.\n");
		monsterhp--;
	} else {
		printf("Your wild strike just bounces off ROP's armor.\n");
		printf("You deal no damage.\n");
	}
}

void attack() {
	int d20 = rand()%20 + 1;
	if (turn&1) {
		printf("You can feel yourself powering up...\n");
		d20 = 20;
	}
	printf("You rolled a %d!\n", d20);
	if (d20 == 20) {
		printf("Critical HIT!\n");
	}
	if (d20 == 1) {
		printf("Critical MISS!\n");
	}
	hit(d20);
}

void drink() {
	hp += 20;
	printf("You can feel your wounds closing up.\n");
	printf("You gain 20 hp.\n");
}

void flee() {
	printf("You try to run, but ROP is faster and catches up to you.\n");
}

void playerAction() {
	int valid = 0;
	while (!valid) {
		valid = 1;
		printf("Choose your action: \n");
		printf("1: Attack\n");
		printf("2: Drink Potion\n");
		printf("3: Flee\n\n");
		printf("Enter your choice: ");
		char choice[3];
		gets(choice);
		if (*choice == '1') {
			attack();
		} else if (*choice == '2') {
			drink();
		} else if (*choice == '3') {
			flee();
		} else {
			printf("Invalid Action.\n");
			valid = 0;
		}
	}
}

void check() {
	if (hp <= 0) {
		printf("You have died a valiant death.\n\n");
		printf("Unfortunately, your sacrifice was in vain.\n");
		printf("With no one to keep him in check, the tyranny of Roger of Palormin spread.\n");
		printf("ROP systematically destroys everything you care for, starting with your hometown of Bof.\n");
		printf("\nThe world spirals into despair.\n");
		exit(1);
	}
	if (monsterhp <= 0) {
		printf("You have slain the feared ROP!\n");
		printf("With his dying breath, ROP attempts to cast one final spell:\n");
		printf("'%s!' He roars, but the flag spell seems to have no effect any longer.\n",flag);
		printf("You return home a celebrated hero.\n");
		exit(0);
	}
}

void printStatus() {
	printf("================================================================================\n");
	printf("%s of Bof: %d/100 HP\n", name, hp);
	printf("Roger of Palormin: %d/100 HP\n", monsterhp);
	printf("================================================================================\n");
}

int main() {
	setbuf(stdout, NULL);
	setbuf(stdin, NULL);
	time_t t;
	srand((unsigned) time(&t));
	FILE* f = fopen("flag.txt", "r");
	fscanf(f, "%s", flag);
	fclose(f);
	getname();
	while (1) {
		printStatus();
		playerAction();
		check();
		monsterAction();
		check();
		turn++;
	}
}