From 42bf216c8c6f059e33a6bff29b3f71a90a08fb3d Mon Sep 17 00:00:00 2001 From: bgiraudr Date: Tue, 3 May 2022 00:40:09 +0200 Subject: [PATCH] Begin types --- CMakeLists.txt | 2 + assets-cg/converters.py | 38 +++++++++++++++++-- assets-cg/fxconv-metadata.txt | 4 ++ assets-cg/table_type.csv | 5 +++ include/define.h | 3 +- include/talkable.h | 3 +- include/type.h | 20 ++++++++++ src/engine.c | 7 +++- src/talkable.c | 16 ++++++-- src/type.c | 69 +++++++++++++++++++++++++++++++++++ 10 files changed, 156 insertions(+), 11 deletions(-) create mode 100644 assets-cg/table_type.csv create mode 100644 include/type.h create mode 100644 src/type.c diff --git a/CMakeLists.txt b/CMakeLists.txt index beeb2c5..407673b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ set(SOURCES src/event.c src/inventory.c src/item.c + src/type.c ) set(ASSETS_cg @@ -70,6 +71,7 @@ set(ASSETS_cg assets-cg/items/items.txt assets-cg/items/item.png assets-cg/items/item2.png + assets-cg/table_type.csv ) fxconv_declare_assets(${ASSETS} ${ASSETS_fx} ${ASSETS_cg} WITH_METADATA) diff --git a/assets-cg/converters.py b/assets-cg/converters.py index ae7ebb9..e29c34d 100644 --- a/assets-cg/converters.py +++ b/assets-cg/converters.py @@ -1,6 +1,7 @@ import fxconv import json import pathlib +import csv def convert(input, output, params, target): if params["custom-type"] == "map": @@ -18,6 +19,9 @@ def convert(input, output, params, target): elif params["custom-type"] == "items": convert_items(input, output, params, target) return 0 + elif params["custom-type"] == "table_type": + convert_table_type(input, output, params, target) + return 0 else: return 1 @@ -116,9 +120,7 @@ def convert_map(input, output, params, target): for x in range(w*h): for i in range(nbTilelayer): value = tile_value.get(data["layers"][i]["data"][x]) - if value == TILE_SOLID: - maxValue = TILE_SOLID - break + #attention : priorité aux valeurs hautes : un bloc d'herbe sera prioritaire sur un bloc solide if value == None: value = TILE_AIR if value > maxValue: maxValue = value if value == TILE_BRIDGE: @@ -315,4 +317,32 @@ def convert_items(input, output, params, target): except KeyError: raise Exception(f"convert_items() : L'item {data['name']} est mal configuré") - fxconv.elf(items, output, "_" + params["name"], **target) \ No newline at end of file + fxconv.elf(items, output, "_" + params["name"], **target) + +def convert_table_type(input, output, params, target): + data = csv.DictReader(open(input,'r')) + + table_type = fxconv.Structure() + for i in data: + type = fxconv.Structure() + type += fxconv.string(i["type"]) + type += fxconv.u32(list(i).index(i["type"])) + + taille = len(i) + b,l,n = [],[],[] + for j in i: + id = list(i).index(j) + if(i[j]=="2"): b.append(id) + if(i[j]=="0,5"): l.append(id) + if(i[j]=="0"): n.append(id) + for a in range(len(b),taille):b.append(0) + for a in range(len(l),taille):l.append(0) + for a in range(len(n),taille):n.append(0) + + type += b"".join(fxconv.u32(value) for value in b) + type += b"".join(fxconv.u32(value) for value in l) + type += b"".join(fxconv.u32(value) for value in n) + + table_type += fxconv.ptr(type) + + fxconv.elf(table_type, output, "_" + params["name"], **target) \ No newline at end of file diff --git a/assets-cg/fxconv-metadata.txt b/assets-cg/fxconv-metadata.txt index f855440..c6cf87f 100644 --- a/assets-cg/fxconv-metadata.txt +++ b/assets-cg/fxconv-metadata.txt @@ -38,3 +38,7 @@ battle_ui.png: player_moves.txt: custom-type: player_moves name:levelupplayer + +table_type.csv: + custom-type: table_type + name:table_type diff --git a/assets-cg/table_type.csv b/assets-cg/table_type.csv new file mode 100644 index 0000000..330c3cb --- /dev/null +++ b/assets-cg/table_type.csv @@ -0,0 +1,5 @@ +type,Administrateur,Modérateur,Rédacteur,Labélisateur +Administrateur,0,1,1,2 +Modérateur,0,"0,5",2,2 +Rédacteur,2,1,"0,5",1 +Labélisateur,2,"0,5",1,0 diff --git a/include/define.h b/include/define.h index cb41b78..d70b41e 100644 --- a/include/define.h +++ b/include/define.h @@ -5,4 +5,5 @@ #define NB_INTERIORS 2 #define NB_PLAYER_MOVES 3 -#define NB_PLAYER_ITEMS 30 \ No newline at end of file +#define NB_PLAYER_ITEMS 30 +#define NB_TYPES 4 \ No newline at end of file diff --git a/include/talkable.h b/include/talkable.h index 246d099..e07cffb 100644 --- a/include/talkable.h +++ b/include/talkable.h @@ -17,4 +17,5 @@ void draw_dialog(struct Game *game); struct Talkable* get_dialog_xy(struct Map *map, int x, int y); char *word_boundary_before(char *str, char *limit); char *skip_spaces(char *str); -void format_text(int x, int y, const int color, char const *format, ...); \ No newline at end of file +void format_text(int x, int y, const int color, char const *format, ...); +void format_text_opt(int x, int y, int width, int height, const int color, char const *format, ...); \ No newline at end of file diff --git a/include/type.h b/include/type.h new file mode 100644 index 0000000..ca000cc --- /dev/null +++ b/include/type.h @@ -0,0 +1,20 @@ +#pragma once +#include "define.h" + +struct Type { + char* name; + const int id; + int buff[NB_TYPES+1]; + int less[NB_TYPES+1]; + int null[NB_TYPES+1]; +}; + +struct Types { + struct Type *type[NB_TYPES]; +}; + +bool parseArray(int array[], char *nom); +float getTypeEffect(struct Type atk, struct Type def); +struct Type getTypeFromName(char* name); +struct Type getTypeFromId(int id); +void drawTypeEffects(struct Type type); \ No newline at end of file diff --git a/src/engine.c b/src/engine.c index 46dd979..ff4410b 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "engine.h" #include "game.h" @@ -17,6 +18,7 @@ #include "inventory.h" #include "item.h" #include "event.h" +#include "type.h" /*draw the current state of the game*/ void engine_draw(struct Game const *game) { @@ -30,7 +32,6 @@ void engine_draw(struct Game const *game) { dprint(1,1,C_WHITE,"%d:%d",game->player->pos.x, game->player->pos.y); dprint(1,20,C_WHITE,"%d",game->player->sprint); - dprint(1,40,C_WHITE,"%d",map_get_player_tile(game)); } void engine_draw_map(struct Game const *game) { @@ -133,7 +134,9 @@ void engine_action(struct Game *game, int action) { open_inventory(game, &game->player->inventory, "Consultation", true); } if(action == ACTION_F2) { - add_item_to_inventory(game, &game->player->inventory, get_item_id(1)); + //add_item_to_inventory(game, &game->player->inventory, get_item_id(1)); + srand(game->player->pos.x * game->player->pos.y); + drawTypeEffects(getTypeFromId(rand_range(1,5))); } if(action == ACTION_OPTN) { draw_stats(game->player->stats); diff --git a/src/talkable.c b/src/talkable.c index 876ac0f..b979284 100644 --- a/src/talkable.c +++ b/src/talkable.c @@ -12,8 +12,6 @@ #include "event.h" #include "player.h" -#define LIMIT 35 - struct Talkable default_value = { .x = 0, .y = 0, @@ -88,8 +86,20 @@ void format_text(int x, int y, const int color, char const *format, ...) { vsnprintf(text_arg, 512, format, args); va_end(args); + format_text_opt(x, y, DIALOG_WIDTH, LINE_HEIGHT, color, text_arg); +} + +void format_text_opt(int x, int y, int width, int height, const int color, char const *format, ...) { + int const DIALOG_WIDTH = width, LINE_HEIGHT = height; + + char text_arg[512]; + va_list args; + va_start(args, format); + vsnprintf(text_arg, 512, format, args); + va_end(args); + char *text = (char *)malloc(strlen(text_arg)+1); - strcpy(text,text_arg); + strcpy(text, text_arg); while(*text) { char *end = (char *)drsize(text, NULL, DIALOG_WIDTH, NULL); diff --git a/src/type.c b/src/type.c new file mode 100644 index 0000000..a45ed9b --- /dev/null +++ b/src/type.c @@ -0,0 +1,69 @@ +#include +#include +#include "type.h" + +#include +#include +#include "util.h" + +extern struct Types table_type; + +struct Type default_type = { + .name = "-", + .id = 0, + .buff = {}, + .less = {}, + .null = {} +}; + +struct Type getTypeFromName(char* name) { + for(int i = 0; i < NB_TYPES; i++) { + if(!strcmp(table_type.type[i]->name, name)) return *table_type.type[i]; + } + return default_type; +} + +struct Type getTypeFromId(int id) { + for(int i = 0; i < NB_TYPES; i++) { + if(table_type.type[i]->id == id) return *table_type.type[i]; + } + return default_type; +} + +bool parseArray(int array[], char *nom) { + struct Type type = getTypeFromName(nom); + for(int i = 0; i < NB_TYPES+1; i++) { + if(array[i] == 0) return false; + if(array[i] == type.id) return true; + } + return false; +} + +float getTypeEffect(struct Type atk, struct Type def) { + if(parseArray(atk.buff, def.name)) return 2; + if(parseArray(atk.less, def.name)) return 0.5; + if(parseArray(atk.null, def.name)) return 0; + return 1; +} + +void drawTypeEffects(struct Type type) { + while(1) { + dclear(C_WHITE); + dprint(120,10,C_RED, "Table de %s", type.name); + dtext(55,40,C_BLUE, "Super efficace"); + dtext(160,40,C_BLUE, "Peu efficace"); + dtext(260,40,C_BLUE, "Non efficace"); + + for(int i = 0; i <= 3; i++) { + for(int j = 0; j <= NB_TYPES; j++) { + if(i==0) dprint(70+100*i, 60+20*j, C_BLACK, "%s", getTypeFromId(type.buff[j]).name); + if(i==1) dprint(70+100*i, 60+20*j, C_BLACK, "%s", getTypeFromId(type.less[j]).name); + if(i==2) dprint(70+100*i, 60+20*j, C_BLACK, "%s", getTypeFromId(type.null[j]).name); + } + } + dupdate(); + pollevent(); + if(keydown(KEY_OPTN)) break; + } + wait_for_input(KEY_SHIFT); +} \ No newline at end of file