This commit is contained in:
komiko44240BB
2024-09-16 11:44:23 +02:00
parent cd288a3215
commit 1ed87b329a
11 changed files with 111 additions and 180 deletions

40
main.c
View File

@@ -13,16 +13,38 @@
// Formula to calculate dmg and combo dmg : ( base dmg + flat train dmg + flat perk dmg ) * ( 1 + % perk dmg + % train dmg ) * ( 1 + % fire train dmg + % fire perk dmg )
int main() {
/*int gear[5][2] = {{5,8},{3,4},{1,2},{5,2},{2,4}};
/*int gear[5][2] = {{5,8},{3,4},{0,0},{0,0},{0,0}};
struct Ship* s = initialiseShip("shooter",1.2,0.9,0.5,1596,5);
setUpShip(s,gear);*/
//char* object_name= "standard_cannon";
//char* rarity = "legendary";
//struct Object* o = readObjectFile(1,object_name,rarity);
//addOject(s,o);
//readTraining(o,object_name);
//printf("\n");
//printObject(o);
setUpShip(s,gear);
for (int i = 0 ; i < 5; i++){
char* object_name= "standard_cannon";
char* rarity = "legendary";
struct Object* o = readObjectFile(1,object_name,rarity);
bool isAdded = addOject(s,o);
if(!isAdded){
printf("Could not add objects, not enought slot points or slots\n");
i++;
}else{
printf("Object added sucesfuly\n");
}
//readTraining(o,object_name);
}
for (int i = 0 ; i < 3; i++){
char* object_name= "standard_shield";
char* rarity = "legendary";
struct Object* o = readObjectFile(2,object_name,rarity);
bool isAdded = addOject(s,o);
if(!isAdded){
printf("Could not add objects, not enought slot points or slots\n");
i++;
}else{
printf("Object added sucesfuly\n");
}
//readTraining(o,object_name);
}
printf("\n");
printLoadout(s);
printObject(o);*/
start();
return 0;
}

19
menu.c
View File

