effg
This commit is contained in:
+12
-12
File diff suppressed because one or more lines are too long
@@ -0,0 +1,84 @@
|
|||||||
|
#include "demon.h"
|
||||||
|
#include "villager.h"
|
||||||
|
#include "weapons.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
struct demon *init_demon(enum demon_category category, char name[50], int HP_max, int damage, enum distance range){
|
||||||
|
|
||||||
|
struct demon *demon = malloc(sizeof(struct demon));
|
||||||
|
if (demon == NULL) return NULL;
|
||||||
|
|
||||||
|
strcpy(demon->name, name);
|
||||||
|
demon->category = category;
|
||||||
|
demon->damage = damage * demon->category;
|
||||||
|
demon->HP_max = HP_max * demon->category;
|
||||||
|
demon->cur_HP = demon->HP_max;
|
||||||
|
demon->range = range;
|
||||||
|
|
||||||
|
return demon;
|
||||||
|
}
|
||||||
|
void update_demon_hp(struct demon *demon, int amount){
|
||||||
|
if (demon == NULL) return;
|
||||||
|
demon->cur_HP -= amount;
|
||||||
|
if (demon->cur_HP <= 0) printf("You killed the demon %s !!\n", demon->name);
|
||||||
|
|
||||||
|
}
|
||||||
|
void destroy_demon(struct demon *demon){
|
||||||
|
|
||||||
|
if (demon == NULL) return;
|
||||||
|
free(demon);
|
||||||
|
}
|
||||||
|
|
||||||
|
void chase(struct demon *demon, struct villager *villager){
|
||||||
|
|
||||||
|
if(demon == NULL || villager == NULL) return;
|
||||||
|
|
||||||
|
if (villager->distance != CLOSE) villager->distance --;
|
||||||
|
printf("The demon %s is chasing you.\n",demon->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_attack(struct demon *demon, struct villager *target){
|
||||||
|
|
||||||
|
if (demon == NULL || target == NULL) return;
|
||||||
|
printf("RAAAAAGH! %s attacks you to deal %i damage.\n",demon->name,demon->damage);
|
||||||
|
update_villager_hp(target, demon->damage);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draining_attack(struct demon *demon, struct villager *target){
|
||||||
|
|
||||||
|
if(demon == NULL || target == NULL) return;
|
||||||
|
|
||||||
|
//printf("Degats: %i\n", demon->damage);
|
||||||
|
//target->cur_HP -= demon->damage;
|
||||||
|
int damage = demon->damage/2;
|
||||||
|
update_villager_hp(target, damage);
|
||||||
|
demon->cur_HP += damage/2;
|
||||||
|
printf("SLURRRRP! %s attacks you to deal %i damage and regenerates %i HP.\n", demon->name, damage, damage/2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void heavy_attack(struct demon *demon, struct villager *target){
|
||||||
|
|
||||||
|
if (demon == NULL || target == NULL) return;
|
||||||
|
|
||||||
|
int damage = demon->damage * 2;
|
||||||
|
int hits = demon->HP_max * 0.10;
|
||||||
|
update_villager_hp(target, damage);
|
||||||
|
update_demon_hp(demon, hits);
|
||||||
|
printf("BAM! %s attacks you with all his strength to deal %i damage.\n", demon->name, damage);
|
||||||
|
}
|
||||||
|
|
||||||
|
void demon_action(struct demon *demon, struct villager *villager){
|
||||||
|
|
||||||
|
if (demon == NULL || villager == NULL) return;
|
||||||
|
|
||||||
|
if (villager->distance > demon->range) chase(demon, villager);
|
||||||
|
else if (demon->cur_HP > demon->HP_max * 0.75) heavy_attack(demon, villager);
|
||||||
|
else if (demon->cur_HP < demon->HP_max * 0.25) draining_attack(demon,villager);
|
||||||
|
else basic_attack(demon, villager);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -1,14 +1,318 @@
|
|||||||
#include <stdio.h>
|
//#include <stdio.h>
|
||||||
|
|
||||||
#include "demon.h"
|
//#include "demon.h"
|
||||||
#include "villager.h"
|
//#include "villager.h"
|
||||||
#include "weapons.h"
|
//#include "weapons.h"
|
||||||
|
|
||||||
int main()
|
//int main()
|
||||||
{
|
//{
|
||||||
|
/*
|
||||||
char gun_name[50] = "BFG";
|
char gun_name[50] = "BFG";
|
||||||
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
|
||||||
printf("Gun name: %s\nDamage: %d\nRange: %d\nMagazine: %d/%d\n", gun->name, gun->damage, gun->range, gun->current_magazine, gun->magazine_size);
|
printf("Gun name: %s\nDamage: %d\nRange: %d\nMagazine: %d/%d\n", gun->name, gun->damage, gun->range, gun->current_magazine, gun->magazine_size);
|
||||||
|
|
||||||
|
|
||||||
|
//char gun_name[50] = "BFG";
|
||||||
|
//struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
|
||||||
|
gun->current_magazine = 2;
|
||||||
|
printf("Before reload: %d\n", gun->current_magazine);
|
||||||
|
reload(gun);
|
||||||
|
printf("After reload: %d\n", gun->current_magazine);
|
||||||
|
|
||||||
|
printf("villager\n");
|
||||||
|
char name[50] = "Isabelle";
|
||||||
|
//char gun_name[50] = "BFG";
|
||||||
|
gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
struct villager *villager = init_villager(name, 50, gun, 1);
|
||||||
|
update_villager_hp(villager, 100);
|
||||||
|
|
||||||
|
char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, CLOSE, 16);
|
||||||
|
struct villager *villager = init_villager(name, 50, gun, 1);
|
||||||
|
pp_villager(villager);
|
||||||
|
destroy_villager(villager);
|
||||||
|
*/
|
||||||
|
/* char demon_name[50] = "Cyberdemon";
|
||||||
|
struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);
|
||||||
|
update_demon_hp(demon, 200);
|
||||||
|
*/
|
||||||
|
/* char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 50, gun, 1);
|
||||||
|
|
||||||
|
// Init demon
|
||||||
|
char demon_name[50] = "Cyberdemon";
|
||||||
|
struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);
|
||||||
|
// // Shoot
|
||||||
|
shoot(villager, demon);
|
||||||
|
pp_villager(villager);
|
||||||
|
pp_demon(demon);
|
||||||
|
destroy_demon(demon);
|
||||||
|
destroy_villager(villager);
|
||||||
|
*/
|
||||||
|
/* char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 50, gun, 1);
|
||||||
|
|
||||||
|
// Heal
|
||||||
|
villager->cur_HP -= 40;
|
||||||
|
pp_villager(villager);
|
||||||
|
putchar('\n');
|
||||||
|
heal(villager);
|
||||||
|
pp_villager(villager);
|
||||||
|
destroy_villager(villager);
|
||||||
|
*/
|
||||||
|
/* char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 50, gun, 1);
|
||||||
|
|
||||||
|
// Walk
|
||||||
|
pp_villager(villager);
|
||||||
|
putchar('\n');
|
||||||
|
walk(villager, 50);
|
||||||
|
pp_villager(villager);
|
||||||
|
destroy_villager(villager);
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
// Init Villager
|
||||||
|
char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 50, gun, 1);
|
||||||
|
|
||||||
|
// Prepare Medicine
|
||||||
|
villager->medicines -= 5;
|
||||||
|
pp_villager(villager);
|
||||||
|
prepare_medicine(villager);
|
||||||
|
pp_villager(villager);
|
||||||
|
destroy_villager(villager);
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
// Init Villager
|
||||||
|
char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 50, gun, 1);
|
||||||
|
|
||||||
|
// Init demon
|
||||||
|
char demon_name[50] = "Cyberdemon";
|
||||||
|
struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);
|
||||||
|
|
||||||
|
// Chase
|
||||||
|
pp_villager(villager);
|
||||||
|
chase(demon, villager);
|
||||||
|
pp_villager(villager);
|
||||||
|
destroy_villager(villager);
|
||||||
|
destroy_demon(demon);
|
||||||
|
*/
|
||||||
|
// Init Villager
|
||||||
|
/* char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 50, gun, 1);
|
||||||
|
|
||||||
|
// Init demon
|
||||||
|
char demon_name[50] = "Cyberdemon";
|
||||||
|
struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, FAR);
|
||||||
|
|
||||||
|
// Basic attack
|
||||||
|
basic_attack(demon, villager);
|
||||||
|
pp_villager(villager);
|
||||||
|
destroy_villager(villager);
|
||||||
|
destroy_demon(demon);
|
||||||
|
*/
|
||||||
|
// Init Villager
|
||||||
|
/* char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 50, gun, 1);
|
||||||
|
|
||||||
|
// Init demon
|
||||||
|
char demon_name[50] = "Cyberdemon";
|
||||||
|
struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, FAR);
|
||||||
|
|
||||||
|
// pp_villager(villager);
|
||||||
|
// pp_demon(demon);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Draining attack
|
||||||
|
demon->cur_HP -= 10;
|
||||||
|
draining_attack(demon, villager);
|
||||||
|
pp_villager(villager);
|
||||||
|
pp_demon(demon);
|
||||||
|
destroy_demon(demon);
|
||||||
|
destroy_villager(villager);
|
||||||
|
*/
|
||||||
|
// Init Villager
|
||||||
|
/* char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 50, gun, 1);
|
||||||
|
|
||||||
|
// Init demon
|
||||||
|
char demon_name[50] = "Cyberdemon";
|
||||||
|
struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, FAR);
|
||||||
|
|
||||||
|
// Heavy attack
|
||||||
|
heavy_attack(demon, villager);
|
||||||
|
pp_villager(villager);
|
||||||
|
pp_demon(demon);
|
||||||
|
destroy_demon(demon);
|
||||||
|
destroy_villager(villager);
|
||||||
|
*/
|
||||||
|
/* // Init Villager
|
||||||
|
char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 500, gun, 1);
|
||||||
|
|
||||||
|
// Init demon
|
||||||
|
char demon_name[50] = "Cyberdemon";
|
||||||
|
struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);
|
||||||
|
|
||||||
|
// Demon action
|
||||||
|
demon_action(demon, villager); // Distance = Near but its range is close
|
||||||
|
demon_action(demon, villager); // In range and high HP
|
||||||
|
demon->cur_HP = demon->HP_max / 2;
|
||||||
|
demon_action(demon, villager); // In range and mid HP
|
||||||
|
demon->cur_HP = 1;
|
||||||
|
demon_action(demon, villager); // In range and low HP
|
||||||
|
|
||||||
|
destroy_villager(villager);
|
||||||
|
destroy_demon(demon);
|
||||||
|
|
||||||
|
}*/
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "demon.h"
|
||||||
|
#include "villager.h"
|
||||||
|
#include "weapons.h"
|
||||||
|
|
||||||
|
#define PURPLE(string) "\x1b[35m" string "\x1b[0m"
|
||||||
|
|
||||||
|
#define MAX_BUFFER 1024
|
||||||
|
|
||||||
|
void villager_action(struct villager *villager, struct demon *demon, char *command)
|
||||||
|
{
|
||||||
|
if (!strcmp(command, "a"))
|
||||||
|
{
|
||||||
|
shoot(villager, demon);
|
||||||
|
}
|
||||||
|
else if (!strcmp(command, "r"))
|
||||||
|
{
|
||||||
|
printf("You reload your gun");
|
||||||
|
reload(villager->gun);
|
||||||
|
}
|
||||||
|
else if (!strcmp(command, "h"))
|
||||||
|
{
|
||||||
|
heal(villager);
|
||||||
|
}
|
||||||
|
else if (!strcmp(command, "z+"))
|
||||||
|
{
|
||||||
|
walk(villager, 1);
|
||||||
|
}
|
||||||
|
else if (!strcmp(command, "s-"))
|
||||||
|
{
|
||||||
|
walk(villager, -1);
|
||||||
|
}
|
||||||
|
else if (!strcmp(command, "p"))
|
||||||
|
{
|
||||||
|
prepare_medicine(villager);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf(PURPLE("No such action\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void end_fight(struct villager *villager, struct demon *demon)
|
||||||
|
{
|
||||||
|
if (villager->cur_HP <= 0 || demon->cur_HP <= 0)
|
||||||
|
{
|
||||||
|
pp_villager(villager);
|
||||||
|
pp_demon(demon);
|
||||||
|
destroy_villager(villager);
|
||||||
|
destroy_demon(demon);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("\033[2J\033[1;1H");
|
||||||
|
// Init gun and villager
|
||||||
|
char name[50] = "Isabelle";
|
||||||
|
char gun_name[50] = "BFG";
|
||||||
|
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
|
||||||
|
struct villager *villager =
|
||||||
|
init_villager(name, 50, gun, 1);
|
||||||
|
|
||||||
|
// Init demon
|
||||||
|
char demon_name[50] = "Cyberdemon";
|
||||||
|
struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);
|
||||||
|
pp_villager(villager);
|
||||||
|
pp_demon(demon);
|
||||||
|
|
||||||
|
char input[MAX_BUFFER];
|
||||||
|
|
||||||
|
while (fgets(input, MAX_BUFFER, stdin) != NULL)
|
||||||
|
{
|
||||||
|
printf("\033[2J\033[1;1H");
|
||||||
|
int i = 0;
|
||||||
|
while (input[i] != '\0')
|
||||||
|
{
|
||||||
|
if (input[i] == '\n')
|
||||||
|
{
|
||||||
|
input[i] = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\x1b[34m");
|
||||||
|
villager_action(villager, demon, input);
|
||||||
|
printf("\x1b[0m");
|
||||||
|
|
||||||
|
end_fight(villager, demon);
|
||||||
|
|
||||||
|
printf("\x1b[31m");
|
||||||
|
demon_action(demon, villager);
|
||||||
|
printf("\x1b[0m");
|
||||||
|
|
||||||
|
end_fight(villager, demon);
|
||||||
|
|
||||||
|
pp_villager(villager);
|
||||||
|
pp_demon(demon);
|
||||||
|
|
||||||
|
printf("> ");
|
||||||
|
}
|
||||||
|
|
||||||
|
destroy_demon(demon);
|
||||||
|
destroy_villager(villager);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
#include "villager.h"
|
||||||
|
#include "demon.h"
|
||||||
|
#include "weapons.h"
|
||||||
|
#include <complex.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
char *cat(enum demon_category demon_category){
|
||||||
|
|
||||||
|
if (demon_category == 1) return "Parasite";
|
||||||
|
else if (demon_category == 2) return "Threat";
|
||||||
|
else if (demon_category == 3) return "Nightmare";
|
||||||
|
else if (demon_category == 4) return "Calamity";
|
||||||
|
else if (demon_category == 5) return "Tom Nook";
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *dist(enum distance distance){
|
||||||
|
if (distance == 0) return "Close";
|
||||||
|
else if (distance == 1) return "Near";
|
||||||
|
else return "Far";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void pp_villager(struct villager* villager){
|
||||||
|
|
||||||
|
if (villager == NULL) return;
|
||||||
|
|
||||||
|
printf("============================= Villager info =============================\n");
|
||||||
|
printf("Name: %s\n",villager->name);
|
||||||
|
printf("HP: %i/%i\n", villager->cur_HP,villager->HP_max);
|
||||||
|
printf("Gun: %s | Magazine: %i/%i | Mastery: %i\n", villager->gun->name, villager->gun->current_magazine, villager->gun->magazine_size, villager->gun_mastery);
|
||||||
|
printf("Medicine: %i/10\n",villager->medicines);
|
||||||
|
|
||||||
|
/*
|
||||||
|
if (villager->distance == 1) printf("Distance: NEAR\n");
|
||||||
|
else if (villager->distance == 0) printf("Ditance: CLOSE\n");
|
||||||
|
else printf("Distance: FAR\n");
|
||||||
|
*/
|
||||||
|
printf("Distance: %s\n",dist(villager->distance));
|
||||||
|
}
|
||||||
|
|
||||||
|
void pp_demon(struct demon* demon){
|
||||||
|
|
||||||
|
if(demon == NULL) return;
|
||||||
|
|
||||||
|
printf("============================= Demon info =============================\n");
|
||||||
|
printf("Name: %s | Category: %s\n", demon->name, cat(demon->category));
|
||||||
|
printf("Base damage: %i | Range: %s\n", demon->damage, dist(demon->range));
|
||||||
|
printf("HP: %i/%i\n", demon->cur_HP, demon->HP_max);
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,134 @@
|
|||||||
|
#include "villager.h"
|
||||||
|
#include "demon.h"
|
||||||
|
#include "weapons.h"
|
||||||
|
#include <printf.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
struct villager *init_villager(char name[50], int HP_max, struct gun *gun, int gun_mastery){
|
||||||
|
struct villager *villager = malloc(sizeof(struct villager));
|
||||||
|
if (villager == NULL) return NULL;
|
||||||
|
|
||||||
|
//init des var du villager
|
||||||
|
strcpy(villager->name, name);
|
||||||
|
villager->medicines = 10;
|
||||||
|
villager->distance = NEAR;
|
||||||
|
villager->cur_HP = HP_max;
|
||||||
|
villager->gun = gun;
|
||||||
|
villager->gun_mastery = gun_mastery;
|
||||||
|
villager->HP_max = HP_max;
|
||||||
|
return villager;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_villager_hp(struct villager *villager, int amount){
|
||||||
|
|
||||||
|
if(villager == NULL) return;
|
||||||
|
villager->cur_HP -= amount;
|
||||||
|
if(villager->cur_HP > villager->HP_max) villager->cur_HP = villager->HP_max;
|
||||||
|
if (villager->cur_HP <= 0) printf("You died.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_villager(struct villager *villager){
|
||||||
|
|
||||||
|
if (villager == NULL) return;
|
||||||
|
free(villager->gun);
|
||||||
|
free(villager);
|
||||||
|
}
|
||||||
|
|
||||||
|
void shoot(struct villager *villager, struct demon *demon){
|
||||||
|
|
||||||
|
if (villager == NULL || demon == NULL) return;
|
||||||
|
if (villager->gun == NULL){
|
||||||
|
printf("Equip a gun before trying to use one.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(villager->gun->current_magazine <= 0) {
|
||||||
|
printf("Empty gun : You gotta reload.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//villager shoot le demon
|
||||||
|
villager->gun->current_magazine --;
|
||||||
|
|
||||||
|
if((villager->distance >= demon->range) && (villager->gun->range >= demon->range)){
|
||||||
|
|
||||||
|
update_demon_hp(demon, villager->gun->damage + villager->gun_mastery);
|
||||||
|
printf("BAM! %s lost %i HP.\n",demon->name, villager->gun_mastery + villager->gun->damage);
|
||||||
|
}
|
||||||
|
else printf("No demon in range.\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
void heal(struct villager *villager){
|
||||||
|
|
||||||
|
if(villager == NULL) return;
|
||||||
|
|
||||||
|
if(villager->medicines <= 0) {
|
||||||
|
printf("No medicine left, maybe prepare some ?\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
villager->medicines --;
|
||||||
|
|
||||||
|
int gain = villager->cur_HP * 1.25;
|
||||||
|
villager->cur_HP += gain;
|
||||||
|
|
||||||
|
if (villager->cur_HP > villager->HP_max) villager->cur_HP = villager->HP_max;
|
||||||
|
printf("Nice, you healed %i HP.\n", gain);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum distance walk(struct villager *villager, int direction){
|
||||||
|
|
||||||
|
// peut aller x<0 ou x>3
|
||||||
|
// A voir si la mouli accepte
|
||||||
|
|
||||||
|
if(villager == NULL) return NEAR;
|
||||||
|
/*
|
||||||
|
if (direction >= 0){
|
||||||
|
if(villager->distance == CLOSE)printf("Too close.\n");
|
||||||
|
else{
|
||||||
|
printf("You decided to move.\n");
|
||||||
|
villager->distance += direction;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (villager->distance == FAR) printf("I can't run away.\n");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("You decided to move.\n");
|
||||||
|
villager->distance ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
// trop loin
|
||||||
|
if((villager->distance == FAR) && (direction < 0)){
|
||||||
|
printf( "I can't run away.\n");
|
||||||
|
return villager->distance;
|
||||||
|
}
|
||||||
|
// trop proche
|
||||||
|
else if((villager->distance == CLOSE) && (direction >= 0)) {
|
||||||
|
printf("Too close.\n");
|
||||||
|
return villager->distance;
|
||||||
|
}
|
||||||
|
//printf("Distance act: %d\n", villager->distance);
|
||||||
|
villager->distance -= direction;
|
||||||
|
//printf("Distance apres chg: %d\n",villager->distance);
|
||||||
|
return villager->distance;
|
||||||
|
}
|
||||||
|
|
||||||
|
int prepare_medicine(struct villager *villager){
|
||||||
|
|
||||||
|
if (villager == NULL) return 0;
|
||||||
|
|
||||||
|
//on peut pas ajouter plus, il est deja au max
|
||||||
|
if(villager->medicines >= 10) return villager->medicines;
|
||||||
|
|
||||||
|
villager->medicines ++;
|
||||||
|
printf("Preparation ready !\n");
|
||||||
|
|
||||||
|
return villager->medicines;
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ struct villager{
|
|||||||
char name[50];
|
char name[50];
|
||||||
int cur_HP;
|
int cur_HP;
|
||||||
int HP_max;
|
int HP_max;
|
||||||
// struct gun;
|
struct gun *gun;
|
||||||
int gun_mastery;
|
int gun_mastery;
|
||||||
int medicines;
|
int medicines;
|
||||||
enum distance distance;
|
enum distance distance;
|
||||||
@@ -23,6 +23,4 @@ void heal(struct villager *villager);
|
|||||||
enum distance walk(struct villager *villager, int direction);
|
enum distance walk(struct villager *villager, int direction);
|
||||||
int prepare_medicine(struct villager *villager);
|
int prepare_medicine(struct villager *villager);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -4,14 +4,21 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
struct gun *init_gun(char name[50], int damage, enum distance range, int magazine_size){
|
struct gun *init_gun(char name[50], int damage, enum distance range, int magazine_size){
|
||||||
|
|
||||||
struct gun *rslt = malloc(sizeof( struct gun));
|
struct gun *gun = malloc(sizeof( struct gun));
|
||||||
//mettre protection
|
if (gun == NULL) return NULL;
|
||||||
|
|
||||||
//init des var du struct
|
//init des var du struct
|
||||||
strcpy(rslt->name, name);
|
strcpy(gun->name, name);
|
||||||
rslt->damage = damage;
|
gun->damage = damage;
|
||||||
rslt->range = range;
|
gun->range = range;
|
||||||
rslt->magazine_size = magazine_size;
|
gun->magazine_size = magazine_size;
|
||||||
|
gun->current_magazine = magazine_size;
|
||||||
|
|
||||||
return rslt;
|
return gun;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reload(struct gun *gun){ gun->current_magazine = gun->magazine_size; }
|
||||||
|
|
||||||
|
void destroy_gun(struct gun *gun){
|
||||||
|
if( gun == NULL) return;
|
||||||
|
free(gun);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
CC=gcc
|
CC=gcc
|
||||||
SRC = fish.c insects.c vector.c main.c
|
SRC = fish.c insect.c vector.c main.c
|
||||||
CFLAGS= -Wall -Wextra -Werror
|
CFLAGS= -Wall -Wextra -Werror
|
||||||
LDFLAGS= -fsanitize=address -g
|
LDFLAGS= -fsanitize=address -g
|
||||||
# Computes the basic main for tests
|
# Computes the basic main for tests
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef ANIMALS_H
|
||||||
|
#define ANIMALS_H
|
||||||
|
#include <stdio.h>
|
||||||
|
enum animal_type
|
||||||
|
{
|
||||||
|
INSECT,
|
||||||
|
FISH,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct fish
|
||||||
|
{
|
||||||
|
char* species;
|
||||||
|
int swimming_speed;
|
||||||
|
size_t size;
|
||||||
|
int lives_in_fresh_water;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct insect
|
||||||
|
{
|
||||||
|
char* species;
|
||||||
|
size_t size;
|
||||||
|
int can_fly;
|
||||||
|
int can_swim;
|
||||||
|
};
|
||||||
|
|
||||||
|
union animals
|
||||||
|
{
|
||||||
|
struct insect* insect;
|
||||||
|
struct fish* fish;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct animal
|
||||||
|
{
|
||||||
|
enum animal_type type;
|
||||||
|
union animals animal;
|
||||||
|
char* color;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct fish* fish_init(const char* species, int speed, size_t size, int fresh_water);
|
||||||
|
struct animal* animal_from_fish(const char* color, struct fish* fish);
|
||||||
|
void free_fish(struct fish* fish);
|
||||||
|
struct insect *insect_init(const char *species, size_t size, int can_fly, int can_swim);
|
||||||
|
struct animal* animal_from_insect(const char* color, struct insect* insect);
|
||||||
|
void free_insect(struct insect* insect);
|
||||||
|
void free_animal(struct animal* animal);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,68 @@
|
|||||||
|
#include "animals.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
|
struct fish *fish_init(const char *species, int speed, size_t size, int fresh_water){
|
||||||
|
//je sens que la mouli vas tester ça grr
|
||||||
|
if(species == NULL) return NULL;
|
||||||
|
|
||||||
|
struct fish *fish = malloc(sizeof(struct fish));
|
||||||
|
if (fish == NULL) return NULL;
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
while (*(species + index) != '\0') index ++;
|
||||||
|
|
||||||
|
fish->species = malloc((index + 1 ) * sizeof(char *));
|
||||||
|
if (fish->species == NULL){
|
||||||
|
free(fish);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(fish->species, species);
|
||||||
|
fish->lives_in_fresh_water = fresh_water;
|
||||||
|
fish->size = size;
|
||||||
|
fish->swimming_speed = speed;
|
||||||
|
|
||||||
|
return fish;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct animal *animal_from_fish(const char *color, struct fish *fish){
|
||||||
|
|
||||||
|
if (color == NULL || fish == NULL) return NULL;
|
||||||
|
|
||||||
|
struct animal *animal = malloc(sizeof(struct animal));
|
||||||
|
if(animal == NULL)return NULL;
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
while(*(color + index) != '\0') index ++;
|
||||||
|
|
||||||
|
animal->color = malloc((index + 1) * sizeof(char*));
|
||||||
|
if(animal->color == NULL) {
|
||||||
|
//free(animal->color);
|
||||||
|
free(animal);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(animal->color, color);
|
||||||
|
animal->type = 1;
|
||||||
|
/*
|
||||||
|
union animals *animals = malloc(sizeof(union animals));
|
||||||
|
if(animals == NULL){
|
||||||
|
free(animal->color);
|
||||||
|
free(animal);
|
||||||
|
return NULL;
|
||||||
|
}*/
|
||||||
|
//animals->fish = fish;
|
||||||
|
animal->animal.fish = fish;
|
||||||
|
return animal;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_fish(struct fish *fish){
|
||||||
|
|
||||||
|
if(fish == NULL) return;
|
||||||
|
|
||||||
|
if(fish->species != NULL) free(fish->species);
|
||||||
|
free(fish);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#include "animals.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
struct insect *insect_init(const char *species, size_t size, int can_fly, int can_swim){
|
||||||
|
|
||||||
|
if(species == NULL) return NULL;
|
||||||
|
|
||||||
|
struct insect *insect = malloc(sizeof(struct insect));
|
||||||
|
if (insect == NULL) return NULL;
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
while (*(species + index) != '\0') index ++;
|
||||||
|
|
||||||
|
insect->species = malloc((index + 1)*sizeof(char*));
|
||||||
|
if (insect->species == NULL) {
|
||||||
|
free(insect);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(insect->species, species);
|
||||||
|
insect->size = size;
|
||||||
|
insect->can_fly = can_fly;
|
||||||
|
insect->can_swim = can_swim;
|
||||||
|
|
||||||
|
return insect;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct animal *animal_from_insect(const char *color, struct insect *insect){
|
||||||
|
|
||||||
|
if (color == NULL || insect == NULL) return NULL;
|
||||||
|
|
||||||
|
struct animal *animal = malloc(sizeof(struct animal));
|
||||||
|
if(animal == NULL) return NULL;
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
while(*(color + index) != '\0') index ++;
|
||||||
|
|
||||||
|
animal->color = malloc((index + 1) * sizeof(char*));
|
||||||
|
if(animal->color == NULL){
|
||||||
|
free(animal);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(animal->color, color);
|
||||||
|
animal->type = 0;
|
||||||
|
animal->animal.insect = insect;
|
||||||
|
return animal;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_insect(struct insect *insect){
|
||||||
|
|
||||||
|
if (insect == NULL) return;
|
||||||
|
|
||||||
|
//Normalement il ne devrait pas etre NULL
|
||||||
|
//if(insect->species != NULL) free(insect->species);
|
||||||
|
free(insect->species);
|
||||||
|
free(insect);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,115 @@
|
|||||||
|
|
||||||
#include "animals.h"
|
#include "animals.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
int main(){
|
||||||
|
// ================= FISH ===============
|
||||||
|
/*
|
||||||
|
struct fish *f = fish_init("Salmon", 10, 5, 1);
|
||||||
|
printf("Fish species: %s\n", f->species);
|
||||||
|
printf("Swimming speed: %d\n", f->swimming_speed);
|
||||||
|
printf("Size: %zu\n", f->size);
|
||||||
|
printf("Lives in fresh water: %d\n", f->lives_in_fresh_water);
|
||||||
|
free_fish(f);
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
struct fish *f = fish_init("Trout", 8, 4, 1);
|
||||||
|
struct animal *a = animal_from_fish("Blue", f);
|
||||||
|
printf("Animal type: %d\n", a->type);
|
||||||
|
printf("Animal color: %s\n", a->color);
|
||||||
|
printf("Fish species from animal: %s\n", a->animal.fish->species);
|
||||||
|
free_fish(f);
|
||||||
|
free(a->color);
|
||||||
|
free(a);
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
struct fish *f = fish_init("Carp", 6, 3, 0);
|
||||||
|
free_fish(f);
|
||||||
|
*/
|
||||||
|
|
||||||
int main()
|
// ================ INSECT ==============
|
||||||
{}
|
/* struct insect *i = insect_init("Dragonfly", 2, 1, 0);
|
||||||
|
printf("Insect species: %s\n", i->species);
|
||||||
|
printf("Size: %zu\n", i->size);
|
||||||
|
printf("Can fly: %d\n", i->can_fly);
|
||||||
|
printf("Can swim: %d\n", i->can_swim);
|
||||||
|
free(i->species);
|
||||||
|
free(i);
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
struct insect *i = insect_init("Butterfly", 1, 1, 0);
|
||||||
|
struct animal *a = animal_from_insect("Yellow", i);
|
||||||
|
printf("Animal type: %d\n", a->type);
|
||||||
|
printf("Animal color: %s\n", a->color);
|
||||||
|
printf("Insect species from animal: %s\n", a->animal.insect->species);
|
||||||
|
free(i->species);
|
||||||
|
free(i);
|
||||||
|
free(a->color);
|
||||||
|
free(a);
|
||||||
|
*/
|
||||||
|
/* struct insect *i = insect_init("Beetle", 3, 0, 1);
|
||||||
|
free_insect(i);
|
||||||
|
*/
|
||||||
|
// ============ VECTOR ==============
|
||||||
|
/*
|
||||||
|
struct vector *v = vector_init(5);
|
||||||
|
printf("Initial size: %zu\n", v->size);
|
||||||
|
printf("Initial capacity: %zu\n", v->capacity);
|
||||||
|
free(v->animal);
|
||||||
|
free(v);
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
struct vector *v = vector_init(2);
|
||||||
|
v = vector_resize(v, 4);
|
||||||
|
printf("Resized capacity: %zu\n", v->capacity);
|
||||||
|
free(v->animal);
|
||||||
|
free(v);
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
struct vector *v = vector_init(2);
|
||||||
|
|
||||||
|
struct fish *f1 = fish_init("Salmon", 10, 5, 1);
|
||||||
|
struct animal *a1 = animal_from_fish("Blue", f1);
|
||||||
|
v = vector_append(v, a1);
|
||||||
|
|
||||||
|
struct fish *f2 = fish_init("Trout", 8, 4, 1);
|
||||||
|
struct animal *a2 = animal_from_fish("Green", f2);
|
||||||
|
v = vector_append(v, a2);
|
||||||
|
|
||||||
|
struct fish *f3 = fish_init("Carp", 6, 3, 0);
|
||||||
|
struct animal *a3 = animal_from_fish("Red", f3);
|
||||||
|
v = vector_append(v, a3);
|
||||||
|
|
||||||
|
printf("Vector size after appends: %zu\n", v->size);
|
||||||
|
printf("Vector capacity after appends: %zu\n", v->capacity);
|
||||||
|
free(f1->species);
|
||||||
|
free(f1);
|
||||||
|
free(a1->color);
|
||||||
|
free(a1);
|
||||||
|
|
||||||
|
free(f2->species);
|
||||||
|
free(f2);
|
||||||
|
free(a2->color);
|
||||||
|
free(a2);
|
||||||
|
|
||||||
|
free(f3->species);
|
||||||
|
free(f3);
|
||||||
|
free(a3->color);
|
||||||
|
free(a3);
|
||||||
|
|
||||||
|
free(v->animal);
|
||||||
|
free(v);
|
||||||
|
*/
|
||||||
|
/* struct fish *f = fish_init("Salmon", 10, 5, 1);
|
||||||
|
struct animal *a = animal_from_fish("Blue", f);
|
||||||
|
free_animal(a);
|
||||||
|
*/
|
||||||
|
struct vector *v = vector_init(5);
|
||||||
|
|
||||||
|
struct fish *f = fish_init("Trout", 8, 4, 1);
|
||||||
|
struct animal *a = animal_from_fish("Green", f);
|
||||||
|
v = vector_append(v, a);
|
||||||
|
|
||||||
|
vector_destroy(v);
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "vector.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include "animals.h"
|
||||||
|
struct vector *vector_init(size_t n){
|
||||||
|
|
||||||
|
//if (n < 0) return NULL;
|
||||||
|
struct vector *vector = malloc(sizeof(struct vector));
|
||||||
|
|
||||||
|
vector->animal = malloc((n) * sizeof(struct animal));
|
||||||
|
if(vector->animal == NULL) return NULL;
|
||||||
|
|
||||||
|
vector->capacity = n;
|
||||||
|
vector->size = 0;
|
||||||
|
|
||||||
|
return vector;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vector *vector_resize(struct vector *v, size_t new_capacity){
|
||||||
|
|
||||||
|
if(v == NULL) return NULL;
|
||||||
|
|
||||||
|
v->animal = realloc(v->animal, (new_capacity + 1) * sizeof(struct animal));
|
||||||
|
v->capacity = new_capacity;
|
||||||
|
|
||||||
|
return v;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vector *vector_append(struct vector *v, struct animal *animal){
|
||||||
|
|
||||||
|
if(v == NULL || animal == NULL) return NULL;
|
||||||
|
|
||||||
|
if (v->capacity <= v->size) v = vector_resize(v, v->capacity * 2);
|
||||||
|
// TODO A voir si le realloc ajoute de l'espace a gauche ou a droite
|
||||||
|
// car sinon y a ecrasement de data
|
||||||
|
*(v->animal + (v->capacity - v->size )) = animal;
|
||||||
|
v->size ++;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_animal(struct animal *animal){
|
||||||
|
|
||||||
|
if (animal == NULL) return;
|
||||||
|
|
||||||
|
//if(animal->color != NULL) free(animal->color);
|
||||||
|
printf("%s\n",animal->animal.fish->species);
|
||||||
|
printf("%s\n", animal->animal.insect->species);
|
||||||
|
if(animal->type == INSECT) free_insect(animal->animal.insect);
|
||||||
|
if(animal->type == FISH) free_fish(animal->animal.fish);
|
||||||
|
free(animal->color);
|
||||||
|
free(animal);
|
||||||
|
}
|
||||||
|
|
||||||
|
void vector_destroy(struct vector *v){
|
||||||
|
|
||||||
|
if(v == NULL) return;
|
||||||
|
if(v->animal != NULL){
|
||||||
|
|
||||||
|
//size_t index = 1;
|
||||||
|
/*while (index < (v->capacity - v->size)){
|
||||||
|
free_animal(*(v->animal + (v->size - index)));
|
||||||
|
}*/
|
||||||
|
for(size_t i = 0; i < v->size; i++) free_animal(v->animal[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(v->animal);
|
||||||
|
free(v);
|
||||||
|
//free_animal(v->animal);
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef VECTOR_H
|
||||||
|
#define VECTOR_H
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct vector
|
||||||
|
{
|
||||||
|
struct animal **animal;
|
||||||
|
size_t size;
|
||||||
|
size_t capacity;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct vector *vector_init(size_t n);
|
||||||
|
struct vector *vector_resize(struct vector *v, size_t new_capacity);
|
||||||
|
struct vector *vector_append(struct vector *v, struct animal *animal);
|
||||||
|
void vector_destroy(struct vector *v);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user