public WorldBlock Get(HexXY p) { for (int i = 0; i < cacheSz; i++) { if (cache[i].pos == p) { return(cache[i].worldBlock); } } WorldBlock block = null; all.TryGetValue(p, out block); if (block != null) { for (int i = 1; i < cacheMaxSz; i++) { cache[i] = cache[i - 1]; } cache[0] = new CacheEntry() { pos = p, worldBlock = block }; cacheSz = Math.Min(cacheSz + 1, cacheMaxSz); } return(block); }
public bool RemoveEntity(HexXY p, Entity ent) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); return(block.entityMap[localPos.x, localPos.y].Remove(ent)); }
public int GetPFExpandMap(HexXY p) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); return(block.pfExpandMap[localPos.x, localPos.y]); }
public void OnMove(HexXY from, HexXY to, bool isDrawing) { EnsurePrevMoveFinished(); if (!WorldBlock.CanTryToMoveToBlockType(Level.S.GetPFBlockedMap(to))) { avatar.finishState = Avatar.FinishedState.CantMoveThere; return; } if (!isDrawing) { if (!avatar.spell.caster.SpendMana(1)) { avatar.finishState = Avatar.FinishedState.NoManaLeft; return; } } else { if (!avatar.spell.caster.SpendMana(5)) { avatar.finishState = Avatar.FinishedState.NoManaLeft; return; } } this.isDrawing = isDrawing; Interfacing.PerformInterfaceMove(graphicsHandle, to, movTime); movTimeLeft = movTime * 0.75f; //TODO: this is point in time when avatar element entity changes pos movPos = to; }
public byte GetPFStepsMap(HexXY p) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); return(block.pfStepsMap[localPos.x, localPos.y]); }
public void AddEntity(HexXY p, Entity ent) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); block.entityMap[localPos.x, localPos.y].Add(ent); }
public WorldBlock GetNoCache(HexXY p) { WorldBlock block = null; all.TryGetValue(p, out block); return(block); }
public WorldBlock SetCellType(HexXY p, TerrainCellType type) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, true, out localPos); block.SetCellType(localPos, type); return(block); }
public WorldBlock SetPFBlockedMap(HexXY p, WorldBlock.PFBlockType val) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); block.pfBlockedMap[localPos.x, localPos.y] = val; return(block); }
public WorldBlock SetPFExpandMap(HexXY p, int val) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); block.pfExpandMap[localPos.x, localPos.y] = val; return(block); }
public WorldBlock SetPFStepsMap(HexXY p, byte val) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); block.pfStepsMap[localPos.x, localPos.y] = val; return(block); }
public void Move(HexXY p) { if (!cantMove && WorldBlock.CanTryToMoveToBlockType(Level.S.GetPFBlockedMap(p)) && (!dest.HasValue || p != dest)) { afterMoveAction = null; dest = p; distToStop = 0; MovePart(); } }
public static void Load(BinaryReader reader) { var level = new Level(); int count = reader.ReadInt32(); for (int i = 0; i < count; i++) { var wb = WorldBlock.Load(reader); level.wbCache.Add(wb.position, wb); } }
public void OnMove(HexXY from, HexXY to, bool isDrawing) { if (!WorldBlock.CanTryToMoveToBlockType(Level.S.GetPFBlockedMap(to))) { avatar.finishState = Avatar.FinishedState.CantMoveThere; } else { Interfacing.PerformInterfaceMove(graphicsHandle, to, movTime); movTimeLeft = movTime * 0.75f; //TODO: this is point in time when avatar changes pos } }
public WorldBlock.PFBlockType GetPFBlockedMap(HexXY p) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); if (block == null) { return(WorldBlock.PFBlockType.EdgeBlocked); } else { return(block.pfBlockedMap[localPos.x, localPos.y]); } }
public TerrainCellType GetCellType(HexXY p) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); if (block == null) { return(TerrainCellType.Empty); } else { return(block.GetCellType(localPos)); } }
public IEnumerable <Entity> GetEntities(HexXY p) { HexXY localPos; WorldBlock block = GetBlockWithCell(p, false, out localPos); if (block == null) { yield break; } else { foreach (var ent in block.entityMap[localPos.x, localPos.y]) { yield return(ent); } } }
public static WorldBlock Load(BinaryReader reader) { var pos = new HexXY(reader.ReadInt32(), reader.ReadInt32()); var wb = new WorldBlock(pos); for (int x = 0; x < sz; x++) { for (int y = 0; y < sz; y++) { var type = (TerrainCellType)reader.ReadByte(); wb.cellTypes[x, y] = type; ++wb.cellTypeCounts[(int)type]; wb.pfBlockedMap[x, y] = (PFBlockType)reader.ReadByte(); } } return(wb); }
public void OnMove(HexXY from, HexXY to, bool isDrawing) { if (!isDrawing || !WorldBlock.CanTryToMoveToBlockType(Level.S.GetPFBlockedMap(to))) { if (!avatar.spell.caster.SpendMana(1)) { avatar.finishState = Avatar.FinishedState.NoManaLeft; return; } } else { if (!avatar.spell.caster.SpendMana(10)) { avatar.finishState = Avatar.FinishedState.NoManaLeft; return; } var spellEffect = new SpellEffects.Stone(1); spellEffect.StackOn(to); } }
WorldBlock GetBlockWithCell(HexXY p, bool shouldCreateIfNotFound, out HexXY localp) { localp = new HexXY(0, 0); HexXY blockPos = GetBlockCoords(p); WorldBlock block = wbCache.Get(blockPos); if (block == null) { if (!shouldCreateIfNotFound) { return(null); } else { block = new WorldBlock(blockPos); wbCache.Add(blockPos, block); } } localp.x = p.x - blockPos.x * WorldBlock.sz; localp.y = p.y - blockPos.y * WorldBlock.sz; return(block); }
public static uint?FindPath(HexXY from, HexXY to, HexXY[] pathStorage, uint distToStop = 0, uint dynBlockCost = 0) { front.Reset(); front.Enqueue(new XYCost(from, 0, 0, GetHeuristic(from, to))); //TODO: assume we're in the single worldblock for now ++Level.S.pfExpandMarker; Level.S.SetPFExpandMap(from, Level.S.pfExpandMarker); XYCost c; bool isFound = false; do { c = front.Dequeue(); if (HexXY.Dist(c.p, to) <= distToStop) { isFound = true; break; } foreach (var st in steps) { var np = c.p + st; var blockType = Level.S.GetPFBlockedMap(np); if (WorldBlock.CanTryToMoveToBlockType(blockType) && Level.S.GetPFExpandMap(np) < Level.S.pfExpandMarker) { Level.S.SetPFExpandMap(np, Level.S.pfExpandMarker); uint cost = (uint)(c.cost + WorldBlock.PFGetPassCost(np)); if (dynBlockCost > 0 && np != to && blockType == WorldBlock.PFBlockType.DynamicBlocked) { cost += dynBlockCost; } var n = new XYCost(np, c.len + 1, cost, cost + GetHeuristic(np, to)); front.Enqueue(n); Level.S.SetPFStepsMap(np, st.backIdx); } } } while (front.Count > 0); if (isFound && c.len <= pathStorage.Length) { uint pathLen = c.len; HexXY p = c.p; for (int i = 0; i < pathLen; i++) { pathStorage[pathLen - i - 1] = p; var backIdx = Level.S.GetPFStepsMap(p); p = p + steps[backIdx]; } return(pathLen); } else { return(null); } }
public void Add(HexXY p, WorldBlock block) { all.Add(p, block); }