NXEngine v1.0.0.6
[NXEngine.git] / playerstats.cpp
blobac221f9b9d6ac81a9e9ccbd2e2b80b0efbaf253a
2 #include "nx.h"
3 #include "playerstats.fdh"
6 void AddHealth(int hp)
8 player->hp += hp;
9 if (player->hp > player->maxHealth) player->hp = player->maxHealth;
12 void AddXP(int xp, bool quiet)
14 Weapon *weapon = &player->weapons[player->curWeapon];
15 bool leveled_up = false;
17 weapon->xp += xp;
19 // leveling up...
20 while(weapon->xp > weapon->max_xp[weapon->level])
22 if (weapon->level < 2)
24 weapon->xp -= weapon->max_xp[weapon->level];
25 weapon->level++;
26 leveled_up = true;
28 else
30 weapon->xp = weapon->max_xp[weapon->level];
31 break;
35 statusbar.xpflashcount = 30;
37 if (player->curWeapon == WPN_SPUR)
38 leveled_up = false;
40 if (!quiet)
42 if (!player->hide)
44 if (leveled_up)
46 sound(SND_LEVEL_UP);
47 effect(player->CenterX(), player->CenterY(), EFFECT_LEVELUP);
49 else
51 sound(SND_GET_XP);
55 player->XPText->AddQty(xp);
59 void SubXP(int xp, bool quiet)
61 Weapon *weapon = &player->weapons[player->curWeapon];
62 bool leveled_down = false;
64 weapon->xp -= xp;
66 // leveling down...
67 while(weapon->xp < 0)
69 if (weapon->level > 0)
71 weapon->level--;
72 weapon->xp += weapon->max_xp[weapon->level];
73 leveled_down = true;
75 else
77 weapon->xp = 0;
78 break;
82 if (player->curWeapon == WPN_SPUR)
83 leveled_down = false;
85 if (leveled_down && !quiet && !player->hide)
87 effect(player->CenterX(), player->CenterY(), EFFECT_LEVELDOWN);
92 void c------------------------------() {}
95 // add an item to the inventory list (generates an error msg if inventory is full)
96 void AddInventory(int item)
98 if (player->ninventory+1 >= MAX_INVENTORY)
99 { staterr("<<<AddInventory: inventory is full>>"); game.running = 0; return; }
101 player->inventory[player->ninventory++] = item;
102 sound(SND_GET_ITEM);
103 RefreshInventoryScreen();
106 // remove an item from the inventory list (does nothing if it's not in there)
107 void DelInventory(int item)
109 int slot;
110 int i;
112 for(;;)
114 slot = FindInventory(item);
115 if (slot == -1) break;
117 for(i=slot;i<player->ninventory-1;i++)
119 player->inventory[i] = player->inventory[i+1];
121 player->ninventory--;
124 RefreshInventoryScreen();
127 // find which slot an item is in (returns -1 if player does not have it)
128 int FindInventory(int item)
130 return CheckInventoryList(item, player->inventory, player->ninventory);
133 // checks if the inventory list given contains the given item.
134 // if so, returns the index of the item. if not, returns -1.
135 int CheckInventoryList(int item, int *list, int nitems)
137 int i;
139 for(i=0;i<nitems;i++)
140 if (list[i] == item) return i;
142 return -1;
147 void c------------------------------() {}
150 // AM+ command.
151 // adds "ammo" ammo to the specified weapons ammo and maxammo,
152 // and if you don't have it already, gives it to you.
153 void GetWeapon(int wpn, int ammo)
155 if (!player->weapons[wpn].hasWeapon)
157 player->weapons[wpn].ammo = 0; // will be filled to full by AddAmmo below
158 player->weapons[wpn].maxammo = ammo;
159 player->weapons[wpn].level = 0;
160 player->weapons[wpn].xp = 0;
161 player->weapons[wpn].hasWeapon = true;
162 player->curWeapon = wpn;
164 else
165 { // missile capacity powerups
166 player->weapons[wpn].maxammo += ammo;
169 AddAmmo(wpn, ammo);
170 sound(SND_GET_ITEM);
173 // AM- command. Drops specified weapon.
174 void LoseWeapon(int wpn)
176 player->weapons[wpn].hasWeapon = false;
178 // lost current weapon?
179 if (wpn == player->curWeapon)
181 // in case he has no weapons left at all
182 player->curWeapon = WPN_NONE;
184 // find a new weapon for him
185 for(int i=0;i<WPN_COUNT;i++)
187 if (player->weapons[i].hasWeapon)
189 player->curWeapon = i;
190 break;
196 // TAM command.
197 void TradeWeapon(int oldwpn, int newwpn, int ammo)
199 int oldcurwpn = player->curWeapon;
201 // ammo 0 = no change; used when you get missiles are upgraded to Super Missiles
202 if (ammo == 0)
203 ammo = player->weapons[oldwpn].maxammo;
205 GetWeapon(newwpn, ammo);
206 LoseWeapon(oldwpn);
208 // switch to new weapon if the weapon traded was the
209 // one we were using. Otherwise, don't change current weapon.
210 if (oldwpn == oldcurwpn)
211 player->curWeapon = newwpn;
212 else
213 player->curWeapon = oldcurwpn;
216 // adds "ammo" ammo to the specified weapon, but doesn't go over the limit.
217 void AddAmmo(int wpn, int ammo)
219 player->weapons[wpn].ammo += ammo;
220 if (player->weapons[wpn].ammo > player->weapons[wpn].maxammo)
221 player->weapons[wpn].ammo = player->weapons[wpn].maxammo;
224 // sets all weapons to max ammo. AE+ command.
225 void RefillAllAmmo(void)
227 for(int i=0;i<WPN_COUNT;i++)
229 if (player->weapons[i].hasWeapon)
230 player->weapons[i].ammo = player->weapons[i].maxammo;