Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 27.40 KB | None | 0 0
  1. using JoyLib.Code.Entities;
  2. using JoyLib.Code.Entities.AI;
  3. using JoyLib.Code.Entities.Items;
  4. using JoyLib.Code.Helpers;
  5. using JoyLib.Code.IO;
  6. using JoyLib.Code.Managers;
  7. using JoyLib.Code.States;
  8. using Microsoft.Xna.Framework;
  9. using Newtonsoft.Json;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Threading;
  14.  
  15. namespace JoyLib.Code.World
  16. {
  17.     public class WorldInstance
  18.     {
  19.         protected WorldTile[,] m_Tiles;
  20.         protected int[,] m_Light;
  21.         protected bool[,] m_Discovered;
  22.         protected int m_PlayerIndex;
  23.  
  24.         protected readonly WorldType m_Type;
  25.  
  26.         //Worlds and where to access them
  27.         //[JsonConverter(typeof(AreaConverter))]
  28.         protected Dictionary<string, WorldInstance> m_Areas;
  29.  
  30.         [JsonIgnore]
  31.         protected WorldInstance m_Parent;
  32.  
  33.         protected List<Entity> m_Entities;
  34.  
  35.         protected List<BaseJoyObject> m_Objects;
  36.  
  37.         [JsonConverter(typeof(PointConverter))]
  38.         protected Point m_SpawnPoint;
  39.  
  40.         protected static DateTime s_DateTime;
  41.  
  42.         public WorldInstance(WorldTile[,] tiles, Dictionary<string, WorldInstance> areas, List<Entity> entities,
  43.             List<BaseJoyObject> objects, WorldType type, string name)
  44.         {
  45.             this.name = name;
  46.             worldType = type;
  47.             m_Tiles = tiles;
  48.             m_Areas = areas;
  49.             m_Entities = entities;
  50.             m_Objects = objects;
  51.             GUID = GUIDManager.AssignGUID();
  52.             m_Discovered = new bool[m_Tiles.GetLength(0), m_Tiles.GetLength(1)];
  53.             m_Type = type;
  54.             CalculatePlayerIndex();
  55.             m_Light = new int[m_Tiles.GetLength(0), m_Tiles.GetLength(1)];
  56.         }
  57.  
  58.         public void SetDateTime(DateTime dateTime)
  59.         {
  60.             s_DateTime = dateTime;
  61.         }
  62.  
  63.         protected void CalculatePlayerIndex()
  64.         {
  65.             for (int i = 0; i < m_Entities.Count; i++)
  66.             {
  67.                 if (m_Entities[i].playerControlled)
  68.                 {
  69.                     m_PlayerIndex = i;
  70.                     return;
  71.                 }
  72.             }
  73.         }
  74.  
  75.         protected void CalculateLightLevels()
  76.         {
  77.             m_Light = new int[m_Light.GetLength(0), m_Light.GetLength(1)];
  78.  
  79.             //Do objects first
  80.             List<BaseJoyObject> objects = m_Objects.ToList();
  81.  
  82.             for(int i = 0; i < objects.Count; i++)
  83.             {
  84.                 if (objects[i] is ItemInstance == false)
  85.                     continue;
  86.  
  87.                 ItemInstance item = (ItemInstance)objects[i];
  88.  
  89.                 int xMin, xMax;
  90.                 int yMin, yMax;
  91.  
  92.                 xMin = Math.Max(0, item.position.X - (item.lightLevel));
  93.                 xMax = Math.Min(m_Light.GetLength(0) - 1, item.position.X + (item.lightLevel));
  94.  
  95.                 yMin = Math.Max(0, item.position.Y - (item.lightLevel));
  96.                 yMax = Math.Min(m_Light.GetLength(1) - 1, item.position.Y + (item.lightLevel));
  97.  
  98.                 if(item.lightLevel > 0)
  99.                 {
  100.                     for(int x = xMin; x < xMax; x++)
  101.                     {
  102.                         for(int y = yMin; y < yMax; y++)
  103.                         {
  104.                             m_Light[x, y] = (int)Math.Max(0, (item.lightLevel - Vector2.Distance(item.position.ToVector2(), new Point(x, y).ToVector2())));
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             //Then the objects used by entities
  111.             List<Entity> entities = m_Entities.ToList();
  112.  
  113.             for(int i = 0; i < entities.Count; i++)
  114.             {
  115.                 Entity entity = entities[i];
  116.                 for(int j = 0; j < entity.backpack.Count; j++)
  117.                 {
  118.                     ItemInstance item = entity.backpack[j];
  119.  
  120.                     int xMin, xMax;
  121.                     int yMin, yMax;
  122.  
  123.                     xMin = Math.Max(0, entity.position.X - (item.lightLevel));
  124.                     xMax = Math.Min(m_Light.GetLength(0) - 1, entity.position.X + (item.lightLevel));
  125.  
  126.                     yMin = Math.Max(0, entity.position.Y - (item.lightLevel));
  127.                     yMax = Math.Min(m_Light.GetLength(1) - 1, entity.position.Y + (item.lightLevel));
  128.  
  129.                     if (item.lightLevel > 0)
  130.                     {
  131.                         for (int x = xMin; x < xMax; x++)
  132.                         {
  133.                             for (int y = yMin; y < yMax; y++)
  134.                             {
  135.                                 m_Light[x, y] += (int)Math.Max(0, (item.lightLevel - Vector2.Distance(entity.position.ToVector2(), new Point(x, y).ToVector2())));
  136.                                 m_Light[x, y] = Math.Min(16, m_Light[x, y]);
  137.                             }
  138.                         }
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.  
  144.         public void AddObject(BaseJoyObject objectRef)
  145.         {
  146.             lock(m_Objects)
  147.             {
  148.                 m_Objects.Add(objectRef);
  149.             }
  150.         }
  151.  
  152.         public void RemoveObject(Point positionRef)
  153.         {
  154.             lock(m_Objects)
  155.             {
  156.                 for (int i = 0; i < m_Objects.Count; i++)
  157.                 {
  158.                     if (m_Objects[i].position == positionRef)
  159.                     {
  160.                         m_Objects.RemoveAt(i);
  161.                         return;
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.  
  167.         public BaseJoyObject GetObject(Point position)
  168.         {
  169.             for(int i = 0; i < m_Objects.Count; i++)
  170.             {
  171.                 if (m_Objects[i].position == position)
  172.                 {
  173.                     return m_Objects[i];
  174.                 }
  175.             }
  176.             return null;
  177.         }
  178.  
  179.         public void Tick()
  180.         {
  181.             DateTime oldTime = s_DateTime;
  182.             if (worldType != WorldType.Overworld)
  183.             {
  184.                 s_DateTime = s_DateTime.AddSeconds(6.0);
  185.             }
  186.             else
  187.             {
  188.                 s_DateTime = s_DateTime.AddHours(1.0);
  189.             }
  190.         }
  191.  
  192.         public void Update()
  193.         {
  194.             foreach(Entity entity in m_Entities)
  195.             {
  196.                 Thread childThread = new Thread(new ThreadStart(entity.Update));
  197.                 childThread.Start();
  198.             }
  199.  
  200.             CalculateLightLevels();
  201.         }
  202.  
  203.         public List<NeedAIData> SearchForObjects(Entity entityRef, string objectType, Intent intentRef)
  204.         {
  205.             List<NeedAIData> data = new List<NeedAIData>();
  206.  
  207.             if (entityRef.vision.GetLength(0) == 1)
  208.                 return data;
  209.  
  210.             //Special cases
  211.             //Ownable objects
  212.             if(objectType.Equals("Ownable"))
  213.             {
  214.                 for(int i = 0; i < m_Objects.Count; i++)
  215.                 {
  216.                     if (!entityRef.vision[m_Objects[i].position.X, m_Objects[i].position.Y])
  217.                         continue;
  218.  
  219.                     if (!m_Objects[i].GetType().Equals(typeof(ItemInstance)))
  220.                         continue;
  221.  
  222.                     NeedAIData tempData = new NeedAIData();
  223.                     tempData.intent = intentRef;
  224.                     tempData.target = m_Objects[i];
  225.                     data.Add(tempData);
  226.                 }
  227.             }
  228.  
  229.             for(int i = 0; i < m_Objects.Count; i++)
  230.             {
  231.                 if(entityRef.vision[m_Objects[i].position.X, m_Objects[i].position.Y])
  232.                 {
  233.                     if (m_Objects[i].baseType.Equals(objectType))
  234.                     {
  235.                         NeedAIData tempData = new NeedAIData();
  236.                         tempData.intent = intentRef;
  237.                         tempData.target = m_Objects[i];
  238.                         data.Add(tempData);
  239.                     }
  240.                 }
  241.             }
  242.  
  243.             return data;
  244.         }
  245.  
  246.         public List<NeedAIData> SearchForEntities(Entity searcher, string entityTypeRef, Intent intentRef)
  247.         {
  248.             List<NeedAIData> data = new List<NeedAIData>();
  249.  
  250.             //Special cases
  251.             //Non-sentient entities
  252.             if (entityTypeRef.Equals("Entities-ns"))
  253.             {
  254.                 List<Entity> entities = m_Entities.Where(x => x.sentient == false).ToList();
  255.  
  256.                 for (int i = 0; i < entities.Count; i++)
  257.                 {
  258.                     if (searcher.vision[entities[i].position.X, entities[i].position.Y] && searcher.GUID != entities[i].GUID)
  259.                     {
  260.                         NeedAIData tempData = new NeedAIData();
  261.                         tempData.intent = intentRef;
  262.                         tempData.target = entities[i];
  263.                         data.Add(tempData);
  264.                     }
  265.                 }
  266.                 return data;
  267.             }
  268.  
  269.             //Sentient entities
  270.             else if (entityTypeRef.Equals("Entities-s"))
  271.             {
  272.                 List<Entity> entities = m_Entities.Where(x => x.sentient).ToList();
  273.  
  274.                 for (int i = 0; i < entities.Count; i++)
  275.                 {
  276.                     if (searcher.vision[entities[i].position.X, entities[i].position.Y] && searcher.GUID != entities[i].GUID)
  277.                     {
  278.                         NeedAIData tempData = new NeedAIData();
  279.                         tempData.intent = intentRef;
  280.                         tempData.target = entities[i];
  281.                         data.Add(tempData);
  282.                     }
  283.                 }
  284.                 return data;
  285.             }
  286.  
  287.             //All entities
  288.             else if(entityTypeRef.Equals("Entities"))
  289.             {
  290.                 List<NeedAIData> dataList = new List<NeedAIData>();
  291.                 for(int i = 0; i < m_Entities.Count; i++)
  292.                 {
  293.                     if(searcher.vision[m_Entities[i].position.X, m_Entities[i].position.Y] && searcher.GUID != m_Entities[i].GUID)
  294.                     {
  295.                         NeedAIData tempData = new NeedAIData();
  296.                         tempData.intent = intentRef;
  297.                         tempData.target = m_Entities[i];
  298.                         dataList.Add(tempData);
  299.                     }
  300.                 }
  301.                 return dataList;
  302.             }
  303.  
  304.             else
  305.             {
  306.                 for (int i = 0; i < m_Entities.Count; i++)
  307.                 {
  308.                     if (searcher.vision[m_Entities[i].position.X, m_Entities[i].position.Y])
  309.                     {
  310.                         if ((m_Entities[i].baseType.Equals(entityTypeRef) ||
  311.                             m_Entities[i].creatureType.Equals(entityTypeRef)) &&
  312.                             searcher.GUID != entities[i].GUID)
  313.                         {
  314.                             NeedAIData tempData = new NeedAIData();
  315.                             tempData.intent = intentRef;
  316.                             tempData.target = entities[i];
  317.                             data.Add(tempData);
  318.                         }
  319.                     }
  320.                 }
  321.                 return data;
  322.             }
  323.         }
  324.  
  325.         public NeedAIData SearchForEntities(Entity entityRef, List<int> GUIDs, Intent intentRef)
  326.         {
  327.             NeedAIData data = new NeedAIData();
  328.             data.intent = intentRef;
  329.  
  330.             Dictionary<int, Entity> chosenEntities = m_Entities.Where(x => GUIDs.Contains(x.GUID)).ToDictionary(x => x.GUID, x => x);
  331.             List<Entity> visibleEntities = new List<Entity>();
  332.  
  333.             foreach(Entity entity in chosenEntities.Values)
  334.             {
  335.                 if(entityRef.vision[entity.position.X, entity.position.Y] && entity.GUID != entityRef.GUID)
  336.                 {
  337.                     visibleEntities.Add(entity);
  338.                 }
  339.             }
  340.             if (visibleEntities.Count > 0)
  341.             {
  342.                 data.target = visibleEntities[RNG.Roll(0, visibleEntities.Count - 1)];
  343.             }
  344.             return data;
  345.         }
  346.  
  347.         public NeedAIData SearchForMate(Entity entityRef)
  348.         {
  349.             NeedAIData data = new NeedAIData();
  350.             data.intent = Intent.Interact;
  351.  
  352.             List<Entity> validPartners = new List<Entity>();
  353.             if (entityRef.sexuality == Sexuality.Heterosexual)
  354.             {
  355.                 validPartners = m_Entities.Where(x => x.gender != entityRef.gender && x.sentient == entityRef.sentient &&
  356.                 (x.sexuality == Sexuality.Heterosexual || x.sexuality == Sexuality.Bisexual) && x.creatureType.Equals(entityRef.creatureType) &&
  357.                 x.needs[NeedIndex.Sex].contributingHappiness == false).ToList();
  358.             }
  359.             else if(entityRef.sexuality == Sexuality.Homosexual)
  360.             {
  361.                 validPartners = m_Entities.Where(x => x.gender == entityRef.gender && x.sentient == entityRef.sentient &&
  362.                 (x.sexuality == Sexuality.Homosexual || x.sexuality == Sexuality.Bisexual) && x.creatureType.Equals(entityRef.creatureType) &&
  363.                 x.needs[NeedIndex.Sex].contributingHappiness == false).ToList();
  364.             }
  365.             else if(entityRef.sexuality == Sexuality.Bisexual)
  366.             {
  367.                 validPartners = m_Entities.Where(x => x.sentient == entityRef.sentient &&
  368.                 (x.sexuality == Sexuality.Heterosexual || x.sexuality == Sexuality.Bisexual) && x.creatureType.Equals(entityRef.creatureType) &&
  369.                 x.needs[NeedIndex.Sex].contributingHappiness == false).ToList();
  370.             }
  371.  
  372.             List<Entity> visiblePartners = new List<Entity>();
  373.             for(int i = 0; i < validPartners.Count; i++)
  374.             {
  375.                 if (validPartners[i].GUID == entityRef.GUID)
  376.                     continue;
  377.  
  378.                 if (entityRef.vision[validPartners[i].position.X, validPartners[i].position.Y])
  379.                     visiblePartners.Add(validPartners[i]);
  380.             }
  381.             if (visiblePartners.Count > 0)
  382.                 data.target = visiblePartners[RNG.Roll(0, visiblePartners.Count - 1)];
  383.  
  384.             return data;
  385.         }
  386.  
  387.         public Entity GetRandomSentient()
  388.         {
  389.             List<Entity> sentients = m_Entities.Where(x => x.sentient).ToList();
  390.             if (sentients.Count > 0)
  391.                 return sentients[RNG.Roll(0, sentients.Count - 1)];
  392.             else
  393.                 return null;
  394.         }
  395.  
  396.         public Entity GetRandomSentientWorldWide()
  397.         {
  398.             List<WorldInstance> worlds = GetWorlds(WorldState.overworld);
  399.             int result = RNG.Roll(0, worlds.Count - 1);
  400.             Entity entity = worlds[result].GetRandomSentient();
  401.             while (entity == null)
  402.             {
  403.                 result = RNG.Roll(0, worlds.Count - 1);
  404.                 entity = worlds[result].GetRandomSentient();
  405.             }
  406.             return entity;
  407.         }
  408.  
  409.         public List<WorldInstance> GetWorlds(WorldInstance parent)
  410.         {
  411.             List<WorldInstance> worlds = new List<WorldInstance>();
  412.             worlds.Add(parent);
  413.             for(int i = 0; i < worlds.Count; i++)
  414.             {
  415.                 foreach(WorldInstance world in worlds[i].m_Areas.Values)
  416.                 {
  417.                     List<WorldInstance> newWorlds = GetWorlds(world);
  418.                     for(int j = 0; j < newWorlds.Count; j++)
  419.                     {
  420.                         if (!worlds.Contains(newWorlds[j]))
  421.                             worlds.Add(newWorlds[j]);
  422.                     }
  423.                 }
  424.             }
  425.  
  426.             return worlds;
  427.         }
  428.  
  429.         public void SwapPosition(Entity left, Entity right)
  430.         {
  431.             Point tempPosition = right.position;
  432.             right.Move(left.position);
  433.             left.Move(tempPosition);
  434.         }
  435.  
  436.         public void PickUpObject(Entity entityRef)
  437.         {
  438.             ItemInstance item = (ItemInstance)GetObject(entityRef.position);
  439.             if (item != null)
  440.             {
  441.                 item.ownerGUID = entityRef.GUID;
  442.                 entityRef.AddItem(item);
  443.                 lock (m_Objects)
  444.                 {
  445.                     m_Objects.Remove(item);
  446.                 }
  447.             }
  448.         }
  449.  
  450.         public void TradeObjects(Entity leftEntity, ItemInstance leftItem, Entity rightEntity, ItemInstance rightItem)
  451.         {
  452.             if(leftItem != null)
  453.             {
  454.                 leftEntity.RemoveItemFromBackpack(leftItem);
  455.                 rightEntity.AddItem(leftItem);
  456.             }
  457.  
  458.             if(rightItem != null)
  459.             {
  460.                 rightEntity.RemoveItemFromBackpack(rightItem);
  461.                 leftEntity.AddItem(rightItem);
  462.             }
  463.         }
  464.  
  465.         public bool[,] GetVision(Entity entityRef)
  466.         {
  467.             bool[,] vision = new bool[m_Tiles.GetLength(0), m_Tiles.GetLength(1)];
  468.             vision = DiscoverTiles(entityRef.position, entityRef, vision);
  469.             if (entityRef.playerControlled)
  470.             {
  471.                 m_Discovered = DiscoverTiles(entityRef.position, entityRef, m_Discovered);
  472.             }
  473.             return vision;
  474.         }
  475.  
  476.         protected bool[,] DiscoverTiles(Point pointRef, Entity entityRef, bool[,] visionRef)
  477.         {
  478.             bool[,] vision = visionRef;
  479.  
  480.             float entityPerceptionMod = entityRef.statistics[StatisticIndex.Perception] / 10 + 1;
  481.  
  482.             lock(m_Objects)
  483.             {
  484.                 List<BaseJoyObject> wallsUnsorted = m_Objects.Where(x => x.baseType.Equals("Wall")).ToList();
  485.                 Dictionary<Point, BaseJoyObject> walls = wallsUnsorted.ToDictionary(x => x.position, x => x);
  486.                 walls = walls.Where(x => x.Key.X <= pointRef.X + entityPerceptionMod &&
  487.                     x.Key.X >= pointRef.X - entityPerceptionMod &&
  488.                     x.Key.Y <= pointRef.Y + entityPerceptionMod &&
  489.                     x.Key.Y >= pointRef.Y - entityPerceptionMod).ToDictionary(x => x.Key, x => x.Value);
  490.  
  491.                 for (int i = 0; i < 360; i++)
  492.                 {
  493.                     float x = (float)Math.Cos(i * 0.01745f);
  494.                     float y = (float)Math.Sin(i * 0.01745f);
  495.                     vision = DiscoverTile(x, y, entityRef, entityPerceptionMod, walls, vision);
  496.                 }
  497.             }
  498.  
  499.             return vision;
  500.         }
  501.  
  502.         protected bool[,] DiscoverTile(float x, float y, Entity entityRef, float perceptionMod,
  503.             Dictionary<Point, BaseJoyObject> walls, bool[,] visionRef)
  504.         {
  505.             bool[,] vision = visionRef;
  506.             float oX, oY;
  507.  
  508.             oX = entityRef.position.X + 0.5f;
  509.             oY = entityRef.position.Y + 0.5f;
  510.  
  511.             for (int i = 0; i < perceptionMod; i++)
  512.             {
  513.  
  514.                 if (oX < 0.0f || oY < 0.0f || oX >= vision.GetLength(0) || oY >= vision.GetLength(1))
  515.                     return vision;
  516.  
  517.                 if (m_Light[(int)oX, (int)oY] == 0 && entityRef.visionType != VisionType.Nocturnal)
  518.                     return vision;
  519.  
  520.                 vision[(int)oX, (int)oY] = true;
  521.                 if (walls.ContainsKey(new Point((int)oX, (int)oY)))
  522.                     return vision;
  523.  
  524.                 oX += x;
  525.                 oY += y;
  526.             }
  527.  
  528.             return vision;
  529.         }
  530.  
  531.         public void AddEntity(Entity entityRef)
  532.         {
  533.             m_Entities.Add(entityRef);
  534.             CalculatePlayerIndex();
  535.         }
  536.  
  537.         public void RemoveEntity(Point positionRef)
  538.         {
  539.             for (int i = 0; i < m_Entities.Count; i++)
  540.             {
  541.                 if (m_Entities[i].position.Equals(positionRef))
  542.                 {
  543.                     m_Entities.RemoveAt(i);
  544.                     break;
  545.                 }
  546.             }
  547.             CalculatePlayerIndex();
  548.         }
  549.  
  550.         public Entity GetEntity(Point positionRef)
  551.         {
  552.             for (int i = 0; i < m_Entities.Count; i++)
  553.             {
  554.                 if (m_Entities[i].position.Equals(positionRef))
  555.                 {
  556.                     return m_Entities[i];
  557.                 }
  558.             }
  559.             return null;
  560.         }
  561.  
  562.         public Entity GetEntityRecursive(int GUID)
  563.         {
  564.             for(int i = 0; i < m_Entities.Count; i++)
  565.             {
  566.                 if(m_Entities[i].GUID == GUID)
  567.                 {
  568.                     return m_Entities[i];
  569.                 }
  570.             }
  571.  
  572.             List<WorldInstance> worlds = m_Areas.Values.ToList();
  573.             for(int i = 0; i < worlds.Count; i++)
  574.             {
  575.                 Entity entity = worlds[i].GetEntityRecursive(GUID);
  576.                 if(entity != null)
  577.                 {
  578.                     return entity;
  579.                 }
  580.             }
  581.  
  582.             return null;
  583.         }
  584.  
  585.         public WorldInstance GetWorldOfEntity(int GUID)
  586.         {
  587.             for(int i = 0; i < m_Entities.Count; i++)
  588.             {
  589.                 if(m_Entities[i].GUID == GUID)
  590.                 {
  591.                     return this;
  592.                 }
  593.             }
  594.  
  595.             List<WorldInstance> worlds = m_Areas.Values.ToList();
  596.             for(int i = 0; i < worlds.Count; i++)
  597.             {
  598.                 WorldInstance world = worlds[i].GetWorldOfEntity(GUID);
  599.                 if(world != null)
  600.                 {
  601.                     return world;
  602.                 }
  603.             }
  604.  
  605.             return null;
  606.         }
  607.  
  608.         public Sector GetSectorFromPoint(Point point)
  609.         {
  610.             int xCentreBegin, xRightBegin;
  611.             int yCentreBegin, yBottomBegin;
  612.  
  613.             xCentreBegin = m_Tiles.GetLength(0) / 3;
  614.             xRightBegin = xCentreBegin * 2;
  615.  
  616.             yCentreBegin = m_Tiles.GetLength(1) / 3;
  617.             yBottomBegin = yCentreBegin * 2;
  618.  
  619.             int sectorX;
  620.             int sectorY;
  621.  
  622.             if (point.X < xCentreBegin)
  623.                 sectorX = 0;
  624.             else if (point.X < xRightBegin)
  625.                 sectorX = 1;
  626.             else
  627.                 sectorX = 2;
  628.  
  629.             if (point.Y < yCentreBegin)
  630.                 sectorY = 0;
  631.             else if (point.Y < yBottomBegin)
  632.                 sectorY = 1;
  633.             else
  634.                 sectorY = 2;
  635.  
  636.             switch(sectorX)
  637.             {
  638.                 case 0:
  639.                     switch(sectorY)
  640.                     {
  641.                         case 0:
  642.                             return Sector.NorthWest;
  643.  
  644.                         case 1:
  645.                             return Sector.North;
  646.  
  647.                         case 2:
  648.                             return Sector.NorthEast;
  649.                     }
  650.                     break;
  651.  
  652.                 case 1:
  653.                     switch(sectorY)
  654.                     {
  655.                         case 0:
  656.                             return Sector.West;
  657.  
  658.                         case 1:
  659.                             return Sector.Centre;
  660.  
  661.                         case 2:
  662.                             return Sector.East;
  663.                     }
  664.                     break;
  665.  
  666.                 case 2:
  667.                     switch (sectorY)
  668.                     {
  669.                         case 0:
  670.                             return Sector.SouthWest;
  671.  
  672.                         case 1:
  673.                             return Sector.South;
  674.  
  675.                         case 2:
  676.                             return Sector.SouthEast;
  677.                     }
  678.                     break;
  679.             }
  680.             return Sector.Centre;
  681.         }
  682.  
  683.         public WorldTile[,] tiles
  684.         {
  685.             get
  686.             {
  687.                 return m_Tiles;
  688.             }
  689.         }
  690.  
  691.         public bool[,] discoveredTiles
  692.         {
  693.             get
  694.             {
  695.                 return m_Discovered;
  696.             }
  697.         }
  698.  
  699.         public Dictionary<Point, BaseJoyObject> GetObjectsOfType(string type)
  700.         {
  701.             Dictionary<Point, BaseJoyObject> objects = new Dictionary<Point, BaseJoyObject>();
  702.             foreach (BaseJoyObject joyObject in m_Objects)
  703.             {
  704.                 if (joyObject.baseType.Equals(type))
  705.                 {
  706.                     objects.Add(joyObject.position, joyObject);
  707.                 }
  708.             }
  709.             return objects;
  710.         }
  711.  
  712.         public void AddArea(string key, WorldInstance value)
  713.         {
  714.             value.parent = this;
  715.             m_Areas.Add(key, value);
  716.         }
  717.  
  718.         public string GetLocalAreaInfo(Entity entityRef)
  719.         {
  720.             if (m_Type == WorldType.Interior)
  721.             {
  722.                 int result = RNG.Roll(0, 100);
  723.                 if (result <= 50)
  724.                 {
  725.                     int numberOfLevels = 1;
  726.                     numberOfLevels = WorldConversationDataHelper.GetNumberOfFloors(numberOfLevels, this);
  727.  
  728.                     if (numberOfLevels == 1)
  729.                         return "This place only has " + numberOfLevels + " floor to it.";
  730.                     else
  731.                         return "This place has at least " + numberOfLevels + " floors.";
  732.                 }
  733.                 else if (result > 50)
  734.                 {
  735.                     int exactNumber = WorldConversationDataHelper.GetNumberOfCreatures(entityRef, this);
  736.                     int roughNumber = 0;
  737.                     if (exactNumber % 10 < 6)
  738.                     {
  739.                         roughNumber = exactNumber - (exactNumber % 10);
  740.                     }
  741.                     else
  742.                     {
  743.                         roughNumber = exactNumber + (exactNumber % 10);
  744.                     }
  745.                     return "There are around " + roughNumber + " " + entityRef.creatureType + "s here.";
  746.                 }
  747.             }
  748.  
  749.             return "I don't know much about this place, sorry.";
  750.         }
  751.        
  752.         //[JsonConverter(typeof(AreaConverter))]
  753.         public Dictionary<string, WorldInstance> areas
  754.         {
  755.             get
  756.             {
  757.                 return m_Areas;
  758.             }
  759.         }
  760.  
  761.         public List<Entity> entities
  762.         {
  763.             get
  764.             {
  765.                 Entity[] newEntities = new Entity[m_Entities.Count];
  766.                 m_Entities.CopyTo(newEntities);
  767.                 return newEntities.ToList();
  768.             }
  769.             set
  770.             {
  771.                 m_Entities = value;
  772.             }
  773.         }
  774.  
  775.         public List<BaseJoyObject> objects
  776.         {
  777.             get
  778.             {
  779.                 return m_Objects;
  780.             }
  781.         }
  782.  
  783.         [JsonConverter(typeof(PointConverter))]
  784.         public Point spawnPoint
  785.         {
  786.             get
  787.             {
  788.                 return m_SpawnPoint;
  789.             }
  790.             set
  791.             {
  792.                 m_SpawnPoint = value;
  793.             }
  794.         }
  795.  
  796.         [JsonIgnore]
  797.         public WorldInstance parent
  798.         {
  799.             get
  800.             {
  801.                 return m_Parent;
  802.             }
  803.             set
  804.             {
  805.                 m_Parent = value;
  806.             }
  807.         }
  808.  
  809.         public int GUID
  810.         {
  811.             get;
  812.             protected set;
  813.         }
  814.  
  815.         public string name
  816.         {
  817.             get;
  818.             protected set;
  819.         }
  820.  
  821.         public Entity player
  822.         {
  823.             get
  824.             {
  825.                 if (!m_Entities.Any(x => x.playerControlled))
  826.                     return null;
  827.  
  828.                 return m_Entities[m_PlayerIndex];
  829.             }
  830.         }
  831.  
  832.         public WorldType worldType
  833.         {
  834.             get;
  835.             protected set;
  836.         }
  837.  
  838.         public int[,] lightLevels
  839.         {
  840.             get
  841.             {
  842.                 return m_Light;
  843.             }
  844.         }
  845.     }
  846. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement