Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointers should be used as LifeForm identifiers instead of strings [moved] #46

Open
ooxi opened this issue Oct 28, 2012 · 1 comment
Open

Comments

@ooxi
Copy link
Owner

ooxi commented Oct 28, 2012

This is Issue 46 moved from a Google Code project.
Added by 2011-03-26T18:36:42.000Z by [email protected].
Please review that bug for more context and additional comments, but update this bug.

Original labels: Type-Defect, Priority-Medium

Original description

It will give some speedup.
Patch attached.
@ooxi
Copy link
Owner Author

ooxi commented Nov 5, 2012

Appended patch by [email protected]

Index: system/utility/Templates.h
===================================================================
--- system/utility/Templates.h (revision 284)
+++ system/utility/Templates.h (working copy)
@@ -1,6 +1,17 @@
#ifndef TEMPLATES_H_
#define TEMPLATES_H_

+#include <set>
+#include <map>
+
+template<typename T> void clearSet(std::set<T>* set) {
+ typename std::set<T>::const_iterator i;
+ for (i = set->begin(); i != set->end(); ++i) {
+ delete *i;
+ }
+ set->clear();
+}
+
template<typename T1, typename T2> void clearMap(std::map<T1, T2>* map) {
typename std::map<T1, T2>::const_iterator i;
for (i = map->begin(); i != map->end(); ++i) {
Index: program.cpp
===================================================================
--- program.cpp (revision 284)
+++ program.cpp (working copy)
@@ -90,7 +90,7 @@
Terrain* terrain;

GameState* gameState;
-string playerId;
+LifeForm* playerId;

map<string, Window*> windows;

@@ -162,8 +162,7 @@
break;
}

- gameState->lifeForms.insert(map<string, LifeForm*>::value_type(
- newMonster->Id, newMonster));
+ gameState->lifeForms.insert(newMonster);
}

// The beginning of new game in selected mode
@@ -197,10 +196,9 @@
player->setWeapon(weaponManager->getWeaponByName("PM"));
player->HitR = 0.28f;

- gameState->lifeForms.insert(map<string, LifeForm*>::value_type(player->Id,
- player));
+ gameState->lifeForms.insert(player);

- playerId = player->Id;
+ playerId = player;

