Skip to content

Commit

Permalink
ZappyServer: move function to distinct file (+debug)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jun 19, 2023
1 parent 388e687 commit a303934
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/SERVER/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ SRC_TRNT := \
trantorien_add_act_broadcast.c \
trantorien_add_act_set.c \
trantorien_add_act_take.c \
trantorien_check_incantation.c \
trantorien_destroy.c \
trantorien_init.c \
trantorien_is_cmd_availible.c \
Expand Down
2 changes: 2 additions & 0 deletions src/SERVER/include/zappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#define NB_FREQ_BEFORE_RESOURCE 20

extern const int level_ressources[LVL_MAX - 1][PLAYER];

struct zappy_s {
ntw_t *ntw;
map_t *map;
Expand Down
17 changes: 8 additions & 9 deletions src/SERVER/src/trantorien/broadcasts/incantation_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,26 @@ static void process_start_incant_trnt(ntw_client_t *cl, list_t *list_trnt,
trnt->incantation = ref_trnt->actions[0];
}

static void broadcast_incantation(trantorien_t *ref_trantorien, ntw_t *ntw,
static void broadcast_incantation(trantorien_t *ref_tr, ntw_t *ntw,
list_t *list_trnt)
{
int trantorien_lvl = ref_trantorien->level;
int trantorien_lvl = ref_tr->level;
client_t *cl = NULL;
ntw_client_t *ntw_cl = NULL;
trantorien_t *trantorien = NULL;
trantorien_t *tr = NULL;

for (L_EACH(client, ntw->clients)) {
ntw_cl = L_DATA(client);
cl = L_DATA(ntw_cl);
if (cl == NULL || cl->type != AI) {
continue;
}
trantorien = cl->cl.ai.trantorien;
if (trantorien != NULL && trantorien->level == trantorien_lvl
&& ref_trantorien->x == trantorien->x
&& ref_trantorien->y == trantorien->y) {
process_start_incant_trnt(ntw_cl, list_trnt, trantorien,
ref_trantorien);
tr = cl->cl.ai.trantorien;
if (tr == NULL || tr->level != trantorien_lvl || ref_tr->x != tr->x ||
ref_tr->y != tr->y) {
continue;
}
process_start_incant_trnt(ntw_cl, list_trnt, tr, ref_tr);
}
}

Expand Down
68 changes: 1 addition & 67 deletions src/SERVER/src/trantorien/commands/command_incantation.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,7 @@
#include "client.h"
#include "command_reponses.h"

static const char *format_error_too_many_level = "Incantation: too many"
"trantoriens at level %d\n";

static const char *format_error_too_less_level = "Incantation: too less "
"trantoriens at level %d\n";

static const char *format_error_resource = "Incantation: too less "
"resources of %d (on case: %d)\n";

static const char *format_error_err = "Incantation: error\n";

static const int level_ressources[LVL_MAX - 1][PLAYER] = {
const int level_ressources[LVL_MAX - 1][PLAYER] = {
{1, 0, 0, 0, 0, 0},
{1, 1, 1, 0, 0, 0},
{2, 0, 1, 0, 2, 0},
Expand All @@ -36,61 +25,6 @@ static const int level_ressources[LVL_MAX - 1][PLAYER] = {
{2, 2, 2, 2, 2, 1},
};

static const int nb_level_players[LVL_MAX - 1] = {
1, 2, 2, 4, 4, 6, 6
};

static bool check_incantation_lvl_availability(trantorien_t *ref_trnt,
ntw_t *ntw)
{
int nb_trantorien_lvl = 0;
int trantorien_lvl = ref_trnt->level;
client_t *cl = NULL;
trantorien_t *trnt = NULL;

for (L_EACH(client, ntw->clients)) {
cl = L_DATA(L_DATAT(ntw_client_t *, client));
if (cl == NULL || cl->type != AI || cl->cl.ai.trantorien == NULL) {
continue;
}
trnt = cl->cl.ai.trantorien;
if (trnt->level == ref_trnt->level && ref_trnt->x == trnt->x
&& ref_trnt->y == trnt->y) {
nb_trantorien_lvl += 1;
}
}
if (nb_trantorien_lvl < nb_level_players[trantorien_lvl - 1]) {
fprintf(stderr, format_error_too_less_level, trantorien_lvl);
} else if (nb_trantorien_lvl > nb_level_players[trantorien_lvl - 1]) {
fprintf(stderr, format_error_too_many_level, trantorien_lvl);
}
return (nb_trantorien_lvl == nb_level_players[trantorien_lvl - 1]);
}

bool check_incantation_availability(trantorien_t *trantorien, map_t *map,
ntw_t *ntw)
{
int map_i = 0;

if (trantorien == NULL || map == NULL || ntw == NULL) {
fprintf(stderr, "%s", format_error_err);
return false;
}
map_index_x_y_to_i(map, trantorien->x, trantorien->y, &map_i);
for (int i = FOOD; i < PLAYER; i++) {
if (map->tiles[map_i].ressources[i + LINEMATE] <
level_ressources[trantorien->level - 1][i]) {
fprintf(stderr, format_error_resource, i + LINEMATE,
map->tiles[map_i].ressources[i + LINEMATE]);
return false;
}
}
if (check_incantation_lvl_availability(trantorien, ntw) == false) {
return false;
}
return true;
}

static void update_level_trantoriens(ntw_t *ntw, int lvl, trantorien_t *tr)
{
client_t *cl = NULL;
Expand Down
81 changes: 81 additions & 0 deletions src/SERVER/src/trantorien/trantorien_check_incantation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdbool.h>
#include <stdio.h>
#include "ntw.h"
#include "client.h"
#include "trantorien.h"
#include "zappy.h"

static const char *format_error_too_many_level = "Incantation: too many "
"trantoriens at level %d"
"initial trantorien ID: %d\n";

static const char *format_error_too_less_level = "Incantation: too less "
"trantoriens at level %d; "
"initial trantorien ID: %d\n";

static const char *format_error_resource = "Incantation: too less "
"resources of %d (on case: %d); "
"initial trantorien ID: %d\n";

static const char *format_error_err = "Incantation: error; "
"initial trantorien ID: %d\n";

static const int nb_level_players[LVL_MAX - 1] = {
1, 2, 2, 4, 4, 6, 6
};

static void print_debug_error(int nb_tr_case, int lvl, int id)
{
if (nb_tr_case < nb_level_players[lvl - 1]) {
fprintf(stderr, format_error_too_less_level, lvl, id);
} else if (nb_tr_case > nb_level_players[lvl - 1]) {
fprintf(stderr, format_error_too_many_level, lvl, id);
}
}

static bool check_incantation_lvl_availability(trantorien_t *ref_tr,
ntw_t *ntw)
{
int nb_trantorien_lvl = 0;
int ref_tr_lvl = ref_tr->level;
client_t *cl = NULL;
trantorien_t *tr = NULL;

for (L_EACH(client, ntw->clients)) {
cl = L_DATA(L_DATAT(ntw_client_t *, client));
if (cl == NULL || cl->type != AI || cl->cl.ai.trantorien == NULL) {
continue;
}
tr = cl->cl.ai.trantorien;
if (tr->level == ref_tr_lvl && ref_tr->x == tr->x &&
ref_tr->y == tr->y) {
nb_trantorien_lvl += 1;
}
}
print_debug_error(nb_trantorien_lvl, ref_tr_lvl, ref_tr->id);
return (nb_trantorien_lvl == nb_level_players[ref_tr_lvl - 1]);
}

bool check_incantation_availability(trantorien_t *trantorien, map_t *map,
ntw_t *ntw)
{
int map_i = 0;

if (trantorien == NULL || map == NULL || ntw == NULL) {
fprintf(stderr, format_error_err, trantorien->id);
return false;
}
map_index_x_y_to_i(map, trantorien->x, trantorien->y, &map_i);
for (int i = FOOD; i < PLAYER; i++) {
if (map->tiles[map_i].ressources[i + LINEMATE] <
level_ressources[trantorien->level - 1][i]) {
fprintf(stderr, format_error_resource, i + LINEMATE,
map->tiles[map_i].ressources[i + LINEMATE], trantorien->id);
return false;
}
}
if (check_incantation_lvl_availability(trantorien, ntw) == false) {
return false;
}
return true;
}
16 changes: 8 additions & 8 deletions src/SERVER/src/trantorien/trantorien_reduce_freq.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ static void move_up(trantorien_t *trantorien, zappy_t *zappy,
trantorien->actions[i - 1] = trantorien->actions[i];
}
trantorien->actions[NB_PARALLEL_ACTION - 1] = NULL;
if (trantorien->actions[0] == NULL) {
if (trantorien->actions[0] == NULL ||
trantorien->actions[0]->code != INCANTATION) {
return;
}
if (trantorien->actions[0]->code == INCANTATION) {
if (broadcast_incantation_start(trantorien, zappy) == false) {
circular_buffer_write(cl->write_to_outside, KO_RESPONSE);
action_destroy(trantorien->actions[0]);
trantorien->actions[0] = NULL;
move_up(trantorien, zappy, cl);
}
if (broadcast_incantation_start(trantorien, zappy) == true) {
return;
}
circular_buffer_write(cl->write_to_outside, KO_RESPONSE);
action_destroy(trantorien->actions[0]);
trantorien->actions[0] = NULL;
move_up(trantorien, zappy, cl);
}

void trantorien_reduce_freq(trantorien_t *trantorien, zappy_t *zappy,
Expand Down

0 comments on commit a303934

Please sign in to comment.