LootDB.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 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. zone->AddLootDrop(id, drop);
  59. LogWrite(LOOT__DEBUG, 5, "Loot", "---Loading LootDrop item_id %u (tableID: %u", drop->item_id, id);
  60. LogWrite(LOOT__DEBUG, 5, "Loot", "---charges: %i, equip_item: %i, prob: %.2f", drop->item_charges, drop->equip_item, drop->probability);
  61. count++;
  62. }
  63. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loaded %u loot drop%s.", count, count == 1 ? "" : "s");
  64. }
  65. // Finally, load loot tables into spawns that are set to use these loot tables
  66. if (database_new.Select(&result, "SELECT spawn_id, loottable_id FROM spawn_loot")) {
  67. count = 0;
  68. LogWrite(LOOT__DEBUG, 0, "Loot", "--Assigning loot table(s) to spawn(s)...");
  69. while(result.Next()) {
  70. int32 spawn_id = result.GetInt32Str("spawn_id");
  71. int32 table_id = result.GetInt32Str("loottable_id");
  72. zone->AddSpawnLootList(spawn_id, table_id);
  73. LogWrite(LOOT__DEBUG, 5, "Loot", "---Adding loot table %u to spawn %u", table_id, spawn_id);
  74. count++;
  75. }
  76. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loaded %u spawn loot list%s.", count, count == 1 ? "" : "s");
  77. }
  78. // Load global loot lists
  79. LoadGlobalLoot(zone);
  80. }
  81. void WorldDatabase::LoadGlobalLoot(ZoneServer* zone) {
  82. LogWrite(LOOT__INFO, 0, "Loot", "-Loading Global loot data...");
  83. DatabaseResult result;
  84. int32 count = 0;
  85. if (database_new.Select(&result, "SELECT type, loot_table, value1, value2, value3, value4 FROM loot_global")) {
  86. while(result.Next()) {
  87. const char* type = result.GetStringStr("type");
  88. int32 table_id = result.GetInt32Str("loot_table");
  89. if (strcmp(type, "Level") == 0) {
  90. GlobalLoot* loot = new GlobalLoot();
  91. loot->minLevel = result.GetInt8Str("value1");
  92. loot->maxLevel = result.GetInt8Str("value2");
  93. loot->table_id = table_id;
  94. if (loot->minLevel > loot->maxLevel)
  95. loot->maxLevel = loot->minLevel;
  96. zone->AddLevelLootList(loot);
  97. LogWrite(LOOT__DEBUG, 5, "Loot", "---Loading Level %i loot table (id: %u)", loot->minLevel, table_id);
  98. LogWrite(LOOT__DEBUG, 5, "Loot", "---minlevel: %i, maxlevel: %i", loot->minLevel, loot->maxLevel);
  99. }
  100. else if (strcmp(type, "Racial") == 0) {
  101. GlobalLoot* loot = new GlobalLoot();
  102. int16 race_id = result.GetInt16Str("value1");
  103. loot->minLevel = result.GetInt8Str("value2");
  104. loot->maxLevel = result.GetInt8Str("value3");
  105. loot->table_id = table_id;
  106. if (loot->minLevel > loot->maxLevel)
  107. loot->maxLevel = loot->minLevel;
  108. zone->AddRacialLootList(race_id, loot);
  109. LogWrite(LOOT__DEBUG, 5, "Loot", "---Loading Racial %i loot table (id: %u)", race_id, table_id);
  110. LogWrite(LOOT__DEBUG, 5, "Loot", "---minlevel: %i, maxlevel: %i", loot->minLevel, loot->maxLevel);
  111. }
  112. else if (strcmp(type, "Zone") == 0) {
  113. GlobalLoot* loot = new GlobalLoot();
  114. int32 zoneID = result.GetInt32Str("value1");
  115. loot->minLevel = result.GetInt8Str("value2");
  116. loot->maxLevel = result.GetInt8Str("value3");
  117. loot->table_id = table_id;
  118. if (loot->minLevel > loot->maxLevel)
  119. loot->maxLevel = loot->minLevel;
  120. zone->AddZoneLootList(zoneID, loot);
  121. LogWrite(LOOT__DEBUG, 5, "Loot", "---Loading Zone %i loot table (id: %u)", zoneID, table_id);
  122. LogWrite(LOOT__DEBUG, 5, "Loot", "---minlevel: %i, maxlevel: %i", loot->minLevel, loot->maxLevel);
  123. }
  124. count++;
  125. }
  126. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loaded %u Global loot list%s.", count, count == 1 ? "" : "s");
  127. }
  128. }
  129. bool WorldDatabase::LoadSpawnLoot(ZoneServer* zone, Spawn* spawn)
  130. {
  131. if (!spawn->GetDatabaseID())
  132. return false;
  133. DatabaseResult result;
  134. int32 count = 0;
  135. zone->ClearSpawnLootList(spawn->GetDatabaseID());
  136. // Finally, load loot tables into spawns that are set to use these loot tables
  137. if (database_new.Select(&result, "SELECT spawn_id, loottable_id FROM spawn_loot where spawn_id=%u",spawn->GetDatabaseID())) {
  138. count = 0;
  139. LogWrite(LOOT__DEBUG, 0, "Loot", "--Assigning loot table(s) to spawn(s)...");
  140. while (result.Next()) {
  141. int32 spawn_id = result.GetInt32Str("spawn_id");
  142. int32 table_id = result.GetInt32Str("loottable_id");
  143. zone->AddSpawnLootList(spawn_id, table_id);
  144. LogWrite(LOOT__DEBUG, 5, "Loot", "---Adding loot table %u to spawn %u", table_id, spawn_id);
  145. count++;
  146. }
  147. LogWrite(LOOT__DEBUG, 0, "Loot", "--Loaded %u spawn loot list%s.", count, count == 1 ? "" : "s");
  148. return true;
  149. }
  150. return false;
  151. }
  152. void WorldDatabase::AddLootTableToSpawn(Spawn* spawn, int32 loottable_id) {
  153. Query query;
  154. query.RunQuery2(Q_INSERT, "insert into spawn_loot set spawn_id=%u,loottable_id=%u", spawn->GetDatabaseID(), loottable_id);
  155. }
  156. bool WorldDatabase::RemoveSpawnLootTable(Spawn* spawn, int32 loottable_id) {
  157. Query query;
  158. if (loottable_id)
  159. {
  160. string delete_char = string("delete from spawn_loot where spawn_id=%i and loottable_id=%i");
  161. query.RunQuery2(Q_DELETE, delete_char.c_str(), spawn->GetDatabaseID(), loottable_id);
  162. }
  163. else
  164. {
  165. string delete_char = string("delete from spawn_loot where spawn_id=%i");
  166. query.RunQuery2(Q_DELETE, delete_char.c_str(), spawn->GetDatabaseID());
  167. }
  168. if (!query.GetAffectedRows())
  169. {
  170. //No error just in case ppl try doing stupid stuff
  171. return false;
  172. }
  173. return true;
  174. }