hud->addMessage(_("Try to survive as long as you can."));
hud->addMessage(
@@ -1493,43 +1491,43 @@
// AI

// Check if current target exist and is in range
- if (enemy->targetId.compare("ambient") != 0) {
+ if (enemy->targetId != NULL) {
LifeForm* targetLifeForm = gameState->getLifeForm(enemy->targetId);

if (targetLifeForm == NULL)
- enemy->targetId = "ambient";
+ enemy->targetId = NULL;
else if (targetLifeForm->State != LIFEFORM_STATE_ALIVE)
- enemy->targetId = "ambient";
+ enemy->targetId = NULL;
else {
float range = sqrt(pow(-enemy->X + targetLifeForm->X, 2) + pow(
enemy->Y - targetLifeForm->Y, 2));

if (range > 800)
- enemy->targetId = "ambient";
+ enemy->targetId = NULL;
}
}

// Check for new targets in range
- if (enemy->targetId.compare("ambient") == 0) {
+ if (enemy->targetId == NULL) {
if (!gameState->lifeForms.empty()) {
- map<string, LifeForm*>::const_iterator iter;
+ set<LifeForm*>::const_iterator iter;
for (iter = gameState->lifeForms.begin(); iter
!= gameState->lifeForms.end(); ++iter) {
- LifeForm* lifeForm = iter->second;
+ LifeForm* lifeForm = *iter;

float range = sqrt(pow(-enemy->X + lifeForm->X, 2) + pow(
enemy->Y - lifeForm->Y, 2));

if (range < 800 && lifeForm->State == LIFEFORM_STATE_ALIVE
&& lifeForm->Type == LIFEFORM_PLAYER) {
- enemy->targetId = lifeForm->Id;
+ enemy->targetId = lifeForm;
break;
}
}
}
}

- if (enemy->targetId.compare("ambient") == 0) {
+ if (enemy->targetId == NULL) {
// Hang around

float range = sqrt(pow(-enemy->X + enemy->TargetX, 2) + pow(enemy->Y
@@ -1899,10 +1897,10 @@
}

if (!gameState->lifeForms.empty()) {
- map<string, LifeForm*>::const_iterator iter;
+ set<LifeForm*>::const_iterator iter;
for (iter = gameState->lifeForms.begin(); iter
!= gameState->lifeForms.end(); ++iter) {
- LifeForm* lifeForm = iter->second;
+ LifeForm* lifeForm = *iter;

if (lifeForm->Type == LIFEFORM_PLAYER) {
if (!gameState->Lost) {
@@ -1930,7 +1928,7 @@

if (lifeForm->Type == LIFEFORM_MONSTER) {
delete lifeForm;
- gameState->lifeForms.erase(iter->first);
+ gameState->lifeForms.erase(*iter);
}
}
}
@@ -2052,10 +2050,10 @@

if (gameState->bullets[i]->isActive()
&& !gameState->lifeForms.empty()) {
- map<string, LifeForm*>::const_iterator iter;
+ set<LifeForm*>::const_iterator iter;
for (iter = gameState->lifeForms.begin(); iter
!= gameState->lifeForms.end(); ++iter) {
- LifeForm* lf = iter->second;
+ LifeForm* lf = *iter;

if (lf->Type == LIFEFORM_PLAYER || lf->State
!= LIFEFORM_STATE_ALIVE)
@@ -2186,10 +2184,10 @@
case BONUS_FREEZE: {
hud->addMessage(_("All have been frozen around you."));

- map<string, LifeForm*>::const_iterator iter;
+ set<LifeForm*>::const_iterator iter;
for (iter = gameState->lifeForms.begin(); iter
!= gameState->lifeForms.end(); ++iter) {
- LifeForm* lf = iter->second;
+ LifeForm* lf = *iter;

if (lf->Type == LIFEFORM_PLAYER || lf->State
!= LIFEFORM_STATE_ALIVE)
@@ -2383,10 +2381,10 @@
gameState->powerups[i]->draw(false, false);
}

- map<string, LifeForm*>::const_iterator iter;
+ set<LifeForm*>::const_iterator iter;
for (iter = gameState->lifeForms.begin(); iter
!= gameState->lifeForms.end(); ++iter) {
- LifeForm* lifeForm = iter->second;
+ LifeForm* lifeForm = *iter;

if (lifeForm->getLeft() < cam->X + cam->getHalfW()
&& lifeForm->getRight() > cam->X - cam->getHalfW()
Index: game/lifeforms/LifeForm.cpp
===================================================================
--- game/lifeforms/LifeForm.cpp (revision 284)
+++ game/lifeforms/LifeForm.cpp (working copy)
@@ -17,8 +17,6 @@
t = static_cast<unsigned long> (time(NULL));
#endif //linux || __FreeBSD__ || __APPLE__
char buf[30];
- sprintf(buf, "%09i-%09li", (rand() % 999999999), t);
- Id = buf;
Strength = 1.0f;
Agility = 1.0f;
Vitality = 1.0f;
Index: game/lifeforms/Monster.cpp
===================================================================
--- game/lifeforms/Monster.cpp (revision 284)
+++ game/lifeforms/Monster.cpp (working copy)
@@ -5,7 +5,6 @@

violetland::Monster::Monster(MonsterTemplate* base, int lvl) :
LifeForm(0, 0, 128, 128) {
- Id = "10-" + Id;

Base = base;
Level = lvl;
@@ -53,7 +52,7 @@
m_bleedDelay = 0;
m_bleedCount = 0;
Angry = false;
- targetId = "ambient";
+ targetId = NULL;
Type = LIFEFORM_MONSTER;
}

Index: game/lifeforms/LifeForm.h
===================================================================
--- game/lifeforms/LifeForm.h (revision 284)
+++ game/lifeforms/LifeForm.h (working copy)
@@ -38,7 +38,6 @@

void move(float direction, int deltaTime);

- std::string Id;
std::string Name;
int Level;
float Strength;
Index: game/lifeforms/Monster.h
===================================================================
--- game/lifeforms/Monster.h (revision 284)
+++ game/lifeforms/Monster.h (working copy)
@@ -28,7 +28,7 @@
void destroy();
~Monster();
bool Angry;
- string targetId;
+ LifeForm* targetId;
MonsterTemplate* Base;
};
}
Index: game/lifeforms/Player.cpp
===================================================================
--- game/lifeforms/Player.cpp (revision 284)
+++ game/lifeforms/Player.cpp (working copy)
@@ -5,7 +5,6 @@

violetland::Player::Player() :
LifeForm(0, 0, 128, 128) {
- Id = "20-" + Id;
Empty = true;
Xp = 0;
LastLevelXp = 0;
@@ -113,7 +112,7 @@
bullet->Angle = AccuracyDeviation < 1 ? m_arms->Angle
: m_arms->Angle + (rand() % (int) (AccuracyDeviation * 2))
- AccuracyDeviation;
- bullet->OwnerId = Id;
+ bullet->OwnerId = this;
}

if (m_weapon->BulletsAtOnce > 1)
Index: game/GameState.cpp
===================================================================
--- game/GameState.cpp (revision 284)
+++ game/GameState.cpp (working copy)
@@ -40,11 +40,9 @@
Works = false;
}

-violetland::LifeForm* violetland::GameState::getLifeForm(string id) {
- if (lifeForms.count(id) > 0)
- return lifeForms.find(id)->second;
- else
- return NULL;
+violetland::LifeForm* violetland::GameState::getLifeForm(LifeForm* id) {
+ set<LifeForm*>::iterator it = lifeForms.find(id);
+ return it == lifeForms.end() ? NULL : *it;
}

vector<violetland::Blood> violetland::GameState::processExplosion(float x,
@@ -54,9 +52,9 @@
if (lifeForms.empty())
return vBlood;

- map<string, LifeForm*>::const_iterator iter;
+ set<LifeForm*>::const_iterator iter;
for (iter = lifeForms.begin(); iter != lifeForms.end(); ++iter) {
- LifeForm* lifeForm = iter->second;
+ LifeForm* lifeForm = *iter;
if (lifeForm->Type == LIFEFORM_PLAYER && !affectPlayer)
continue;

@@ -99,7 +97,7 @@
}

void violetland::GameState::reset() {
- clearMap<string, LifeForm*> (&lifeForms);
+ clearSet<LifeForm*> (&lifeForms);
clearVector<Powerup*> (&powerups);
clearVector<Bullet*> (&bullets);
}
Index: game/GameState.h
===================================================================
--- game/GameState.h (revision 284)
+++ game/GameState.h (working copy)
@@ -2,7 +2,7 @@
#define GAMESTATE_H_

#include <string>
-#include <map>
+#include <set>
#include <vector>
#include "../system/utility/Templates.h"
#include "Terrain.h"
@@ -31,7 +31,7 @@
float range, bool affectPlayer);
void reset();
void process(int deltaTime);
- LifeForm* getLifeForm(string id);
+ LifeForm* getLifeForm(LifeForm*);
~GameState();

GameMode Mode;
@@ -46,7 +46,7 @@
int Time;
int GameAreaSize;

- map<string, LifeForm*> lifeForms;
+ set<LifeForm*> lifeForms;
vector<Powerup*> powerups;
vector<Bullet*> bullets;
};
Index: game/bullets/Bullet.h
===================================================================
--- game/bullets/Bullet.h (revision 284)
+++ game/bullets/Bullet.h (working copy)
@@ -1,8 +1,8 @@
#ifndef BULLET_H_
#define BULLET_H_

-#include <string>
#include "../../system/Object.h"
+#include "../lifeforms/LifeForm.h"

class Bullet: public Object {
protected:
@@ -24,7 +24,7 @@
float Damage;
float MaxRange;
Bullet::BulletType Type;
- std::string OwnerId;
+ violetland::LifeForm* OwnerId;

bool Poisoned;
bool BigCalibre;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant