1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-25 09:44:02 +00:00

cleanup legacy entity targets; don't allocate 10 slots up front since barely any entity uses even slot 0

Also fix probably wrong target check in entity_hurtTarget()
This commit is contained in:
fgenesis 2023-09-14 00:17:39 +02:00
parent ed5cd03a06
commit 5a9025a2a7
3 changed files with 42 additions and 49 deletions

View file

@ -206,7 +206,6 @@ Entity::Entity()
multColor = Vector(1,1,1); multColor = Vector(1,1,1);
collideRadius = 24; collideRadius = 24;
entityType = EntityType(0); entityType = EntityType(0);
targets.resize(10);
frozenTimer = 0; frozenTimer = 0;
canBeTargetedByAvatar = false; canBeTargetedByAvatar = false;
@ -489,11 +488,8 @@ void Entity::stopFollowingPath()
position.stopPath(); position.stopPath();
} }
void Entity::flipToTarget(Vector pos) void Entity::flipToPos(Vector pos)
{ {
if (pos.x > position.x) if (pos.x > position.x)
{ {
if (!isfh()) if (!isfh())
@ -506,21 +502,27 @@ void Entity::flipToTarget(Vector pos)
} }
} }
Entity* Entity::getTargetEntity(int t) Entity* Entity::getTargetEntity(size_t t) const
{ {
return targets[t]; return t < targets.size() ? targets[t] : NULL;
} }
void Entity::setTargetEntity(Entity *e, int t) void Entity::setTargetEntity(Entity *e, size_t t)
{ {
if(t < targets.size())
{
targets[t] = e;
return;
}
if(!e)
return;
if(targets.size() <= t)
targets.resize(t + 1);
targets[t] = e; targets[t] = e;
} }
bool Entity::hasTarget(int t)
{
return (targets[t]!=0);
}
void Entity::destroy() void Entity::destroy()
{ {
if (stopSoundsOnDeath) if (stopSoundsOnDeath)
@ -1860,25 +1862,21 @@ EntityType Entity::getEntityType()
return entityType; return entityType;
} }
/* types: Entity *Entity::findTarget(int dist, int type, size_t t)
*/
Entity *Entity::findTarget(int dist, int type, int t)
{ {
targets[t] = 0; Entity *target = NULL;
if (type == ET_AVATAR) if (type == ET_AVATAR)
{ {
Vector d = game->avatar->position - this->position; Vector d = game->avatar->position - this->position;
if (d.getSquaredLength2D() < sqr(dist)) if (d.getSquaredLength2D() < sqr(dist))
{ {
targets[t] = game->avatar; target = game->avatar;
} }
} }
else else
{ {
int closestDist = -1; int closestDist = -1;
Entity *target = 0;
FOR_ENTITIES(i) FOR_ENTITIES(i)
{ {
Entity *e = *i; Entity *e = *i;
@ -1892,12 +1890,10 @@ Entity *Entity::findTarget(int dist, int type, int t)
} }
} }
} }
if (target)
{
targets[t] = target;
}
} }
return targets[t]; setTargetEntity(target, t);
return target;
} }
void Entity::moveTowards(Vector p, float dt, int spd) void Entity::moveTowards(Vector p, float dt, int spd)
@ -1930,15 +1926,15 @@ void Entity::moveAroundAngle(int angle, float dt, int spd, int dir)
moveAround(p, dt, spd, dir); moveAround(p, dt, spd, dir);
} }
void Entity::moveTowardsTarget(float dt, int spd, int t) void Entity::moveTowardsTarget(float dt, int spd, size_t t)
{ {
if (!targets[t]) return; if (targets.size() < t || !targets[t]) return;
moveTowards(targets[t]->position, dt, spd); moveTowards(targets[t]->position, dt, spd);
} }
void Entity::moveAroundTarget(float dt, int spd, int dir, int t) void Entity::moveAroundTarget(float dt, int spd, int dir, size_t t)
{ {
if (!targets[t]) return; if (targets.size() < t || !targets[t]) return;
moveAround(targets[t]->position, dt, spd, dir); moveAround(targets[t]->position, dt, spd, dir);
} }

View file

