ManaPlus
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
SpellManager Class Reference

#include <spellmanager.h>

Public Member Functions

 SpellManager ()
 
 ~SpellManager ()
 
TextCommandgetSpell (const int spellId) const
 
const TextCommandgetSpellByItem (const int itemId) const
 
bool addSpell (TextCommand *const spell)
 
TextCommandcreateNewSpell () const
 
const std::vector< TextCommand * > & getAll () const A_CONST
 
void useItem (const int itemId) const
 
void invoke (const int spellId) const
 
void load ()
 
void save () const
 
std::string autoComplete (const std::string &partName) const
 
void swap (const int id1, const int id2)
 

Static Public Member Functions

static void invokeCommand (const std::string &command, const Being *const target)
 

Private Member Functions

void fillSpells ()
 

Static Private Member Functions

static void invokeSpell (const TextCommand *const spell, const Being *const target)
 
static void invokeSpell (const TextCommand *const spell)
 
static std::string parseCommand (std::string command, const Being *const target)
 

Private Attributes

std::map< unsigned int, TextCommand * > mSpells
 
std::vector< TextCommand * > mSpellsVector
 

Detailed Description

Definition at line 36 of file spellmanager.h.

Constructor & Destructor Documentation

◆ SpellManager()

SpellManager::SpellManager ( )

Definition at line 50 of file spellmanager.cpp.

50  :
51  mSpells(),
53 {
54  load();
55 }
std::vector< TextCommand * > mSpellsVector
Definition: spellmanager.h:85
std::map< unsigned int, TextCommand * > mSpells
Definition: spellmanager.h:84

References load().

◆ ~SpellManager()

SpellManager::~SpellManager ( )

Definition at line 57 of file spellmanager.cpp.

58 {
60  mSpells.clear();
61  mSpellsVector.clear();
62 }
void delete_all(Container &c)
Definition: dtor.h:56

References delete_all(), mSpells, and mSpellsVector.

Member Function Documentation

◆ addSpell()

bool SpellManager::addSpell ( TextCommand *const  spell)

Definition at line 98 of file spellmanager.cpp.