@@ -77,8 +77,8 @@ void creatingShip() {
bool createShip = true;
char* ship_name = "";
float speed = 0;
float ship_agility = 0;
float turret_agility = 0;
int ship_agility = 0;
int turret_agility = 0;
unsigned int hit_points = 0;
unsigned int defence = 0;
unsigned int gear [5][2] = {0};
@@ -305,6 +305,7 @@ void addObjects(struct Ship* s) {
unsigned int object_type = 0;
if(i == 8){
printf("You can only save the ship, all slots are full\n");
i--;
}
while(add_objects){
printf("It doesn't matter which type of object you add first\n");
@@ -316,6 +317,7 @@ void addObjects(struct Ship* s) {
printf("4. Green (healing)\n");
printf("5. Teal (guardian's type)\n");
printf("6. Save ship\n");
printf("7. Display ship\n");
printf("0. Cancel ship\n");
printf("------------------------\n");
printf("Your choice: ");
@@ -377,6 +379,12 @@ void addObjects(struct Ship* s) {
//saveShipAs(s);
read_file = false;
return;
case 7:
printShipStats(s);
printLoadout(s);
add_objects = true;
read_file = false;
break;
case 0:
deleteShip(s);
read_file = false;
@@ -390,10 +398,11 @@ void addObjects(struct Ship* s) {
struct Object* o = readObjectFile(object_type,object_name,rarity);
bool isAdded = addOject(s,o);
if(!isAdded){
printf("Could not add objects, not enought slot points or slots\n");
i++;
printf("Could not add object, not enought slot points or slots\n");
i--;
}else{
printf("Object added sucesfuly\n");
struct Training* t = readTraining(o,object_name);
}
}
}

3
menu.h
View File

@@ -1,9 +1,6 @@
#ifndef MENU_H
#define MENU_H
void start();
void clearInputBuffer();
void startMenu();

View File

@@ -75,7 +75,7 @@ void printObject(struct Object* o) {
if(o->spread != 0){
printf("Spread :%.2f",o->spread);
}
if(o->amount_of_projectiles != 1){
if(o->amount_of_projectiles > 1){
printf("Amnt of proj :%d",o->amount_of_projectiles);
}
if(o->duration != 0){

View File

@@ -1,4 +1,3 @@
#include "perks.h"
#ifndef OBJECTS_H
#define OBJECTS_H
@@ -27,7 +26,7 @@ struct Object {
struct Slots {
int type;
char type;
unsigned int slot_amount;
unsigned int occipied_slots;
unsigned int slot_points;

View File

@@ -27,7 +27,7 @@ struct Object* readObjectFile(unsigned int object_type, char* object_name, char*
return NULL;
}
// Loop to prompt user for object level until a valid input is provided
while (true){
/*while (true){
printf("What level is the object ?\n");
printf("(Between 1 and 50)\n");
printf("------------------------\n");
@@ -38,7 +38,8 @@ struct Object* readObjectFile(unsigned int object_type, char* object_name, char*
// Validate the level input
if(object_lvl < 0 || object_lvl > 50){
printf("Invalid choice, please try again\n");
}else{
}else{*/
int object_lvl = 10;
// Allocate memory for the filename using object name and rarity
char* object_file_name = malloc(sizeof(char)*(strlen(object_name) + strlen(rarity) + 16 + 1 + 5 + 1));
if(object_file_name == NULL){
@@ -114,5 +115,5 @@ struct Object* readObjectFile(unsigned int object_type, char* object_name, char*
// Return the newly created object
return o;
}
}
}
//}
//}

View File

@@ -5,13 +5,36 @@
#include <unistd.h>
#include "menu.h"
#include "objects.h"
//#include "readtraining.h"
#include "readtraining.h"
#define MAX_LINE_SIZE 1024
void readTraining(struct Object* o,char* object_name) {
struct Training* createTraining(unsigned int training_lvl) {
struct Training* t = malloc(sizeof(struct Training));
if (t == NULL) {
return NULL;
}
t->type = malloc(sizeof(char*) * training_lvl);
if (t->type == NULL) {
free(t);
return NULL;
}
t->value = malloc(sizeof(float) * training_lvl);
if (t->value == NULL) {
free(t->type);
free(t);
return NULL;
}
return t;
}
struct Training* readTraining(struct Object* o,char* object_name) {
if(o == NULL || object_name == NULL){
return;
return NULL;
}
char* training_file_name = malloc(strlen(object_name) + 17 +13 + 1);
sprintf(training_file_name, "../Training_data/%s_training.csv", object_name);
@@ -19,7 +42,7 @@ void readTraining(struct Object* o,char* object_name) {
FILE* file = fopen(training_file_name, "r");
if (file == NULL) {
printf("Error opening \" %s \", file does not exist\n", training_file_name);
return ;
return NULL;
}
char line[MAX_LINE_SIZE];
@@ -27,7 +50,7 @@ void readTraining(struct Object* o,char* object_name) {
if (fgets(line, sizeof(line), file) == NULL) {
fclose(file);
return ;
return NULL;
}
copiedline = strdup(line);
@@ -46,7 +69,8 @@ void readTraining(struct Object* o,char* object_name) {
if(training_lvl<= 0 || training_lvl > max_training_lvl){
if(training_lvl == 0){
fclose(file);
return;
struct Training* t0 = createTraining(0);
return t0;
}
printf("Wrong training input level\n");
}else{
@@ -56,171 +80,45 @@ void readTraining(struct Object* o,char* object_name) {
if (fgets(line, sizeof(line), file) == NULL) {
fclose(file);
return;
return NULL;
} // skip the line of information
if (fgets(line, sizeof(line), file) == NULL) {
fclose(file);
return;
return NULL;
} // get the first line of training
copiedline = strdup(line);
char** boost_type = malloc(sizeof(char*) * training_lvl);
float* boost_value = malloc(sizeof(float)* training_lvl);
struct Training* t = createTraining(training_lvl);
int iter = 0;
char* token = strtok(copiedline, ",");
unsigned int level = atoi(token);
token = strtok(NULL, ",");
boost_value[iter] = atof(token);
t->value[iter] = atof(token);
token = strtok(NULL, "\r");
boost_type[iter] = strdup(token);
t->type[iter] = strdup(token);
free(copiedline);
while(level < training_lvl){
if (fgets(line, sizeof(line), file) == NULL) {
fclose(file);
return;
return NULL;
}
iter++;
copiedline = strdup(line);
token = strtok(copiedline, ",");
level = atoi(token);
token = strtok(NULL, ",");
boost_value[iter] = atof(token);
t->value[iter] = atof(token);
token = strtok(NULL, "\r");
boost_type[iter] = strdup(token);
t->type[iter] = strdup(token);
free(copiedline);
}
//applyTraining(o,boost_type,boost_value,training_lvl);
fclose(file);
free(boost_type);
free(boost_value);
return;
}
/*
void applyTraining(struct Object* o,const char** boost_type,const float* boost_value,const int training_lvl) {
if(o == NULL || boost_type == NULL || boost_value == NULL){
return;
}
float p_base_stat = 1;
float p_range = 1;
float p_fire_dmg = 1;
float p_cool_down = 1;
float p_projectile_speed = 1;
float p_crit_hit_chance = 1;
float p_crit_hit_dmg = 1;
float p_frost_dmg = 1;
float p_duration = 1;
/*float p_heal_reduction = 1;
float p_frost = 1;
float p_turret_rotation = 1;
float p_healing_block = 1;
float p_rmv_stun = 1;
float p_rmv_frost = 1; //No implementation, see a bit lower in the swich case
int flat_base_stat = 0;
int flat_range = 0;
int flat_duration = 0;
char* boost_type_cpy = "";
float boost_value_copy = 0;
for(int i = 0; i < training_lvl; i++){
boost_type_cpy = strdup(boost_type[i]);
boost_value_copy = boost_value[i];
switch (boost_type_cpy){
case 'BASE_STAT':
if(boost_value_copy < 1){
p_base_stat += boost_value_copy;
}else{
flat_base_stat += boost_value_copy;
}
break;
case 'RANGE':
if(boost_value_copy < 1){
p_range += boost_value_copy;
}else{
flat_range += boost_value_copy;
}
break;
case 'FIRE_DAMAGE':
if(boost_value_copy < 1){
p_fire_dmg += boost_value_copy;
}else{
printf("check training, there is a mistake either in the code or in the training dataset\n");
return;
}
break;
case 'COOL_DOWN':
if(boost_value_copy < 1){
p_cool_down += boost_value_copy;
}else{
printf("check training, there is a mistake either in the code or in the training dataset\n");
return;
}
break;
case 'PROJECTILE_SPEED':
if(boost_value_copy < 1){
p_projectile_speed += boost_value_copy;
}else{
printf("check training, there is a mistake either in the code or in the training dataset\n");
return;
}
break;
case 'CRITICAL_HIT_CHANCE':
if(boost_value_copy < 1){
p_crit_hit_chance += boost_value_copy;
}else{
printf("check training, there is a mistake either in the code or in the training dataset\n");
return;
}
break;
case 'CRITICAL_HIT_DAMAGE':
if(boost_value_copy < 1){
p_crit_hit_dmg += boost_value_copy;
}else{
printf("check training, there is a mistake either in the code or in the training dataset\n");
return;
}
break;
case 'FROST_DAMAGE':
if(boost_value_copy < 1){
p_frost_dmg += boost_value_copy;
}else{
printf("check training, there is a mistake either in the code or in the training dataset\n");
return;
}
break;
case 'DURATION':
if(boost_value_copy < 1){
p_duration += boost_value_copy;
}else{
flat_duration += boost_value_copy;
}
break;
case 'HEALING_BLOCK': //
break; //
case 'REMOVE_STUN': //
break; // Not implemented and won't until I find it usefull
case 'REMOVE_FROST': //
break; //
case 'FROST_EFFECT': //
break; //
case 'TURET_ROTATION': //
break; //
case 'HEALING_REDUCTION': //
break; //
default:
printf("The training type: %s is unknown\n", boost_type_cpy);
}
free(boost_type_cpy);
}
}
*/
return t;
}

View File

@@ -1,7 +1,13 @@
#ifndef READTRAINING_H
#define READTRAINING_H
void readTraining(struct Object* o,char* object_name);
void applyTraining(struct Object* o,char** boost_type,float* boost_value, int training_lvl);
struct Training {
char** type;
float* value;
};
struct Training* readTraining(struct Object* o,char* object_name);
#endif

Binary file not shown.

View File

@@ -18,7 +18,7 @@
* @param defence The defence points of the ship
* @return Pointer to the initialized Ship struct or NULL if memory allocation fails
*/
struct Ship* initialiseShip(char* ship_name, float speed, float ship_agility, float turret_agility, unsigned int hit_points, unsigned int defence) {
struct Ship* initialiseShip(char* ship_name, float speed, int ship_agility, int turret_agility, unsigned int hit_points, unsigned int defence) {
// Allocate memory for the Ship struct
struct Ship* ship = malloc(sizeof(struct Ship));
if (ship == NULL) {
@@ -146,8 +146,8 @@ void printShipStats(struct Ship* s){
}
printShipName(s);
printf("Gear score : unsuported Health : %d\n",s->hit_points);
printf("Turret agility : %.2f Speed : %.2f\n",s->turret_agility,s->speed);
printf("Ship agility : %.2f Defense : %d\n",s->ship_agility,s->defence);
printf("Turret agility : %.d Speed : %.2f\n",s->turret_agility,s->speed);
printf("Ship agility : %.d Defense : %d\n",s->ship_agility,s->defence);
}
/**
@@ -162,11 +162,12 @@ void printLoadout(struct Ship* s){
int count = 0;
for(int i = 0; i < 5; i++){
struct Object* iter = s->loadout[i]->head;
while (iter->next != NULL){
while (iter != NULL){
printObject(iter);
if(count%4 == 0){
printf("\n");
}
iter = iter->next;
}
}
}

14
ships.h
View File

@@ -1,23 +1,21 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include "objects.h"
#ifndef SHIPS_H
#define SHIPS_H
#include<stdbool.h>
#include "objects.h"
struct Ship{
struct Slots* loadout [5];
char* name;
float speed;
float ship_agility;
float turret_agility;
int ship_agility;
int turret_agility;
unsigned int gear_score;
unsigned int hit_points;
unsigned int defence;
};
struct Ship* initialiseShip(char* ship_name, float speed, float ship_agility, float turet_agility, unsigned int hit_points, unsigned int defence);
struct Ship* initialiseShip(char* ship_name, float speed, int ship_agility, int turet_agility, unsigned int hit_points, unsigned int defence);
void setUpShip(struct Ship* s, unsigned int gear[5][2]);
bool addOject(struct Ship* s, struct Object* o);