private static NPC createNPC(SQLiteDataReader reader) { SQLiteCommand command; if (!reader.Read()) { return null; } NPC npc = new NPC(); npc.permanent = true; npc.id = reader.GetInt32(0); npc.name = reader["name"].ToString(); npc.city = reader["city"].ToString(); if (!reader.IsDBNull(3) && !reader.IsDBNull(4) && !reader.IsDBNull(5)) { npc.pos.x = reader.GetInt32(3); npc.pos.y = reader.GetInt32(4); npc.pos.z = reader.GetInt32(5); } npc.image = Image.FromStream(reader.GetStream(6)); npc.job = reader.IsDBNull(7) ? "" : reader.GetString(7); if (npc.image.RawFormat.Guid == ImageFormat.Gif.Guid) { int frames = npc.image.GetFrameCount(FrameDimension.Time); if (frames == 1) { Bitmap new_bitmap = new Bitmap(npc.image); new_bitmap.MakeTransparent(); npc.image.Dispose(); npc.image = new_bitmap; } } // special case for rashid: change location based on day of the week if (npc != null && npc.name == "Rashid") { command = new SQLiteCommand(String.Format("SELECT city, x, y, z FROM RashidPositions WHERE day='{0}'", DateTime.Now.DayOfWeek.ToString()), mainForm.conn); reader = command.ExecuteReader(); if (reader.Read()) { npc.city = reader["city"].ToString(); npc.pos.x = reader.GetInt32(1); npc.pos.y = reader.GetInt32(2); npc.pos.z = reader.GetInt32(3); } } command = new SQLiteCommand(String.Format("SELECT itemid, value FROM SellItems WHERE vendorid={0}", npc.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { ItemSold sellItem = new ItemSold(); sellItem.itemid = reader.GetInt32(0); sellItem.npcid = npc.id; sellItem.price = reader.GetInt32(1); npc.sellItems.Add(sellItem); } command = new SQLiteCommand(String.Format("SELECT itemid, value FROM BuyItems WHERE vendorid={0}", npc.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { ItemSold buyItem = new ItemSold(); buyItem.itemid = reader.GetInt32(0); buyItem.npcid = npc.id; buyItem.price = reader.GetInt32(1); npc.buyItems.Add(buyItem); } command = new SQLiteCommand(String.Format("SELECT spellid,knight,druid,paladin,sorcerer FROM SpellNPCs WHERE npcid={0}", npc.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { SpellTaught t = new SpellTaught(); t.npcid = npc.id; t.spellid = reader.GetInt32(0); t.knight = reader.GetBoolean(1); t.druid = reader.GetBoolean(2); t.paladin = reader.GetBoolean(3); t.sorcerer = reader.GetBoolean(4); npc.spellsTaught.Add(t); } command = new SQLiteCommand(String.Format("SELECT DISTINCT questid FROM QuestNPCs WHERE npcid={0}", npc.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { Quest q = getQuest(reader.GetInt32(0)); npc.involvedQuests.Add(q); } command = new SQLiteCommand(String.Format("SELECT destination,cost,notes FROM NPCDestinations WHERE npcid={0}", npc.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { Transport t = new Transport(); t.destination = reader.GetString(0); t.cost = reader.GetInt32(1); t.notes = reader.GetString(2); npc.transportOffered.Add(t); } return npc; }
private static Item createItem(SQLiteDataReader reader) { SQLiteCommand command; if (!reader.Read()) { return null; } Item item = new Item(); item.permanent = true; item.id = reader.GetInt32(0); item.displayname = reader.GetString(1); item.actual_value = reader.IsDBNull(2) ? DATABASE_NULL : reader.GetInt64(2); item.vendor_value = reader.IsDBNull(3) ? DATABASE_NULL : reader.GetInt64(3); item.stackable = reader.GetBoolean(4); item.capacity = reader.IsDBNull(5) ? DATABASE_NULL : reader.GetFloat(5); item.category = reader.IsDBNull(6) ? "Unknown" : reader.GetString(6); item.discard = reader.GetBoolean(7); item.convert_to_gold = reader.GetBoolean(8); item.look_text = reader.IsDBNull(9) ? String.Format("You see a {0}.", item.displayname) : reader.GetString(9); item.title = reader.GetString(10); item.currency = reader.IsDBNull(11) ? DATABASE_NULL : reader.GetInt32(11); item.image = Image.FromStream(reader.GetStream(12)); if (item.image.RawFormat.Guid == ImageFormat.Gif.Guid) { int frames = item.image.GetFrameCount(FrameDimension.Time); if (frames == 1) { Bitmap new_bitmap = new Bitmap(item.image); new_bitmap.MakeTransparent(); item.image.Dispose(); item.image = new_bitmap; } } command = new SQLiteCommand(String.Format("SELECT vendorid, value FROM SellItems WHERE itemid={0}", item.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { ItemSold sellItem = new ItemSold(); sellItem.itemid = item.id; sellItem.npcid = reader.GetInt32(0); sellItem.price = reader.GetInt32(1); item.sellItems.Add(sellItem); } command = new SQLiteCommand(String.Format("SELECT vendorid, value FROM BuyItems WHERE itemid={0}", item.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { ItemSold buyItem = new ItemSold(); buyItem.itemid = item.id; buyItem.npcid = reader.GetInt32(0); buyItem.price = reader.GetInt32(1); item.buyItems.Add(buyItem); } command = new SQLiteCommand(String.Format("SELECT creatureid, percentage, min, max FROM CreatureDrops WHERE itemid={0}", item.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { ItemDrop itemDrop = new ItemDrop(); itemDrop.itemid = item.id; itemDrop.creatureid = reader.GetInt32(0); itemDrop.percentage = reader.IsDBNull(1) ? DATABASE_NULL : reader.GetFloat(1); if (itemDrop.percentage > 100) { itemDrop.min = 1; itemDrop.max = (int)(itemDrop.percentage / 100.0 * 2.0); itemDrop.percentage = 100; } else { itemDrop.min = Math.Max(reader.GetInt32(2), 1); itemDrop.max = Math.Max(reader.GetInt32(3), itemDrop.min); } item.itemdrops.Add(itemDrop); } command = new SQLiteCommand(String.Format("SELECT questid FROM QuestRewards WHERE itemid={0}", item.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { item.rewardedBy.Add(getQuest(reader.GetInt32(0))); } command = new SQLiteCommand(String.Format("SELECT property, value FROM ItemProperties WHERE itemid={0}", item.id), mainForm.conn); reader = command.ExecuteReader(); while (reader.Read()) { string property = reader.GetString(0); switch(property) { case "Voc": item.vocation = reader.GetString(1); break; case "Level": item.level = reader.GetInt32(1); break; case "Def": item.defensestr = reader["value"].ToString(); if (!int.TryParse(item.defensestr, out item.defense)) { item.defense = int.Parse(item.defensestr.Split(' ')[0]); } break; case "Attrib": item.attrib = reader.GetString(1); break; case "Atk": item.attack = reader.GetInt32(1); break; case "Atk+": item.atkmod = reader.GetInt32(1); break; case "Hit+": string str = reader["value"].ToString(); int.TryParse(str, out item.hitmod); break; case "Arm": item.armor = reader.GetInt32(1); break; case "Range": item.range = reader.GetInt32(1); break; case "Type": item.type = reader.GetString(1); break; } } return item; }