afxSpellBook.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "console/engineAPI.h"
  26. #include "console/consoleTypes.h"
  27. #include "core/stream/bitStream.h"
  28. #include "T3D/gameBase/gameBase.h"
  29. #include "afx/afxSpellBook.h"
  30. #include "afx/afxMagicSpell.h"
  31. #include "afx/rpg/afxRPGMagicSpell.h"
  32. #include "afx/ui/afxSpellButton.h"
  33. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  34. // afxSpellBookData
  35. IMPLEMENT_CO_DATABLOCK_V1(afxSpellBookData);
  36. ConsoleDocClass( afxSpellBookData,
  37. "@brief A spellbook datablock.\n\n"
  38. "@ingroup afxMisc\n"
  39. "@ingroup AFX\n"
  40. "@ingroup Datablocks\n"
  41. );
  42. afxSpellBookData::afxSpellBookData()
  43. {
  44. spells_per_page = 12;
  45. pages_per_book = 12;
  46. dMemset(spells, 0, sizeof(spells));
  47. dMemset(rpg_spells, 0, sizeof(rpg_spells));
  48. // marked true if datablock ids need to
  49. // be converted into pointers
  50. do_id_convert = false;
  51. }
  52. #define myOffset(field) Offset(field, afxSpellBookData)
  53. void afxSpellBookData::initPersistFields()
  54. {
  55. addField("spellsPerPage", TypeS8, myOffset(spells_per_page),
  56. "...");
  57. addField("pagesPerBook", TypeS8, myOffset(pages_per_book),
  58. "...");
  59. addField("spells", TYPEID<GameBaseData>(), myOffset(spells), MAX_PAGES_PER_BOOK*MAX_SPELLS_PER_PAGE,
  60. "...");
  61. addField("rpgSpells", TYPEID<GameBaseData>(), myOffset(rpg_spells), MAX_PAGES_PER_BOOK*MAX_SPELLS_PER_PAGE,
  62. "...");
  63. Parent::initPersistFields();
  64. }
  65. bool afxSpellBookData::preload(bool server, String &errorStr)
  66. {
  67. if (!Parent::preload(server, errorStr))
  68. return false;
  69. // Resolve objects transmitted from server
  70. if (!server)
  71. {
  72. if (do_id_convert)
  73. {
  74. for (S32 i = 0; i < pages_per_book*spells_per_page; i++)
  75. {
  76. SimObjectId db_id = SimObjectId((uintptr_t)rpg_spells[i]);
  77. if (db_id != 0)
  78. {
  79. // try to convert id to pointer
  80. if (!Sim::findObject(db_id, rpg_spells[i]))
  81. {
  82. Con::errorf(ConsoleLogEntry::General,
  83. "afxSpellBookData::preload() -- bad datablockId: 0x%x (afxRPGMagicSpellData)",
  84. db_id);
  85. }
  86. }
  87. }
  88. do_id_convert = false;
  89. }
  90. }
  91. return true;
  92. }
  93. void afxSpellBookData::packData(BitStream* stream)
  94. {
  95. Parent::packData(stream);
  96. stream->write(spells_per_page);
  97. stream->write(pages_per_book);
  98. for (S32 i = 0; i < pages_per_book*spells_per_page; i++)
  99. writeDatablockID(stream, rpg_spells[i], mPacked);
  100. }
  101. void afxSpellBookData::unpackData(BitStream* stream)
  102. {
  103. Parent::unpackData(stream);
  104. stream->read(&spells_per_page);
  105. stream->read(&pages_per_book);
  106. do_id_convert = true;
  107. for (S32 i = 0; i < pages_per_book*spells_per_page; i++)
  108. rpg_spells[i] = (afxRPGMagicSpellData*)(uintptr_t)readDatablockID(stream);
  109. }
  110. DefineEngineMethod(afxSpellBookData, getPageSlotIndex, S32, (Point2I bookSlot),,
  111. "...\n\n"
  112. "@ingroup AFX")
  113. {
  114. return object->getPageSlotIndex(bookSlot.x, bookSlot.y);
  115. }
  116. DefineEngineMethod(afxSpellBookData, getCapacity, S32, (),,
  117. "Get the capacity (total number of spell slots) in a spellbook.\n\n"
  118. "@ingroup AFX")
  119. {
  120. return object->spells_per_page*object->pages_per_book;
  121. }
  122. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  123. // afxSpellBook
  124. IMPLEMENT_CO_NETOBJECT_V1(afxSpellBook);
  125. ConsoleDocClass( afxSpellBook,
  126. "@brief A spellbook object.\n\n"
  127. "@ingroup afxMisc\n"
  128. "@ingroup AFX\n"
  129. );
  130. afxSpellBook::afxSpellBook()
  131. {
  132. mNetFlags.set(Ghostable | ScopeAlways);
  133. mDataBlock = NULL;
  134. all_spell_cooldown = 1.0f;
  135. }
  136. afxSpellBook::~afxSpellBook()
  137. {
  138. }
  139. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  140. void afxSpellBook::initPersistFields()
  141. {
  142. Parent::initPersistFields();
  143. }
  144. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  145. void afxSpellBook::processTick(const Move* m)
  146. {
  147. Parent::processTick(m);
  148. }
  149. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  150. void afxSpellBook::advanceTime(F32 dt)
  151. {
  152. Parent::advanceTime(dt);
  153. if (all_spell_cooldown < 1.0f)
  154. {
  155. all_spell_cooldown += dt/2.0f;
  156. if (all_spell_cooldown > 1.0f)
  157. all_spell_cooldown = 1.0f;
  158. }
  159. }
  160. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  161. bool afxSpellBook::onNewDataBlock(GameBaseData* dptr, bool reload)
  162. {
  163. mDataBlock = dynamic_cast<afxSpellBookData*>(dptr);
  164. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  165. return false;
  166. scriptOnNewDataBlock();
  167. return true;
  168. }
  169. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  170. bool afxSpellBook::onAdd()
  171. {
  172. if (!Parent::onAdd())
  173. return(false);
  174. return(true);
  175. }
  176. void afxSpellBook::onRemove()
  177. {
  178. Parent::onRemove();
  179. }
  180. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  181. U32 afxSpellBook::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  182. {
  183. U32 retMask = Parent::packUpdate(con, mask, stream);
  184. if (stream->writeFlag(mask & InitialUpdateMask))
  185. {
  186. }
  187. // AllSpellCooldown
  188. if (stream->writeFlag(mask & AllSpellCooldownMask))
  189. {
  190. }
  191. return(retMask);
  192. }
  193. void afxSpellBook::unpackUpdate(NetConnection * con, BitStream * stream)
  194. {
  195. Parent::unpackUpdate(con, stream);
  196. // InitialUpdate
  197. if (stream->readFlag())
  198. {
  199. }
  200. // AllSpellCooldown
  201. if (stream->readFlag())
  202. {
  203. all_spell_cooldown = 0.0f;
  204. }
  205. }
  206. #define SPELL_DATA_NOT_FOUND "\n<just:center><font:Arial:20><color:FF0000>** Spell data not found **\n\n\n\n";
  207. const char* afxSpellBook::formatDesc(char* buffer, int len, S32 page, S32 slot) const
  208. {
  209. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  210. if (idx < 0 || !mDataBlock->rpg_spells[idx])
  211. return SPELL_DATA_NOT_FOUND;
  212. return mDataBlock->rpg_spells[idx]->formatDesc(buffer, len);
  213. }
  214. const char* afxSpellBook::getSpellIcon(S32 page, S32 slot) const
  215. {
  216. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  217. if (idx < 0 || !mDataBlock->rpg_spells[idx])
  218. return 0;
  219. return mDataBlock->rpg_spells[idx]->icon_name;
  220. }
  221. bool afxSpellBook::isPlaceholder(S32 page, S32 slot) const
  222. {
  223. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  224. if (idx < 0 || !mDataBlock->rpg_spells[idx])
  225. return false;
  226. return mDataBlock->rpg_spells[idx]->is_placeholder;
  227. }
  228. afxMagicSpellData* afxSpellBook::getSpellData(S32 page, S32 slot)
  229. {
  230. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  231. if (idx < 0 || !mDataBlock->spells[idx])
  232. return 0;
  233. return mDataBlock->spells[idx];
  234. }
  235. afxRPGMagicSpellData* afxSpellBook::getSpellRPGData(S32 page, S32 slot)
  236. {
  237. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  238. if (idx < 0 || !mDataBlock->rpg_spells[idx])
  239. return 0;
  240. return mDataBlock->rpg_spells[idx];
  241. }
  242. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  243. void afxSpellBook::startAllSpellCooldown()
  244. {
  245. //all_spell_cooldown = 0.0f;
  246. setMaskBits(AllSpellCooldownMask);
  247. }
  248. F32 afxSpellBook::getCooldownFactor(S32 page, S32 slot)
  249. {
  250. return all_spell_cooldown;
  251. }
  252. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  253. DefineEngineMethod(afxSpellBook, getPageSlotIndex, S32, (Point2I bookSlot),,
  254. "...\n\n"
  255. "@ingroup AFX")
  256. {
  257. return object->getPageSlotIndex(bookSlot.x, bookSlot.y);
  258. }
  259. DefineEngineMethod(afxSpellBook, getSpellData, S32, (Point2I bookSlot),,
  260. "Get spell datablock for spell stored at spellbook index, (page, slot).\n\n"
  261. "@ingroup AFX")
  262. {
  263. afxMagicSpellData* spell_data = object->getSpellData(bookSlot.x, bookSlot.y);
  264. return (spell_data) ? spell_data->getId() : 0;
  265. }
  266. DefineEngineMethod(afxSpellBook, getSpellRPGData, S32, (Point2I bookSlot),,
  267. "Get spell RPG datablock for spell stored at spellbook index, (page, slot).\n\n"
  268. "@ingroup AFX")
  269. {
  270. afxRPGMagicSpellData* spell_data = object->getSpellRPGData(bookSlot.x, bookSlot.y);
  271. return (spell_data) ? spell_data->getId() : 0;
  272. }
  273. DefineEngineMethod(afxSpellBook, startAllSpellCooldown, void, (),,
  274. "...\n\n"
  275. "@ingroup AFX")
  276. {
  277. object->startAllSpellCooldown();
  278. }
  279. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//