Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate map #93

Merged
merged 11 commits into from
May 21, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ target/
coding-style-reports.log
.cache
compile_commands.json
*gc*
18 changes: 12 additions & 6 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ NAME = ../zappy_server
SRC = $(BASE_SRC)
BASE_SRC = $(addsuffix .c, \
$(addprefix src/, \
main \
MartinFillon marked this conversation as resolved.
Show resolved Hide resolved
$(addprefix cmd_line/, \
display_help \
handle_cmd_line \
handle_nb_flags \
handle_name_flag \
)\
$(addprefix utils/, \
logger_error \
free_box \
$(addprefix utils/, \
logger_error \
free_box \
)\
$(addprefix map/, \
initializer \
destroyer \
drop_on_tile \
take_on_tile \
)\
$(addprefix queue/, \
create \
Expand All @@ -38,6 +43,7 @@ BASE_SRC = $(addsuffix .c, \
)\
)\

SRC += src/main.c

OBJ = $(SRC:.c=.o)

Expand All @@ -59,8 +65,8 @@ fclean: clean
rm -f $(TEST_NAME)

$(TEST_NAME): LDFLAGS += -lcriterion
$(TEST_NAME): $(SRC) $(TEST_SRC)
$(CC) $(SRC) $(TEST_SRC) --coverage $(LDFLAGS) \
$(TEST_NAME): $(BASE_SRC) $(TEST_SRC)
$(CC) $(BASE_SRC) $(TEST_SRC) --coverage $(LDFLAGS) \
-o $(TEST_NAME) $(CPPFLAGS) $(CFLAGS)

tests_run: $(TEST_NAME)
Expand Down
14 changes: 0 additions & 14 deletions server/include/define.h

This file was deleted.

22 changes: 22 additions & 0 deletions server/include/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
** EPITECH PROJECT, 2024
** Zappy
** File description:
** define
*/

#pragma once

#define ERROR -1
#define SUCCESS 0
#define SERV_END 1

#define EPI_ERROR 84

#define FOOD_DENSITY 0.5
#define LINEMATE_DENSITY 0.3
#define DERAUMERE_DENSITY 0.15
#define SIBUR_DENSITY 0.1
#define MENDIANE_DENSITY 0.1
#define PHIRAS_DENSITY 0.08
#define THYSTAME_DENSITY 0.05
45 changes: 45 additions & 0 deletions server/include/map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
** EPITECH PROJECT, 2024
** Zappy
** File description:
** map
*/

#pragma once
MartinFillon marked this conversation as resolved.
Show resolved Hide resolved

#include "types/map.h"

/**
* @brief Create a map object
*
* @param x x size of the map
* @param y y size of the map
* @return map_t* the map object
*/
map_t *create_map(size_t x, size_t y);

/**
* @brief Fill the map with random values
*
* @param map the map object
*/
void fill_map(map_t *map);

/**
* @brief Destroy the map object
*
* @param map the map object
*/
void destroy_map(map_t *map);

/**
* @brief Take an item on a tile
*
* @param map the map object
* @param x x position of the tile
* @param y y position of the tile
* @param obj the object to take
* @return true if the object was taken
* @return false if the object was not taken
*/
bool take_item(map_t *map, size_t x, size_t y, enum object_e obj);
10 changes: 10 additions & 0 deletions server/include/types/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
#include <stdbool.h>
#include <stdlib.h>

enum object_e {
FOOD,
LINEMATE,
DERAUMERE,
SIBUR,
MENDIANE,
PHIRAS,
THYSTAME,
};

struct tile_s {
bool occupied;

Expand Down
7 changes: 5 additions & 2 deletions server/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
*/

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "args_info.h"
#include "macros.h"
#include "server.h"
#include "utils.h"
#include "define.h"
#include "args_info.h"

int main(int ac, char const **av)
{
args_infos_t args = {0};

srand(time(NULL));
if (ac == 2 && strcmp(av[1], "-help") == 0) {
display_help();
return SUCCESS;
Expand Down
16 changes: 16 additions & 0 deletions server/src/map/destroyer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
** EPITECH PROJECT, 2024
** Zappy
** File description:
** destroyer
*/

#include "types/map.h"

void destroy_map(map_t *map)
{
for (size_t i = 0; i < map->x; i++) {
free(map->arena[i]);
}
free(map);
}
67 changes: 67 additions & 0 deletions server/src/map/drop_on_tile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
** EPITECH PROJECT, 2024
** Zappy
** File description:
** take_on_tile
*/

#include <stdlib.h>

#include "types/map.h"

static void drop_food(map_t *map, size_t x, size_t y)
{
map->arena[y][x].content.food += 1;
}

static void drop_linemate(map_t *map, size_t x, size_t y)
{
map->arena[y][x].content.linemate += 1;
}

static void drop_deraumere(map_t *map, size_t x, size_t y)
{
map->arena[y][x].content.deraumere += 1;
}

static void drop_sibur(map_t *map, size_t x, size_t y)
{
map->arena[y][x].content.sibur += 1;
}

static void drop_mendiane(map_t *map, size_t x, size_t y)
{
map->arena[y][x].content.mendiane += 1;
}

static void drop_phiras(map_t *map, size_t x, size_t y)
{
map->arena[y][x].content.phiras += 1;
}

static void drop_thystame(map_t *map, size_t x, size_t y)
{
map->arena[y][x].content.thystame += 1;
}

void drop_item(map_t *map, size_t x, size_t y, enum object_e obj)
{
if (x >= map->x || y >= map->y)
return;
switch (obj) {
case FOOD:
return drop_food(map, x, y);
case LINEMATE:
return drop_linemate(map, x, y);
case DERAUMERE:
return drop_deraumere(map, x, y);
case SIBUR:
return drop_sibur(map, x, y);
case MENDIANE:
return drop_mendiane(map, x, y);
case PHIRAS:
return drop_phiras(map, x, y);
case THYSTAME:
return drop_thystame(map, x, y);
}
}
126 changes: 126 additions & 0 deletions server/src/map/initializer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
** EPITECH PROJECT, 2024
** Zappy
** File description:
** initializer
*/

#include <stdlib.h>

#include "macros.h"
#include "map.h"
#include "types/map.h"

map_t *create_map(size_t x, size_t y)
{
map_t *map = calloc(1, sizeof(map_t));

if (map == NULL)
return NULL;
map->x = x;
map->y = y;
map->arena = calloc(y + 1, sizeof(struct tile_s *));
if (map->arena == NULL)
return NULL;
for (size_t i = 0; i < y; i++) {
map->arena[i] = calloc(x + 1, sizeof(struct tile_s));
if (map->arena[i] == NULL)
return NULL;
}
return map;
}

static void fill_food(map_t *map, size_t number)
{
int x = 0;
int y = 0;

for (size_t i = 0; i < number; i++) {
x = rand() % map->x;
y = rand() % map->y;
map->arena[y][x].content.food += 1;
}
}

static void fill_deraumere(map_t *map, size_t number)
{
int x = 0;
int y = 0;

for (size_t i = 0; i < number; i++) {
x = rand() % map->x;
y = rand() % map->y;
map->arena[y][x].content.deraumere += 1;
}
}

static void fill_linemate(map_t *map, size_t number)
{
int x = 0;
int y = 0;

for (size_t i = 0; i < number; i++) {
x = rand() % map->x;
y = rand() % map->y;
map->arena[y][x].content.linemate += 1;
}
}

static void fill_sibur(map_t *map, size_t number)
{
int x = 0;
int y = 0;

for (size_t i = 0; i < number; i++) {
x = rand() % map->x;
y = rand() % map->y;
map->arena[y][x].content.sibur += 1;
}
}

static void fill_mendiane(map_t *map, size_t number)
{
int x = 0;
int y = 0;

for (size_t i = 0; i < number; i++) {
x = rand() % map->x;
y = rand() % map->y;
map->arena[y][x].content.mendiane += 1;
}
}

static void fill_phiras(map_t *map, size_t number)
{
int x = 0;
int y = 0;

for (size_t i = 0; i < number; i++) {
x = rand() % map->x;
y = rand() % map->y;
map->arena[y][x].content.phiras += 1;
}
}

static void fill_thystame(map_t *map, size_t number)
{
int x = 0;
int y = 0;

for (size_t i = 0; i < number; i++) {
x = rand() % map->x;
y = rand() % map->y;
map->arena[y][x].content.thystame += 1;
}
}

void fill_map(map_t *map)
{
fill_food(map, map->x * map->y * FOOD_DENSITY);
fill_deraumere(map, map->x * map->y * DERAUMERE_DENSITY);
fill_linemate(map, map->x * map->y * LINEMATE_DENSITY);
fill_mendiane(map, map->x * map->y * MENDIANE_DENSITY);
fill_sibur(map, map->x * map->y * SIBUR_DENSITY);
fill_phiras(map, map->x * map->y * PHIRAS_DENSITY);
fill_thystame(map, map->x * map->y * THYSTAME_DENSITY);
}
Loading
Loading