aboutsummaryrefslogtreecommitdiff
path: root/src/unit
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-01-27 15:58:53 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-01-27 17:38:11 +0100
commit0aef4f319caa2572d459b18e4e994122d53abcbe (patch)
treec27f3cee65e565db4bf5f457d03190f227cccdf4 /src/unit
parent39ecbd690b2327db4328123f3f9a50c8492794ad (diff)
downloadjancity-0aef4f319caa2572d459b18e4e994122d53abcbe.tar.gz
Remove resource, tech and old game resources
Diffstat (limited to 'src/unit')
-rw-r--r--src/unit/CMakeLists.txt11
-rw-r--r--src/unit/inc/unit.h27
-rw-r--r--src/unit/src/unit.c250
3 files changed, 44 insertions, 244 deletions
diff --git a/src/unit/CMakeLists.txt b/src/unit/CMakeLists.txt
index 460cf43..5f52fbf 100644
--- a/src/unit/CMakeLists.txt
+++ b/src/unit/CMakeLists.txt
@@ -1,3 +1,12 @@
add_library(unit "src/unit.c")
target_include_directories(unit PUBLIC "inc")
-target_link_libraries(unit PUBLIC fixmath container camera gfx instance resource sfx tech util)
+target_link_libraries(unit
+ PUBLIC
+ fixmath
+ container
+ camera
+ gfx
+ instance
+ sfx
+ util
+)
diff --git a/src/unit/inc/unit.h b/src/unit/inc/unit.h
index 5af0c3f..a200c63 100644
--- a/src/unit/inc/unit.h
+++ b/src/unit/inc/unit.h
@@ -5,8 +5,6 @@
#include <gfx.h>
#include <sfx.h>
#include <instance.h>
-#include <resource_type.h>
-#include <tech.h>
#include <unit_type.h>
#include <util.h>
#include <fixmath.h>
@@ -18,19 +16,9 @@ extern "C"
{
#endif
-struct unit_tech
-{
- enum tech_level carry;
-};
-
enum unit_state
{
- UNIT_STATE_IDLE_MOVING,
- UNIT_STATE_SHELTERED,
- UNIT_STATE_HARVESTING_WOOD,
- UNIT_STATE_HARVESTING_GOLD,
- UNIT_STATE_CARRYING,
- UNIT_STATE_ATTACKING
+ UNIT_STATE_IDLE_MOVING
};
struct unit_target
@@ -67,18 +55,7 @@ struct unit
unsigned char t, i;
} frame;
- union
- {
- struct unit_harvester
- {
- enum resource_type type;
- unsigned char carry, t;
- struct unit_target prev_target;
- } harvester;
- } us;
-
fix16_t rx, ry, tx, ty;
-
struct unit_target target;
};
@@ -97,7 +74,7 @@ bool unit_target_valid(const struct unit *u, const struct unit_target *t);
void unit_set_target(struct unit *u, const struct unit_target *t);
void unit_move_to(struct unit *u, unsigned long x, unsigned long y);
bool unit_attacked(struct instance *, instance_hp ap);
-void unit_update(const struct unit_tech *t, struct unit *u);
+void unit_update(struct unit *u);
instance_hp unit_maxhp(const struct unit *u);
const char *unit_str(const struct unit *u);
diff --git a/src/unit/src/unit.c b/src/unit/src/unit.c
index f25d406..0590adb 100644
--- a/src/unit/src/unit.c
+++ b/src/unit/src/unit.c
@@ -115,16 +115,6 @@ static enum unit_dir get_direction(const struct unit *const u)
return dir;
}
-static instance_hp attack_points(const struct unit *const u)
-{
- static instance_hp ap[] =
- {
- [UNIT_TYPE_PEASANT] = 1
- };
-
- return ap[u->type];
-}
-
static void unit_stop(struct unit *const u)
{
u->tx = u->rx;
@@ -151,68 +141,18 @@ static void target_interact(struct unit *const u)
const struct instance *const ins = t->ins;
/* TODO: lose u->ti if not visible. */
- if (ins->alive)
+ if (!ins->alive)
{
- if (t->state != u->state)
- {
- struct instance *const ui = &u->instance;
-
- if (util_collision(&ins->r, &ui->r))
- {
- switch (t->state)
- {
- case UNIT_STATE_CARRYING:
- u->target.done(&u->instance, u->target.op);
- break;
-
- case UNIT_STATE_ATTACKING:
- t->attack(t->ins, attack_points(u));
- u->state = t->state;
- break;
-
- case UNIT_STATE_HARVESTING_WOOD:
- {
- struct unit_harvester *const uh = &u->us.harvester;
-
- if (uh->type != RESOURCE_TYPE_WOOD)
- uh->carry = 0;
-
- uh->type = RESOURCE_TYPE_WOOD;
- u->state = t->state;
- unit_stop(u);
- }
- break;
-
- case UNIT_STATE_HARVESTING_GOLD:
- {
- struct unit_harvester *const uh = &u->us.harvester;
-
- if (uh->type != RESOURCE_TYPE_GOLD)
- uh->carry = 0;
-
- uh->type = RESOURCE_TYPE_GOLD;
-
- if (t->shelter(t->ins, ui))
- u->state = t->state;
- else
- unit_stop(u);
- }
- break;
-
- case UNIT_STATE_SHELTERED:
- u->state = t->state;
- break;
-
- default:
- break;
- }
- }
- else
- unit_chase(ins, u);
- }
- }
- else
target_reset(u);
+ return;
+ }
+ else if (t->state == u->state)
+ return;
+
+ struct instance *const ui = &u->instance;
+
+ if (!util_collision(&ins->r, &ui->r))
+ unit_chase(ins, u);
}
static bool must_move(const struct unit *const u)
@@ -220,164 +160,40 @@ static bool must_move(const struct unit *const u)
return u->rx != u->tx || u->ry != u->ty;
}
-static void update_harvest(const struct unit_tech *const t, struct unit *const u)
+void unit_update(struct unit *const u)
{
- static const unsigned char carry[] =
- {
- [TECH_LEVEL_1] = 10,
- [TECH_LEVEL_2] = 15,
- [TECH_LEVEL_3] = 25
- };
-
- static const char inc[] =
- {
- [TECH_LEVEL_1] = 10,
- [TECH_LEVEL_2] = 50,
- [TECH_LEVEL_3] = 25
- };
-
- struct unit_harvester *const uh = &u->us.harvester;
- bool ret = false;
-
- if (++uh->t >= inc[t->carry])
- {
- if (uh->carry < carry[t->carry])
- {
- ++uh->carry;
- ret = u->target.attack(u->target.ins, 1);
- }
+ const struct instance *const i = &u->instance;
- uh->t = 0;
- }
+ if (!i->alive)
+ return;
- if (ret || uh->carry >= carry[t->carry])
+ if (u->target.ins)
+ target_interact(u);
+ else if (must_move(u))
{
- u->state = UNIT_STATE_CARRYING;
- u->target.done(&u->instance, u->target.op);
- }
-}
+ fix16_t x_step, y_step;
-void unit_update(const struct unit_tech *const t, struct unit *const u)
-{
- const struct instance *const i = &u->instance;
+ u->dir = get_direction(u);
+ get_speed(u, &x_step, &y_step);
+ move_unit(u, x_step, y_step);
- if (i->alive)
- {
- if (u->target.ins)
- target_interact(u);
+ enum {FRAME_RATE = 6};
- switch (u->state)
+ if (++u->frame.t >= FRAME_RATE)
{
- case UNIT_STATE_SHELTERED:
- break;
-
- case UNIT_STATE_HARVESTING_GOLD:
- /* Fall through. */
- case UNIT_STATE_HARVESTING_WOOD:
- update_harvest(t, u);
- break;
-
- case UNIT_STATE_ATTACKING:
- if (must_move(u))
- u->state = UNIT_STATE_IDLE_MOVING;
-
- break;
-
- case UNIT_STATE_CARRYING:
- /* Fall through. */
- case UNIT_STATE_IDLE_MOVING:
-
- if (must_move(u))
- {
- fix16_t x_step, y_step;
-
- u->dir = get_direction(u);
- get_speed(u, &x_step, &y_step);
- move_unit(u, x_step, y_step);
+ u->frame.t = 0;
- enum {FRAME_RATE = 6};
-
- if (++u->frame.t >= FRAME_RATE)
- {
- u->frame.t = 0;
-
- if (++u->frame.i >= N_FRAMES)
- u->frame.i = 0;
- }
-
- u->state = UNIT_STATE_IDLE_MOVING;
- }
- else
- u->frame.i = 0;
-
- u->instance.r.x = fix16_to_int(u->rx);
- u->instance.r.y = fix16_to_int(u->ry);
- break;
+ if (++u->frame.i >= N_FRAMES)
+ u->frame.i = 0;
}
- }
-}
-
-bool can_attack(const struct unit *const u)
-{
- static const bool a[] =
- {
- [UNIT_TYPE_PEASANT] = true
- };
-
- return a[u->type];
-}
-
-static bool can_shelter(const struct unit *const u)
-{
- static const bool s[] =
- {
- [UNIT_TYPE_PEASANT] = true
- };
-
- return s[u->type];
-}
-bool unit_can_harvest(const struct unit *const u)
-{
- static const bool h[] =
- {
- [UNIT_TYPE_PEASANT] = true
- };
-
- return h[u->type];
-}
-
-bool unit_attacked(struct instance *const i, const instance_hp ap)
-{
- return instance_attacked(i, ap);
-}
-
-bool unit_target_valid(const struct unit *const u,
- const struct unit_target *const t)
-{
- switch (t->state)
- {
- case UNIT_STATE_HARVESTING_GOLD:
- /* Fall through. */
- case UNIT_STATE_HARVESTING_WOOD:
- return unit_can_harvest(u);
-
- case UNIT_STATE_ATTACKING:
- return can_attack(u);
-
- case UNIT_STATE_SHELTERED:
- if (t->shelter)
- return can_shelter(u);
- else
- break;
-
- case UNIT_STATE_IDLE_MOVING:
- /* Fall through. */
- case UNIT_STATE_CARRYING:
- break;
+ u->state = UNIT_STATE_IDLE_MOVING;
}
+ else
+ u->frame.i = 0;
- return false;
+ u->instance.r.x = fix16_to_int(u->rx);
+ u->instance.r.y = fix16_to_int(u->ry);
}
void unit_set_target(struct unit *const u, const struct unit_target *const t)
@@ -582,9 +398,7 @@ static int unit_quad(const struct unit *const u, struct render_cfg *const rcfg)
int unit_render(const struct unit *const u, const struct camera *const cam,
const bool sel)
{
- if (!u->instance.alive
- || u->state == UNIT_STATE_SHELTERED
- || u->state == UNIT_STATE_HARVESTING_GOLD)
+ if (!u->instance.alive)
return 0;
struct render_cfg rcfg;