afxSpellBook.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. docsURL;
  56. addField("spellsPerPage", TypeS8, myOffset(spells_per_page),
  57. "...");
  58. addField("pagesPerBook", TypeS8, myOffset(pages_per_book),
  59. "...");
  60. addField("spells", TYPEID<GameBaseData>(), myOffset(spells), MAX_PAGES_PER_BOOK*MAX_SPELLS_PER_PAGE,
  61. "...");
  62. addField("rpgSpells", TYPEID<GameBaseData>(), myOffset(rpg_spells), MAX_PAGES_PER_BOOK*MAX_SPELLS_PER_PAGE,
  63. "...");
  64. Parent::initPersistFields();
  65. }
  66. bool afxSpellBookData::preload(bool server, String &errorStr)
  67. {
  68. if (!Parent::preload(server, errorStr))
  69. return false;
  70. // Resolve objects transmitted from server
  71. if (!server)
  72. {
  73. if (do_id_convert)
  74. {
  75. for (S32 i = 0; i < pages_per_book*spells_per_page; i++)
  76. {
  77. SimObjectId db_id = SimObjectId((uintptr_t)rpg_spells[i]);
  78. if (db_id != 0)
  79. {
  80. // try to convert id to pointer
  81. if (!Sim::findObject(db_id, rpg_spells[i]))
  82. {
  83. Con::errorf(ConsoleLogEntry::General,
  84. "afxSpellBookData::preload() -- bad datablockId: 0x%x (afxRPGMagicSpellData)",
  85. db_id);
  86. }
  87. }
  88. }
  89. do_id_convert = false;
  90. }
  91. }
  92. return true;
  93. }
  94. void afxSpellBookData::packData(BitStream* stream)
  95. {
  96. Parent::packData(stream);
  97. stream->write(spells_per_page);
  98. stream->write(pages_per_book);
  99. for (S32 i = 0; i < pages_per_book*spells_per_page; i++)
  100. writeDatablockID(stream, rpg_spells[i], mPacked);
  101. }
  102. void afxSpellBookData::unpackData(BitStream* stream)
  103. {
  104. Parent::unpackData(stream);
  105. stream->read(&spells_per_page);
  106. stream->read(&pages_per_book);
  107. do_id_convert = true;
  108. for (S32 i = 0; i < pages_per_book*spells_per_page; i++)
  109. rpg_spells[i] = (afxRPGMagicSpellData*)(uintptr_t)readDatablockID(stream);
  110. }
  111. DefineEngineMethod(afxSpellBookData, getPageSlotIndex, S32, (Point2I bookSlot),,
  112. "...\n\n"
  113. "@ingroup AFX")
  114. {
  115. return object->getPageSlotIndex(bookSlot.x, bookSlot.y);
  116. }
  117. DefineEngineMethod(afxSpellBookData, getCapacity, S32, (),,
  118. "Get the capacity (total number of spell slots) in a spellbook.\n\n"
  119. "@ingroup AFX")
  120. {
  121. return object->spells_per_page*object->pages_per_book;
  122. }
  123. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  124. // afxSpellBook
  125. IMPLEMENT_CO_NETOBJECT_V1(afxSpellBook);
  126. ConsoleDocClass( afxSpellBook,
  127. "@brief A spellbook object.\n\n"
  128. "@ingroup afxMisc\n"
  129. "@ingroup AFX\n"
  130. );
  131. afxSpellBook::afxSpellBook()
  132. {
  133. mNetFlags.set(Ghostable | ScopeAlways);
  134. mDataBlock = NULL;
  135. all_spell_cooldown = 1.0f;
  136. }
  137. afxSpellBook::~afxSpellBook()
  138. {
  139. }
  140. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  141. void afxSpellBook::initPersistFields()
  142. {
  143. docsURL;
  144. Parent::initPersistFields();
  145. }
  146. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  147. void afxSpellBook::processTick(const Move* m)
  148. {
  149. Parent::processTick(m);
  150. }
  151. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  152. void afxSpellBook::advanceTime(F32 dt)
  153. {
  154. Parent::advanceTime(dt);
  155. if (all_spell_cooldown < 1.0f)
  156. {
  157. all_spell_cooldown += dt/2.0f;
  158. if (all_spell_cooldown > 1.0f)
  159. all_spell_cooldown = 1.0f;
  160. }
  161. }
  162. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  163. bool afxSpellBook::onNewDataBlock(GameBaseData* dptr, bool reload)
  164. {
  165. mDataBlock = dynamic_cast<afxSpellBookData*>(dptr);
  166. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  167. return false;
  168. scriptOnNewDataBlock();
  169. return true;
  170. }
  171. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  172. bool afxSpellBook::onAdd()
  173. {
  174. if (!Parent::onAdd())
  175. return(false);
  176. return(true);
  177. }
  178. void afxSpellBook::onRemove()
  179. {
  180. Parent::onRemove();
  181. }
  182. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  183. U32 afxSpellBook::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  184. {
  185. U32 retMask = Parent::packUpdate(con, mask, stream);
  186. if (stream->writeFlag(mask & InitialUpdateMask))
  187. {
  188. }
  189. // AllSpellCooldown
  190. if (stream->writeFlag(mask & AllSpellCooldownMask))
  191. {
  192. }
  193. return(retMask);
  194. }
  195. void afxSpellBook::unpackUpdate(NetConnection * con, BitStream * stream)
  196. {
  197. Parent::unpackUpdate(con, stream);
  198. // InitialUpdate
  199. if (stream->readFlag())
  200. {
  201. }
  202. // AllSpellCooldown
  203. if (stream->readFlag())
  204. {
  205. all_spell_cooldown = 0.0f;
  206. }
  207. }
  208. #define SPELL_DATA_NOT_FOUND "\n<just:center><font:Arial:20><color:FF0000>** Spell data not found **\n\n\n\n";
  209. const char* afxSpellBook::formatDesc(char* buffer, int len, S32 page, S32 slot) const
  210. {
  211. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  212. if (idx < 0 || !mDataBlock->rpg_spells[idx])
  213. return SPELL_DATA_NOT_FOUND;
  214. return mDataBlock->rpg_spells[idx]->formatDesc(buffer, len);
  215. }
  216. const char* afxSpellBook::getSpellIcon(S32 page, S32 slot) const
  217. {
  218. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  219. if (idx < 0 || !mDataBlock->rpg_spells[idx])
  220. return 0;
  221. return mDataBlock->rpg_spells[idx]->icon_name;
  222. }
  223. bool afxSpellBook::isPlaceholder(S32 page, S32 slot) const
  224. {
  225. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  226. if (idx < 0 || !mDataBlock->rpg_spells[idx])
  227. return false;
  228. return mDataBlock->rpg_spells[idx]->is_placeholder;
  229. }
  230. afxMagicSpellData* afxSpellBook::getSpellData(S32 page, S32 slot)
  231. {
  232. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  233. if (idx < 0 || !mDataBlock->spells[idx])
  234. return 0;
  235. return mDataBlock->spells[idx];
  236. }
  237. afxRPGMagicSpellData* afxSpellBook::getSpellRPGData(S32 page, S32 slot)
  238. {
  239. S32 idx = mDataBlock->getPageSlotIndex(page, slot);
  240. if (idx < 0 || !mDataBlock->rpg_spells[idx])
  241. return 0;
  242. return mDataBlock->rpg_spells[idx];
  243. }
  244. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  245. void afxSpellBook::startAllSpellCooldown()
  246. {
  247. //all_spell_cooldown = 0.0f;
  248. setMaskBits(AllSpellCooldownMask);
  249. }
  250. F32 afxSpellBook::getCooldownFactor(S32 page, S32 slot)
  251. {
  252. return all_spell_cooldown;
  253. }
  254. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  255. DefineEngineMethod(afxSpellBook, getPageSlotIndex, S32, (Point2I bookSlot),,
  256. "...\n\n"
  257. "@ingroup AFX")
  258. {
  259. return object->getPageSlotIndex(bookSlot.x, bookSlot.y);
  260. }
  261. DefineEngineMethod(afxSpellBook, getSpellData, S32, (Point2I bookSlot),,
  262. "Get spell datablock for spell stored at spellbook index, (page, slot).\n\n"
  263. "@ingroup AFX")
  264. {
  265. afxMagicSpellData* spell_data = object->getSpellData(bookSlot.x, bookSlot.y);
  266. return (spell_data) ? spell_data->getId() : 0;
  267. }
  268. DefineEngineMethod(afxSpellBook, getSpellRPGData, S32, (Point2I bookSlot),,
  269. "Get spell RPG datablock for spell stored at spellbook index, (page, slot).\n\n"
  270. "@ingroup AFX")
  271. {
  272. afxRPGMagicSpellData* spell_data = object->getSpellRPGData(bookSlot.x, bookSlot.y);
  273. return (spell_data) ? spell_data->getId() : 0;
  274. }
  275. DefineEngineMethod(afxSpellBook, startAllSpellCooldown, void, (),,
  276. "...\n\n"
  277. "@ingroup AFX")
  278. {
  279. object->startAllSpellCooldown();
  280. }
  281. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//