@ -176,8 +176,8 @@ public:
void moveTowardsAngle(int angle, float dt, int spd); void moveTowardsAngle(int angle, float dt, int spd);
void moveAroundAngle(int angle, float dt, int spd, int dir); void moveAroundAngle(int angle, float dt, int spd, int dir);
void moveTowardsTarget(float dt, int spd, int t=0); void moveTowardsTarget(float dt, int spd, size_t t=0);
void moveAroundTarget(float dt, int spd, int d, int t=0); void moveAroundTarget(float dt, int spd, int d, size_t t=0);
void moveAroundEntity(float dt, int spd, int d, Entity *e); void moveAroundEntity(float dt, int spd, int d, Entity *e);
void moveTowardsGroupCenter(float dt, int spd); void moveTowardsGroupCenter(float dt, int spd);
void moveTowardsGroupHeading(float dt, int spd); void moveTowardsGroupHeading(float dt, int spd);
@ -185,13 +185,12 @@ public:
void doSpellAvoidance(float dt, int range, float mod); void doSpellAvoidance(float dt, int range, float mod);
void doEntityAvoidance(float dt, int range, float mod, Entity *ignore =0); void doEntityAvoidance(float dt, int range, float mod, Entity *ignore =0);
void setMaxSpeed(float ms); void setMaxSpeed(float ms);
Entity *findTarget(int dist, int type, int t=0); Entity *findTarget(int dist, int type, size_t t=0);
bool hasTarget(int t=0);
bool isTargetInRange(int range, size_t t=0); bool isTargetInRange(int range, size_t t=0);
void doGlint(const Vector &position, const Vector &scale=Vector(2,2), const std::string &tex="Glint", BlendType bt=BLEND_DEFAULT); void doGlint(const Vector &position, const Vector &scale=Vector(2,2), const std::string &tex="Glint", BlendType bt=BLEND_DEFAULT);
Entity *getTargetEntity(int t=0); Entity *getTargetEntity(size_t t=0) const;
void setTargetEntity(Entity *e, int t=0); void setTargetEntity(Entity *e, size_t t=0);
virtual void activate(Entity *by, int source){} virtual void activate(Entity *by, int source){}
@ -199,7 +198,7 @@ public:
void setEntityType(EntityType et); void setEntityType(EntityType et);
EntityType getEntityType(); EntityType getEntityType();
void flipToTarget(Vector pos); void flipToPos(Vector pos);
bool isFollowingPath(); bool isFollowingPath();
void stopFollowingPath(); void stopFollowingPath();
void overideMaxSpeed(int ms, float time); void overideMaxSpeed(int ms, float time);

View file

@ -5990,7 +5990,7 @@ luaFunc(entity_flipToEntity)
Entity *e2 = entity(L, 2); Entity *e2 = entity(L, 2);
if (e && e2) if (e && e2)
{ {
e->flipToTarget(e2->position); e->flipToPos(e2->position);
} }
luaReturnNil(); luaReturnNil();
} }
@ -6002,7 +6002,7 @@ luaFunc(entity_flipToNode)
PathNode *n = &p->nodes[0]; PathNode *n = &p->nodes[0];
if (e && n) if (e && n)
{ {
e->flipToTarget(n->position); e->flipToPos(n->position);
} }
luaReturnNil(); luaReturnNil();
} }
@ -7413,22 +7413,20 @@ luaFunc(entity_setActivationType)
luaFunc(entity_hasTarget) luaFunc(entity_hasTarget)
{ {
Entity *e = entity(L); Entity *e = entity(L);
if (e) luaReturnBool(e && e->getTargetEntity(e->currentEntityTarget));
luaReturnBool(e->hasTarget(e->currentEntityTarget));
else
luaReturnBool(false);
} }
luaFunc(entity_hurtTarget) luaFunc(entity_hurtTarget)
{ {
Entity *e = entity(L); Entity *e = entity(L);
if (e && e->getTargetEntity()) if (e)
{ if(Entity *t = e->getTargetEntity(e->currentEntityTarget))
DamageData d; {
d.attacker = e; DamageData d;
d.damage = lua_tointeger(L, 2); d.attacker = e;
e->getTargetEntity(e->currentEntityTarget)->damage(d); d.damage = lua_tointeger(L, 2);
} t->damage(d);
}
luaReturnNil(); luaReturnNil();
} }