public override bool DoAction(MudAction action) { if (action.Type == "enterrealm") { var mob = Game.Instance.GetEntityById(action.SenderId); Game.Instance.SendMessage(_acctId, $"{mob.Name} has entered the realm."); } else if (action.Type == "leaverealm") { var mob = Game.Instance.GetEntityById(action.SenderId); Game.Instance.SendMessage(_acctId, $"{mob.Name} has left the realm."); } else if (action.Type == "enterroom") { EnterRoom(action); } else if (action.Type == "look") { EnterRoom(action); } else if (action.Type == "say") { Game.Instance.SendMessage(_acctId, $"{action.Args[1]} says, \"{action.Args[0]}\""); } else if (action.Type == "listinventory") { SeeInventory(action.SenderId); } else if (action.Type == "receivedentity") { ReceivedEntity(action); } return(true); }
private void Logout(MudAction action) { var character = _entityFactory.GetEntityById(action.SenderId); var room = GetRoomWithEntity(character.Id); var zone = _zones[room.Zone]; //tell everyone about it var leaveRoom = new MudAction("leaveroom", character.Id, 0); ActionRoomItems(leaveRoom, room.Id); ActionRoomMobs(leaveRoom, room.Id); room.DoAction(leaveRoom); var leaveZone = new MudAction("leavezone", character.Id); character.DoAction(leaveZone); zone.DoAction(leaveZone); ActionRealmPlayers(new MudAction("leaverealm", character.Id)); //remove from game room.Entities.Remove(character.Id); _entities.Remove(character.Id); ExitToMainMenu(int.Parse(character.Traits.Get("accountId").Value)); }
private void Say(MudAction action) { //TODO: check if entity CAN talk var mob = _entities[action.SenderId]; var say = new MudAction("say", mob.Id, 0, 0, action.Args[0], mob.Name); ActionRoomMobs(say, GetRoomWithEntity(mob.Id).Id); }
private void ActionRealmPlayers(MudAction action) { var players = GetAllPlayers(); foreach (var player in players) { player.DoAction(action); } }
public bool DoAction(MudAction action) { foreach (var c in _components) { if (c.IsActive && !c.DoAction(action)) { return(false); } } return(true); }
private void GameToPlayer(MudAction action) { var character = _entities[action.SenderId]; var acctId = character.Traits.Get("accountId")?.Value; if (string.IsNullOrWhiteSpace("accountId")) { return; } SendMessage(int.Parse(acctId), action.Args[0]); }
private void Login(MudAction action) { var character = _entityFactory.GetEntityById(action.SenderId); if (character == null) { return; } if (!_entities.ContainsKey(character.Id)) { _entities.Add(character.Id, character); } character.Components.Add(new ReporterComponent(character, "reporter", null)); var roomTrait = character.Traits.Get("room")?.Value; var roomId = !string.IsNullOrWhiteSpace(roomTrait) ? int.Parse(roomTrait) : 0; if (roomId > 0) { var room = _rooms[roomId]; room.Entities.Add(character.Id); ActionRealmPlayers(new MudAction("enterrealm", character.Id)); var zone = _zones[room.Zone]; var enterZone = new MudAction("enterzone", character.Id); var enterRoom = new MudAction("enterroom", character.Id, 0); zone.DoAction(enterRoom); character.DoAction(enterZone); room.DoAction(enterRoom); ActionRoomMobs(enterRoom, room.Id); ActionRoomItems(enterRoom, room.Id); Commands.AssignCommand(character.Id, "quit"); Commands.AssignCommand(character.Id, "look"); Commands.AssignCommand(character.Id, "say"); Commands.AssignCommand(character.Id, "west"); Commands.AssignCommand(character.Id, "east"); Commands.AssignCommand(character.Id, "take"); Commands.AssignCommand(character.Id, "items"); _timerRegistry.Add(new TimedMudAction(5, "infotoplayer", character.Id, "This is an action delayed by 5 seconds.")); } }
private void ActionRoomItems(MudAction action, int roomId) { var room = _rooms[roomId]; if (room == null) { return; } var mobs = GetEntitiesInRoom(roomId, x => x.Traits.Has("item")); foreach (var mob in mobs) { mob.DoAction(action); } }
public void DoAction(MudAction action) { var type = action.Type.ToLower(); if (type == "enterworld") { Login(action); } else if (type == "leaveworld") { Logout(action); } else if (type == "infotoplayer") { GameToPlayer(action); } else if (type == "look") { RouteActionToEntity(action.SenderId, action); } else if (type == "listinventory") { RouteActionToEntity(action.SenderId, action); } else if (type == "attemptsay") { Say(action); } else if (type == "attemptenterportal") { EnterPortal(action); } else if (type == "attemptreceive") { Transfer(action); } // else if (type == "attemptgetitem") // GetItem(action.SenderId, action.ReceiverId, action.OtherEntity1); //custom action else if (_actionRunners.ContainsKey(type)) { _actionRunners[type].Run(action); } }
private void ReceivedEntity(MudAction action) { var transType = action.Args[0]; var subject = Game.Instance.GetEntityById(action.OtherEntity1); if (transType == "r2e") { var receiver = Game.Instance.GetEntityById(action.ReceiverId); if (receiver.Id == Owner.Id) { Game.Instance.SendMessage(_acctId, $"You pick up {subject.Name}."); } else { Game.Instance.SendMessage(_acctId, $"{receiver.Name} picks up {subject.Name}."); } } }
private void ForceTransport(int entityId, int roomId) { // LOOKUPS var entity = _entities[entityId]; var oldRoom = GetRoomWithEntity(entityId); var newRoom = _rooms[roomId]; var changeZone = oldRoom.Zone != newRoom.Zone; var oldZone = _zones[oldRoom.Zone]; var newZone = _zones[newRoom.Zone]; //PHYSICAL MOVEMENT if (changeZone) { //move entity between zones -- not used yet } oldRoom.Entities.Remove(entityId); newRoom.Entities.Add(entityId); entity.Traits.Set("room", newRoom.Id.ToString()); //EVENT NOTIFICATIONS if (changeZone) { var leaveZone = new MudAction("leavezone", entityId, oldZone.Id); oldZone.DoAction(leaveZone); entity.DoAction(leaveZone); var enterZone = new MudAction("enterzone", entityId, newZone.Id); newZone.DoAction(enterZone); entity.DoAction(enterZone); } var leaveRoom = new MudAction("leaveroom", entityId, 0); oldRoom.DoAction(leaveRoom); entity.DoAction(leaveRoom); var enterRoom = new MudAction("enterroom", entityId, 0); newRoom.DoAction(enterRoom); ActionRoomMobs(enterRoom, newRoom.Id); ActionRoomItems(enterRoom, newRoom.Id); }
private void EnterRoom(MudAction action) { var mobId = action.SenderId; if (mobId == Owner.Id) { SeeRoom(int.Parse(Owner.Traits.Get("room").Value), mobId); return; } var mob = Game.Instance.GetEntityById(mobId); var portalId = action.ReceiverId; if (portalId == 0) { Game.Instance.SendMessage(_acctId, $"{mob.Name} has entered."); return; } var portal = Game.Instance.GetPortalById(portalId); Game.Instance.SendMessage(_acctId, $"{mob.Name} enters from the {portal.Name}."); }
public bool DoAction(MudAction action) { return(Components.DoAction(action)); }
private void RouteActionToEntity(int entityId, MudAction action) { var mob = _entities[entityId]; mob.DoAction(action); }
private void Transfer(MudAction action) { /* entity to entity * entity to room * room to entity */ //types of entities involved var e2e = _entities.ContainsKey(action.SenderId) && _entities.ContainsKey(action.ReceiverId); var e2r = _entities.ContainsKey(action.SenderId) && _rooms.ContainsKey(action.ReceiverId); var r2e = _rooms.ContainsKey(action.SenderId) && _entities.ContainsKey(action.ReceiverId); var requestor = r2e ? _rooms[action.SenderId] : _entities[action.SenderId]; var receiver = e2r ? _rooms[action.ReceiverId] : _entities[action.ReceiverId]; var subject = _entities[action.OtherEntity1]; var zone = _zones[GetRoomWithEntity(subject.Id).Zone]; var isItem = subject.Components.Has("item"); var requestedQty = 1; int.TryParse(action.Args[0], out requestedQty); if (isItem && requestedQty > 1 && (e2e || e2r)) { if (requestedQty > int.Parse(subject.Traits.Get("quantity").Value)) { DoAction(new MudAction("infotoplayer", 0, requestor.Id, "There aren't that many to pick up.")); return; } } //permission var canTake = new MudAction("cantake", requestor.Id, receiver.Id, subject.Id, action.Args); if (!subject.DoAction(canTake) || !receiver.DoAction(canTake) || !zone.DoAction(canTake) || !requestor.DoAction(canTake)) { return; } //physical movement var newEntityId = 0; MudEntity newEntity; if (isItem && requestedQty > 1 && requestedQty != int.Parse(subject.Traits.Get("quantity").Value)) { newEntity = _entityFactory.CreateEntity(subject.Name, (subject.Traits.GetAll().ToDictionary(x => x.Name, x => x.Value))); _entities.Add(newEntity.Id, newEntity); newEntityId = newEntity.Id; newEntity.Traits.Set("quantity", requestedQty.ToString()); var comps = subject.Components.GetAll(); foreach (var c in comps) { ComponentManager.Instance.AssignComponent(newEntity, c.Name); } } else { if (r2e) { _rooms[requestor.Id].Entities.Remove(subject.Id); } else if (e2r) { var inventory = GetEntitiesFromTrait(requestor, "items").ToList(); inventory.Remove(subject); requestor.Traits.Set("items", string.Join(",", inventory.Select(x => x.Id.ToString()))); _rooms[receiver.Id].Entities.Add(subject.Id); } newEntityId = subject.Id; newEntity = subject; } if (r2e) { //is subject an item? if (newEntity.Components.Has("item")) { if (receiver.Traits.Has("items")) { var itemsString = receiver.Traits.Get("items").Value; var itemIds = itemsString.Split(',').Select(x => int.Parse(x)).ToList(); itemIds.Add(subject.Id); receiver.Traits.Set("items", string.Join(",", itemIds.Select(x => x.ToString()))); } else { receiver.Traits.Add("items", newEntityId.ToString()); } } } else if (e2r) { } //notifications var transType = ""; if (e2e) { transType = "e2e"; } else if (e2r) { transType = "e2r"; } else { transType = "r2e"; } var receivedEntity = new MudAction("receivedentity", requestor.Id, receiver.Id, newEntity.Id, transType, newEntity.Traits.Has("quantity") ? newEntity.Traits.Get("quantity").Value : null); var room = r2e ? _rooms[requestor.Id] : GetRoomWithEntity(requestor.Id); ActionRoomMobs(receivedEntity, room.Id); ActionRoomItems(receivedEntity, room.Id); //TODO: CLEANUP ITEMS if (!isItem || !newEntity.Traits.Has("isStackable")) { return; } var entityItems = GetEntitiesFromTrait(receiver, "items").ToList(); var existingItems = entityItems.Where(x => x.Name == newEntity.Name).ToList(); if (existingItems.Count <= 1) { return; } var totalQty = existingItems.Sum(x => int.Parse(x.Traits.Get("quantity").Value)); newEntity.Traits.Set("quantity", totalQty.ToString()); var oldItem = existingItems.Single(x => x.Id != newEntity.Id); entityItems.Remove(oldItem); _entities.Remove(oldItem.Id); receiver.Traits.Set("items", string.Join(",", entityItems.Select(x => x.Id.ToString()))); }
private void EnterPortal(MudAction action) { var mob = _entities[action.SenderId]; var portal = _portals[action.ReceiverId]; int oldRoomId; if (!int.TryParse(mob.Traits.Get("room")?.Value, out oldRoomId)) { return; } var oldRoom = _rooms[oldRoomId]; if (!oldRoom.Portals.Contains(portal.Id)) { // log - mob cannot enter a portab when it's not in the room they're in return; } // get the destination room var entry = _portalEntries.Values.Where(x => x.StartRoom == oldRoomId).Single(x => x.Direction == action.Args[0]); var newRoom = _rooms[entry.EndRoom]; var changeZone = oldRoom.Zone != newRoom.Zone; var oldZone = _zones[oldRoom.Zone]; var newZone = _zones[newRoom.Zone]; // ask permission of everyone to leave current room if (changeZone) { var canLeaveZone = new MudAction("canleavezone", mob.Id, oldZone.Id); var canEnterZone = new MudAction("canenterzone", mob.Id, newZone.Id); if (!oldZone.DoAction(canLeaveZone)) { return; } if (!newZone.DoAction(canEnterZone)) { return; } if (!mob.DoAction(canLeaveZone)) { return; } if (!mob.DoAction(canEnterZone)) { return; } } var canLeaveRoom = new MudAction("canleaveroom", mob.Id); var canEnterRoom = new MudAction("canenterroom", mob.Id); var canEnterPortal = new MudAction("canenterportal", mob.Id); if (!oldRoom.DoAction(canLeaveRoom)) { return; } if (!newRoom.DoAction(canEnterRoom)) { return; } if (!mob.DoAction(canLeaveRoom)) { return; } if (!portal.DoAction(canEnterPortal)) { return; } //tell the room/region that the player is leaving if (changeZone) { var leaveZone = new MudAction("leavezone", mob.Id); oldZone.DoAction(leaveZone); mob.DoAction(leaveZone); } var leaveRoom = new MudAction("leaveroom", mob.Id); ActionRoomMobs(leaveRoom, oldRoom.Id); ActionRoomItems(leaveRoom, oldRoom.Id); //tell the portal the mob has entered var enterPortal = new MudAction("enterportal", mob.Id, portal.Id); portal.DoAction(enterPortal); mob.DoAction(enterPortal); //now move the mob oldRoom.Entities.Remove(mob.Id); mob.Traits.Change("room", newRoom.Id.ToString()); newRoom.Entities.Add(mob.Id); //tell everyone in the region/room that the player has entered if (changeZone) { var enterZone = new MudAction("enterzone", mob.Id, newZone.Id); newZone.DoAction(enterZone); mob.DoAction(enterZone); } var enterRoom = new MudAction("enterroom", mob.Id, portal.Id); newRoom.DoAction(enterRoom); ActionRoomMobs(enterRoom, newRoom.Id); ActionRoomItems(enterRoom, newRoom.Id); }
// public bool Query(GameAction action) // { // if (!IsActive) return true; // return _script.Call(_script.Globals["query"], action).Boolean; // } // public bool Consider(GameAction action) // { // if (!IsActive) return true; // return _script.Call(_script.Globals["consider"], action).Boolean; // } // public void Notify(GameAction action) // { // if (!IsActive) return; // _script.Call(_script.Globals["notify"], action); // } public virtual bool DoAction(MudAction action) { return(_script.Call(_script.Globals["do"], action).Boolean); }