private void Load(string world, Del done) { ThreadStart loadThread = delegate() { try { currentWorld = world; bool foundInvalid = false; string invalid = ""; using (BinaryReader b = new BinaryReader(File.Open(world, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { int version = b.ReadInt32(); //now we care about the version if (version > MapVersion) // new map format throw new Exception("Unsupported map version: " + version); string title = b.ReadString(); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { Title = title; })); worldID = b.ReadInt32(); b.BaseStream.Seek(16, SeekOrigin.Current); //skip bounds tilesHigh = b.ReadInt32(); tilesWide = b.ReadInt32(); moonType = 0; if (version >= 63) moonType = b.ReadByte(); treeX[0] = treeX[1] = treeX[2] = 0; treeStyle[0] = treeStyle[1] = treeStyle[2] = treeStyle[3] = 0; if (version >= 44) { treeX[0] = b.ReadInt32(); treeX[1] = b.ReadInt32(); treeX[2] = b.ReadInt32(); treeStyle[0] = b.ReadInt32(); treeStyle[1] = b.ReadInt32(); treeStyle[2] = b.ReadInt32(); treeStyle[3] = b.ReadInt32(); } caveBackX[0] = caveBackX[1] = caveBackX[2] = 0; caveBackStyle[0] = caveBackStyle[1] = caveBackStyle[2] = caveBackStyle[3] = 0; iceBackStyle = jungleBackStyle = hellBackStyle = 0; if (version >= 60) { caveBackX[0] = b.ReadInt32(); caveBackX[1] = b.ReadInt32(); caveBackX[2] = b.ReadInt32(); caveBackStyle[0] = b.ReadInt32(); caveBackStyle[1] = b.ReadInt32(); caveBackStyle[2] = b.ReadInt32(); caveBackStyle[3] = b.ReadInt32(); iceBackStyle = b.ReadInt32(); if (version >= 61) { jungleBackStyle = b.ReadInt32(); hellBackStyle = b.ReadInt32(); } } spawnX = b.ReadInt32(); spawnY = b.ReadInt32(); groundLevel = (int)b.ReadDouble(); rockLevel = (int)b.ReadDouble(); gameTime = b.ReadDouble(); dayNight = b.ReadBoolean(); moonPhase = b.ReadInt32(); bloodMoon = b.ReadBoolean(); eclipse = false; if (version >= 70) eclipse = b.ReadBoolean(); dungeonX = b.ReadInt32(); dungeonY = b.ReadInt32(); crimson = false; if (version >= 56) crimson = b.ReadBoolean(); killedBoss1 = b.ReadBoolean(); killedBoss2 = b.ReadBoolean(); killedBoss3 = b.ReadBoolean(); killedQueenBee = false; if (version >= 66) killedQueenBee = b.ReadBoolean(); killedMechBoss1 = killedMechBoss2 = killedMechBoss3 = killedMechBossAny = false; if (version >= 44) { killedMechBoss1 = b.ReadBoolean(); killedMechBoss2 = b.ReadBoolean(); killedMechBoss3 = b.ReadBoolean(); killedMechBossAny = b.ReadBoolean(); } killedPlantBoss = killedGolemBoss = false; if (version >= 64) { killedPlantBoss = b.ReadBoolean(); killedGolemBoss = b.ReadBoolean(); } savedTinkerer = savedWizard = savedMechanic = killedGoblins = killedClown = killedFrost = killedPirates = false; if (version >= 29) { savedTinkerer = b.ReadBoolean(); savedWizard = b.ReadBoolean(); if (version >= 34) savedMechanic = b.ReadBoolean(); killedGoblins = b.ReadBoolean(); if (version >= 32) killedClown = b.ReadBoolean(); if (version >= 37) killedFrost = b.ReadBoolean(); if (version >= 56) killedPirates = b.ReadBoolean(); } smashedOrb = b.ReadBoolean(); meteorSpawned = b.ReadBoolean(); shadowOrbCount = b.ReadByte(); altarsSmashed = 0; hardMode = false; if (version >= 23) { altarsSmashed = b.ReadInt32(); hardMode = b.ReadBoolean(); } goblinsDelay = b.ReadInt32(); goblinsSize = b.ReadInt32(); goblinsType = b.ReadInt32(); goblinsX = b.ReadDouble(); isRaining = false; rainTime = 0; maxRain = 0.0F; oreTier1 = 107; oreTier2 = 108; oreTier3 = 111; if (version >= 23 && altarsSmashed == 0) oreTier1 = oreTier2 = oreTier3 = -1; if (version >= 53) { isRaining = b.ReadBoolean(); rainTime = b.ReadInt32(); maxRain = b.ReadSingle(); if (version >= 54) { oreTier1 = b.ReadInt32(); oreTier2 = b.ReadInt32(); oreTier3 = b.ReadInt32(); } } if (version >= 55) { int numstyles = 3; if (version >= 60) numstyles = 8; for (int i = 0; i < numstyles; i++) styles[i] = b.ReadByte(); //skip clouds if (version >= 60) { b.BaseStream.Seek(4, SeekOrigin.Current); if (version >= 62) //skip wind b.BaseStream.Seek(6, SeekOrigin.Current); } } ResizeMap(); for (int x = 0; x < tilesWide; x++) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { serverText.Text = ((int)((float)x * 100.0 / (float)tilesWide)) + "% - Reading tiles"; })); for (int y = 0; y < tilesHigh; y++) { tiles[x, y].isActive = b.ReadBoolean(); if (tiles[x, y].isActive) { tiles[x, y].type = b.ReadByte(); if (tiles[x, y].type > MaxTile) // something screwy in the map { tiles[x, y].isActive = false; foundInvalid = true; invalid = String.Format("{0} is not a valid tile type", tiles[x, y].type); } else if (tileInfos[tiles[x, y].type].hasExtra) { // torches and platforms didn't have extra in older versions. if ((version < 28 && tiles[x, y].type == 4) || (version < 40 && tiles[x, y].type == 19)) { tiles[x, y].u = -1; tiles[x, y].v = -1; } else { tiles[x, y].u = b.ReadInt16(); tiles[x, y].v = b.ReadInt16(); if (tiles[x, y].type == 144) //timer tiles[x, y].v = 0; } } else { tiles[x, y].u = -1; tiles[x, y].v = -1; } if (version >= 48 && b.ReadBoolean()) { tiles[x, y].color = b.ReadByte(); } } if (version <= 25) b.ReadBoolean(); //skip obsolete hasLight if (b.ReadBoolean()) { tiles[x, y].wall = b.ReadByte(); if (tiles[x, y].wall > MaxWall) // bad wall { foundInvalid = true; invalid = String.Format("{0} is not a valid wall type", tiles[x, y].wall); tiles[x, y].wall = 0; } if (version >= 48 && b.ReadBoolean()) tiles[x, y].wallColor = b.ReadByte(); tiles[x, y].wallu = -1; tiles[x, y].wallv = -1; } else tiles[x, y].wall = 0; if (b.ReadBoolean()) { tiles[x, y].liquid = b.ReadByte(); tiles[x, y].isLava = b.ReadBoolean(); if (version >= 51) tiles[x, y].isHoney = b.ReadBoolean(); } else tiles[x, y].liquid = 0; tiles[x, y].hasRedWire = false; tiles[x, y].hasGreenWire = false; tiles[x, y].hasBlueWire = false; tiles[x, y].half = false; tiles[x, y].actuator = false; tiles[x, y].inactive = false; tiles[x, y].slope = 0; if (version >= 33) { tiles[x, y].hasRedWire = b.ReadBoolean(); if (version >= 43) { tiles[x, y].hasGreenWire = b.ReadBoolean(); tiles[x, y].hasBlueWire = b.ReadBoolean(); } if (version >= 41) { tiles[x, y].half = b.ReadBoolean(); if (version >= 49) tiles[x, y].slope = b.ReadByte(); if (!tileInfos[tiles[x, y].type].solid) { tiles[x, y].half = false; tiles[x, y].slope = 0; } if (version >= 42) { tiles[x, y].actuator = b.ReadBoolean(); tiles[x, y].inactive = b.ReadBoolean(); } } } if (version >= 25) //RLE { int rle = b.ReadInt16(); for (int r = y + 1; r < y + 1 + rle; r++) { tiles[x, r].isActive = tiles[x, y].isActive; tiles[x, r].type = tiles[x, y].type; tiles[x, r].u = tiles[x, y].u; tiles[x, r].v = tiles[x, y].v; tiles[x, r].wall = tiles[x, y].wall; tiles[x, r].wallu = -1; tiles[x, r].wallv = -1; tiles[x, r].liquid = tiles[x, y].liquid; tiles[x, r].isLava = tiles[x, y].isLava; tiles[x, r].isHoney = tiles[x, y].isHoney; tiles[x, r].hasRedWire = tiles[x, y].hasRedWire; tiles[x, r].hasGreenWire = tiles[x, y].hasGreenWire; tiles[x, r].hasBlueWire = tiles[x, y].hasBlueWire; tiles[x, r].half = tiles[x, y].half; tiles[x, r].slope = tiles[x, y].slope; tiles[x, r].actuator = tiles[x, y].actuator; tiles[x, r].inactive = tiles[x, y].inactive; tiles[x, r].color = tiles[x, y].color; tiles[x, r].wallColor = tiles[x, y].wallColor; } y += rle; } } } int itemsPerChest = 40; if (version < 58) itemsPerChest = 20; chests.Clear(); for (int i = 0; i < 1000; i++) { if (b.ReadBoolean()) { Chest chest = new Chest(); chest.items = new ChestItem[itemsPerChest]; chest.x = b.ReadInt32(); chest.y = b.ReadInt32(); for (int ii = 0; ii < itemsPerChest; ii++) { if (version < 59) chest.items[ii].stack = b.ReadByte(); else chest.items[ii].stack = b.ReadInt16(); if (chest.items[ii].stack > 0) { string name = "Unknown"; if (version >= 38) //item names not stored { Int32 itemid = b.ReadInt32(); if (itemid < 0) { itemid = -itemid; if (itemid < itemNames2.Length) name = itemNames2[itemid]; } else if (itemid < itemNames.Length) name = itemNames[itemid]; } else name = b.ReadString(); string prefix = ""; if (version >= 36) //item prefixes { int pfx = b.ReadByte(); if (pfx < prefixes.Length) prefix = prefixes[pfx]; } chest.items[ii].name = name; chest.items[ii].prefix = prefix; } } chests.Add(chest); } } signs.Clear(); for (int i = 0; i < 1000; i++) { if (b.ReadBoolean()) { Sign sign = new Sign(); sign.text = b.ReadString(); sign.x = b.ReadInt32(); sign.y = b.ReadInt32(); signs.Add(sign); } } npcs.Clear(); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { NPCs.Items.Clear(); serverText.Text = "Loading NPCs..."; })); while (b.ReadBoolean()) { NPC npc = new NPC(); npc.title = b.ReadString(); npc.name = ""; npc.x = b.ReadSingle(); npc.y = b.ReadSingle(); npc.isHomeless = b.ReadBoolean(); npc.homeX = b.ReadInt32(); npc.homeY = b.ReadInt32(); npc.order = -1; npc.num = 0; npc.sprite = 0; for (int i = 0; i < friendlyNPCs.Length; i++) if (friendlyNPCs[i].title == npc.title) { npc.sprite = friendlyNPCs[i].id; npc.num = friendlyNPCs[i].num; npc.order = friendlyNPCs[i].order; } npcs.Add(npc); addNPCToMenu(npc); } if (version >= 31) //read npcs { int numNames = 9; if (version >= 34) numNames++; if (version >= 65) numNames += 8; for (int i = 0; i < numNames; i++) { string name = b.ReadString(); for (int j = 0; j < npcs.Count; j++) { if (npcs[j].order == i) { npcs[j].name = name; addNPCToMenu(npcs[j]); } } } } } if (foundInvalid) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { MessageBox.Show("Found problems with the map: " + invalid + "\nIt may not display properly.", "Warning"); })); } Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { serverText.Text = "Loading Fog of War..."; })); //load player's map loadPlayerMap(); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { QuickHiliteToggle.IsEnabled = true; render.SetWorld(tilesWide, tilesHigh, groundLevel, rockLevel, styles, treeX, treeStyle, caveBackX, caveBackStyle, jungleBackStyle, hellBackStyle, npcs); loaded = true; done(); })); calculateLight(); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { serverText.Text = ""; })); } catch (Exception e) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { MessageBox.Show(e.Message); loaded = false; serverText.Text = ""; done(); })); } }; new Thread(loadThread).Start(); }
private void LoadChests(BinaryReader b, int version) { int numChests = b.ReadInt16(); int itemsPerChest = b.ReadInt16(); chests.Clear(); for (int i = 0; i < numChests; i++) { Chest chest = new Chest(); chest.items = new ChestItem[itemsPerChest]; chest.x = b.ReadInt32(); chest.y = b.ReadInt32(); chest.name = b.ReadString(); for (int ii = 0; ii < itemsPerChest; ii++) { chest.items[ii].stack = b.ReadInt16(); if (chest.items[ii].stack > 0) { string name = "Unknown"; Int32 itemid = b.ReadInt32(); if (itemid < 0) { itemid = -itemid; if (itemid < itemNames2.Length) name = itemNames2[itemid]; } else if (itemid < itemNames.Length) name = itemNames[itemid]; string prefix = ""; int pfx = b.ReadByte(); if (pfx < prefixes.Length) prefix = prefixes[pfx]; chest.items[ii].name = name; chest.items[ii].prefix = prefix; } } chests.Add(chest); } }
private void LoadOldChests(BinaryReader b, int version) { int itemsPerChest = 40; if (version < 58) itemsPerChest = 20; chests.Clear(); for (int i = 0; i < 1000; i++) { if (b.ReadBoolean()) { Chest chest = new Chest(); chest.items = new ChestItem[itemsPerChest]; chest.x = b.ReadInt32(); chest.y = b.ReadInt32(); if (version >= 85) chest.name = b.ReadString(); for (int ii = 0; ii < itemsPerChest; ii++) { if (version < 59) chest.items[ii].stack = b.ReadByte(); else chest.items[ii].stack = b.ReadInt16(); if (chest.items[ii].stack > 0) { string name = "Unknown"; if (version >= 38) //item names not stored { Int32 itemid = b.ReadInt32(); if (itemid < 0) { itemid = -itemid; if (itemid < itemNames2.Length) name = itemNames2[itemid]; } else if (itemid < itemNames.Length) name = itemNames[itemid]; } else name = b.ReadString(); string prefix = ""; if (version >= 36) //item prefixes { int pfx = b.ReadByte(); if (pfx < prefixes.Length) prefix = prefixes[pfx]; } chest.items[ii].name = name; chest.items[ii].prefix = prefix; } } chests.Add(chest); } } }
private void Load(string world, Del done) { ThreadStart loadThread = delegate() { try { currentWorld = world; bool foundInvalid = false; string invalid = ""; using (BinaryReader b = new BinaryReader(File.Open(world,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))) { uint version = b.ReadUInt32(); //now we care about the version if (version > MapVersion) // new map format throw new Exception("Unsupported map version: "+version); string title = b.ReadString(); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { Title = title; })); b.BaseStream.Seek(20, SeekOrigin.Current); //skip id and bounds tilesHigh = b.ReadInt32(); tilesWide = b.ReadInt32(); spawnX = b.ReadInt32(); spawnY = b.ReadInt32(); groundLevel = (int)b.ReadDouble(); rockLevel = (int)b.ReadDouble(); gameTime = b.ReadDouble(); dayNight = b.ReadBoolean(); moonPhase = b.ReadInt32(); bloodMoon = b.ReadBoolean(); dungeonX = b.ReadInt32(); dungeonY = b.ReadInt32(); killedBoss1 = b.ReadBoolean(); killedBoss2 = b.ReadBoolean(); killedBoss3 = b.ReadBoolean(); savedTinkerer = savedWizard = savedMechanic = killedGoblins = killedClown = killedFrost = false; if (version >= 29) { savedTinkerer = b.ReadBoolean(); savedWizard = b.ReadBoolean(); if (version >= 34) savedMechanic = b.ReadBoolean(); killedGoblins = b.ReadBoolean(); if (version >= 32) killedClown = b.ReadBoolean(); if (version >= 37) killedFrost = b.ReadBoolean(); } smashedOrb = b.ReadBoolean(); meteorSpawned = b.ReadBoolean(); shadowOrbCount = b.ReadByte(); altarsSmashed = 0; hardMode = false; if (version >= 23) { altarsSmashed = b.ReadInt32(); hardMode = b.ReadBoolean(); } goblinsDelay = b.ReadInt32(); goblinsSize = b.ReadInt32(); goblinsType = b.ReadInt32(); goblinsX = b.ReadDouble(); ResizeMap(); for (int x = 0; x < tilesWide; x++) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { serverText.Text = ((int)((float)x * 100.0 / (float)tilesWide)) + "% - Reading tiles"; })); for (int y = 0; y < tilesHigh; y++) { tiles[x, y].isActive = b.ReadBoolean(); if (tiles[x, y].isActive) { tiles[x, y].type = b.ReadByte(); if (tiles[x, y].type > MaxTile) // something screwy in the map { tiles[x, y].isActive = false; foundInvalid = true; invalid = String.Format("{0} is not a valid tile type", tiles[x, y].type); } else if (tileInfos[tiles[x, y].type].hasExtra) { // torches didn't have extra in older versions. if (version < 0x1c && tiles[x, y].type == 4) { tiles[x, y].u = -1; tiles[x, y].v = -1; } else { tiles[x, y].u = b.ReadInt16(); tiles[x, y].v = b.ReadInt16(); if (tiles[x, y].type == 144) //timer tiles[x, y].v = 0; } } else { tiles[x, y].u = -1; tiles[x, y].v = -1; } } if (version <= 0x19) b.ReadBoolean(); //skip obsolete hasLight if (b.ReadBoolean()) { tiles[x, y].wall = b.ReadByte(); if (tiles[x, y].wall > MaxWall) // bad wall { foundInvalid = true; invalid = String.Format("{0} is not a valid wall type", tiles[x, y].wall); tiles[x, y].wall = 0; } tiles[x, y].wallu = -1; tiles[x, y].wallv = -1; } else tiles[x, y].wall = 0; if (b.ReadBoolean()) { tiles[x, y].liquid = b.ReadByte(); tiles[x, y].isLava = b.ReadBoolean(); } else tiles[x, y].liquid = 0; if (version >= 0x21) tiles[x, y].hasWire = b.ReadBoolean(); else tiles[x, y].hasWire = false; if (version >= 0x19) //RLE { int rle = b.ReadInt16(); for (int r = y + 1; r < y + 1 + rle; r++) { tiles[x, r].isActive = tiles[x, y].isActive; tiles[x, r].type = tiles[x, y].type; tiles[x, r].u = tiles[x, y].u; tiles[x, r].v = tiles[x, y].v; tiles[x, r].wall = tiles[x, y].wall; tiles[x, r].wallu = -1; tiles[x, r].wallv = -1; tiles[x, r].liquid = tiles[x, y].liquid; tiles[x, r].isLava = tiles[x, y].isLava; tiles[x, r].hasWire = tiles[x, y].hasWire; } y += rle; } } } chests.Clear(); for (int i = 0; i < 1000; i++) { if (b.ReadBoolean()) { Chest chest = new Chest(); chest.items = new ChestItem[20]; chest.x = b.ReadInt32(); chest.y = b.ReadInt32(); for (int ii = 0; ii < 20; ii++) { chest.items[ii].stack = b.ReadByte(); if (chest.items[ii].stack > 0) { string name="Unknown"; if (version >= 0x26) //item names not stored { Int32 itemid = b.ReadInt32(); if (itemid < 0) { itemid = -itemid; if (itemid < itemNames2.Length) name = itemNames2[itemid]; } else if (itemid < itemNames.Length) name = itemNames[itemid]; } else name = b.ReadString(); string prefix = ""; if (version >= 0x24) //item prefixes { int pfx = b.ReadByte(); if (pfx < prefixes.Length) prefix = prefixes[pfx]; } if (prefix != "") prefix += " "; chest.items[ii].name = prefix + name; } } chests.Add(chest); } } signs.Clear(); for (int i = 0; i < 1000; i++) { if (b.ReadBoolean()) { Sign sign = new Sign(); sign.text = b.ReadString(); sign.x = b.ReadInt32(); sign.y = b.ReadInt32(); signs.Add(sign); } } npcs.Clear(); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { NPCs.Items.Clear(); serverText.Text = "Loading NPCs..."; })); while (b.ReadBoolean()) { NPC npc = new NPC(); npc.title = b.ReadString(); npc.name = ""; npc.x = b.ReadSingle(); npc.y = b.ReadSingle(); npc.isHomeless = b.ReadBoolean(); npc.homeX = b.ReadInt32(); npc.homeY = b.ReadInt32(); npc.order = -1; npc.num = 0; npc.sprite = 0; for (int i=0;i<friendlyNPCs.Length;i++) if (friendlyNPCs[i].title == npc.title) { npc.sprite = friendlyNPCs[i].id; npc.num = friendlyNPCs[i].num; npc.order = friendlyNPCs[i].order; } npcs.Add(npc); addNPCToMenu(npc); } if (version >= 31) //read npcs { int numNames = 9; if (version>=34) numNames++; for (int i = 0; i < numNames; i++) { string name = b.ReadString(); for (int j = 0; j < npcs.Count; j++) { if (npcs[j].order == i) { npcs[j].name = name; addNPCToMenu(npcs[j]); } } } } } if (foundInvalid) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { MessageBox.Show("Found problems with the map: " + invalid + "\nIt may not display properly.", "Warning"); })); } Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { render.SetWorld(tilesWide, tilesHigh, groundLevel, rockLevel, npcs); loaded = true; done(); })); calculateLight(); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { serverText.Text = ""; })); } catch (Exception e) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { MessageBox.Show(e.Message); loaded = false; serverText.Text = ""; done(); })); } }; new Thread(loadThread).Start(); }