99 {
100  if (spell == nullptr)
101  return false;
102 
103  const int id = spell->getId();
104  if (id < 0 || id >= CAST_S32(SPELL_SHORTCUT_ITEMS
106  {
107  delete spell;
108  return false;
109  }
110  const std::map<unsigned int, TextCommand*>::const_iterator
111  i = mSpells.find(spell->getId());
112  if (i == mSpells.end())
113  {
114  mSpells[spell->getId()] = spell;
115  mSpellsVector.push_back(spell);
116  return true;
117  }
118  return false;
119 }
#define CAST_S32
Definition: cast.h:30
int getId() const
Definition: textcommand.h:94
const unsigned int SPELL_SHORTCUT_ITEMS
Definition: spells.h:28
const unsigned int SPELL_SHORTCUT_TABS
Definition: spells.h:29

References CAST_S32, TextCommand::getId(), mSpells, mSpellsVector, SPELL_SHORTCUT_ITEMS, and SPELL_SHORTCUT_TABS.

Referenced by fillSpells(), and load().

◆ autoComplete()

std::string SpellManager::autoComplete ( const std::string &  partName) const

Definition at line 373 of file spellmanager.cpp.

374 {
375  STD_VECTOR<TextCommand*>::const_iterator i = mSpellsVector.begin();
376  const STD_VECTOR<TextCommand*>::const_iterator
377  i_end = mSpellsVector.end();
378  std::string newName;
379  const TextCommand *newCommand = nullptr;
380 
381  while (i != i_end)
382  {
383  const TextCommand *const cmd = *i;
384  const std::string line = cmd->getCommand();
385 
386  if (!line.empty())
387  {
388  const size_t pos = line.find(partName, 0);
389  if (pos == 0)
390  {
391  if (!newName.empty())
392  {
393  newName = findSameSubstring(line, newName);
394  newCommand = nullptr;
395  }
396  else
397  {
398  newName = line;
399  newCommand = cmd;
400  }
401  }
402  }
403  ++i;
404  }
405  if (!newName.empty() &&
406  (newCommand != nullptr) &&
407  newCommand->getTargetType() == CommandTarget::NeedTarget)
408  {
409  return newName.append(" ");
410  }
411  return newName;
412 }
std::string getCommand() const
Definition: textcommand.h:85
CommandTargetT getTargetType() const
Definition: textcommand.h:97
const std::string findSameSubstring(const std::string &str1, const std::string &str2)

References findSameSubstring(), TextCommand::getCommand(), TextCommand::getTargetType(), mSpellsVector, and CommandTarget::NeedTarget.

Referenced by ChatWindow::autoComplete().

◆ createNewSpell()

TextCommand * SpellManager::createNewSpell ( ) const

Definition at line 257 of file spellmanager.cpp.

258 {
259  return new TextCommand(CAST_U32(mSpellsVector.size()));
260 }
#define CAST_U32
Definition: cast.h:31

References CAST_U32, and mSpellsVector.

◆ fillSpells()

void SpellManager::fillSpells ( )
private

Definition at line 80 of file spellmanager.cpp.

81 {
83 
84  CommandsMap &commands = CommandsDB::getAll();
85  FOR_EACH (CommandsMapIter, it, commands)
86  addSpell((*it).second);
87 
88  for (unsigned f = 0; f < SPELL_SHORTCUT_ITEMS * SPELL_SHORTCUT_TABS; f++)
89  {
90  const std::map<unsigned int, TextCommand*>::const_iterator
91  it = mSpells.find(f);
92  if (it == mSpells.end())
93  addSpell(new TextCommand(f));
94  }
96 }
bool addSpell(TextCommand *const spell)
CommandsMap::iterator CommandsMapIter
Definition: commandsdb.h:37
std::map< int, TextCommand * > CommandsMap
Definition: commandsdb.h:34
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
void unload()
Definition: commandsdb.cpp:153
CommandsMap & getAll()
Definition: commandsdb.cpp:160
void load()
Definition: commandsdb.cpp:40

References addSpell(), FOR_EACH, CommandsDB::getAll(), CommandsDB::load(), mSpells, SPELL_SHORTCUT_ITEMS, SPELL_SHORTCUT_TABS, and CommandsDB::unload().

Referenced by load().

◆ getAll()

const std::vector< TextCommand * > & SpellManager::getAll ( ) const

Definition at line 121 of file spellmanager.cpp.

122 {
123  return mSpellsVector;
124 }

References mSpellsVector.

Referenced by SpellShortcut::load().

◆ getSpell()

TextCommand * SpellManager::getSpell ( const int  spellId) const

Definition at line 64 of file spellmanager.cpp.

65 {
66  if (spellId < 0 || CAST_SIZE(spellId) >= mSpells.size())
67  return nullptr;
68 
69  const std::map<unsigned int, TextCommand*>::const_iterator
70  it = mSpells.find(spellId);
71 
72  return it != mSpells.end() ? (*it).second : nullptr;
73 }
#define CAST_SIZE
Definition: cast.h:34

References CAST_SIZE, and mSpells.

Referenced by SpellShortcutContainer::draw(), getSpellByItem(), invoke(), SpellShortcutContainer::mouseDragged(), SpellShortcutContainer::mouseMoved(), SpellShortcutContainer::mouseReleased(), and SpellShortcutContainer::safeDraw().

◆ getSpellByItem()

const TextCommand * SpellManager::getSpellByItem ( const int  itemId) const

Definition at line 75 of file spellmanager.cpp.

76 {
77  return getSpell(itemId - SPELL_MIN_ID);
78 }
TextCommand * getSpell(const int spellId) const
const int SPELL_MIN_ID
Definition: spells.h:27

References getSpell(), and SPELL_MIN_ID.

Referenced by ItemShortcutContainer::draw(), ItemShortcutContainer::mouseDragged(), ItemShortcutContainer::mouseMoved(), and ItemShortcutContainer::safeDraw().

◆ invoke()

void SpellManager::invoke ( const int  spellId) const

Definition at line 131 of file spellmanager.cpp.

132 {
133  if (localPlayer == nullptr)
134  return;
135 
136  const TextCommand *const spell = getSpell(spellId);
137  if (spell == nullptr)
138  return;
139 
140  if ((playerHandler == nullptr) || spell->getCommand().empty())
141  return;
142 
143 #ifdef TMWA_SUPPORT
144  if (spell->getCommandType() == TextCommandType::Text ||
147  >= CAST_S32(spell->getBaseLvl()) &&
149  spell->getSchool())) >= CAST_S32(spell->getSchoolLvl())
151  >= CAST_S32(spell->getMana()))
152  )
153 #endif // TMWA_SUPPORT
154  {
155  const Being *const target = localPlayer->getTarget();
156  if (spell->getTargetType() == CommandTarget::NoTarget)
157  {
158  invokeSpell(spell);
159  }
160 #ifdef TMWA_SUPPORT
161  if ((target != nullptr &&
162  (target->getType() != ActorType::Monster ||
163  spell->getCommandType() == TextCommandType::Text)) &&
166 #else // TMWA_SUPPORT
167 
168  if (target != nullptr &&
171 #endif // TMWA_SUPPORT
172  {
173  invokeSpell(spell, target);
174  }
175  else if (spell->getTargetType() == CommandTarget::AllowTarget)
176  {
177  invokeSpell(spell);
178  }
179  }
180 }
Definition: being.h:96
ActorTypeT getType() const
Definition: being.h:116
Being * getTarget() const
virtual bool canUseMagic() const =0
static void invokeSpell(const TextCommand *const spell, const Being *const target)
unsigned getBaseLvl() const
Definition: textcommand.h:110
unsigned int getMana() const
Definition: textcommand.h:104
unsigned getSchoolLvl() const
Definition: textcommand.h:113
MagicSchoolT getSchool() const
Definition: textcommand.h:107
TextCommandTypeT getCommandType() const
Definition: textcommand.h:128
LocalPlayer * localPlayer
int32_t getAttribute(const AttributesT id)
Definition: playerinfo.cpp:102
int getSkillLevel(const int id)
Definition: playerinfo.cpp:120
Net::PlayerHandler * playerHandler
Definition: net.cpp:96

References CommandTarget::AllowTarget, Net::PlayerHandler::canUseMagic(), CAST_S32, PlayerInfo::getAttribute(), TextCommand::getBaseLvl(), TextCommand::getCommand(), TextCommand::getCommandType(), TextCommand::getMana(), TextCommand::getSchool(), TextCommand::getSchoolLvl(), PlayerInfo::getSkillLevel(), getSpell(), LocalPlayer::getTarget(), TextCommand::getTargetType(), Being::getType(), invokeSpell(), localPlayer, ActorType::Monster, CommandTarget::NeedTarget, CommandTarget::NoTarget, Attributes::PLAYER_MP, playerHandler, MagicSchool::SkillMagic, and TextCommandType::Text.

Referenced by SpellShortcutContainer::mousePressed(), and useItem().

◆ invokeCommand()

void SpellManager::invokeCommand ( const std::string &  command,
const Being *const  target 
)
static

Definition at line 201 of file spellmanager.cpp.

203 {
204  if (chatWindow == nullptr)
205  return;
206  chatWindow->localChatInput(parseCommand(command, target));
207 }
ChatWindow * chatWindow
Definition: chatwindow.cpp:94
void localChatInput(const std::string &msg) const
Definition: chatwindow.cpp:691
static std::string parseCommand(std::string command, const Being *const target)

References chatWindow, ChatWindow::localChatInput(), and parseCommand().

Referenced by SkillDialog::useSkill().

◆ invokeSpell() [1/2]

void SpellManager::invokeSpell ( const TextCommand *const  spell)
staticprivate

Definition at line 182 of file spellmanager.cpp.

183 {
184  if ((chatWindow == nullptr) || (spell == nullptr))
185  return;
186  chatWindow->localChatInput(parseCommand(spell->getCommand(), nullptr));
187 }

References chatWindow, TextCommand::getCommand(), ChatWindow::localChatInput(), and parseCommand().

◆ invokeSpell() [2/2]

void SpellManager::invokeSpell ( const TextCommand *const  spell,
const Being *const  target 
)
staticprivate

Definition at line 189 of file spellmanager.cpp.

191 {
192  if (chatWindow == nullptr ||
193  spell == nullptr ||
194  target == nullptr)
195  {
196  return;
197  }
198  chatWindow->localChatInput(parseCommand(spell->getCommand(), target));
199 }

References chatWindow, TextCommand::getCommand(), ChatWindow::localChatInput(), and parseCommand().

Referenced by invoke().

◆ load()

void SpellManager::load ( )

Definition at line 262 of file spellmanager.cpp.

263 {
264  const Configuration *cfg = &serverConfig;
265 
267  mSpells.clear();
268  mSpellsVector.clear();
269 
270  if (cfg->getValue("commandShortcutFlags0", "").empty())
271  {
272  fillSpells();
273  save();
274  return;
275  }
276 
277  for (unsigned i = 0; i < SPELL_SHORTCUT_ITEMS * SPELL_SHORTCUT_TABS; i++)
278  {
279  unsigned int targetType;
280  unsigned int basicLvl;
281  unsigned int school;
282  unsigned int schoolLvl;
283  unsigned int mana;
284  unsigned int commandType;
285 
286  std::string flags =
287  cfg->getValue("commandShortcutFlags" + toString(i), "");
288  std::stringstream ss(flags);
289  ss >> commandType;
290  ss >> targetType;
291  ss >> basicLvl;
292  ss >> school;
293  ss >> schoolLvl;
294  ss >> mana;
295 
296  std::string cmd = cfg->getValue("commandShortcutCmd"
297  + toString(i), "");
298  std::string comment = cfg->getValue("commandShortcutComment"
299  + toString(i), "");
300  std::string symbol = cfg->getValue("commandShortcutSymbol"
301  + toString(i), "");
302  std::string icon = cfg->getValue("commandShortcutIcon"
303  + toString(i), "");
304 
305 #ifdef TMWA_SUPPORT
306  if (static_cast<TextCommandTypeT>(commandType) ==
308  {
309  addSpell(new TextCommand(i, symbol, cmd, comment,
310  static_cast<CommandTargetT>(targetType), icon, basicLvl,
311  static_cast<MagicSchoolT>(school), schoolLvl, mana));
312  }
313  else
314 #endif // TMWA_SUPPORT
315  {
316  addSpell(new TextCommand(i, symbol, cmd, comment,
317  static_cast<CommandTargetT>(targetType), icon));
318  }
319  }
320 }
std::string getValue(const std::string &key, const std::string &deflt) const
void fillSpells()
void save() const
CommandTarget ::T CommandTargetT
Definition: commandtarget.h:33
Configuration serverConfig
MagicSchool ::T MagicSchoolT
Definition: magicschool.h:38
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
TextCommandType ::T TextCommandTypeT

References addSpell(), delete_all(), fillSpells(), ConfigurationObject::getValue(), TextCommandType::Magic, mSpells, mSpellsVector, save(), serverConfig, SPELL_SHORTCUT_ITEMS, SPELL_SHORTCUT_TABS, and Catch::toString().

Referenced by SpellManager().

◆ parseCommand()

std::string SpellManager::parseCommand ( std::string  command,
const Being *const  target 
)
staticprivate

Definition at line 209 of file spellmanager.cpp.

211 {
212  if (localPlayer == nullptr)
213  return command;
214 
215  std::string name;
216  std::string id;
217  std::string name2;
218 
219  if (target != nullptr)
220  {
221  name = target->getName();
222  name2 = name;
223  id = toString(toInt(target->getId(), int));
224  }
225  else
226  {
227  name2 = localPlayer->getName();
228  }
229 
230  bool found = false;
231 
232  size_t idx = command.find("<TARGET>");
233  if (idx != std::string::npos)
234  {
235  found = true;
236  command = replaceAll(command, "<TARGET>", name);
237  }
238  idx = command.find("<TARGETID>");
239  if (idx != std::string::npos)
240  {
241  found = true;
242  command = replaceAll(command, "<TARGETID>", id);
243  }
244  idx = command.find("<TARGETORSELF>");
245  if (idx != std::string::npos)
246  {
247  found = true;
248  command = replaceAll(command, "<TARGETORSELF>", name2);
249  }
250 
251  if (!found && !name.empty())
252  command.append(" ").append(name);
253 
254  return command;
255 }
BeingId getId() const
Definition: actorsprite.h:64
const std::string & getName() const
Definition: being.h:232
#define toInt(val, name)
Definition: intdefines.h:47
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)

References ActorSprite::getId(), Being::getName(), localPlayer, replaceAll(), toInt, and Catch::toString().

Referenced by invokeCommand(), and invokeSpell().

◆ save()

void SpellManager::save ( ) const

Definition at line 329 of file spellmanager.cpp.

330 {
331  for (unsigned i = 0; i < SPELL_SHORTCUT_ITEMS * SPELL_SHORTCUT_TABS; i++)
332  {
333  const TextCommand *const spell = mSpellsVector[i];
334  if (spell != nullptr)
335  {
336  setOrDel("commandShortcutCmd", getCommand);
337  setOrDel("commandShortcutComment", getComment);
338  setOrDel("commandShortcutSymbol", getSymbol);
339  setOrDel("commandShortcutIcon", getIcon);
340  if (!spell->getCommand().empty() &&
341  !spell->getSymbol().empty())
342  {
343 #ifdef TMWA_SUPPORT
344  serverConfig.setValue("commandShortcutFlags" + toString(i),
345  strprintf("%u %u %u %u %u %u",
346  CAST_U32(spell->getCommandType()),
347  CAST_U32(spell->getTargetType()),
348  spell->getBaseLvl(),
349  CAST_U32(spell->getSchool()),
350  spell->getSchoolLvl(),
351  CAST_U32(spell->getMana())));
352 #else // TMWA_SUPPORT
353 
354  serverConfig.setValue("commandShortcutFlags" + toString(i),
355  strprintf("%u %u %u %u %u %u", 1U,
356  CAST_U32(spell->getTargetType()),
357  0U,
358  0U,
359  0U,
360  0U));
361 #endif // TMWA_SUPPORT
362  }
363  else
364  {
365  serverConfig.deleteKey("commandShortcutFlags" + toString(i));
366  }
367  }
368  }
369 }
void deleteKey(const std::string &key)
void setValue(const std::string &key, const std::string &value)
std::string getSymbol() const
Definition: textcommand.h:91
const std::string & getIcon(const int id)
Definition: languagedb.cpp:111
#define setOrDel(str, method)
std::string strprintf(const char *const format,...)

References CAST_U32, ConfigurationObject::deleteKey(), TextCommand::getBaseLvl(), TextCommand::getCommand(), TextCommand::getCommandType(), LanguageDb::getIcon(), TextCommand::getMana(), TextCommand::getSchool(), TextCommand::getSchoolLvl(), TextCommand::getSymbol(), TextCommand::getTargetType(), mSpellsVector, serverConfig, setOrDel, Configuration::setValue(), SPELL_SHORTCUT_ITEMS, SPELL_SHORTCUT_TABS, strprintf(), and Catch::toString().

Referenced by TextCommandEditor::deleteCommand(), load(), SpellShortcutContainer::mouseReleased(), and TextCommandEditor::save().

◆ swap()

void SpellManager::swap ( const int  id1,
const int  id2 
)

Definition at line 414 of file spellmanager.cpp.

415 {
416  TextCommand *const spell1 = mSpells[id1];
417  TextCommand *const spell2 = mSpells[id2];
418  if ((spell1 == nullptr) || (spell2 == nullptr))
419  return;
420 
421  // swap in map
422  mSpells[id1] = spell2;
423  mSpells[id2] = spell1;
424 
425  // swap id
426  const int tmp = spell1->getId();
427  spell1->setId(spell2->getId());
428  spell2->setId(tmp);
429 
430  // swap in vector
431  const size_t sz = SPELL_SHORTCUT_ITEMS * SPELL_SHORTCUT_TABS;
432  for (size_t f = 0; f < sz; f++)
433  {
434  const TextCommand *const spellA = mSpellsVector[f];
435  if (spellA == spell1)
436  {
437  for (size_t d = 0; d < sz; d++)
438  {
439  const TextCommand *const spellB = mSpellsVector[d];
440  if (spellB == spell2)
441  {
442  mSpellsVector[f] = spell2;
443  mSpellsVector[d] = spell1;
444  return;
445  }
446  }
447  }
448  }
449 }
void setId(const int id)
Definition: textcommand.h:144

References TextCommand::getId(), mSpells, mSpellsVector, TextCommand::setId(), SPELL_SHORTCUT_ITEMS, and SPELL_SHORTCUT_TABS.

Referenced by SpellShortcutContainer::mouseReleased().

◆ useItem()

void SpellManager::useItem ( const int  itemId) const

Definition at line 126 of file spellmanager.cpp.

127 {
128  invoke(itemId - SPELL_MIN_ID);
129 }
void invoke(const int spellId) const

References invoke(), and SPELL_MIN_ID.

Referenced by ItemShortcut::useItem().

Field Documentation

◆ mSpells

std::map<unsigned int, TextCommand*> SpellManager::mSpells
private

Definition at line 84 of file spellmanager.h.

Referenced by addSpell(), fillSpells(), getSpell(), load(), swap(), and ~SpellManager().

◆ mSpellsVector

std::vector<TextCommand*> SpellManager::mSpellsVector
private

Definition at line 85 of file spellmanager.h.

Referenced by addSpell(), autoComplete(), createNewSpell(), getAll(), load(), save(), swap(), and ~SpellManager().


The documentation for this class was generated from the following files: