From b481cd74dfd1629ca7b045cde57562e752c49638 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 27 Feb 2014 12:48:21 +0300 Subject: Dehardcode swords, shields and bows for auto switching weapons. --- data/perserver/default/CMakeLists.txt | 1 + data/perserver/default/Makefile.am | 3 +- data/perserver/default/weapons.xml | 19 ++++++++ src/CMakeLists.txt | 2 + src/Makefile.am | 2 + src/being/localplayer.cpp | 51 +++++++++++----------- src/client.cpp | 3 ++ src/resources/db/weaponsdb.cpp | 81 +++++++++++++++++++++++++++++++++++ src/resources/db/weaponsdb.h | 45 +++++++++++++++++++ 9 files changed, 180 insertions(+), 27 deletions(-) create mode 100644 data/perserver/default/weapons.xml create mode 100644 src/resources/db/weaponsdb.cpp create mode 100644 src/resources/db/weaponsdb.h diff --git a/data/perserver/default/CMakeLists.txt b/data/perserver/default/CMakeLists.txt index ed7e61e1e..19cf3686d 100644 --- a/data/perserver/default/CMakeLists.txt +++ b/data/perserver/default/CMakeLists.txt @@ -4,6 +4,7 @@ SET (FILES deadmessages.xml features.xml gmcommands.txt + weapons.xml ) INSTALL(FILES ${FILES} DESTINATION ${DATA_DIR}/perserver/default) diff --git a/data/perserver/default/Makefile.am b/data/perserver/default/Makefile.am index 8a66984b8..5b478d82d 100644 --- a/data/perserver/default/Makefile.am +++ b/data/perserver/default/Makefile.am @@ -5,7 +5,8 @@ default_DATA = \ chatcommands.txt \ deadmessages.xml \ features.xml \ - gmcommands.txt + gmcommands.txt \ + weapons.xml EXTRA_DIST = \ $(default_DATA) \ diff --git a/data/perserver/default/weapons.xml b/data/perserver/default/weapons.xml new file mode 100644 index 000000000..051e1f59e --- /dev/null +++ b/data/perserver/default/weapons.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6c9d82731..054ab4823 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -525,6 +525,8 @@ SET(SRCS resources/sdlmusic.h resources/db/sounddb.cpp resources/db/sounddb.h + resources/db/weaponsdb.cpp + resources/db/weaponsdb.h resources/soundeffect.cpp resources/soundeffect.h resources/soundinfo.h diff --git a/src/Makefile.am b/src/Makefile.am index e45a5397a..2b1eb4c68 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -624,6 +624,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ resources/sdlmusic.h \ resources/db/sounddb.cpp \ resources/db/sounddb.h \ + resources/db/weaponsdb.cpp \ + resources/db/weaponsdb.h \ resources/soundeffect.cpp \ resources/soundeffect.h \ resources/soundinfo.h \ diff --git a/src/being/localplayer.cpp b/src/being/localplayer.cpp index 2a3c5fa2f..0e7f8a9b0 100644 --- a/src/being/localplayer.cpp +++ b/src/being/localplayer.cpp @@ -67,6 +67,7 @@ #include "resources/iteminfo.h" #include "resources/db/emotedb.h" +#include "resources/db/weaponsdb.h" #include "utils/gettext.h" #include "utils/timer.h" @@ -2337,23 +2338,14 @@ void LocalPlayer::changeEquipmentBeforeAttack(const Being *const target) const // if attack distance for sword if (allowSword) { - // finding sword - item = inv->findItem(571, 0); - - if (!item) - item = inv->findItem(570, 0); - - if (!item) - item = inv->findItem(579, 0); - - if (!item) - item = inv->findItem(867, 0); - - if (!item) - item = inv->findItem(536, 0); - - if (!item) - item = inv->findItem(758, 0); + // searching swords + const WeaponsInfos &swords = WeaponsDB::getSwords(); + FOR_EACH (WeaponsInfosIter, it, swords) + { + item = inv->findItem(*it, 0); + if (item) + break; + } // no swords if (!item) @@ -2366,10 +2358,14 @@ void LocalPlayer::changeEquipmentBeforeAttack(const Being *const target) const // if need equip shield too if (mAttackWeaponType == 3) { - // finding shield - item = inv->findItem(601, 0); - if (!item) - item = inv->findItem(602, 0); + // searching shield + const WeaponsInfos &shields = WeaponsDB::getShields(); + FOR_EACH (WeaponsInfosIter, it, shields) + { + item = inv->findItem(*it, 0); + if (item) + break; + } if (item && !item->isEquipped()) PlayerInfo::equipItem(item, true); } @@ -2377,11 +2373,14 @@ void LocalPlayer::changeEquipmentBeforeAttack(const Being *const target) const // big distance. allowed only bow else { - // finding bow - item = inv->findItem(545, 0); - - if (!item) - item = inv->findItem(530, 0); + // searching bow + const WeaponsInfos &bows = WeaponsDB::getBows(); + FOR_EACH (WeaponsInfosIter, it, bows) + { + item = inv->findItem(*it, 0); + if (item) + break; + } // no bow if (!item) diff --git a/src/client.cpp b/src/client.cpp index 1238e9b8d..990306fc1 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -108,6 +108,7 @@ #endif #include "resources/db/palettedb.h" #include "resources/db/petdb.h" +#include "resources/db/weaponsdb.h" #include "utils/base64.h" #include "utils/cpu.h" @@ -838,6 +839,7 @@ void Client::gameClear() MonsterDB::unload(); NPCDB::unload(); AvatarDB::unload(); + WeaponsDB::unload(); PaletteDB::unload(); PETDB::unload(); StatusEffect::unload(); @@ -1468,6 +1470,7 @@ int Client::gameExec() SpecialDB::load(); #endif AvatarDB::load(); + WeaponsDB::load(); NPCDB::load(); PETDB::load(); EmoteDB::load(); diff --git a/src/resources/db/weaponsdb.cpp b/src/resources/db/weaponsdb.cpp new file mode 100644 index 000000000..82892cc5f --- /dev/null +++ b/src/resources/db/weaponsdb.cpp @@ -0,0 +1,81 @@ +/* + * The ManaPlus Client + * Copyright (C) 2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "resources/db/weaponsdb.h" + +#include "logger.h" + +#include "utils/gettext.h" +#include "utils/xmlutils.h" + +#include "configuration.h" + +#include "debug.h" + +namespace +{ + WeaponsInfos mBows; + WeaponsInfos mSwords; + WeaponsInfos mShields; + bool mLoaded = false; +} + +static void loadDB(const std::string name, WeaponsInfos &arr) +{ + readXmlIntVector("weapons.xml", + "weapons", + name, + "item", + "id", + arr); +} + +void WeaponsDB::load() +{ + if (mLoaded) + unload(); + + loadDB("swords", mSwords); + loadDB("bows", mBows); + loadDB("shields", mShields); +} + +void WeaponsDB::unload() +{ + mBows.clear(); + mSwords.clear(); + mShields.clear(); + mLoaded = false; +} + +const WeaponsInfos &WeaponsDB::getBows() +{ + return mBows; +} + +const WeaponsInfos &WeaponsDB::getSwords() +{ + return mSwords; +} + +const WeaponsInfos &WeaponsDB::getShields() +{ + return mShields; +} diff --git a/src/resources/db/weaponsdb.h b/src/resources/db/weaponsdb.h new file mode 100644 index 000000000..287237c48 --- /dev/null +++ b/src/resources/db/weaponsdb.h @@ -0,0 +1,45 @@ +/* + * The ManaPlus Client + * Copyright (C) 2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef RESOURCES_DB_WEAPONSDB_H +#define RESOURCES_DB_WEAPONSDB_H + +#include +#include + +#include "localconsts.h" + +typedef std::vector WeaponsInfos; +typedef WeaponsInfos::const_iterator WeaponsInfosIter; + +namespace WeaponsDB +{ + void load(); + + void unload(); + + const WeaponsInfos &getBows(); + + const WeaponsInfos &getSwords(); + + const WeaponsInfos &getShields(); +} // namespace WeaponsDB + +#endif // RESOURCES_DB_WEAPONSDB_H -- cgit v1.2.3-70-g09d2