/// <summary> /// Method for evaluating commands. Calls private functions based on what type of command is being evaluated. /// </summary> /// <param name="req">Command that is to be evaluated.</param> /// <param name="rules">Rulebook for checking dependancies, build time, etc...</param> /// <param name="player">Player that is requesting the command being evaluated.</param> /// <param name="time">Current game time.</param> /// <returns>Queue of translated commands resulting from the evaluations of req.</returns> internal Queue<Command> EvaluateCommand(Command req, RuleBook rules, Player player, TimeSpan time) { Queue<Command> retval = new Queue<Command>(); switch (req.CmdType) { case Command.T_COMMAND.MOVE: retval = evaluateMove(req, rules, player, time); break; case Command.T_COMMAND.ADD: retval = evaluateAdd(req, rules, player, time); break; case Command.T_COMMAND.REMOVE: retval = evaluateRemove(req, rules, player, time); break; case Command.T_COMMAND.SET: retval = evaluateSet(req, rules, player, time); break; case Command.T_COMMAND.ERROR: retval = evaluateError(req, rules, player, time); break; case Command.T_COMMAND.CANCEL: retval = evaluateMove(req, rules, player, time); break; case Command.T_COMMAND.ATTACK: retval = evaluateAttack(req, rules, player, time); break; default: break; } return retval; }
public void updateBuffer(GameTime elps) { byte[] rawCmd = new byte[16]; fileReader.Read(rawCmd, lastCmd, 16); lastCmd++; ulong first = 0; for (int i = 0; i < 8; i++) { first <<= 8; first |= rawCmd[i]; } ulong second = 0; for (int i = 0; i < 8; i++) { second <<= 8; first |= rawCmd[i+8]; } ulong [] cmdDat = new ulong[2]; cmdDat[0] = first; cmdDat[1] = second; Command cmd = new Command(cmdDat); if (NewCommandEvent != null) NewCommandEvent.Invoke(this, new NewCommandEventArgs(cmd)); }
protected virtual void handleMoveCommand(Command moveCommand) { MoveDecorator mov = new MoveDecorator(moveCommand); Vector2 position = new Vector2((float)mov.X, (float)mov.Y); Unit u = (Unit)GameObjectFactory.The.getGameObject(mov.UnitID); u.forceFinishAction(); u.moveTo(position); }
protected virtual void handleAttackCommand(Command cmd) { }
/// <summary> /// Handle a unit added command. This function should read /// the data out of the command and either: /// 1. Use it to create a new game object from the factory or /// 2. Use it to ressurect a game object from the factory /// </summary> /// <param name="addCommand"></param> protected void handleAddUnitCommand(Command cmd) { AddDecorator addCommand = new AddDecorator(cmd); Unit newUnit = createNewUnit(addCommand.UnitID,addCommand.Type, addCommand.ParentID); Unit adder = (Unit)GameObjectFactory.The.getGameObject(addCommand.ParentID); newUnit.setPosition(adder.getPosition()); units.Add(newUnit); }
/// <summary> /// Collect Changes from the host /// </summary> /// <param name="gamers">Clients.</param> public void clientReadPackets(GamerCollection<LocalNetworkGamer> gamers) { UInt64[] data = new UInt64[2]; Command command; foreach (LocalNetworkGamer gamer in gamers) { while (gamer.IsDataAvailable) { NetworkGamer sender; gamer.ReceiveData(_reader, out sender); if (sender.IsLocal) return; data[0] = (UInt64)_reader.ReadInt64(); data[1] = (UInt64)_reader.ReadInt64(); command = new Command(data); if (command.CmdType == Command.T_COMMAND.ADD) { } else if (command.CmdType == Command.T_COMMAND.SET) { } } } }
/// <summary> /// Collect requests from clients. /// </summary> /// <param name="gamers">List of Clients.</param> public void serverReadPackets(GamerCollection<LocalNetworkGamer> gamers, GameTime time) { UInt64[] data = new UInt64[2]; foreach (LocalNetworkGamer gamer in gamers) { while (gamer.IsDataAvailable) { NetworkGamer sender; gamer.ReceiveData(_reader, out sender); if (sender.IsLocal) return; data[0] = (UInt64)_reader.ReadInt64(); data[1] = (UInt64)_reader.ReadInt64(); Command command = new Command(data); // TODO: Get Player from PlayerList if(NewCommandEvent != null) NewCommandEvent.Invoke(this, new NewCommandEventArgs(command, time.TotalGameTime, new Player())); } } }
private void init() { velocity = new Vector2(); target = null; activeCommand = null; aggressive = false; pursue = false; engine = (Engine)((UnitType)Type).Engine.create(); }
public void update(GameTime elps, GamerCollection<LocalNetworkGamer> Gamers) { if (HostSession) { NetworkController.serverReadPackets(Gamers, elps); Queue<Command> commandsToDo = new Queue<Command>(2); Command nextCommand = new Command(); //needs to be outvalue monirator.UpdateSchedule(elps); while (monirator.GetNextScheduledCommand(ref nextCommand)) { commandsToDo.Enqueue(nextCommand); } simulator.dispatchCommands(commandsToDo); simulator.step(elps); } else { } }
public CommandDecorator(Command cmd) : base(cmd.Cmd) { this.decoratedCommand = cmd; }
/// <summary> /// TODO: Impliment. /// </summary> /// <param name="req"></param> /// <param name="rules"></param> /// <param name="player"></param> /// <param name="time"></param> /// <returns></returns> private Queue<Command> evaluateSet(Command req, RuleBook rules, Player player, TimeSpan time) { throw new NotImplementedException(); }
/// <summary> /// Method for evaluating Move Commands. /// </summary> /// <param name="req">Move Command being evaluated.</param> /// <param name="rules">RuleBook for evaluating the command.</param> /// <param name="player">Player who requested the move.</param> /// <param name="time">Current game time.</param> /// <returns>Queue of translated commands resulting from the evaluation.</returns> private Queue<Command> evaluateMove(Command req, RuleBook rules, Player player, TimeSpan time) { Queue<Command> retval = new Queue<Command>(); // Decorate the command to get the required functionality. // Probably a better way to do this... Command move = new MoveDecorator(req); // Unit the command was acting on. ActiveGameObject unit = (ActiveGameObject)GameObjectFactory.The.getGameObject(move.Actor); Vector2 origin = unit.getPosition(); Vector2 dest = new Vector2((long)move.X, (long)move.Y); List<Vector2> waypoints = _explorer.GetPath(origin, dest, _map); // Check that next waypoint is valid. Depending on implimentaiton of PathFinder, this may not be nessisary. if (waypoints.Count > 0 && _map.hasUnitsInPath(origin, waypoints[0])) { Command deny = new DenyDecorator(req.Actor, time.Ticks, new Command()); retval.Enqueue(deny); // TODO: trigger request denied event. return retval; } Engine engine = ((Unit)unit).Engine; // Build Move Commands out of waypoints. foreach(Vector2 p in waypoints) { float ttd = engine.timeToReach(dest); float curt = time.Ticks; retval.Enqueue(new MoveDecorator(req.Actor, (UInt16)p.X, (UInt16)p.Y, curt + ttd, new Command())); } return retval; }
/// <summary> /// TODO: Impliment /// </summary> /// <param name="req"></param> /// <param name="rules"></param> /// <returns></returns> private Queue<Command> evaluateCancel(Command req, RuleBook rules) { throw new NotImplementedException(); }
/// <summary> /// Method for evaluating the attack command. /// </summary> /// <param name="req">Attack Command</param> /// <param name="rules">Rules for evaluating the command.</param> /// <param name="player">Player requesting the attack command.</param> /// <param name="time">Current game time.</param> /// <returns>Queue of translated commands resulting from the evaluation.</returns> private Queue<Command> evaluateAttack(Command req, RuleBook rules, Player player, TimeSpan time) { Queue<Command> retval = new Queue<Command>(); // Decorate the command to get the required functionallity. // Probably a better solution. Suggestions welcome. Command attack = new AttackDecorator(req); // Get the target of the attack. ActiveGameObject targ = (ActiveGameObject)GameObjectFactory.The.getGameObject(attack.Target); if (targ == null) { // TODO: flush the unit's queue of commands. Signifies the attack is over / target is dead. } // Unit the command is acting on. Unit unit = (Unit)GameObjectFactory.The.getGameObject(attack.Actor); // Pathfind to the target. List<Vector2> waypoints = _explorer.GetPath(unit.getPosition(), targ.getPosition(), _map); Engine engine = unit.Engine; // Time To Destination. // Note for attack commands we only used the first waypoint. float ttd = engine.timeToReach(waypoints[0]); float curt = time.Ticks; retval.Enqueue(new MoveDecorator(req.Actor, (UInt16)targ.getPosition().X, (UInt16)targ.getPosition().Y, curt + ttd, new Command())); // TODO: push an attack on as well. return retval; }
// TODO: Get cost of unit // TODO: Get player resources // TODO: Get buildtime of the unit private Queue<Command> evaluateAdd(Command req, RuleBook rules, Player player, TimeSpan time) { // Decorate the command Command add = new AddDecorator(req); // Get the units parent UInt16 parent = add.ParentID; // Get its name to look up in the rule book String name = GameObjectFactory.The.getType(add.Type).Name; // Get the type evaluator from the rule book UnitEvaluation dep = rules.getDependancy(name); // Get the name of the type to add name = dep.getType(rules._unitExists); // Get UInt16 type id from name UInt16 type = GameObjectFactory.The.getType(name).ID; // TODO: Get cost of unit // TODO: Get player resources // TODO: Get buildtime of the unit Queue<Command> retval = new Queue<Command>(); Command toadd = new AddDecorator(parent, 0, type, time, new Command()); retval.Enqueue(toadd); return retval; }
private void handleRemoveCommand(Command cmd) { //No remove decorator yet //removeObjectFromWorld(ActiveGameObject toRemove) }
/// <summary> /// Pull Commands off of the currently exicuting Command queue. /// </summary> /// <param name="cmd">Command will be passed back through cmd.</param> /// <returns>True if there are commands currently exicuting.</returns> public bool GetNextScheduledCommand(ref Command cmd) { if (_ScheduledCommands.Count <= 0) return false; cmd = _ScheduledCommands.Dequeue(); return true; }
public CommandChangedEventArgs(Command c) { newCmd = c; }