-- Commands.hs -- Tom Moertel -- CVS $Id: Commands.hs,v 1.6 2002/09/06 18:02:36 thor Exp $ -- | The Commands module represents the commands sent from the server -- to specify the state of the game. module Commands where import Char import BasicTypes import Board data RobotCommandList = RCL { rclRobotID :: RobotID, rclCommands :: [Command] } deriving (Show, Read, Eq, Ord) data Command = Move Direction | Drop [PackageID] | PickUp [PackageID] | SetRobot Point | SetMoney Money -- ^pseudocommand deriving (Show, Read, Eq, Ord) data RobotConfiguration = RobotConfig { rcRbtID :: RobotID , rcRbtMaxWeight :: Weight , rcRbtInitialMoney :: Money } deriving (Show, Read, Eq, Ord) -- |PackageInfo represents the information about local packages that -- the server provides to a robot when the robot enters a game cell -- containing packages. data PackageInfo = PackageInfo { piPkgID :: PackageID , piPkgDestination :: Point , piPkgWeight :: Weight } deriving (Show, Read, Eq, Ord) readRCommands :: String -> [RobotCommandList] readRCommands ('#':rest)= RCL rid commands : readRCommands rest' where rid = read ridStr commands = readCommands cmds (ridStr:cmds) = words rcmdStr (rcmdStr, rest') = break (=='#') rest readRCommands _ = [] readCommands :: [String] -> [Command] readCommands [] = [] readCommands cmd@(cmd0@(x:_):rest) | x `elem` "NSEW" = Move direction : readCommands rest | x `elem` "PD" = pkgCmd (map read pkgList) : readCommands pkgRest | x == 'X' = setRobotCmd | otherwise = error ("readCommands: bad command: " ++ unwords cmd) where pkgCmd = if x == 'P' then PickUp else Drop (pkgList, pkgRest) = span (isDigit . head) rest setRobotCmd = case rest of { (xc:_:yc:rest') -> SetRobot (read xc, read yc) : readCommands rest' ; _ -> error $ "Bad SetRobot command: " ++ cmd0} direction = lookupDirection x readCommands (cmd0:_) = error $ "readCommands: Bad command: " ++ cmd0 readRobotConfig :: String -> RobotConfiguration readRobotConfig s = case words s of { [ridStr, weightStr, moneyStr] -> RobotConfig (read ridStr) (read weightStr) (read moneyStr) ; _ -> error$"bad RobotConfig: "++s} readPackageInfoList :: String -> [PackageInfo] readPackageInfoList = readPIL' . words where readPIL' (i:x:y:w:rest) = PackageInfo (read i) (read x, read y) (read w) : readPIL' rest readPIL' _ = [] -- ================================================================= -- -- Copyright (C) 2002 Thomas Moertel. -- -- This program is free software; you can redistribute it and/or -- modify it under the terms of the GNU General Public License -- as published by the Free Software Foundation; either version 2 -- of the License, or (at your option) any later version. -- -- The text of the GNU GPL may be found in the LICENSE file, -- included with this software, or online at the following URL: -- -- http://www.gnu.org/copyleft/gpl.html -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- Except as provided for under the terms of the GNU GPL, all rights -- are reserved worldwide. -- -- =================================================================