diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-01-27 15:58:53 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-01-27 17:38:11 +0100 |
| commit | 0aef4f319caa2572d459b18e4e994122d53abcbe (patch) | |
| tree | c27f3cee65e565db4bf5f457d03190f227cccdf4 /src/player | |
| parent | 39ecbd690b2327db4328123f3f9a50c8492794ad (diff) | |
| download | jancity-0aef4f319caa2572d459b18e4e994122d53abcbe.tar.gz | |
Remove resource, tech and old game resources
Diffstat (limited to 'src/player')
| -rw-r--r-- | src/player/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/player/inc/human_player.h | 5 | ||||
| -rw-r--r-- | src/player/inc/player.h | 11 | ||||
| -rw-r--r-- | src/player/src/human_player.c | 328 | ||||
| -rw-r--r-- | src/player/src/human_player_gui.c | 197 | ||||
| -rw-r--r-- | src/player/src/player.c | 9 |
6 files changed, 44 insertions, 508 deletions
diff --git a/src/player/CMakeLists.txt b/src/player/CMakeLists.txt index f722a53..3297619 100644 --- a/src/player/CMakeLists.txt +++ b/src/player/CMakeLists.txt @@ -14,8 +14,6 @@ target_link_libraries(player input mouse pad - resource - tech unit util PRIVATE diff --git a/src/player/inc/human_player.h b/src/player/inc/human_player.h index abc25e9..9cfd498 100644 --- a/src/player/inc/human_player.h +++ b/src/player/inc/human_player.h @@ -39,8 +39,7 @@ struct human_player enum sel_type { INSTANCE_TYPE_UNIT, - INSTANCE_TYPE_BUILDING, - INSTANCE_TYPE_RESOURCE + INSTANCE_TYPE_BUILDING } type; union sel_data @@ -61,8 +60,6 @@ struct human_player } target; size_t n_sel; - bool top_gui; - uint32_t gui_res[MAX_RESOURCE_TYPES]; }; int human_player_init(const struct human_player_cfg *cfg, struct human_player *h); diff --git a/src/player/inc/player.h b/src/player/inc/player.h index 153740c..10557b3 100644 --- a/src/player/inc/player.h +++ b/src/player/inc/player.h @@ -5,8 +5,6 @@ #include <camera.h> #include <instance.h> #include <pad.h> -#include <resource.h> -#include <tech.h> #include <unit.h> #include <stdbool.h> #include <stddef.h> @@ -36,13 +34,7 @@ struct player bool alive; struct unit units[PLAYER_MAX_UNITS]; struct building buildings[PLAYER_MAX_BUILDINGS]; - uint32_t resources[MAX_RESOURCE_TYPES]; unsigned char pop, bpop; - - struct - { - struct unit_tech u; - } tree; }; struct player_cfg @@ -55,8 +47,7 @@ struct player_cfg struct player_others { struct player *pl; - struct resource *res; - size_t n_pl, n_res; + size_t n_pl; }; int player_init(const struct player_cfg *cfg, struct player *pl); diff --git a/src/player/src/human_player.c b/src/player/src/human_player.c index cded3b0..08cfbe0 100644 --- a/src/player/src/human_player.c +++ b/src/player/src/human_player.c @@ -9,7 +9,6 @@ #include <input.h> #include <keyboard.h> #include <pad.h> -#include <resource.h> #include <unit.h> #include <util.h> #include <stdbool.h> @@ -42,61 +41,49 @@ static bool select_units(struct human_player *const h, const short x, const struct unit *const u = &pl->units[i]; const struct instance *const in = &u->instance; - if (in->alive) - { - switch (u->state) - { - default: - { - const union sel_data d = {.u = u}; + if (!in->alive) + continue; - if (!instance_selected(h, &d) - && cursor_collision(&h->cam, &in->r) - && h->n_sel < sizeof h->sel / sizeof *h->sel) - { - struct sel_instance *sel = NULL; + const union sel_data d = {.u = u}; - if (excl) - { - sel = h->sel; - h->n_sel = 1; + if (!instance_selected(h, &d) + && cursor_collision(&h->cam, &in->r) + && h->n_sel < sizeof h->sel / sizeof *h->sel) + { + struct sel_instance *sel = NULL; - for (size_t i = 1; i < sizeof h->sel / sizeof *h->sel; i++) - h->sel[i].d.u = NULL; - } - else - { - for (size_t i = 0; i < sizeof h->sel / sizeof *h->sel; i++) - { - struct sel_instance *const s = &h->sel[i]; - - if (!s->d.u) - { - sel = s; - h->n_sel++; - break; - } - } - } + if (excl) + { + sel = h->sel; + h->n_sel = 1; - if (sel) - { - sel->type = INSTANCE_TYPE_UNIT; - sel->d.u = u; - sfx_play(&unit_sounds[UNIT_SOUND_SELECTED]); - return true; - } + for (size_t i = 1; i < sizeof h->sel / sizeof *h->sel; i++) + h->sel[i].d.u = NULL; + } + else + { + for (size_t i = 0; i < sizeof h->sel / sizeof *h->sel; i++) + { + struct sel_instance *const s = &h->sel[i]; - return false; + if (!s->d.u) + { + sel = s; + h->n_sel++; + break; } } - break; + } - case UNIT_STATE_SHELTERED: - /* Fall through. */ - case UNIT_STATE_HARVESTING_GOLD: - break; + if (sel) + { + sel->type = INSTANCE_TYPE_UNIT; + sel->d.u = u; + sfx_play(&unit_sounds[UNIT_SOUND_SELECTED]); + return true; } + + return false; } } @@ -161,62 +148,6 @@ static bool select_buildings(struct human_player *const h, const short x, return false; } -static bool select_resources(struct human_player *const h, const short x, - const short y, const struct player_others *const o, const bool excl) -{ - for (size_t i = 0; i < o->n_res; i++) - { - const struct resource *const r = &o->res[i]; - const struct instance *const in = &r->instance; - - if (in->alive) - { - const union sel_data d = {.r = r}; - - if (!instance_selected(h, &d) - && cursor_collision(&h->cam, &in->r) - && h->n_sel < sizeof h->sel / sizeof *h->sel) - { - struct sel_instance *sel = NULL; - - if (excl) - { - sel = h->sel; - h->n_sel = 1; - - for (size_t i = 1; i < sizeof h->sel / sizeof *h->sel; i++) - h->sel[i].d.r = NULL; - } - else - { - for (size_t i = 0; i < sizeof h->sel / sizeof *h->sel; i++) - { - struct sel_instance *const s = &h->sel[i]; - - if (!s->d.r) - { - sel = s; - h->n_sel++; - break; - } - } - } - - if (sel) - { - sel->type = INSTANCE_TYPE_RESOURCE; - sel->d.r = r; - return true; - } - - return false; - } - } - } - - return false; -} - static bool select_instances(struct human_player *const h, const struct player_others *const o, const bool excl, const bool same_type) @@ -227,8 +158,7 @@ static bool select_instances(struct human_player *const h, if (!same_type || !h->n_sel) return select_buildings(h, x, y, excl) - || select_units(h, x, y, excl) - || select_resources(h, x, y, o, excl); + || select_units(h, x, y, excl); else { switch (h->sel->type) @@ -238,9 +168,6 @@ static bool select_instances(struct human_player *const h, case INSTANCE_TYPE_BUILDING: return select_buildings(h, x, y, excl); - - case INSTANCE_TYPE_RESOURCE: - select_resources(h, x, y, o, excl); } } @@ -253,62 +180,6 @@ static bool instance_collision(const struct camera *const cam, return in->alive && cursor_collision(cam, &in->r); } -static void resources_stored(struct instance *const i, void *const op) -{ - struct player *const p = op; - struct unit *const u = (struct unit *)i; - struct unit_harvester *const uh = &u->us.harvester; - - p->resources[uh->type] += uh->carry; - uh->carry = 0; - unit_set_target(u, &uh->prev_target); -} - -static void harvest_done(struct instance *const ins, void *const op) -{ - struct player *const p = op; - struct unit *const u = (struct unit *)ins; - struct resource *const res = (struct resource *)u->target.ins; - - if (res->type == RESOURCE_TYPE_GOLD) - { - struct resource_gold *const g = &res->res.gold; - - for (size_t i = 0; i < sizeof g->miners / sizeof *g->miners; i++) - { - struct instance **const pi = &g->miners[i]; - - if (*pi == ins) - { - *pi = NULL; - g->n_miners--; - break; - } - } - } - - for (size_t i = 0; i < sizeof p->buildings / sizeof *p->buildings; i++) - { - struct building *const b = &p->buildings[i]; - - if (b->instance.alive && b->type == BUILDING_TYPE_BARRACKS) - { - const struct unit_target t = - { - .ins = &b->instance, - .state = UNIT_STATE_CARRYING, - .done = resources_stored, - .op = p - /* TODO .state = UNIT_STATE_SHELTERED */ - }; - - u->us.harvester.prev_target = u->target; - unit_set_target(u, &t); - break; - } - } -} - static bool target_from_own(struct player *const p, const struct camera *const cam, struct unit_target *const t) { @@ -332,101 +203,6 @@ static bool target_from_own(struct player *const p, return false; } -static bool target_from_others(const struct player *const p, - const struct player_others *const o, - const struct camera *const cam, struct unit_target *const t) -{ - for (size_t i = 0; i < o->n_pl; i++) - { - struct player *const pl = &o->pl[i]; - - if (p->team != pl->team) - { - for (size_t i = 0; i < sizeof pl->units / sizeof *pl->units; i++) - { - struct unit *const u = &pl->units[i]; - struct instance *const in = &u->instance; - - if (instance_collision(cam, in)) - { - *t = (const struct unit_target) - { - .ins = in, - .attack = unit_attacked, - .state = UNIT_STATE_ATTACKING - }; - - return true; - } - } - - for (size_t i = 0; i < sizeof pl->buildings / sizeof *pl->buildings; i++) - { - struct building *const b = &pl->buildings[i]; - struct instance *const in = &b->instance; - - if (instance_collision(cam, in)) - { - *t = (const struct unit_target) - { - .ins = in, - .state = UNIT_STATE_ATTACKING - }; - - return true; - } - } - } - } - - return false; -} - -static enum unit_state target_state_from_res(const struct resource *const res) -{ - switch (res->type) - { - case RESOURCE_TYPE_GOLD: - return UNIT_STATE_HARVESTING_GOLD; - - case RESOURCE_TYPE_WOOD: - return UNIT_STATE_HARVESTING_WOOD; - - default: - break; - } - - return UNIT_STATE_IDLE_MOVING; -} - -static bool target_from_res(struct player *const p, - const struct player_others *const o, - const struct camera *const cam, struct unit_target *const t) -{ - for (size_t i = 0; i < o->n_res; i++) - { - struct resource *const res = &o->res[i]; - struct instance *const in = &res->instance; - - if (instance_collision(cam, in)) - { - *t = (const struct unit_target) - { - .ins = in, - .attack = resource_harvested, - .shelter = resource_shelter(res), - .state = target_state_from_res(res), - .done = harvest_done, - .op = p - }; - - return true; - } - } - - return false; -} - static void set_target(struct human_player *const h, struct unit *const u, const struct unit_target *const t) { @@ -443,9 +219,7 @@ static void target_from_pos(struct player *const p, const struct player_others *const o, const struct camera *const cam, struct unit_target *const t) { - if (!target_from_own(p, cam, t) - && !target_from_others(p, o, cam, t) - && !target_from_res(p, o, cam, t)) + if (!target_from_own(p, cam, t)) t->ins = NULL; } @@ -472,7 +246,7 @@ static void move_units(struct human_player *const h, { struct unit *const u = si->d.rw_u; - if (!t.ins || !unit_target_valid(u, &t)) + if (!t.ins) unit_move_to(u, x, y); else set_target(h, u, &t); @@ -506,9 +280,7 @@ static void update_selected(struct human_player *const h) struct sel_instance *const si = &h->sel[i]; if (si->d.i && (!si->d.i->alive || si->d.i->dying - || (si->type == INSTANCE_TYPE_UNIT - && (si->d.u->state == UNIT_STATE_HARVESTING_GOLD - || si->d.u->state == UNIT_STATE_SHELTERED)))) + || si->type == INSTANCE_TYPE_UNIT)) { si->d.i = NULL; h->n_sel--; @@ -552,8 +324,6 @@ static void update_from_pad(struct human_player *const h, move_units(h, o); else if (input_pad_justpressed(in, p, PAD_KEY_C)) deselect_instances(h); - else if (input_pad_justpressed(in, p, PAD_KEY_E)) - h->top_gui ^= true; } static void update_from_touch(struct human_player *const h, @@ -686,30 +456,12 @@ static int render_own_buildings(const struct human_player *const h) return 0; } -static int render_resources(const struct human_player *const h, - const struct resource *const res, const size_t n) -{ - for (size_t i = 0; i < n; i++) - { - const struct resource *const r = &res[i]; - const union sel_data d = {.r = r}; - const bool sel = instance_selected(h, &d); - - if (resource_render(r, &h->cam, sel)) - return -1; - } - - return 0; -} - int human_player_render(const struct human_player *const h, const struct player_others *const o) { if (render_target(h) || render_own_units(h) || render_own_buildings(h) - || render_resources(h, o->res, o->n_res) - || human_player_gui_render(h) || input_render(&h->in, h->periph)) return -1; @@ -742,9 +494,5 @@ int human_player_init(const struct human_player_cfg *const cfg, cursor_init(&h->cam.cursor); h->periph = cfg->p; h->cam.dim = cfg->dim; - h->top_gui = true; - UTIL_STATIC_ASSERT(sizeof h->gui_res == sizeof h->pl.resources, - "unexpected sizeof for h->gui_res"); - memmove(h->gui_res, h->pl.resources, sizeof h->gui_res); return 0; } diff --git a/src/player/src/human_player_gui.c b/src/player/src/human_player_gui.c index 79245dc..60a7ecd 100644 --- a/src/player/src/human_player_gui.c +++ b/src/player/src/human_player_gui.c @@ -17,68 +17,8 @@ enum {X_OFF = 8, Y_OFF = 8, HP_Y = 32}; -static int draw_hp(const struct instance *const i, const instance_hp max_hp, - struct gui_common *const r) -{ - enum {WIDTH = 64, HEIGHT = 4}; - struct gui_progress_bar pb; - - gui_progress_bar_init(&pb); - pb.common.x = X_OFF; - pb.common.y = HP_Y - 8; - pb.progress = ((unsigned)GUI_PROGRESS_BAR_MAX * i->hp) / max_hp; - pb.fg.g = UCHAR_MAX >> 1; - pb.bg.r = UCHAR_MAX >> 1; - pb.w = WIDTH; - pb.h = HEIGHT; - pb.stp = true; - gui_add_child(r, &pb.common); - - return gui_render(r); -} - -#if 0 -static int draw_miners(const struct resource *const r) -{ - const struct resource_gold *const g = &r->res.gold; - - if (!g->n_miners) - return 0; - - for (size_t i = 0, n = 0; i < sizeof g->miners / sizeof *g->miners; i++) - { - if (g->miners[i]) - { - enum {OFFSET = 112, SZ = 16, GAP = 4}; - - rect_get_or_ret(r, -1); - semitrans_rect_init(r); - r->x = OFFSET + n * (SZ + GAP); - r->y = screen_h - 40; - r->r = r->g = r->b = 127; - r->w = r->h = SZ; - rect_sort(r); - - if (++n >= g->n_miners) - break; - } - } - - return 0; -} -#endif - void human_player_gui_update(struct human_player *const h) { - struct player *const pl = &h->pl; - - for (size_t i = 0; i < sizeof pl->resources / sizeof *pl->resources; i++) - { - if (h->gui_res[i] > pl->resources[i]) - h->gui_res[i]--; - else if (h->gui_res[i] < pl->resources[i]) - h->gui_res[i]++; - } } static int render_sel_single_building(const struct human_player *const h, @@ -110,7 +50,7 @@ static int render_sel_single_building(const struct human_player *const h, hpl.text = hp_str; gui_add_child(r, &hpl.common); - return draw_hp(in, max_hp, r); + return gui_render(r); } static int render_sel_single_unit(const struct human_player *const h, @@ -144,60 +84,7 @@ static int render_sel_single_unit(const struct human_player *const h, hpl.text = hp_str; gui_add_child(r, &hpl.common); - char c_str[sizeof "255"]; - struct gui_label cl; - - if (unit_can_harvest(u)) - { - const struct unit_harvester *const uh = &u->us.harvester; - - if (uh->carry) - { - const int rs = snprintf(c_str, sizeof c_str, "%hhu", uh->carry); - - if (rs < 0 || rs >= sizeof c_str) - return -1; - - gui_label_init(&cl); - cl.common.x = CARRY_X; - cl.common.y = CARRY_Y; - cl.text = c_str; - gui_add_child(r, &cl.common); - } - } - - return draw_hp(in, max_hp, r); -} - -static int render_sel_single_resource(const struct human_player *const h, - const struct sel_instance *const sel, struct gui_common *const r) -{ - const struct resource *const res = sel->d.r; - const struct instance *const in = &res->instance; - const instance_hp hp = in->hp, max_hp = resource_maxhp(res); - struct gui_label rl; - - gui_label_init(&rl); - rl.common.x = X_OFF; - rl.common.y = Y_OFF; - rl.text = resource_str(res); - gui_add_child(r, &rl.common); - - char hp_str[sizeof "65535/65535"]; - const int rs = snprintf(hp_str, sizeof hp_str, "%u/%u", hp, max_hp); - - if (rs < 0 || rs >= sizeof hp_str) - return -1; - - struct gui_label hpl; - - gui_label_init(&hpl); - hpl.common.x = X_OFF; - hpl.common.y = HP_Y; - hpl.text = hp_str; - gui_add_child(r, &hpl.common); - - return draw_hp(in, max_hp, r); + return gui_render(r); } static int render_sel_single(const struct human_player *const h, @@ -216,14 +103,11 @@ static int render_sel_single(const struct human_player *const h, case INSTANCE_TYPE_UNIT: return render_sel_single_unit(h, sel, r); - - case INSTANCE_TYPE_RESOURCE: - return render_sel_single_resource(h, sel, r); } } } - /* Unreachable. */ + fprintf(stderr, "%s: unreachable\n", __func__); return -1; } @@ -262,83 +146,8 @@ static int render_sel(const struct human_player *const h) return render_sel_multiple(h, &r.common); } -static int render_top(const struct human_player *const h) -{ - const struct player *const pl = &h->pl; - struct gui_container c; - struct gui_bar b; - - gui_bar_init(&b); - b.w = screen_w; - gui_container_init(&c); - c.mode = GUI_CONTAINER_MODE_H; - c.spacing = 16; - c.common.hcentered = true; - c.common.vcentered = true; - gui_add_child(&b.common, &c.common); - - char wood_str[sizeof "Wood=429496729"]; - - { - const int rs = snprintf(wood_str, sizeof wood_str, - "Wood=%" PRIu32, h->gui_res[RESOURCE_TYPE_WOOD]); - - if (rs < 0 || rs >= sizeof wood_str) - return -1; - } - - struct gui_label wl; - - gui_label_init(&wl); - wl.common.vcentered = true; - wl.text = wood_str; - gui_add_child(&c.common, &wl.common); - - char gold_str[sizeof "Gold=429496729"]; - - { - const int rs = snprintf(gold_str, sizeof gold_str, - "Gold=%" PRIu32, h->gui_res[RESOURCE_TYPE_GOLD]); - - if (rs < 0 || rs >= sizeof wood_str) - return -1; - } - - struct gui_label gl; - - gui_label_init(&gl); - wl.common.vcentered = true; - gl.text = gold_str; - gui_add_child(&c.common, &gl.common); - - char pop_str[sizeof "Pop.=255/255"]; - - { - const int rs = snprintf(pop_str, sizeof pop_str, - "Pop.=%hhu/%zu", pl->pop, sizeof pl->units / sizeof *pl->units); - - if (rs < 0 || rs >= sizeof wood_str) - return -1; - } - - struct gui_label popl; - - gui_label_init(&popl); - wl.common.vcentered = true; - popl.text = pop_str; - gui_add_child(&c.common, &popl.common); - - if (gui_render(&b.common)) - return -1; - - return 0; -} - int human_player_gui_render(const struct human_player *const h) { - if (h->top_gui && render_top(h)) - return -1; - if (h->n_sel && render_sel(h)) return -1; diff --git a/src/player/src/player.c b/src/player/src/player.c index a638cb8..e1932a5 100644 --- a/src/player/src/player.c +++ b/src/player/src/player.c @@ -9,7 +9,7 @@ void player_update(struct player *const p) { struct unit *const u = &p->units[i]; - unit_update(&p->tree.u, u); + unit_update(u); } } @@ -98,13 +98,6 @@ int player_init(const struct player_cfg *const cfg, struct player *const pl) if (player_create_unit(&cfgs[i], pl)) return -1; - for (size_t i = 0; i < sizeof pl->resources / sizeof *pl->resources; i++) - { - enum {DEFAULT_RES = 120}; - - pl->resources[i] = DEFAULT_RES; - } - pl->alive = true; pl->color = cfg->color; pl->team = cfg->team; |
