From ab67a00d7970db9d2fa84ea451b32b5092b593bd Mon Sep 17 00:00:00 2001 From: bgiraudr Date: Sat, 19 Feb 2022 01:21:38 +0100 Subject: [PATCH] Random monster in zone --- CMakeLists.txt | 3 +- assets-cg/converters.py | 53 +++++++++++++++++-------- assets-cg/maps/testCarte.tmx | 2 + assets-cg/monsters/fxconv-metadata.txt | 8 ++-- assets-cg/monsters/monsters.txt | 0 assets-cg/monsters/test.json | 5 +-- assets-cg/monsters/test2.json | 16 ++++++++ assets-cg/monsters/test2.png | Bin 0 -> 2706 bytes include/map.h | 5 ++- include/monster.h | 7 ++++ src/map.c | 18 +++++++-- src/monster.c | 32 +++++++++++---- src/player.c | 2 +- 13 files changed, 114 insertions(+), 37 deletions(-) create mode 100644 assets-cg/monsters/monsters.txt create mode 100644 assets-cg/monsters/test2.json create mode 100644 assets-cg/monsters/test2.png diff --git a/CMakeLists.txt b/CMakeLists.txt index 29aa970..903fd13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,8 @@ set(ASSETS_cg assets-cg/uf8x9 assets-cg/capacites/capacites.txt assets-cg/monsters/test.png - assets-cg/monsters/test.json + assets-cg/monsters/test2.png + assets-cg/monsters/monsters.txt ) fxconv_declare_assets(${ASSETS} ${ASSETS_fx} ${ASSETS_cg} WITH_METADATA) diff --git a/assets-cg/converters.py b/assets-cg/converters.py index 074d7c4..981e941 100644 --- a/assets-cg/converters.py +++ b/assets-cg/converters.py @@ -187,11 +187,20 @@ def parseZone(layer): zone += fxconv.u32(to[0]) zone += fxconv.u32(to[1]) + monsters = bytes() try: zone += fxconv.u32(i["properties"][0]["value"]) + monster_list = i["properties"][1]["value"].split(";") + zone += fxconv.u32(len(monster_list)) + for j in monster_list: + monsters += fxconv.u16(int(j)) + except IndexError: + raise Exception(f"parseZone() : La zone {origin};{to} n'a pas de monstres associés") except KeyError: print(f"parseZone() : Zone {origin};{to} sans niveau de référence, passage automatique à -1") zone += fxconv.u32(-1) + + zone += fxconv.ptr(monsters) return zone def convert_capa(input, output, params, target): @@ -229,23 +238,35 @@ def convert_capa(input, output, params, target): fxconv.elf(capacities, output, "_" + params["name"], **target) def convert_monster(input, output, params, target): - file = open(input, "r") - data = json.load(file) + liste_file = list(pathlib.Path(input).parent.glob('*.json')) - stats = fxconv.Structure() - if len(data["stats"]) != 6: raise Exception(f"convert_monster : Les statistiques de {data['name']} sont mauvaises") - for i in data["stats"].values(): - stats+=fxconv.u32(i) + monsters = fxconv.Structure() + monsters += fxconv.u32(len(liste_file)) + for f in liste_file: + file = open(f,"r") + data = json.load(file) + stats = fxconv.Structure() - moves = bytes() - for i in data["moves"]: - moves+=fxconv.u16(i) + if len(data["stats"]) != 4: raise Exception(f"convert_monster : Les statistiques de {data['name']} sont mauvaises") + stats+=fxconv.u32(data["stats"]["atk"]) + stats+=fxconv.u32(data["stats"]["def"]) + stats+=fxconv.u32(data["stats"]["pv"]) + stats+=fxconv.u32(1) + stats+=fxconv.u32(data["stats"]["xp"]) + stats+=fxconv.u32(data["stats"]["pv"]) - monster = fxconv.Structure() - monster += fxconv.string(data["name"]) - monster += fxconv.ptr(f"img_{data['sprite']}") - monster += fxconv.u32(len(data["moves"])) - monster += fxconv.ptr(stats) - monster += fxconv.ptr(moves) + moves = bytes() + for i in data["moves"]: + moves+=fxconv.u16(i) - fxconv.elf(monster, output, "_" + params["name"], **target) + monster = fxconv.Structure() + monster += fxconv.string(data["name"]) + monster += fxconv.ptr(f"img_{data['sprite']}") + monster += fxconv.u32(data["id"]) + monster += fxconv.u32(len(data["moves"])) + monster += fxconv.ptr(stats) + monster += fxconv.ptr(moves) + + monsters += fxconv.ptr(monster) + + fxconv.elf(monsters, output, "_" + params["name"], **target) diff --git a/assets-cg/maps/testCarte.tmx b/assets-cg/maps/testCarte.tmx index 8d3030a..501b052 100644 --- a/assets-cg/maps/testCarte.tmx +++ b/assets-cg/maps/testCarte.tmx @@ -205,11 +205,13 @@ + + diff --git a/assets-cg/monsters/fxconv-metadata.txt b/assets-cg/monsters/fxconv-metadata.txt index 74a2ef7..57a725c 100644 --- a/assets-cg/monsters/fxconv-metadata.txt +++ b/assets-cg/monsters/fxconv-metadata.txt @@ -1,7 +1,7 @@ -test.png: +*.png: type: bopti-image - name: img_test + name_regex: (.*)\.png img_\1 -*.json: +monsters.txt: custom-type: monster - name_regex: (.*)\.json monster_\1 + name: monsters diff --git a/assets-cg/monsters/monsters.txt b/assets-cg/monsters/monsters.txt new file mode 100644 index 0000000..e69de29 diff --git a/assets-cg/monsters/test.json b/assets-cg/monsters/test.json index 25bbb0a..87912f6 100644 --- a/assets-cg/monsters/test.json +++ b/assets-cg/monsters/test.json @@ -1,13 +1,12 @@ { "name":"test", "sprite":"test", + "id":1, "stats":{ "atk":10, "def":10, "pv":20, - "level":1, - "xp":300, - "max_pv":20 + "xp":300 }, "moves":[ 0, diff --git a/assets-cg/monsters/test2.json b/assets-cg/monsters/test2.json new file mode 100644 index 0000000..167da9d --- /dev/null +++ b/assets-cg/monsters/test2.json @@ -0,0 +1,16 @@ +{ + "name":"Citron", + "sprite":"test2", + "id":2, + "stats":{ + "atk":80, + "def":5, + "pv":45, + "xp":150 + }, + "moves":[ + 0, + 1, + 2 + ] +} diff --git a/assets-cg/monsters/test2.png b/assets-cg/monsters/test2.png new file mode 100644 index 0000000000000000000000000000000000000000..a328f5a6f6ccce6cdb1d071e276f9952f5420e02 GIT binary patch literal 2706 zcmV;D3T^d?P)TWlQF8OMLKv-YlUaePgjSc!26brMLL1Srs?m75eLs8uUy zQ>r3pD;i3bUQ`}flIMgsMsRzmHAi%?o<(om{A1aT=1C2bub0V^?qU?*|x zIJRf)otZwEwb$#NnRCvZ*;%9fq$u8)I-(sHqyD* zm(DP@ZWogxaTkC0E3WSH3_tmFs5)H`g1Dz?udLyE;2PJZ%Sg~^CTO>9nkQW<=`SA7 zcHq<-(|>pF$M`$f4=~H+KhzX&9OR%ukRUh0R$$xu(qv6@l`Fi9--0 zG02_CN*>8LER8A?FHt%-LFEHtA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@7_#hwiva}4w~mEk8A4k{1F#*CIRq-b%8?%77ZWx=kc#xc?4N#O%o!v`JUH}S zk<@4%vgZw@1#!>OWtbcjJs*-!(0F7A#?sm)49LV$I%8=krN<97`Hc5&8qZO^vTDZM$7iM)G&`&4)^OwOE+@{fDu?kK4v?2I?X&4CJilu zJoq5VpZ=H^tSq0^k)C!U76PUhpcZ{Pe0^%$AUb-2mgZJowQR1?m8KFUsQf}~2i|^% zl;}?_`Jr{ku6vHIbOnbkn`WKq7DMZv+mO7UqPcwy=XIo||K8p+T$ifi?-?g}r?(O@ zSb!fBwXCSVlJ|6_7s;M>_cS+D)p1}#^%z5pl`iaIWx;CHdxfG!?y2|qwQBrQTf#ir*kLT;VAR=^ z^q`t`oKE8+?^Y`B5Y~sNz+)3&FQTb_o!X_Avc&T%Z9{zB?&l9tWuV2HvXETK=j;|r zdj2Yo2|?Fs2md^oYpWLyNTy( zZ^U@{IMtOa`SP&PZdggO(<0E!wukmn(XbVxT*QF#vJg)Ods*7v#&xHU@|Wr~q*K#B zq7ua91Y{f-w_wQzK0o{(|59CAEfotwkN^^=4o^>HE~>>X_X-c<{|${T&N=;Dod5s; M07*qoM6N<$g0li5N&o-= literal 0 HcmV?d00001 diff --git a/include/map.h b/include/map.h index 67870e6..ba8b361 100644 --- a/include/map.h +++ b/include/map.h @@ -12,6 +12,8 @@ struct Zone { int start_x, start_y; int end_x, end_y; int level; + int nbMonsters; + short *monsters; }; struct Map { @@ -53,4 +55,5 @@ struct Vec2 locate_tile(struct Map const *map, int tile); struct Teleporter get_teleporter_xy(struct Map *map, struct Vec2 pos); -int get_level_zone(struct Player *player, struct Map *map); \ No newline at end of file +int is_in_zone(struct Player *player, struct Map *map); +struct Zone get_zone(struct Player *player, struct Map *map); \ No newline at end of file diff --git a/include/monster.h b/include/monster.h index 71bacb1..e9e2e38 100644 --- a/include/monster.h +++ b/include/monster.h @@ -8,11 +8,18 @@ struct Monster { char *name; bopti_image_t *sprite; + int id; int nbMoves; struct Stats *stats; short *moves; }; +struct Monsters { + int nbMonsters; + struct Monster *monster[]; +}; + +struct Monster *get_monster_id(int id); struct Monster *generate_monster(struct Game *game); struct Move *monster_select(struct Player *player, struct Monster *monster); struct Monster *copyMonster(struct Monster *source); diff --git a/src/map.c b/src/map.c index 7b7a738..84ec366 100644 --- a/src/map.c +++ b/src/map.c @@ -67,12 +67,22 @@ struct Teleporter get_teleporter_xy(struct Map *map, struct Vec2 pos) { return map->teleporters[0]; } -int get_level_zone(struct Player *player, struct Map *map) { +int is_in_zone(struct Player *player, struct Map *map) { const int posx = player->pos.x; const int posy = player->pos.y; for(int i = 0; i < map->zone_count; i++) { - const struct Zone zone = map->zones[i]; - if(zone.start_x <= posx && zone.start_y <= posy && zone.end_x >= posx && zone.end_y >= posy) return zone.level; + struct Zone zone = map->zones[i]; + if(zone.start_x <= posx && zone.start_y <= posy && zone.end_x >= posx && zone.end_y >= posy) return 1; } - return -1; + return 0; +} + +struct Zone get_zone(struct Player *player, struct Map *map) { + const int posx = player->pos.x; + const int posy = player->pos.y; + for(int i = 0; i < map->zone_count; i++) { + struct Zone zone = map->zones[i]; + if(zone.start_x <= posx && zone.start_y <= posy && zone.end_x >= posx && zone.end_y >= posy) return zone; + } + return map->zones[0]; } \ No newline at end of file diff --git a/src/monster.c b/src/monster.c index fd375ce..1d045e2 100644 --- a/src/monster.c +++ b/src/monster.c @@ -9,16 +9,33 @@ #include "map.h" #include "util.h" + +#include +#include + +extern struct Monsters monsters; + +struct Monster *get_monster_id(int id) { + for(int i = 0; i < monsters.nbMonsters; i++) { + if(monsters.monster[i]->id == id) return monsters.monster[i]; + } + return monsters.monster[0]; +} + struct Monster *generate_monster(struct Game *game) { - extern struct Monster monster_test; - - struct Monster *monster = copyMonster(&monster_test); - srand(rtc_ticks()); - int level_zone = get_level_zone(game->player, game->map); - //En cas d'erreur / aucune zone trouvée - if(level_zone == -1) { + + int level_zone; + + //set the monster depends of the current zone + struct Monster *monster; + if(is_in_zone(game->player, game->map)) { + struct Zone zone = get_zone(game->player, game->map); + monster = copyMonster(get_monster_id(zone.monsters[rand_range(0,zone.nbMonsters)])); + level_zone = zone.level; + } else { + monster = copyMonster(get_monster_id(0)); level_zone = game->player->stats.level; } @@ -62,6 +79,7 @@ struct Monster *copyMonster(struct Monster *source) { dest->name = source->name; dest->nbMoves = source->nbMoves; dest->sprite = source->sprite; + dest->id = source->id; dest->moves = malloc(dest->nbMoves * sizeof *source->moves); memcpy(dest->moves, source->moves, dest->nbMoves * sizeof *source->moves); diff --git a/src/player.c b/src/player.c index f485256..c54aaec 100644 --- a/src/player.c +++ b/src/player.c @@ -93,7 +93,7 @@ void replace_capacities(struct Player *player, struct Move move) { if(selection > NB_PLAYER_MOVES-1) selection = NB_PLAYER_MOVES-1; if(selection < 0) selection = 0; - // draw_classic_move(200,DHEIGHT/2-30, move); + draw_classic_move(200,DHEIGHT/2-30, &move); for(int i = 0; i < NB_PLAYER_MOVES; i++) { draw_classic_move(0,65*i, player->moves[i]); }