+#include "item.h"
#include "player.h"
+#include "pouch.h"
#include "extern.h"
-#include <stdlib.h>
+#include "flags.h"
+
+#include <string>
#include <stdio.h>
+#if defined(HAVE_CRYPT_H)
+
+#include <crypt.h>
+
+#elif defined(HAVE_UNISTD_H)
+
+#include <unistd.h>
+
+#endif
+
+using namespace std;
+
void Player::reset()
{
- memset(name, 0, 255);
- memset(password, 0, 255);
-
exp = 1;
gold = 0;
bank = 500;
hp = 10;
maxhp = 10;
- strength = 0;
- defense = 0;
- armor = 1;
- weapon = 1;
+ strength = 2;
+ defense = 1;
level = 1;
- alive = 1;
forest_fights = forestfights;
player_fights = 3;
fight = NULL;
master = NULL;
battle = NULL;
flags = 0;
+ lastcommand = 0;
+ lastlogin = time(NULL);
+ setAlive(this);
+ clearMaster(this);
+ w = NULL;
+ a = NULL;
+ inventory->clear();
}
-Player::Player(aClient *user)
+Player::Player()
{
-
- name = new char[256];
- password = new char[256];
-
- if (user)
- {
- setData(user->stats);
- strcpy(name, user->getNick());
- }
- else
- setData(NULL);
-
- cout << "New Player: " << name << endl << flush;
+ inventory = new pouch();
+ setData(NULL);
+
+#ifdef DEBUGMODE
+ log("New Player: %s", name.c_str());
+#endif
}
Player::Player(char *n)
{
- name = new char[256];
- password = new char[256];
-
+ inventory = new pouch();
reset(); // Set defaults
- strcpy(name, n);
+ name = n;
+
+#ifdef DEBUGMODE
+ log("New Player: %s", name.c_str());
+#endif
+}
- cout << "New Player: " << name << endl << flush;
+Player::Player(string n)
+{
+ inventory = new pouch();
+ reset();
+ name = n;
+#ifdef DEBUGMODE
+ log("New Player: %s", name.c_str());
+#endif
}
Player::~Player()
-{
- cout << "Deleting player";
+{
+ if (inventory != NULL)
+ delete inventory;
+
+ delMonster();
+ delMaster();
+ delBattle();
+#ifdef DEBUGMODE
+ string *output;
+ output = new string("Deleting Player");
+ *output += ": " + name;
+ *output += " Password: " + password;
+ log ("%s", output->c_str());
+ delete output;
+#endif
+}
- if (name)
- {
- cout << ": " << name;
- delete [] name;
- }
- if (password)
- {
- cout << " Password: " << password;
- delete [] password;
- }
- cout << endl << "Player Deleted";
+void Player::setPassword(const char *p)
+{
+ static char saltChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV\
+ WXYZ0123456789./";
+ static char salt[3];
+
+ salt[0] = saltChars[rand() % strlen(saltChars)];
+ salt[1] = saltChars[rand() % strlen(saltChars)];
+ salt[2] = '\0';
+
+ password = crypt(p, salt);
+}
+
+void Player::setRawPassword(const char *p)
+{
+ password = p;
}
void Player::setData(Player *right)
{
if (right)
{
- strcpy(name, right->name);
- strcpy(password, right->password);
+ name = right->name;
+ password = right->password;
exp = right->exp;
gold = right->gold;
bank = right->bank;
maxhp = right->maxhp;
strength = right->strength;
defense = right->defense;
- armor = right->armor;
- weapon = right->weapon;
level = right->level;
- alive = right->alive;
forest_fights = right->forest_fights;
player_fights = right->player_fights;
- flags = right->flags;
+ flags = right->flags;
+ *inventory = *right->inventory;
+ client = right->client;
+ lastcommand = right->lastcommand;
+ lastlogin = right->lastlogin;
+ w = right->w;
+ a = right->a;
}
else
{
+ char *temp;
reset();
-
- int num = rand() % 32767;
- sprintf(name, "Player%d", num);
+ temp = new char[16];
+ client = NULL;
+ int num = rand() % 32767;
+ snprintf(temp, 16, "Player: %d", num);
+ name = temp;
+ delete []temp;
}
-
- cout << "Setting Player data for " << name << endl;
+
+#ifdef DEBUGMODE
+ log("Setting Player data for %s", name.c_str());
+#endif
}
-monster_::~monster_()
+Monster::~Monster()
{
- delete name;
- delete weapon;
- delete death;
}
-monster_::monster_()
+Monster::Monster()
{
- name = new char[32];
- weapon = new char[32];
- death = new char[256];
-
- memset(name, 0, 32);
- memset(weapon, 0, 32);
- memset(death, 0, 256);
+ name.erase();
+ weapon.erase();
+ death.erase();
+ strength = 0;
+ defense = 0;
+ exp = 0;
+ gold = 0;
+ maxhp = hp = 0;
}
-monster_::monster_(monster_ *m)
+Monster::Monster(Monster *m)
{
- name = new char[strlen(m->name) + 1];
- weapon = new char[strlen(m->weapon) + 1];
- death = new char[strlen(m->death) + 1];
-
- strcpy(name, m->name);
- strcpy(weapon, m->weapon);
- strcpy(death, m->death);
-
+ name = m->name;
+ weapon = m->weapon;
+ death = m->death;
strength = m->strength;
+ defense = m->defense;
maxhp = m->maxhp;
hp = m->hp;
gold = m->gold;
exp = m->exp;
}
+Monster::Monster(Monster &m)
+{
+ name = m.name;
+ weapon = m.weapon;
+ death = m.death;
+ strength = m.strength;
+ defense = m.defense;
+ maxhp = m.maxhp;
+ hp = m.hp;
+ gold = m.gold;
+ exp = m.exp;
+}
+
+Monster::Monster(const Monster &m)
+{
+ name = m.name;
+ weapon = m.weapon;
+ death = m.death;
+ strength = m.strength;
+ defense = m.defense;
+ maxhp = m.maxhp;
+ hp = m.hp;
+ gold = m.gold;
+ exp = m.exp;
+}
+
long int Player::setFlags(long int newflags)
{
flags = newflags;
return getFlags();
}
+void Player::setWeapon(item &weap)
+{
+ w = &weap;
+}
+void Player::setArmor(item &arm)
+{
+ a = &arm;
+}
+
+void Player::setName(string &n)
+{
+ name = n;
+}
+
+void Player::setName(const char *n)
+{
+ name = n;
+}
+
+void Player::setLevel(int l)
+{
+ if (l > 0 && l <= REALLEVELS)
+ {
+ level = l;
+ }
+}
+
+void Player::setExp(long int e)
+{
+ if (e <= 2000000000 && e > 0)
+ {
+ exp = e;
+ }
+}
+
+void Player::setGold(long int g)
+{
+ if (g <= 2000000000 && g >= 0)
+ {
+ gold = g;
+ }
+ else if (g > 2000000000)
+ {
+ gold = 2000000000;
+ }
+}
+
+void Player::setBank(long int b)
+{
+ if (b <= 2000000000 && b >= 0)
+ {
+ bank = b;
+ }
+ else if (b > 2000000000)
+ {
+ bank = 2000000000;
+ }
+}
+
+void Player::setHP(int h)
+{
+ hp = h;
+
+ if (hp < 0)
+ {
+ hp = 0;
+ }
+ else if (hp > maxhp)
+ {
+ hp = maxhp; // You can't go higher than your maxhp
+ }
+}
+
+void Player::setMaxHP(int m)
+{
+ maxhp = m;
+
+ if (maxhp < 0)
+ {
+ maxhp = 10;
+ }
+ else if (maxhp > 32000)
+ {
+ maxhp = 32000;
+ }
+}
+
+void Player::setStrength(int s)
+{
+ strength = s;
+ if (strength < 0)
+ {
+ strength = 1;
+ }
+ else if (strength > 32000)
+ {
+ strength = 32000;
+ }
+}
+
+void Player::setDefense(int d)
+{
+ defense = d;
+
+ if (defense < 0)
+ {
+ defense = 1;
+ }
+ else if (defense > 32000)
+ {
+ defense = 32000;
+ }
+}
+
+void Player::setForestFights(int ff)
+{
+ forest_fights = ff;
+
+ if (forest_fights < 0)
+ {
+ forest_fights = 0;
+ }
+ else if (forest_fights > 32000)
+ {
+ forest_fights = 32000;
+ }
+}
+
+void Player::setPlayerFights(int pf)
+{
+ player_fights = pf;
+
+ if (player_fights < 0)
+ {
+ player_fights = 0;
+ }
+ else if (player_fights > 32000)
+ {
+ player_fights = 32000;
+ }
+}
+
+void Player::setClient(aClient *c)
+{
+ client = c;
+}
+
+void Player::setMonster(Monster *m)
+{
+ fight = new Monster(m);
+}
+
+void Player::setMyMaster(Monster *m)
+{
+ master = new Monster(m);
+}
+
+void Player::setBattle(aClient *b)
+{
+ battle = b;
+}
+
+void Player::addExp(int e)
+{
+ setExp(getExp() + e);
+}
+
+void Player::addGold(int g)
+{
+ setGold(getGold() + g);
+}
+
+void Player::addBank(int b)
+{
+ setBank(getBank() + b);
+}
+
+void Player::addHP(int h)
+{
+ setHP(getHP() + h);
+}
+
+void Player::addMaxHP(int m)
+{
+ setMaxHP(getMaxHP() + m);
+}
+
+void Player::addStrength(int s)
+{
+ setStrength(getStrength() + s);
+}
+
+void Player::addDefense(int d)
+{
+ setDefense(getDefense() + d);
+}
+
+void Player::addForestFights(int ff)
+{
+ setForestFights(getForestFights() + ff);
+}
+
+void Player::addPlayerFights(int pf)
+{
+ setPlayerFights(getPlayerFights() + pf);
+}
+
+void Player::addLevel(int l)
+{
+ setLevel(getLevel() + l);
+}
+
+void Player::subtractLevel(int l)
+{
+ setLevel(getLevel() - l);
+}
+
+void Player::subtractExp(long int e)
+{
+ setExp(getExp() - e);
+}
+
+void Player::subtractGold(long int g)
+{
+ setGold(getGold() - g);
+}
+
+void Player::subtractBank(long int b)
+{
+ setBank(getBank() - b);
+}
+
+void Player::subtractHP(int h)
+{
+ setHP(getHP() - h);
+}
+
+void Player::subtractMaxHP(int m)
+{
+ setMaxHP(getMaxHP() - m);
+}
+
+void Player::subtractStrength(int s)
+{
+ setStrength(getStrength() - s);
+}
+
+void Player::subtractDefense(int d)
+{
+ setDefense(getDefense() - d);
+}
+
+void Player::subtractForestFights(int ff)
+{
+ setForestFights(getForestFights() - ff);
+}
+
+void Player::subtractPlayerFights(int pf)
+{
+ setPlayerFights(getPlayerFights() - pf);
+}
+
+void Player::delMonster()
+{
+ if (fight)
+ delete fight;
+
+ fight = NULL;
+}
+
+void Player::delMaster()
+{
+ if (master)
+ delete master;
+
+ master = NULL;
+}
+
+void Player::delBattle()
+{
+ // Don't delete the other player's aClient
+ battle = NULL;
+}
+
+bool Player::operator < (Player &right)
+{
+ if (getLevel() < right.getLevel())
+ {
+ return true;
+ }
+ else if (getExp() < right.getExp())
+ {
+ return true;
+ }
+ else if (getStrength() < right.getStrength() && getDefense() < right.getDefense())
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}