using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace mmoRL { public class Map { public static int MAP_WIDTH = 70; public static int MAP_HEIGHT = 15; public string id = "unknown"; public Tile[,] tiles = new Tile[70, 15]; public Object[,] objects = new Object[70, 15]; public List items = new List(); public List monsters = new List(); public bool turnBased = false; public bool canDestroy = true; public bool canBuild = true; public string instanceOwner = ""; public Map(byte[] serialized) { System.IO.BinaryReader br = new System.IO.BinaryReader(new System.IO.MemoryStream(serialized)); //Version check byte b1 = br.ReadByte(); byte b2 = br.ReadByte(); byte b3 = 0x00; //Will be used later if (b1 != 0x00 || b2 != 0x09) throw new Exception("Map version mismatch!"); //Map ID b1 = br.ReadByte(); byte[] mapID = br.ReadBytes(b1); id = Encoding.ASCII.GetString(mapID); //Turnbased map if (br.ReadByte() == 0x01) { turnBased = true; } else { turnBased = false; } //Can destroy stuff on map if (br.ReadByte() == 0x01) { canDestroy = true; } else { canDestroy = false; } //Can build stuff on map if (br.ReadByte() == 0x01) { canBuild = true; } else { canBuild = false; } //Get instance owner int instanceOwnerLength = br.ReadByte(); instanceOwner = Encoding.ASCII.GetString(br.ReadBytes(instanceOwnerLength)); //Then fetch every tile in the map for (int iy = 0; iy < 15; iy++) { for (int ix = 0; ix < 70; ix++) { b1 = br.ReadByte(); if (b1 != 0xFF) { tiles[ix, iy] = new Tile(b1); } else //Custom tile { b1 = br.ReadByte(); //Fetch length tiles[ix, iy] = new Tile(br.ReadBytes(b1)); } } } //Then fetch every object in the map for (int iy = 0; iy < 15; iy++) { for (int ix = 0; ix < 70; ix++) { b1 = br.ReadByte(); if (b1 != 0xFF) { objects[ix, iy] = new Object(b1); } else //Custom object { b1 = br.ReadByte(); //Fetch length objects[ix, iy] = new Object(br.ReadBytes(b1)); } } } //Get number of items b1 = br.ReadByte(); b2 = br.ReadByte(); int numItems = b1 * 256 + b2; for (int i = 0; i < numItems; i++) { int x = br.ReadByte(); int y = br.ReadByte(); b1 = br.ReadByte(); if (b1 != 0xFF) { b2 = br.ReadByte(); b3 = br.ReadByte(); int itemID = b2 * 256 + b3; Item toAdd = new Item(itemID, x, y); items.Add(toAdd); toAdd.amount = b1; } else { b1 = br.ReadByte(); //Fetch length Item item = new Item(br.ReadBytes(b1)); item.x = x; item.y = y; items.Add(item); } } //Get number of monsters int numMonsters = br.ReadByte(); for (int i = 0; i < numMonsters; i++) { int x = br.ReadByte(); int y = br.ReadByte(); int monsterType = br.ReadByte(); monsters.Add(new Monster(monsterType, x, y)); } //Final confirmation, closing check b1 = br.ReadByte(); if (b1 != 0xFF) throw new Exception("Invalid endpoint for map data."); br.Close(); } public void RemoveItem(int x, int y, int itemID) { Item toRemove = null; for (int i = 0; i < MainClass.map.items.Count; i++) { if (MainClass.map.items[i].id == itemID) { if (MainClass.map.items[i].x == x && MainClass.map.items[i].y == y) { toRemove = MainClass.map.items[i]; } } } if (toRemove != null) MainClass.map.items.Remove(toRemove); } public void KillMonster(Monster monster) { monsters.Remove(monster); if (monsters.Count == 0) this.turnBased = false; } } }