ajout lecture des fichiers d'objets

This commit is contained in:
komiko44240BB
2024-09-09 07:08:40 +02:00
parent 982b37f0db
commit 7de616dc41
6 changed files with 137 additions and 10 deletions

4
main.c
View File

@@ -5,10 +5,12 @@
#include <unistd.h>
#include "menu.h"
#include "readobjects.h"
int main() {
start();
//readObjectFile(1,"standard_cannon","legendary");
start();
return 0;
}

8
menu.c
View File

@@ -363,7 +363,7 @@ void addObjects(struct Ship* s) {
read_file = true;
break;
case 6:
saveShipAs(s);
//saveShipAs(s);
read_file = false;
return;
case 0:
@@ -377,9 +377,7 @@ void addObjects(struct Ship* s) {
}
}
if(read_file){
char* object_file_name = malloc(strlen(object_name) + strlen(rarity) + 5 + 1);
sprintf(object_file_name, "%s_%s.csv", object_name, rarity);
struct Object* o = readObjectFile(object_file_name, object_type);
struct Object* o = readObjectFile(object_type,object_name,rarity);
printf("------------------------\n");
printf("What is the level of training of your object ?\n");
printf("------------------------\n");
@@ -390,7 +388,7 @@ void addObjects(struct Ship* s) {
if(training_lvl > 0){
char* training_file_name = malloc(strlen(object_name) + 8 + 5 + 1);
sprintf(training_file_name, "%s_training.csv", object_name);
readAndApplyTraining(o,training_file_name, training_lvl);
//readAndApplyTraining(o,training_file_name, training_lvl);
printf("Training aplied succesfully\n");
}
addOject(s,o);

View File

@@ -105,6 +105,13 @@ void deleteObject(struct Object* o) {
}
// free the rarity string
free(o->rarity);
free(o->name);
// free the current object
free(o);
}
void editObjectType(struct Object* o, unsigned int type) {
type--;
o->type = type;
}

View File

@@ -27,7 +27,7 @@ struct Object {
struct Slots {
char type;
int type;
unsigned int slot_amount;
unsigned int occipied_slots;
unsigned int slot_points;
@@ -40,6 +40,8 @@ struct Object* createObject(char* rarity,char* name ,unsigned int level, float b
int getObjectType(struct Object* o);
int getObjectPoints(struct Object* o);
void editObjectType(struct Object* o, unsigned int type);
void printObject(struct Object* o);
void deleteObject(struct Object* o);

View File

@@ -1,6 +1,124 @@
#include "readobjects.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include <unistd.h>
#include "readobjects.h"
#include "menu.h"
#include "objects.h"
#define MAX_LINE_SIZE 4096
/**
* Reads object data from a CSV file based on the provided object type, name, and rarity.
*
* The function prompts the user for the object's level and retrieves data from a file formatted
* as "<object_name>_<rarity>.csv". It then extracts and processes the object's properties
* corresponding to the specified level.
*
* @param object_type The type/category of the object (used to store it in the ship acordingly).
* @param object_name The name of the object (used to form the filename).
* @param rarity The rarity of the object (used to form the filename).
* @return A pointer to a dynamically allocated Object structure containing the
* object's properties, or NULL if an error occurs.
*/
struct Object* readObjectFile(unsigned int object_type, char* object_name, char* rarity) {
// Loop to prompt user for object level until a valid input is provided
while (true){
printf("------------------------\n");
printf("What level is the object ?\n");
printf("(Between 1 and 50)\n");
printf("------------------------\n");
printf("Your choice: ");
clearInputBuffer(); // Clear any leftover input from previous user inputs
int object_lvl = 0;
scanf("%d", &object_lvl); // Get user input for object level
struct Object* readObjectFile(char* object_file_name, unsigned int object_type) {
// Validate the level input
if(object_lvl < 0 || object_lvl > 50){
printf("Invalid choice, please try again\n");
}else{
// Allocate memory for the filename using object name and rarity
char* object_file_name = malloc(strlen(object_name) + strlen(rarity) + 16 + 5 + 1);
if(object_file_name == NULL){
printf("error malloc object_file_name\n");
return NULL;
}
sprintf(object_file_name, "../Weapons_data/%s_%s.csv", object_name, rarity); // Create filename in the format: "<object_name>_<rarity>.csv"
// Try to open the CSV file for reading
FILE* file = fopen(object_file_name, "r");
if (file == NULL) {
printf("Error opening \"%s\", file does not exist\n", object_file_name);
return NULL; // Return NULL if the file cannot be opened
}
char line[MAX_LINE_SIZE];
char* copiedline = NULL;
// Read the first line to get the slot points, if file is not empty
if (fgets(line, sizeof(line), file) == NULL) {
fclose(file);
return NULL; // Return NULL if there's an issue reading the file
}
copiedline = malloc(strlen(line) + 1);
if (copiedline == NULL) {
fclose(file);
printf("Malloc copied line failed\n");
return NULL; // Return NULL if memory allocation fails
}
strcpy(copiedline, line); // Copy the line
unsigned int slot_points = atoi(copiedline); // Convert the line to an integer for slot points
free(copiedline); // Free allocated memory after use
// Skip lines to reach the desired object level
for(int i = 0; i < object_lvl; i++){
if (fgets(line, sizeof(line), file) == NULL) {
fclose(file);
return NULL; // Return NULL if reading the next line fails
}
}
// Process the line containing the object's attributes for the specified level
copiedline = malloc(strlen(line) + 1);
if (copiedline == NULL) {
printf("Malloc copied line failed(2)\n");
fclose(file);
return NULL; // Return NULL if memory allocation fails
}
strcpy(copiedline, line); // Copy the line
// Parse the CSV line, using semicolon ';' as the delimiter
char* token = strtok(copiedline, ";");
unsigned int level = atoi(token); // Object level
token = strtok(NULL, ";");
float base_stat = atof(token); // Base stat of the object
token = strtok(NULL, ";");
float cool_down = atof(token); // Cooldown time
token = strtok(NULL, ";");
float range = atof(token); // Object's range
token = strtok(NULL, ";");
float projectile_speed = atof(token); // Speed of the projectile
token = strtok(NULL, ";");
float critical_hit_chance= atof(token); // Critical hit chance percentage
token = strtok(NULL, ";");
float critcal_hit_multiplier= atof(token); // Critical hit damage multiplier
token = strtok(NULL, ";");
float radius = atof(token); // Radius of effect
token = strtok(NULL, ";");
float spread = atof(token); // Spread of the projectiles
token = strtok(NULL, ";");
float amount_of_projectiles = atof(token); // Number of projectiles
printf("test2\n");
token = strtok(NULL, "\n");
float duration= atof(token); // Duration of the effect
printf("test3\n");
// Create a new Object instance using the parsed data
struct Object* o = createObject(rarity,object_name,level,base_stat,cool_down,range,projectile_speed,critical_hit_chance,critcal_hit_multiplier,radius,spread,amount_of_projectiles,duration,slot_points,1);
// Edit the object's type based on the provided object_type parameter
editObjectType(o,object_type);
// Return the newly created object
return o;
}
}
}

View File

@@ -1,6 +1,6 @@
#ifndef READOBJECTS_H
#define READOBJECTS_H
struct Object* readObjectFile(char* object_file_name, unsigned int object_type);
struct Object* readObjectFile(unsigned int object_type, char* object_name, char* rarity);
#endif