private void ReadNPCs() { Boolean nextNPC; NPC theNPC; String nameCrunch; String[] nameArray; NPCType npcType; Int32 i; npcs = new List<NPC>(20); i = 0; if (bw != null) bw.ReportProgress((Int32)(((Single)progressPosition / stream.Length) * readWorldPerc) , "Reading NPCs"); nextNPC = reader.ReadBoolean(); while (nextNPC == true) { theNPC = new NPC(); theNPC.Id = i; theNPC.Active = nextNPC; theNPC.Name = reader.ReadString(); theNPC.Position = new PointSingle(reader.ReadSingle(), reader.ReadSingle()); theNPC.Homeless = reader.ReadBoolean(); theNPC.HomeTile = new Point(reader.ReadInt32(), reader.ReadInt32()); nameArray = theNPC.Name.Split(' '); nameCrunch = ""; for (Int32 j = 0; j < nameArray.Length; j++) nameCrunch += nameArray[j]; if (Enum.TryParse<NPCType>(nameCrunch, out npcType)) theNPC.Type = npcType; else theNPC.Type = NPCType.Unknown; npcs.Add(theNPC); i++; nextNPC = reader.ReadBoolean(); progressPosition = stream.Position; } posFooter = stream.Position; }
/// <summary> /// Tries to make the next bytes backward in the stream fit into an NPC object. /// If it fails sets the position back to where it was. /// </summary> /// <returns>An NPC object. activeNpc will be true for a valid Sign.</returns> private NPC tryReadNPCBackwards() { Int32 x, y; NPC returnNpc = new NPC(); long oldPosition = stream.Position; Boolean validNPC = false; #if (DEBUG == false) try { #endif y = backReader.ReadBackwardsInt32(); x = backReader.ReadBackwardsInt32(); returnNpc.HomeTile = new Point(x, y); returnNpc.Homeless = backReader.ReadBackwardsBoolean(); returnNpc.Position = new PointSingle(backReader.ReadBackwardsSingle(), backReader.ReadBackwardsSingle()); returnNpc.Name = backReader.ReadBackwardsString(); if (!String.IsNullOrEmpty(returnNpc.Name)) { returnNpc.Active = backReader.ReadBackwardsBoolean(); if (returnNpc.Active == true) validNPC = true; } #if (DEBUG == false) } catch (EndOfStreamException e) { e.GetType(); } #endif if (validNPC == false) { stream.Seek(oldPosition, SeekOrigin.Begin); returnNpc.Active = false; } return returnNpc; }