|
From: <be...@us...> - 2017-03-19 18:25:58
|
Revision: 1021
http://sourceforge.net/p/freesynd/code/1021
Author: benblan
Date: 2017-03-19 18:25:55 +0000 (Sun, 19 Mar 2017)
Log Message:
-----------
Rework on weapons : gameplaymenu now animate only weapons that have no owner
Modified Paths:
--------------
freesynd/trunk/src/agent.cpp
freesynd/trunk/src/agent.h
freesynd/trunk/src/agentmanager.cpp
freesynd/trunk/src/agentmanager.h
freesynd/trunk/src/app.cpp
freesynd/trunk/src/core/gamecontroller.cpp
freesynd/trunk/src/core/gamecontroller.h
freesynd/trunk/src/ia/actions.cpp
freesynd/trunk/src/ia/behaviour.cpp
freesynd/trunk/src/menus/gameplaymenu.cpp
freesynd/trunk/src/menus/maprenderer.cpp
freesynd/trunk/src/menus/minimaprenderer.cpp
freesynd/trunk/src/mission.cpp
freesynd/trunk/src/mission.h
freesynd/trunk/src/missionmanager.cpp
freesynd/trunk/src/missionmanager.h
freesynd/trunk/src/model/shot.cpp
freesynd/trunk/src/model/weaponholder.cpp
freesynd/trunk/src/model/weaponholder.h
freesynd/trunk/src/ped.cpp
freesynd/trunk/src/ped.h
freesynd/trunk/src/weapon.cpp
freesynd/trunk/src/weapon.h
Modified: freesynd/trunk/src/agent.cpp
===================================================================
--- freesynd/trunk/src/agent.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/agent.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -47,10 +47,6 @@
is_alive_ = true;
}
-Agent::~Agent() {
- removeAllWeapons();
-}
-
bool Agent::saveToFile(PortableFile &file) {
// id
file.write32(id_);
@@ -91,7 +87,7 @@
bool Agent::loadFromFile(PortableFile &infile, const FormatVersion& v) {
// if this instance has already been populated reset it
clearSlots();
- removeAllWeapons();
+ destroyAllWeapons();
// id
id_ = infile.read32();
// update counter
Modified: freesynd/trunk/src/agent.h
===================================================================
--- freesynd/trunk/src/agent.h 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/agent.h 2017-03-19 18:25:55 UTC (rev 1021)
@@ -44,7 +44,7 @@
class Agent : public WeaponHolder, public ModOwner {
public:
Agent(const char *agent_name, bool male);
- ~Agent();
+ ~Agent() {}
int getId() { return id_;}
const char *getName() { return name_.c_str(); }
Modified: freesynd/trunk/src/agentmanager.cpp
===================================================================
--- freesynd/trunk/src/agentmanager.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/agentmanager.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -118,31 +118,25 @@
agents_.add(NULL);
}
-AgentManager::~AgentManager() {
+void AgentManager::loadAgents() {
+ // TODO : load names from file
+}
+
+void AgentManager::destroy() {
for (int i = 0; i != MAX_AGENT; ++i) {
if (agents_.get(i)) {
delete agents_.get(i);
+ agents_.setAt(i, NULL);
}
}
- agents_.clear();
clearSquad();
}
-void AgentManager::loadAgents() {
- // TODO : load names from file
-}
-
void AgentManager::reset(bool onlyWomen) {
nextName_ = 0;
// First delete existing agents
- clearSquad();
- for (int i = 0; i < MAX_AGENT; i++) {
- if (agents_.get(i)) {
- delete agents_.get(i);
- agents_.setAt(i, NULL);
- }
- }
+ destroy();
// Then recreate the first 8 available agents
for (size_t i = 0; i < 8; i++) {
@@ -160,8 +154,6 @@
void AgentManager::destroyAgentSlot(size_t squadSlot) {
Agent *p_agent = squadMember(squadSlot);
- p_agent->removeAllWeapons();
- p_agent->clearSlots();
setSquadMember(squadSlot, NULL);
for (int inc = 0; inc < AgentManager::MAX_AGENT; inc++) {
if (agent(inc) == p_agent) {
Modified: freesynd/trunk/src/agentmanager.h
===================================================================
--- freesynd/trunk/src/agentmanager.h 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/agentmanager.h 2017-03-19 18:25:55 UTC (rev 1021)
@@ -43,7 +43,7 @@
* Provides methods for managing player's agents and squad.
* The Squad is the team of agent selected for a mission. It holds up to 4 agents.
* An agent can be selected for a squad but not active : in this case, he will not
- * participate
+ * participate
*/
class AgentManager {
public:
@@ -61,8 +61,10 @@
static const size_t kSlot4;
AgentManager();
- ~AgentManager();
+ ~AgentManager() {}
+ void destroy();
+
void setWeaponManager(WeaponManager *pWeaponManager) {
pWeaponManager_ = pWeaponManager;
}
@@ -94,7 +96,7 @@
* \param n The agent's index in the team (from 0 to 3)
* \param a The new agent
*/
- void setSquadMember(size_t slotId, Agent *pAgent) {
+ void setSquadMember(size_t slotId, Agent *pAgent) {
assert(slotId < kMaxSlot);
a_squad_[slotId] = pAgent;
}
Modified: freesynd/trunk/src/app.cpp
===================================================================
--- freesynd/trunk/src/app.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/app.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -79,13 +79,6 @@
App::~App() {
}
-/*!
- * Destory the application.
- */
-void App::destroy() {
- menus_.destroy();
-}
-
static void addMissingSlash(string& str) {
if (str[str.length() - 1] != '/') str.push_back('/');
}
@@ -474,7 +467,7 @@
void App::cheatEquipFancyWeapons() {
for (int i = 0; i < AgentManager::MAX_AGENT; i++) {
if (g_gameCtrl.agents().agent(i)) {
- g_gameCtrl.agents().agent(i)->removeAllWeapons();
+ g_gameCtrl.agents().agent(i)->destroyAllWeapons();
#ifdef _DEBUG
g_gameCtrl.agents().agent(i)->addWeapon(
WeaponInstance::createInstance(g_gameCtrl.weaponManager().getWeapon(Weapon::Minigun)));
@@ -489,7 +482,7 @@
g_gameCtrl.agents().agent(i)->addWeapon(
WeaponInstance::createInstance(g_gameCtrl.weaponManager().getWeapon(Weapon::Persuadatron)));
g_gameCtrl.agents().agent(i)->addWeapon(
- WeaponInstance::createInstance(g_gameCtrl.weaponManager().getWeapon(Weapon::Laser)));
+ WeaponInstance::createInstance(g_gameCtrl.weaponManager().getWeapon(Weapon::EnergyShield)));
g_gameCtrl.agents().agent(i)->addWeapon(
WeaponInstance::createInstance(g_gameCtrl.weaponManager().getWeapon(Weapon::AccessCard)));
#else
@@ -582,6 +575,16 @@
return true;
}
+/*!
+ * Destroy the application.
+ */
+void App::destroy() {
+ game_ctlr_->clearAllListeners();
+ menus_.destroy();
+
+ game_ctlr_->destroy();
+}
+
void App::waitForKeyPress() {
while (running_) {
Modified: freesynd/trunk/src/core/gamecontroller.cpp
===================================================================
--- freesynd/trunk/src/core/gamecontroller.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/core/gamecontroller.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -36,9 +36,10 @@
agents_.setWeaponManager(&weaponMgr_);
}
-GameController::~GameController() {
- game_listeners_.clear();
- mission_listeners_.clear();
+GameController::~GameController() {}
+
+void GameController::destroy() {
+ agents_.destroy();
}
bool GameController::reset() {
@@ -50,6 +51,11 @@
return true;
}
+void GameController::clearAllListeners() {
+ game_listeners_.clear();
+ mission_listeners_.clear();
+}
+
/*!
* Adds a listener to the list of listeners for a stream.
* \param pListener The listener
Modified: freesynd/trunk/src/core/gamecontroller.h
===================================================================
--- freesynd/trunk/src/core/gamecontroller.h 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/core/gamecontroller.h 2017-03-19 18:25:55 UTC (rev 1021)
@@ -54,6 +54,9 @@
*/
bool reset();
+ //! Delete all ressources. Called by App:destroy()
+ void destroy();
+
//*************************************
// Event management
//*************************************
@@ -63,6 +66,8 @@
void removeListener(GameEventListener *pListener, GameEvent::EEventStream stream);
//! Sends the event to the listeners
void fireGameEvent(GameEvent & evt);
+ //! Removes all listeners from every stream
+ void clearAllListeners();
//*************************************
// Managers
Modified: freesynd/trunk/src/ia/actions.cpp
===================================================================
--- freesynd/trunk/src/ia/actions.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/ia/actions.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -567,6 +567,7 @@
pWeapon_->setOwner(pPed);
pWeapon_->deactivate();
pPed->addWeapon(pWeapon_);
+ pMission->removeWeaponOnGround(pWeapon_);
setSucceeded();
}
Modified: freesynd/trunk/src/ia/behaviour.cpp
===================================================================
--- freesynd/trunk/src/ia/behaviour.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/ia/behaviour.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -256,9 +256,9 @@
WeaponInstance *pWeaponFound = NULL;
double currentDistance = kMaxRangeForSearchingWeapon;
- int numweapons = pMission->numWeapons();
+ int numweapons = pMission->numWeaponsOnGround();
for (int32 i = 0; i < numweapons; ++i) {
- WeaponInstance *w = pMission->weapon(i);
+ WeaponInstance *w = pMission->weaponOnGround(i);
if (!w->hasOwner() && w->canShoot() && w->ammoRemaining() > 0) {
double length = 0;
if (pMission->getPathLengthBetween(pPed, w, kMaxRangeForSearchingWeapon, &length) == 0) {
Modified: freesynd/trunk/src/menus/gameplaymenu.cpp
===================================================================
--- freesynd/trunk/src/menus/gameplaymenu.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/menus/gameplaymenu.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -371,8 +371,8 @@
for (size_t i = 0; i < mission_->numVehicles(); i++)
change |= mission_->vehicle(i)->animate(diff);
- for (size_t i = 0; i < mission_->numWeapons(); i++)
- change |= mission_->weapon(i)->animate(diff);
+ for (size_t i = 0; i < mission_->numWeaponsOnGround(); i++)
+ change |= mission_->weaponOnGround(i)->animate(diff);
for (size_t i = 0; i < mission_->numStatics(); i++)
change |= mission_->statics(i)->animate(diff, mission_);
@@ -585,8 +585,8 @@
}
}
- for (size_t i = 0; mission_ && i < mission_->numWeapons(); ++i) {
- WeaponInstance *w = mission_->weapon(i);
+ for (size_t i = 0; mission_ && i < mission_->numWeaponsOnGround(); ++i) {
+ WeaponInstance *w = mission_->weaponOnGround(i);
if (w->map() != -1) {
Point2D scPt;
Modified: freesynd/trunk/src/menus/maprenderer.cpp
===================================================================
--- freesynd/trunk/src/menus/maprenderer.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/menus/maprenderer.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -168,8 +168,8 @@
}
// weapons
- for (size_t i = 0; i < pMission_->numWeapons(); i++) {
- WeaponInstance *pWeapon = pMission_->weapon(i);
+ for (size_t i = 0; i < pMission_->numWeaponsOnGround(); i++) {
+ WeaponInstance *pWeapon = pMission_->weaponOnGround(i);
if (pWeapon->isDrawable() && isObjectInsideDrawingArea(pWeapon, viewport)) {
addObjectToDraw(pWeapon);
}
Modified: freesynd/trunk/src/menus/minimaprenderer.cpp
===================================================================
--- freesynd/trunk/src/menus/minimaprenderer.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/menus/minimaprenderer.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -526,9 +526,9 @@
void GamePlayMinimapRenderer::drawWeapons(uint8 * a_minimap) {
const size_t weapon_size = 2;
- for (size_t i = 0; i < p_mission_->numWeapons(); i++)
+ for (size_t i = 0; i < p_mission_->numWeaponsOnGround(); i++)
{
- WeaponInstance * w = p_mission_->weapon(i);
+ WeaponInstance * w = p_mission_->weaponOnGround(i);
// we draw weapons that have no owner ie that are on the ground
// and are not destroyed
if (w->map() == -1)
Modified: freesynd/trunk/src/mission.cpp
===================================================================
--- freesynd/trunk/src/mission.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/mission.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -281,17 +281,10 @@
{
while (p->numWeapons()) {
WeaponInstance *wi = p->removeWeaponAtIndex(0);
- std::vector < WeaponInstance * >::iterator it =
- weaponsOnGround_.begin();
- while (it != weaponsOnGround_.end() && *it != wi)
- it++;
- assert(it != weaponsOnGround_.end());
- weaponsOnGround_.erase(it);
wi->deactivate();
// auto-reload for pistol
if (wi->isInstanceOf(Weapon::Pistol))
wi->reload();
- wi->resetWeaponUsedTime();
pAg->addWeapon(wi);
}
}
@@ -357,7 +350,7 @@
p_squad_->clear();
}
-void Mission::addWeapon(WeaponInstance * w)
+void Mission::addWeaponToGround(WeaponInstance * w)
{
for (unsigned int i = 0; i < weaponsOnGround_.size(); i++) {
// TODO : check if == operator is used correctly (see == in WeaponInstance)
@@ -367,6 +360,14 @@
weaponsOnGround_.push_back(w);
}
+void Mission::removeWeaponOnGround(WeaponInstance *pWeapon) {
+ for (unsigned int i = 0; i < weaponsOnGround_.size(); i++) {
+ if (weaponsOnGround_[i] == pWeapon) {
+ weaponsOnGround_.erase(weaponsOnGround_.begin() + i);
+ }
+ }
+}
+
MapObject * Mission::findObjectWithNatureAtPos(int tilex, int tiley, int tilez,
MapObject::ObjectNature *nature, int *searchIndex,
bool only) {
Modified: freesynd/trunk/src/mission.h
===================================================================
--- freesynd/trunk/src/mission.h 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/mission.h 2017-03-19 18:25:55 UTC (rev 1021)
@@ -193,9 +193,10 @@
Vehicle *vehicle(size_t i) { return vehicles_[i]; }
void addVehicle(Vehicle *pVehicle) { vehicles_.push_back(pVehicle); }
- size_t numWeapons() { return weaponsOnGround_.size(); }
- WeaponInstance *weapon(size_t i) { return weaponsOnGround_[i]; }
- void addWeapon(WeaponInstance *w);
+ size_t numWeaponsOnGround() { return weaponsOnGround_.size(); }
+ WeaponInstance *weaponOnGround(size_t i) { return weaponsOnGround_[i]; }
+ void addWeaponToGround(WeaponInstance *w);
+ void removeWeaponOnGround(WeaponInstance *pWeapon);
size_t numStatics() { return statics_.size(); }
Static *statics(size_t i) { return statics_[i]; }
Modified: freesynd/trunk/src/missionmanager.cpp
===================================================================
--- freesynd/trunk/src/missionmanager.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/missionmanager.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -329,8 +329,8 @@
memset(di.vindx, 0xFF, 2*64);
memset(di.pindx, 0xFF, 2*256);
memset(di.driverindx, 0xFF, 2*256);
- memset(di.windx, 0xFF, 2*512);
+
try {
createVehicles(level_data, di, p_mission);
@@ -402,13 +402,12 @@
if (offset_owner != 0) {
offset_owner = (offset_owner - 2) / 92; // 92 = ped data size
if (offset_owner > 7 && di.pindx[offset_owner] != 0xFFFF) {
- // TODO: still there is a problem of weapons setup
+ // NOTE: still there is a problem of weapons setup
// some police officers can have more then 1 weapon
// others none (pacific Rim)
pMission->ped(di.pindx[offset_owner])->addWeapon(w);
w->setOwner(pMission->ped(di.pindx[offset_owner]));
- di.windx[i] = pMission->numWeapons();
- pMission->addWeapon(w);
+ di.weapons[i] = w;
} else {
delete w;
}
@@ -417,9 +416,8 @@
}
} else {
w->setMap(pMission->mapId());
- w->setOwner(NULL);
- di.windx[i] = pMission->numWeapons();
- pMission->addWeapon(w);
+ di.weapons[i] = w;
+ pMission->addWeaponToGround(w);
}
}
}
@@ -427,6 +425,7 @@
WeaponInstance * MissionManager::create_weapon_instance(const LevelData::Weapons &gamdata) {
Weapon::WeaponType wType = Weapon::Unknown;
+ WeaponInstance *pNewWeapon = NULL;
switch (gamdata.sub_type) {
case 0x01:
@@ -472,20 +471,19 @@
wType = Weapon::EnergyShield;
break;
default:
- wType = Weapon::Unknown;
- break;
+ FSERR(Log::k_FLG_GAME, "Mission", "create_weapon_instance", ("unknown weapon type : %d", gamdata.sub_type));
+ return NULL;
}
Weapon *pWeapon = g_gameCtrl.weaponManager().getWeapon(wType);
if (pWeapon) {
- WeaponInstance *wi = WeaponInstance::createInstance(pWeapon);
- wi->setPosition(gamdata.mapposx[1], gamdata.mapposy[1],
+ pNewWeapon = WeaponInstance::createInstance(pWeapon);
+ pNewWeapon->setPosition(gamdata.mapposx[1], gamdata.mapposy[1],
READ_LE_UINT16(gamdata.mapposz) >> 7, gamdata.mapposx[0],
gamdata.mapposy[0], gamdata.mapposz[0] & 0x7F);
- return wi;
}
- return NULL;
+ return pNewWeapon;
}
@@ -669,10 +667,6 @@
}
if (p->isOurAgent()) {
- // adds all agent's weapons to the mission weapons
- for (int wi=0; wi<p->numWeapons(); wi++) {
- pMission->addWeapon(p->weapon(wi));
- }
// adds the agent to the mission squad
pMission->getSquad()->setMember(i, p);
} else {
@@ -901,12 +895,14 @@
if (bindx >= 0x9562 && bindx < 0xDD62) {
bindx -= 0x9562;
cindx = bindx / 36;
- if ((cindx * 36) == bindx && di.windx[cindx] != 0xFFFF) {
- objd = new ObjTakeWeapon(pMission->weapon(di.windx[cindx]));
- } else
- printf("0x05 incorrect offset");
- } else
- printf("0x05 type not matched %X", bindx);
+ if ((cindx * 36) == bindx && di.weapons[cindx] != NULL) {
+ objd = new ObjTakeWeapon(di.weapons[cindx]);
+ } else {
+ FSERR(Log::k_FLG_GAME, "Mission", "createObjectives", ("Error creating Take Weapon objective(0x05) : incorrect offset %d", cindx));
+ }
+ } else {
+ FSERR(Log::k_FLG_GAME, "Mission", "createObjectives", ("Error creating Take Weapon objective(0x05) : type not matched %X", bindx));
+ }
break;
case 0x0A:
Modified: freesynd/trunk/src/missionmanager.h
===================================================================
--- freesynd/trunk/src/missionmanager.h 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/missionmanager.h 2017-03-19 18:25:55 UTC (rev 1021)
@@ -63,7 +63,10 @@
uint16 pindx[256];
// contains indexes for driver's vehicle
uint16 driverindx[256];
- uint16 windx[512];
+ //uint16 windx[512];
+ WeaponInstance * weapons[512];
+
+ DataIndex() : weapons() {}
};
private:
Modified: freesynd/trunk/src/model/shot.cpp
===================================================================
--- freesynd/trunk/src/model/shot.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/model/shot.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -364,8 +364,8 @@
}
// look at all bombs on the ground except the weapon that generated the shot
- for (size_t i = 0; i < pMission->numWeapons(); ++i) {
- WeaponInstance *w = pMission->weapon(i);
+ for (size_t i = 0; i < pMission->numWeaponsOnGround(); ++i) {
+ WeaponInstance *w = pMission->weaponOnGround(i);
if (w->isInstanceOf(Weapon::TimeBomb) && w != dmg_.pWeapon && !w->hasOwner() && w->isAlive()) {
WorldPoint weaponPosW(w->position());
if (pMission->checkBlockedByTile(originLocW, &weaponPosW, false, dmg_.range) == 1) {
Modified: freesynd/trunk/src/model/weaponholder.cpp
===================================================================
--- freesynd/trunk/src/model/weaponholder.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/model/weaponholder.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -40,6 +40,10 @@
updtPreferedWeapon();
}
+WeaponHolder::~WeaponHolder() {
+ destroyAllWeapons();
+}
+
/*!
* Adds the givent weapon to the inventory.
* Weapon is placed at the end of the inventory.
@@ -46,10 +50,10 @@
* \param w The weapon to add
*/
void WeaponHolder::addWeapon(WeaponInstance *w) {
- assert(w);
- assert(weapons_.size() < kMaxHoldedWeapons);
- w->setDrawable(false);
- weapons_.push_back(w);
+ if (w != NULL && weapons_.size() < kMaxHoldedWeapons) {
+ w->setDrawable(false);
+ weapons_.push_back(w);
+ }
}
/*!
@@ -102,10 +106,9 @@
}
/*!
- * Removes all weapons from the inventory.
- * Caller is responsible for freeing the removed instances.
+ * Removes and delete all weapons from the inventory.
*/
-void WeaponHolder::removeAllWeapons() {
+void WeaponHolder::destroyAllWeapons() {
while (weapons_.size())
delete removeWeaponAtIndex(0);
}
Modified: freesynd/trunk/src/model/weaponholder.h
===================================================================
--- freesynd/trunk/src/model/weaponholder.h 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/model/weaponholder.h 2017-03-19 18:25:55 UTC (rev 1021)
@@ -78,7 +78,7 @@
static const uint8 kMaxHoldedWeapons;
WeaponHolder();
- virtual ~WeaponHolder() {}
+ virtual ~WeaponHolder();
uint8 numWeapons() { return weapons_.size(); }
@@ -94,8 +94,8 @@
//! Removes the given weapon from the inventory.
void removeWeapon(WeaponInstance *w);
- //! Removes all weapons in the inventory
- void removeAllWeapons();
+ //! Removes and delete all weapons in the inventory
+ void destroyAllWeapons();
//! Selects the weapon at given index in the inventory
void selectWeapon(uint8 n);
Modified: freesynd/trunk/src/ped.cpp
===================================================================
--- freesynd/trunk/src/ped.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/ped.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -426,6 +426,11 @@
}
}
+ WeaponInstance *pWeapon = selectedWeapon();
+ if (pWeapon && pWeapon->isInstanceOf(Weapon::EnergyShield)) {
+ pWeapon->animate(elapsed);
+ }
+
return update;
}
@@ -533,7 +538,6 @@
* Return true if :
* - is not doing something that prevents him from using weapon
* - is not already using a weapon
- * - has a weapon in hand
* - weapon is usable (ie a shooting weapon or a medikit)
*/
bool PedInstance::canAddUseWeaponAction(WeaponInstance *pWeapon) {
@@ -546,9 +550,7 @@
}
WeaponInstance *pWi = pWeapon != NULL ? pWeapon : selectedWeapon();
- return (pWi != NULL &&
- (pWi->canShoot() || pWi->isInstanceOf(Weapon::MediKit)) &&
- pWi->ammoRemaining() > 0);
+ return pWi != NULL && pWi->usesAmmo() && pWi->ammoRemaining() > 0;
}
/*!
@@ -582,7 +584,7 @@
int PedInstance::getTimeBetweenShoots(WeaponInstance *pWeapon) {
// TODO : Add IPA and mods influence
return kDefaultShootReactionTime +
- pWeapon->getClass()->timeReload();
+ pWeapon->getClass()->reloadTime();
}
/*!
@@ -871,6 +873,8 @@
// we cas use medikit only if ped is hurt
return health() != startHealth() &&
canAddUseWeaponAction(pNewWeapon);
+ } else if (pNewWeapon->isInstanceOf(Weapon::EnergyShield)) {
+ return canAddUseWeaponAction(pNewWeapon);
}
return true;
@@ -956,7 +960,9 @@
if(pWeapon) {
pWeapon->setMap(map_);
+ pWeapon->setDrawable(true);
pWeapon->setPosition(pos_);
+ g_Session.getMission()->addWeaponToGround(pWeapon);
}
return pWeapon;
@@ -983,14 +989,6 @@
}
}
-void PedInstance::destroyAllWeapons() {
- while (!weapons_.empty()) {
- WeaponInstance * w = removeWeaponAtIndex(0);
- // TODO : Delete weapon
- w->setDrawable(false);
- }
-}
-
bool PedInstance::wePickupWeapon() {
return (state_ & pa_smPickUp) != 0;
}
@@ -1226,7 +1224,6 @@
} else {
setDrawnAnim(PedInstance::ad_NoAnimation);
}
- destroyAllWeapons();
break;
case MapObject::dmg_Explosion:
case MapObject::dmg_Burn:
@@ -1238,7 +1235,6 @@
// was burning because not enough protected or suicide
// so die burning
setDrawnAnim(PedInstance::ad_DieBurnAnim);
- destroyAllWeapons();
}
break;
default:
Modified: freesynd/trunk/src/ped.h
===================================================================
--- freesynd/trunk/src/ped.h 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/ped.h 2017-03-19 18:25:55 UTC (rev 1021)
@@ -359,7 +359,6 @@
//*************************************
WeaponInstance * dropWeapon(uint8 index);
void dropAllWeapons();
- void destroyAllWeapons();
bool wePickupWeapon();
//*************************************
Modified: freesynd/trunk/src/weapon.cpp
===================================================================
--- freesynd/trunk/src/weapon.cpp 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/weapon.cpp 2017-03-19 18:25:55 UTC (rev 1021)
@@ -265,14 +265,12 @@
WeaponInstance::WeaponInstance(Weapon * pWeaponClass, uint16 anId, int remainingAmmo) :
ShootableMapObject(anId, -1, MapObject::kNatureWeapon),
- bombSoundTimer(pWeaponClass->timeReload()), bombExplosionTimer(pWeaponClass->timeForShot()),
+ bombSoundTimer(pWeaponClass->reloadTime()), bombExplosionTimer(pWeaponClass->timeForShot()),
flamerTimer_(180) {
pWeaponClass_ = pWeaponClass;
ammo_remaining_ = remainingAmmo == -1 ? pWeaponClass->ammo() : remainingAmmo;
- weapon_used_time_ = 0;
pOwner_ = NULL;
activated_ = false;
- time_consumed_ = false;
if (pWeaponClass->getType() == Weapon::TimeBomb
|| pWeaponClass->getType() == Weapon::Flamer)
{
@@ -289,15 +287,10 @@
if (activated_) {
if (isInstanceOf(Weapon::EnergyShield)) {
- if (ammo_remaining_ == 0)
- return false;
- int tm_left = elapsed;
- int ammoused = getShots(&tm_left) * pWeaponClass_->ammoPerShot();
- if (ammoused >= ammo_remaining_) {
- ammo_remaining_ = 0;
- pOwner_->selectNextWeapon();
- } else
- ammo_remaining_ -= ammoused;
+ if (ammo_remaining_ && consumeAmmoForEnergyShield(elapsed)) {
+ // no more ammo so deselect shield
+ pOwner_->deselectWeapon();
+ }
return true;
} else if (isInstanceOf(Weapon::TimeBomb)) {
if (bombSoundTimer.update(elapsed)) {
@@ -309,12 +302,7 @@
fire(g_Session.getMission(), dmg, elapsed);
return true;
}
- time_consumed_ = true;
}
-
- updtWeaponUsedTime(elapsed);
- } else if (weapon_used_time_ != 0) {
- updtWeaponUsedTime(elapsed);
}
if (isDrawable()) {
@@ -324,6 +312,36 @@
return false;
}
+bool WeaponInstance::consumeAmmoForEnergyShield(int elapsed) {
+ int timeForShot = pWeaponClass_->timeForShot();
+ shieldTimeUsed_ += elapsed;
+
+ if (shieldTimeUsed_ >= timeForShot) {
+ // here time for shot is the unit of time for spending ammo
+ // there's no time for reloading
+
+ int remainingShots = ammo_remaining_ / pWeaponClass_->ammoPerShot();
+ if (ammo_remaining_ % pWeaponClass_->ammoPerShot()) {
+ remainingShots++;
+ }
+
+ // effective shots is the number of shot we have to do due to elapsed time
+ int effectiveShots = shieldTimeUsed_ / timeForShot;
+ shieldTimeUsed_ %= timeForShot;
+
+ if (effectiveShots > remainingShots) {
+ effectiveShots = remainingShots;
+ shieldTimeUsed_ = 0;
+ }
+
+ ammo_remaining_ -= effectiveShots * pWeaponClass_->ammoPerShot();
+ if (ammo_remaining_ < 0) {
+ ammo_remaining_ = 0;
+ }
+ }
+ return ammo_remaining_ == 0;
+}
+
void WeaponInstance::draw(int x, int y) {
addOffs(x, y);
g_App.gameSprites().drawFrame(pWeaponClass_->anim(), frame_, x, y);
@@ -336,87 +354,13 @@
g_App.gameSounds().play(pWeaponClass_->getSound());
}
-int WeaponInstance::getShots(int *elapsed, uint32 make_shots) {
- int time_for_shot = pWeaponClass_->timeForShot();
- int time_reload = pWeaponClass_->timeReload();
-#if 0
- // TODO: if owner exists these two values should change(IPA, mods)
- if (pOwner_)
-#endif
- // TODO check in weaponinstance animate double consuming of elapsed
- int time_full_shot = time_for_shot + time_reload;
- int elapsed_l = *elapsed;
- *elapsed = 0;
- time_consumed_ = true;
- if (weapon_used_time_ >= time_for_shot) {
- weapon_used_time_ += elapsed_l;
- if (weapon_used_time_ >= time_full_shot) {
- // reloading after previous shot
- weapon_used_time_ -= time_full_shot;
- } else {
- // reload consumed all time, no time for shooting
- return 0;
- }
- } else
- weapon_used_time_ += elapsed_l;
-
- if (weapon_used_time_ == 0) {
- return 0;
- }
- elapsed_l = 0;
-
- uint32 shots_can_do = 0xFFFFFFFF;
- if (shotProperty() & Weapon::spe_UsesAmmo) {
- shots_can_do = ammo_remaining_ / pWeaponClass_->ammoPerShot();
- if (ammo_remaining_ % pWeaponClass_->ammoPerShot())
- shots_can_do++;
- }
- uint32 shots = weapon_used_time_ / time_full_shot;
- weapon_used_time_ %= time_full_shot;
- bool adjusted = false;
- if (weapon_used_time_ >= time_for_shot) {
- shots++;
- adjusted = true;
- }
- // Adjusting time consumed and shots done to ammo
- // that can be used
- if (shots_can_do < shots) {
- if (adjusted) {
- shots--;
- adjusted = false;
- }
- if (shots_can_do < shots) {
- elapsed_l = time_full_shot * (shots - shots_can_do);
- shots = shots_can_do;
- } else
- elapsed_l = weapon_used_time_;
- weapon_used_time_ = 0;
- }
-
- if (make_shots != 0 && shots != 0 && make_shots < shots) {
- // we might have some time left here
- if (adjusted)
- shots--;
- if (make_shots < shots) {
- *elapsed = time_full_shot * (shots - make_shots) + elapsed_l;
- *elapsed += weapon_used_time_;
- shots = make_shots;
- } else
- *elapsed = elapsed_l + weapon_used_time_;
- weapon_used_time_ = 0;
- } else
- *elapsed = elapsed_l;
- return shots;
-}
-
void WeaponInstance::activate() {
activated_ = true;
+ shieldTimeUsed_ = 0;
}
void WeaponInstance::deactivate() {
activated_ = false;
- if (isInstanceOf(Weapon::TimeBomb))
- weapon_used_time_ = 0;
}
/*!
@@ -514,16 +458,3 @@
fire(g_Session.getMission(), d, 0);
}
}
-
-void WeaponInstance::updtWeaponUsedTime(int elapsed) {
- if (time_consumed_) {
- time_consumed_ = false;
- } else if (weapon_used_time_ != 0 ) {
- weapon_used_time_ += elapsed;
- if (weapon_used_time_ >= (pWeaponClass_->timeForShot()
- + pWeaponClass_->timeReload()))
- {
- weapon_used_time_ = 0;
- }
- }
-}
Modified: freesynd/trunk/src/weapon.h
===================================================================
--- freesynd/trunk/src/weapon.h 2017-03-18 11:18:10 UTC (rev 1020)
+++ freesynd/trunk/src/weapon.h 2017-03-19 18:25:55 UTC (rev 1021)
@@ -115,7 +115,7 @@
int ammoPerShot() { return ammo_per_shot_; }
int timeForShot() { return time_for_shot_; }
- int timeReload() { return time_reload_; }
+ int reloadTime() { return time_reload_; }
bool wasSubmittedToSearch() { return submittedToSearch_; }
void submitToSearch() { submittedToSearch_ = true; }
@@ -275,12 +275,13 @@
static WeaponInstance *createInstance(Weapon *pWeaponClass, int remainingAmmo = -1);
WeaponInstance(Weapon *w, uint16 id, int remainingAmmo = -1);
+ ~WeaponInstance() {};
+ //*************************************
+ // Properties
+ //*************************************
Weapon *getClass() const { return pWeaponClass_; }
- bool isInstanceOf(Weapon::WeaponType weaponType) { return pWeaponClass_->getType() == weaponType; }
- bool hasSameTypeAs(const WeaponInstance & otherWeapon) { return pWeaponClass_->getType() == otherWeapon.getClass()->getType();}
-
/*! Sets the owner of the weapon. */
void setOwner(PedInstance *pOwner) { pOwner_ = pOwner; }
/*! Return the owner of the weapon.*/
@@ -289,36 +290,19 @@
bool hasOwner() { return pOwner_ != NULL; }
int ammoRemaining() { return ammo_remaining_; }
- void reload() { ammo_remaining_ = pWeaponClass_->ammo(); }
- bool animate(int elapsed);
- void draw(int x, int y);
-
+ const char * name() { return pWeaponClass_->getName(); }
int range() { return pWeaponClass_->range(); }
int ammo() { return pWeaponClass_->ammo(); }
int rank() { return pWeaponClass_->rank(); }
+ int getWeight() { return pWeaponClass_->weight(); }
uint32 shotProperty() { return pWeaponClass_->shotProperty(); }
- const char * name() { return pWeaponClass_->getName(); }
-
Weapon::WeaponAnimIndex index() { return pWeaponClass_->index(); }
- bool operator==(WeaponInstance wi) {
- // TODO : check if this method is necessary
- return hasSameTypeAs(wi);
+ bool usesAmmo() {
+ return (shotProperty() & Weapon::spe_UsesAmmo) != 0;
}
- //! Plays the weapon's sound.
- void playSound();
-
- void resetWeaponUsedTime() { weapon_used_time_ = 0; }
-
- int getShots(int *elapsed = NULL, uint32 make_shots = 0);
-
- void activate();
- void deactivate();
-
- void handleHit(ShootableMapObject::DamageInflictType & d);
-
bool canShoot() {
return pWeaponClass_->canShoot();
}
@@ -327,14 +311,6 @@
return pWeaponClass_->doesPhysicalDmg();
}
- bool needsReloading() {
- return pWeaponClass_->ammo() > ammo_remaining_;
- }
-
- bool usesAmmo() {
- return (shotProperty() & Weapon::spe_UsesAmmo) != 0;
- }
-
bool doesDmgStrict(uint32 dmg_type) {
return pWeaponClass_->dmgType() == dmg_type;
}
@@ -344,35 +320,58 @@
MapObject::DamageType dmgType() {
return pWeaponClass_->dmgType();
}
- int getWeight() { return pWeaponClass_->weight(); }
- void updtWeaponUsedTime(int elapsed);
+ //*************************************
+ // Behaviour
+ //*************************************
+ bool isInstanceOf(Weapon::WeaponType weaponType) { return pWeaponClass_->getType() == weaponType; }
+ bool hasSameTypeAs(const WeaponInstance & otherWeapon) { return pWeaponClass_->getType() == otherWeapon.getClass()->getType();}
+
+ bool needsReloading() {
+ return pWeaponClass_->ammo() > ammo_remaining_;
+ }
+
+ void reload() { ammo_remaining_ = pWeaponClass_->ammo(); }
+
+ bool operator==(WeaponInstance wi) {
+ // TODO : check if this method is necessary
+ return hasSameTypeAs(wi);
+ }
+
+ //! Plays the weapon's sound.
+ void playSound();
+
+ void activate();
+ void deactivate();
+
+ bool animate(int elapsed);
+ void draw(int x, int y);
+
+ void handleHit(ShootableMapObject::DamageInflictType & d);
+
//! Use weapon
void fire(Mission *pMission, ShootableMapObject::DamageInflictType &dmg, int elapsed);
protected:
+ bool consumeAmmoForEnergyShield(int elapsed);
+
+protected:
static uint16 weaponIdCnt;
Weapon *pWeaponClass_;
/*! Owner of the weapon.*/
PedInstance *pOwner_;
int ammo_remaining_;
- /*! if this value is smaller time_for_shot_ shot cannot be done
- * if is greater then time_for_shot_ reload is in execution
- * if is greater then time_for_shot_ + time_reload_ then full shot is done
- * */
- int weapon_used_time_;
- // used for timebomb sound effect
+ /*! used for timebomb sound effect.*/
fs_utils::Timer bombSoundTimer;
/*! Timer used for bomb explosion.*/
fs_utils::Timer bombExplosionTimer;
/*! Timer used for rotating flamer direction.*/
fs_utils::Timer flamerTimer_;
+ /*! counter for tracking time for ammo consumption for shields.*/
+ int shieldTimeUsed_;
+ /*! TimeBomb, Shield are activated on specific events.*/
bool activated_;
- /*! used to avoid double consuming of same elapsed time,
- * if ped shoots, time is consumed and should not be reused by weapon,
- * NOTE: ped animate executed before weapon animate
- */
- bool time_consumed_;
+
FlamerShot *pFlamerShot_;
};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|