/// <summary> /// Move the specified thing to the ground. /// </summary> /// <param name="thingToMove">Thing to move.</param> /// <param name="posTo">Position to move to.</param> /// <param name="thingsPrepared">A reference to the things /// already prepared.</param> private void MoveToGround() { Thing topThing = map.GetTopThing(posTo); Item topItem = null; if (topThing.IsOfType(Constants.TYPE_STACKABLE)) { topItem = (Item)topThing; } bool stackable = false; bool addItem = true; if (topItem != null && itemToMove != null && topItem.ItemID == itemToMove.ItemID) //Both stackable items of same type { stackable = true; if (topItem.Count + itemToMove.Count > MAX_STACK_COUNT) { byte countRemainder = (byte)(MAX_STACK_COUNT - topItem.Count); topItem.Count += countRemainder; itemToMove.Count -= countRemainder; } else { topItem.Count += itemToMove.Count; addItem = false; } } ThingSet tSet = map.GetThingsInVicinity(posTo); byte stackpos = map.GetStackPosition(itemToMove, posTo); if (stackable) { foreach (Thing thing in tSet.GetThings()) { thing.UpdateItem(posTo, topItem, map.GetStackPosition(topItem, posTo)); } } if (addItem) { map.AddThing(itemToMove, posTo); foreach (Thing thing in tSet.GetThings()) { thing.AddThingToGround(itemToMove, posTo, map.GetStackPosition(itemToMove, posTo)); } } }
/// <summary> /// Move a specified thing from the ground. /// </summary> private void MoveItemFromGround() { bool stackable = itemToMove.IsOfType(Constants.TYPE_STACKABLE) && itemToMove.Count != count; Item oldItem = null; if (!stackable) { map.RemoveThing(itemToMove, posFrom); } else { oldItem = itemToMove; oldItem.Count -= count; itemToMove = Item.CreateItem(itemToMove.ItemID); itemToMove.Count = count; } ThingSet tSet = map.GetThingsInVicinity(posFrom); foreach (Thing thing in tSet.GetThings()) { if (!stackable) { thing.RemoveThing(posFrom, stackpos); } else { thing.UpdateItem(posFrom, oldItem, stackpos); } } }
/// <summary> /// Updates the specified item in a thread-safe manner. /// </summary> /// <param name="oldItem"></param> /// <param name="newItem"></param> public void AppendUpdateItem(Item itemToUpdate) { lock (lockThis) { //Add new attributes to the item w/ the ID Item.CreateItem(itemToUpdate.ItemID).Clone(itemToUpdate); if (itemToUpdate.CarryingInventory != null) { Player player = itemToUpdate.CarryingInventory; byte index = player.GetInventoryIndex(itemToUpdate); player.RemoveInventoryItem(index); player.AddInventoryItem(index, itemToUpdate); } else if (itemToUpdate.CurrentPosition != null) { ThingSet tSet = gameMap.GetThingsInVicinity(itemToUpdate.CurrentPosition); byte stackpos = gameMap.GetStackPosition(itemToUpdate, itemToUpdate.CurrentPosition); foreach (Thing thing in tSet.GetThings()) { thing.UpdateItem(itemToUpdate.CurrentPosition, itemToUpdate, stackpos); } } else if (itemToUpdate.CurrentPosition == null) //In container { Container container = itemToUpdate.Parent; container.UpdateItem(itemToUpdate); } } }
/// <summary> /// Remove the specified item in a thread-safe manner. /// </summary> /// <param name="itemToRemove"></param> public void AppendRemoveItem(Item itemToRemove) { lock (lockThis) { if (itemToRemove.CarryingInventory != null) { Player player = itemToRemove.CarryingInventory; byte index = player.GetInventoryIndex(itemToRemove); player.RemoveInventoryItem(index); } else if (itemToRemove.CurrentPosition != null) { Position pos = itemToRemove.CurrentPosition; Tile mapTile = gameMap.GetTile(pos); byte stackpos = mapTile.GetStackPosition(itemToRemove); mapTile.RemoveThing(itemToRemove); ThingSet tSet = gameMap.GetThingsInVicinity(pos); foreach (Thing thing in tSet.GetThings()) { thing.RemoveThing(pos, stackpos); } } else if (itemToRemove.CurrentPosition == null) //In container { Container container = itemToRemove.Parent; container.RemoveItem(itemToRemove); } } }
/// <summary> /// Handles sending a chat message in a player's local vicinity. /// </summary> /// <param name="type">The type of chat to send.</param> /// <param name="set">The set of things in the player's local vicinity.</param> /// <param name="msg">The message to send.</param> /// <param name="sender">The sender of the message.</param> private void HandleLocalChat(ChatLocal type, ThingSet set, string msg, Creature sender) { foreach (Thing thing in set.GetThings()) { thing.AddLocalChat(type, msg, sender.CurrentPosition, sender); } }
/// <summary> /// Adds a magic effect at the given position. /// </summary> /// <param name="effect">The effect to add.</param> /// <param name="position">Add effect at this position.</param> public void AddMagicEffect(MagicEffect effect, Position position) { ThingSet tSet = gameMap.GetThingsInVicinity(position); foreach (Thing thing in tSet.GetThings()) { thing.AddEffect(effect, position); } }
/// <summary> /// Updates the health status of a creature to all creatures /// in vicinity. /// </summary> /// <param name="creature">Creature whose health status to update.</param> /// <param name="tSet">The things to update for.</param> private void UpdateHealthStatus(Creature creature, Position position) { ThingSet tSet = gameMap.GetThingsInVicinity(position); foreach (Thing thing in tSet.GetThings()) { thing.UpdateHealthStatus(creature); } }
public void AppendUpdateOutfit(Creature creature) { lock (lockThis) { ThingSet tSet = gameMap.GetThingsInVicinity(creature.CurrentPosition); foreach (Thing thing in tSet.GetThings()) { thing.UpdateOutfit(creature); } } }
private void AddShootEffect(DistanceType type, Position origin, Position destination, ThingSet tSet) { gameMap.GetThingsInVicinity(origin, tSet); gameMap.GetThingsInVicinity(destination, tSet); foreach (Thing thing in tSet.GetThings()) { thing.AddShootEffect((byte)type, origin, destination); } }
public void AppendAddItem(Item item, Position position) { lock (lockThis) { gameMap.AddThing(item, position); byte stackpos = gameMap.GetStackPosition(item, position); ThingSet tSet = gameMap.GetThingsInVicinity(position); foreach (Thing thing in tSet.GetThings()) { thing.AddItem(item, position, stackpos); } } }
private void AppendAddCreature(Creature creature, Position position) { position = gameMap.GetFreePosition(position, creature); ThingSet tSet = gameMap.GetThingsInVicinity(position); AddCachedCreature(creature, position); creature.InitCreatureCheck(this); byte stackpos = gameMap.GetStackPosition(creature, position); foreach (Thing thing in tSet.GetThings()) { thing.AddScreenCreature(creature, position, stackpos); } }
//TODO: Completely rework this method... it currently sucks //A LOT... i mean it! public void CheckForRespawn() { Position pos = new Position(CenterX, CenterY, CenterZ); Monster monster = Monster.CreateMonster(MonsterName); if (monster != null && world.GetGameMap().GetTile(pos) != null) { world.AppendAddMonster(monster, pos); } return; Map map = world.GetGameMap(); ThingSet tSet = map.GetThingsInVicinity(pos); //Monster monster = Monster.CreateMonster(MonsterName); if (monster == null) { return; } if (map.GetTile(pos) != null && !map.TileContainsType(pos, Constants.TYPE_BLOCKS_AUTO_WALK)) { bool canRespawn = true; foreach (Thing thing in tSet.GetThings()) { if (thing is Player) //TODO: FIX this crap { canRespawn = false; } } if (canRespawn) { world.SendAddMonster(monster, pos); } } else { return; } // if (map.GetTile(pos) != null && // !map.TileContainsType(pos, Constants.TYPE_BLOCKS_AUTO_WALK)) { // Console.WriteLine("x: " + x++); //world.SendAddMonster(monster, pos); // } world.AddEventInCS(SpawnTime, CheckForRespawn); }
public virtual void HandleChangeDirection(Creature creature, Direction direction) { lock (lockThis) { creature.CurrentDirection = direction; ThingSet tSet = gameMap.GetThingsInVicinity(creature.CurrentPosition); byte stackpos = gameMap.GetStackPosition(creature, creature.CurrentPosition); foreach (Thing thing in tSet.GetThings()) { thing.UpdateDirection(direction, creature, stackpos); } SendProtocolMessages(); } }
/// <summary> /// Removes the creature from the game world and only /// appends the data to the ThingSet without reseting or sending it. /// </summary> /// <param name="creature">The creature to remove.</param> /// <param name="tSet">A reference for which things to notify of the /// creature's removal.</param> private void AppendRemoveCreature(Creature creature) { creaturesOnline.Remove(creature.GetID()); creature.LogedIn = false; byte stackpos = gameMap.GetStackPosition(creature, creature.CurrentPosition); ThingSet tSet = gameMap.GetThingsInVicinity(creature.CurrentPosition); foreach (Monster summon in creature.GetSummons()) { summon.SetMaster(null); } foreach (Thing thing in tSet.GetThings()) { thing.RemoveThing(creature.CurrentPosition, stackpos); } gameMap.RemoveThing(creature, creature.CurrentPosition); }
/// <summary> /// Use this method cast the specified spell. Note: This method only /// appends and does not send protocol data. /// </summary> /// <param name="caster">The creature casting the spell</param> /// <param name="spell">The spell to cast</param> /// <param name="tSet">The set of affected things</param> public void CastSpell(string msg, Creature caster, Spell spell, GameWorld world) { /*string error = caster.CanCastSpell(spell); * if (error != null) { * caster.AddAnonymousChat(ChatAnonymous.WHITE, error); * return; * }*///TODO: Uncomment if (spell.IsSpellValid != null && !spell.IsSpellValid(world, msg)) { world.AddMagicEffect(MagicEffect.PUFF, caster.CurrentPosition); return; } if (spell.RequiresTarget) { Tile tile = map.GetTile(spell.SpellCenter); if (tile == null || !tile.ContainsType(Constants.TYPE_CREATURE)) { world.AddMagicEffect(MagicEffect.PUFF, caster.CurrentPosition); caster.AddAnonymousChat(ChatAnonymous.WHITE, "No target selected."); return; } } //Constants. //Not the most efficient method but it is simple and works. int length = spell.SpellArea.GetLength(0); int width = spell.SpellArea.GetLength(1); Position startPos = new Position(); startPos.x = (ushort)(spell.SpellCenter.x - (width / 2)); startPos.y = (ushort)(spell.SpellCenter.y - (length / 2)); startPos.z = spell.SpellCenter.z; Position local = new Position(); List <Thing> things = new List <Thing>(); for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) { local.x = (ushort)(startPos.x + j); local.y = (ushort)(startPos.y + i); local.z = startPos.z; if (map.GetTile(local) == null /*|| !map.GetTile(local).CanMoveTo(caster) * TODO: Finish*/) { continue; } if (spell.SpellArea[i, j] && !map.GetTile(local).ContainsType(Constants.TYPE_BLOCKS_MAGIC)) { ThingSet tSet = map.GetThingsInVicinity(local); foreach (Thing thing in tSet.GetThings()) { thing.AddEffect(spell.SpellEffect, local); if (spell.HasDistanceType()) { thing.AddShootEffect((byte)spell.DistanceEffect, caster.CurrentPosition, spell.SpellCenter); } } List <Thing> localThings = map.GetTile(local).GetThings(); if (spell.Action != null) { spell.Action.Invoke(world, local, localThings); } foreach (Thing thing in map.GetTile(local).GetThings()) { things.Add(thing); } } } } foreach (Thing thing in things) { thing.AppendHandleDamage(spell.GetDamage(), caster, spell.Immunity, world, true); } //caster.NotifyOfSuccessfulCast(spell); TODO: Uncomment }
public void AppendHandleMove(Creature creature, Position newPos, Direction direction, bool validateMove) { lock (lockThis) { Position oldPos = creature.CurrentPosition; Tile oldTile = gameMap.GetTile(oldPos); Tile newTile = gameMap.GetTile(newPos); if (validateMove) { if (newTile == null || newTile.ContainsType(Constants.TYPE_BLOCKING)) { return; } } foreach (Thing thing in oldTile.GetThings()) { bool proceed = thing.HandleWalkAction(creature, this, WalkType.WALK_OFF); if (!proceed) { return; } } foreach (Thing thing in newTile.GetThings()) { bool proceed = thing.HandleWalkAction(creature, this, WalkType.WALK_ON); if (!proceed) { return; } } if (creature.IsNextTo(newPos)) { //TODO: Finish coding speed int speed = GetGroundSpeed(creature.CurrentPosition); int duration = (100 * 90 /*speed*/) / (creature.GetSpeed()); creature.LastWalk.SetTimeInCS((uint)duration); if (!creature.LastWalk.Elapsed()) { return; } Position oldPosClone = oldPos.Clone(); if (oldPos.y > newPos.y) { direction = Direction.NORTH; oldPosClone.y--; creature.AddScreenMoveByOne(direction, oldPos, oldPosClone, gameMap); } else if (oldPos.y < newPos.y) { direction = Direction.SOUTH; oldPosClone.y++; creature.AddScreenMoveByOne(direction, oldPos, oldPosClone, gameMap); } if (oldPos.x < newPos.x) { direction = Direction.EAST; oldPosClone.x++; creature.AddScreenMoveByOne(direction, oldPos, oldPosClone, gameMap); } else if (oldPos.x > newPos.x) { direction = Direction.WEST; oldPosClone.x--; creature.AddScreenMoveByOne(direction, oldPos, oldPosClone, gameMap); } } ThingSet tSet = gameMap.GetThingsInVicinity(creature.CurrentPosition); byte oldStackPos = gameMap.GetStackPosition(creature, oldPos); gameMap.MoveThing(creature, oldPos, newPos); creature.CurrentDirection = direction; byte newStackPos = gameMap.GetStackPosition(creature, newPos); gameMap.GetThingsInVicinity(newPos, tSet); creature.HandleMove(); foreach (Thing thing in tSet.GetThings()) { thing.AddCreatureMove(direction, creature, oldPos, newPos, oldStackPos, newStackPos); } if (!creature.IsNextTo(oldPos)) { creature.AddTeleport(gameMap); } } }