LootDB.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "../../common/Log.h"
  17. #include "../WorldDatabase.h"
  18. #include "../World.h"
  19. extern World world;
  20. void WorldDatabase::LoadLoot(ZoneServer* zone)
  21. {
  22. // First, clear previous loot tables...
  23. zone->ClearLootTables();
  24. DatabaseResult result;
  25. int32 count = 0;
  26. if (database_new.Select(&result, "SELECT id, name, mincoin, maxcoin, maxlootitems, lootdrop_probability, coin_probability FROM loottable")) {
  27. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loading LootTables...");
  28. LootTable* table = 0;
  29. // Load loottable from DB
  30. while(result.Next()) {
  31. int32 id = result.GetInt32Str("id");
  32. table = new LootTable;
  33. table->name = result.GetStringStr("name");
  34. table->mincoin = result.GetInt32Str("mincoin");
  35. table->maxcoin = result.GetInt32Str("maxcoin");
  36. table->maxlootitems = result.GetInt16Str("maxlootitems");
  37. table->lootdrop_probability = result.GetFloatStr("lootdrop_probability");
  38. table->coin_probability = result.GetFloatStr("coin_probability");
  39. zone->AddLootTable(id, table);
  40. LogWrite(LOOT__DEBUG, 5, "Loot", "---Loading LootTable '%s' (id: %u)", table->name.c_str(), id);
  41. LogWrite(LOOT__DEBUG, 5, "Loot", "---min_coin: %u, max_coin: %u, max_items: %i, prob: %.2f, coin_prob: %.2f", table->mincoin, table->maxcoin, table->maxlootitems, table->lootdrop_probability, table->coin_probability);
  42. count++;
  43. }
  44. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loaded %u loot table%s.", count, count == 1 ? "" : "s");
  45. }
  46. // Now, load Loot Drops for configured loot tables
  47. if (database_new.Select(&result, "SELECT loot_table_id, item_id, item_charges, equip_item, probability, no_drop_quest_completed FROM lootdrop")) {
  48. count = 0;
  49. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loading LootDrops...");
  50. LootDrop* drop = 0;
  51. while(result.Next()) {
  52. int32 id = result.GetInt32Str("loot_table_id");
  53. drop = new LootDrop;
  54. drop->item_id = result.GetInt32Str("item_id");
  55. drop->item_charges = result.GetInt16Str("item_charges");
  56. drop->equip_item = (result.GetInt8Str("equip_item") == 1);
  57. drop->probability = result.GetFloatStr("probability");
  58. drop->no_drop_quest_completed_id = result.GetInt32Str("no_drop_quest_completed");
  59. zone->AddLootDrop(id, drop);
  60. LogWrite(LOOT__DEBUG, 5, "Loot", "---Loading LootDrop item_id %u (tableID: %u", drop->item_id, id);
  61. LogWrite(LOOT__DEBUG, 5, "Loot", "---charges: %i, equip_item: %i, prob: %.2f", drop->item_charges, drop->equip_item, drop->probability);
  62. count++;
  63. }
  64. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loaded %u loot drop%s.", count, count == 1 ? "" : "s");
  65. }
  66. // Finally, load loot tables into spawns that are set to use these loot tables
  67. if (database_new.Select(&result, "SELECT spawn_id, loottable_id FROM spawn_loot")) {
  68. count = 0;
  69. LogWrite(LOOT__DEBUG, 0, "Loot", "--Assigning loot table(s) to spawn(s)...");
  70. while(result.Next()) {
  71. int32 spawn_id = result.GetInt32Str("spawn_id");
  72. int32 table_id = result.GetInt32Str("loottable_id");
  73. zone->AddSpawnLootList(spawn_id, table_id);
  74. LogWrite(LOOT__DEBUG, 5, "Loot", "---Adding loot table %u to spawn %u", table_id, spawn_id);
  75. count++;
  76. }
  77. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loaded %u spawn loot list%s.", count, count == 1 ? "" : "s");
  78. }
  79. // Load global loot lists
  80. LoadGlobalLoot(zone);
  81. }
  82. void WorldDatabase::LoadGlobalLoot(ZoneServer* zone) {
  83. LogWrite(LOOT__INFO, 0, "Loot", "-Loading Global loot data...");
  84. DatabaseResult result;
  85. int32 count = 0;
  86. if (database_new.Select(&result, "SELECT type, loot_table, value1, value2, value3, value4 FROM loot_global")) {
  87. while(result.Next()) {
  88. const char* type = result.GetStringStr("type");
  89. int32 table_id = result.GetInt32Str("loot_table");
  90. if (strcmp(type, "Level") == 0) {
  91. GlobalLoot* loot = new GlobalLoot();
  92. loot->minLevel = result.GetInt8Str("value1");
  93. loot->maxLevel = result.GetInt8Str("value2");
  94. loot->table_id = table_id;
  95. loot->loot_tier = result.GetInt32Str("value4");
  96. if (loot->minLevel > loot->maxLevel)
  97. loot->maxLevel = loot->minLevel;
  98. zone->AddLevelLootList(loot);
  99. LogWrite(LOOT__DEBUG, 5, "Loot", "---Loading Level %i loot table (id: %u)", loot->minLevel, table_id);
  100. LogWrite(LOOT__DEBUG, 5, "Loot", "---minlevel: %i, maxlevel: %i", loot->minLevel, loot->maxLevel);
  101. }
  102. else if (strcmp(type, "Racial") == 0) {
  103. GlobalLoot* loot = new GlobalLoot();
  104. int16 race_id = result.GetInt16Str("value1");
  105. loot->minLevel = result.GetInt8Str("value2");
  106. loot->maxLevel = result.GetInt8Str("value3");
  107. loot->table_id = table_id;
  108. loot->loot_tier = result.GetInt32Str("value4");
  109. if (loot->minLevel > loot->maxLevel)
  110. loot->maxLevel = loot->minLevel;
  111. zone->AddRacialLootList(race_id, loot);
  112. LogWrite(LOOT__DEBUG, 5, "Loot", "---Loading Racial %i loot table (id: %u)", race_id, table_id);
  113. LogWrite(LOOT__DEBUG, 5, "Loot", "---minlevel: %i, maxlevel: %i", loot->minLevel, loot->maxLevel);
  114. }
  115. else if (strcmp(type, "Zone") == 0) {
  116. GlobalLoot* loot = new GlobalLoot();
  117. int32 zoneID = result.GetInt32Str("value1");
  118. loot->minLevel = result.GetInt8Str("value2");
  119. loot->maxLevel = result.GetInt8Str("value3");
  120. loot->table_id = table_id;
  121. loot->loot_tier = result.GetInt32Str("value4");
  122. if (loot->minLevel > loot->maxLevel)
  123. loot->maxLevel = loot->minLevel;
  124. zone->AddZoneLootList(zoneID, loot);
  125. LogWrite(LOOT__DEBUG, 5, "Loot", "---Loading Zone %i loot table (id: %u)", zoneID, table_id);
  126. LogWrite(LOOT__DEBUG, 5, "Loot", "---minlevel: %i, maxlevel: %i", loot->minLevel, loot->maxLevel);
  127. }
  128. count++;
  129. }
  130. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loaded %u Global loot list%s.", count, count == 1 ? "" : "s");
  131. }
  132. }
  133. bool WorldDatabase::LoadSpawnLoot(ZoneServer* zone, Spawn* spawn)
  134. {
  135. if (!spawn->GetDatabaseID())
  136. return false;
  137. DatabaseResult result;
  138. int32 count = 0;
  139. zone->ClearSpawnLootList(spawn->GetDatabaseID());
  140. // Finally, load loot tables into spawns that are set to use these loot tables
  141. if (database_new.Select(&result, "SELECT spawn_id, loottable_id FROM spawn_loot where spawn_id=%u",spawn->GetDatabaseID())) {
  142. count = 0;
  143. LogWrite(LOOT__DEBUG, 0, "Loot", "--Assigning loot table(s) to spawn(s)...");
  144. while (result.Next()) {
  145. int32 spawn_id = result.GetInt32Str("spawn_id");
  146. int32 table_id = result.GetInt32Str("loottable_id");
  147. zone->AddSpawnLootList(spawn_id, table_id);
  148. LogWrite(LOOT__DEBUG, 5, "Loot", "---Adding loot table %u to spawn %u", table_id, spawn_id);
  149. count++;
  150. }
  151. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loaded %u spawn loot list%s.", count, count == 1 ? "" : "s");
  152. return true;
  153. }
  154. return false;
  155. }
  156. void WorldDatabase::AddLootTableToSpawn(Spawn* spawn, int32 loottable_id) {
  157. Query query;
  158. query.RunQuery2(Q_INSERT, "insert into spawn_loot set spawn_id=%u,loottable_id=%u", spawn->GetDatabaseID(), loottable_id);
  159. }
  160. bool WorldDatabase::RemoveSpawnLootTable(Spawn* spawn, int32 loottable_id) {
  161. Query query;
  162. if (loottable_id)
  163. {
  164. string delete_char = string("delete from spawn_loot where spawn_id=%i and loottable_id=%i");
  165. query.RunQuery2(Q_DELETE, delete_char.c_str(), spawn->GetDatabaseID(), loottable_id);
  166. }
  167. else
  168. {
  169. string delete_char = string("delete from spawn_loot where spawn_id=%i");
  170. query.RunQuery2(Q_DELETE, delete_char.c_str(), spawn->GetDatabaseID());
  171. }
  172. if (!query.GetAffectedRows())
  173. {
  174. //No error just in case ppl try doing stupid stuff
  175. return false;
  176. }
  177. return true;
  178. }