tools/replay2hwd.hs
author Wuzzy <Wuzzy2@mail.ru>
Sat, 06 Jun 2020 15:40:51 +0200
changeset 15597 6e72bd61002e
parent 15474 e0ab70a90718
permissions -rw-r--r--
Disable gfMoreWind for land objects on turn end only after a fixed-time delay 15s sounds much, but it's the average amount for gfMineStrike mines to settle naturally. And it would be very confusing to see falling mines suddenly not caring about gfMoreWind for no apparent reason. Note this whole thing is a giant hack anyway, to prevent a turn being blocked by infinitely bouncing mines. The better solution would be to help gfMoreWind-affected land objects settle naturally more reliably even under extreme wind. But this commit is "good enough" for now. If you don't like the delay, you can always tweak the constant.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15474
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
     1
{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
     2
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
     3
import qualified Data.ByteString.Char8 as B
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
     4
import Control.Exception as E
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
     5
import System.Environment
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
     6
import Control.Monad
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
     7
import qualified Data.Map as Map
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
     8
import Data.Word
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
     9
import Data.Int
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    10
import qualified Codec.Binary.Base64 as Base64
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    11
import qualified Data.ByteString.Lazy as BL
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    12
import qualified Data.ByteString as BW
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    13
import qualified Codec.Compression.Zlib.Internal as ZI
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    14
import qualified Codec.Compression.Zlib as Z
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    15
import qualified Data.List as L
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    16
import qualified Data.Set as Set
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    17
import Data.Binary
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    18
import Data.Binary.Put
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    19
import Data.Bits
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    20
import Control.Arrow
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    21
import Data.Maybe
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    22
import qualified Data.Either as Ei
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    23
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    24
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    25
decompressWithoutExceptions :: BL.ByteString -> BL.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    26
decompressWithoutExceptions = BL.fromChunks . ZI.foldDecompressStreamWithInput chunk end err decomp
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    27
    where
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    28
        decomp = ZI.decompressST ZI.zlibFormat ZI.defaultDecompressParams
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    29
        chunk = (:)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    30
        end _ = []
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    31
        err = const $ [BW.empty]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    32
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    33
data HedgehogInfo =
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    34
    HedgehogInfo B.ByteString B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    35
    deriving (Show, Read)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    36
    
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    37
data TeamInfo =
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    38
    TeamInfo
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    39
    {
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    40
        teamowner :: !B.ByteString,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    41
        teamname :: !B.ByteString,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    42
        teamcolor :: !B.ByteString,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    43
        teamgrave :: !B.ByteString,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    44
        teamfort :: !B.ByteString,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    45
        teamvoicepack :: !B.ByteString,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    46
        teamflag :: !B.ByteString,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    47
        isOwnerRegistered :: !Bool,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    48
        difficulty :: !Int,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    49
        hhnum :: !Int,
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    50
        hedgehogs :: ![HedgehogInfo]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    51
    }
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    52
    deriving (Show, Read)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    53
    
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    54
readInt_ :: (Num a) => B.ByteString -> a
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    55
readInt_ str =
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    56
  case B.readInt str of
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    57
       Just (i, t) | B.null t -> fromIntegral i
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    58
       _                      -> 0
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    59
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    60
toEngineMsg :: B.ByteString -> B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    61
toEngineMsg msg = fromIntegral (BW.length msg) `BW.cons` msg
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    62
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    63
em :: B.ByteString -> B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    64
em = toEngineMsg
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    65
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    66
eml :: [B.ByteString] -> B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    67
eml = em . B.concat       
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    68
    
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    69
showB :: (Show a) => a -> B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    70
showB = B.pack . show
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    71
    
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    72
replayToDemo :: [TeamInfo]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    73
        -> Map.Map B.ByteString B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    74
        -> Map.Map B.ByteString [B.ByteString]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    75
        -> [B.ByteString]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    76
        -> B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    77
replayToDemo ti mParams prms msgs = if not sane then "" else (B.concat $ concat [
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    78
        [em "TD"]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    79
        , maybeScript
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    80
        , maybeMap
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    81
        , [eml ["etheme ", head $ prms Map.! "THEME"]]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    82
        , [eml ["eseed ", mParams Map.! "SEED"]]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    83
        , [eml ["e$gmflags ", showB gameFlags]]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    84
        , schemeFlags
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    85
        , schemeAdditional
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    86
        , [eml ["e$template_filter ", mParams Map.! "TEMPLATE"]]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    87
        , [eml ["e$feature_size ", mParams Map.! "FEATURE_SIZE"]]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    88
        , [eml ["e$mapgen ", mapgen]]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    89
        , mapgenSpecific
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    90
        , concatMap teamSetup ti
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    91
        , map (Ei.fromRight "" . Base64.decode) $ reverse msgs
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    92
        , [em "!"]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    93
        ])
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    94
    where
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    95
        keys1, keys2 :: Set.Set B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    96
        keys1 = Set.fromList ["FEATURE_SIZE", "MAP", "MAPGEN", "MAZE_SIZE", "SEED", "TEMPLATE"]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    97
        keys2 = Set.fromList ["AMMO", "SCHEME", "SCRIPT", "THEME"]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    98
        sane = Set.null (keys1 Set.\\ Map.keysSet mParams)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
    99
            && Set.null (keys2 Set.\\ Map.keysSet prms)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   100
            && (not . null . drop 41 $ scheme)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   101
            && (not . null . tail $ prms Map.! "AMMO")
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   102
            && ((B.length . head . tail $ prms Map.! "AMMO") > 200)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   103
        mapGenTypes = ["+rnd+", "+maze+", "+drawn+", "+perlin+"]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   104
        scriptName = head . fromMaybe ["Normal"] $ Map.lookup "SCRIPT" prms
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   105
        maybeScript = let s = scriptName in if s == "Normal" then [] else [eml ["escript Scripts/Multiplayer/", spaces2Underlining s, ".lua"]]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   106
        maybeMap = let m = mParams Map.! "MAP" in if m `elem` mapGenTypes then [] else [eml ["emap ", m]]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   107
        scheme = tail $ prms Map.! "SCHEME"
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   108
        mapgen = mParams Map.! "MAPGEN"
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   109
        mazeSizeMsg = eml ["e$maze_size ", mParams Map.! "MAZE_SIZE"]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   110
        mapgenSpecific = case mapgen of
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   111
            "1" -> [mazeSizeMsg]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   112
            "2" -> [mazeSizeMsg]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   113
            "3" -> let d = head . fromMaybe [""] $ Map.lookup "DRAWNMAP" prms in if BW.length d <= 4 then [] else drawnMapData d
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   114
            _ -> []
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   115
        gameFlags :: Word32
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   116
        gameFlags = foldl (\r (b, f) -> if b == "false" then r else r .|. f) 0 $ zip scheme gameFlagConsts
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   117
        schemeFlags = map (\(v, (n, m)) -> eml [n, " ", showB $ (readInt_ v) * m])
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   118
            $ filter (\(_, (n, _)) -> not $ B.null n)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   119
            $ zip (drop (length gameFlagConsts) scheme) schemeParams
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   120
        schemeAdditional = let scriptParam = B.tail $ scheme !! 42 in [eml ["e$scriptparam ", scriptParam] | not $ B.null scriptParam]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   121
        ammoStr :: B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   122
        ammoStr = head . tail $ prms Map.! "AMMO"
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   123
        ammo = let l = B.length ammoStr `div` 4; ((a, b), (c, d)) = (B.splitAt l . fst &&& B.splitAt l . snd) . B.splitAt (l * 2) $ ammoStr in
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   124
                   (map (\(x, y) -> eml [x, " ", y]) $ zip ["eammloadt", "eammprob", "eammdelay", "eammreinf"] [a, b, c, d])
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   125
                   ++ [em "eammstore" | scheme !! 14 == "true" || scheme !! 20 == "false"]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   126
        initHealth = scheme !! 27
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   127
        teamSetup :: TeamInfo -> [B.ByteString]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   128
        teamSetup t = (++) ammo $
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   129
                eml ["eaddteam <hash> ", showB $ (1 + (readInt_ $ teamcolor t) :: Int) * 2113696, " ", teamname t]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   130
                : em "erdriven"
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   131
                : eml ["efort ", teamfort t]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   132
                : take (2 * hhnum t) (
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   133
                    concatMap (\(HedgehogInfo hname hhat) -> [
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   134
                            eml ["eaddhh ", showB $ difficulty t, " ", initHealth, " ", hname]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   135
                            , eml ["ehat ", hhat]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   136
                            ])
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   137
                        $ hedgehogs t
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   138
                        )
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   139
        infRopes = ammoStr `B.index` 7  == '9'
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   140
        vamp = gameFlags .&. 0x00000200 /= 0
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   141
        infattacks = gameFlags .&. 0x00100000 /= 0
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   142
        spaces2Underlining = B.map (\c -> if c == ' ' then '_' else c)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   143
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   144
drawnMapData :: B.ByteString -> [B.ByteString]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   145
drawnMapData =
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   146
          L.map (\m -> eml ["edraw ", BW.pack m])
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   147
        . L.unfoldr by200
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   148
        . BL.unpack
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   149
        . unpackDrawnMap
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   150
    where
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   151
        by200 :: [a] -> Maybe ([a], [a])
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   152
        by200 [] = Nothing
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   153
        by200 m = Just $ L.splitAt 200 m
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   154
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   155
unpackDrawnMap :: B.ByteString -> BL.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   156
unpackDrawnMap = either
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   157
        (const BL.empty) 
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   158
        (decompressWithoutExceptions . BL.pack . drop 4 . BW.unpack)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   159
        . Base64.decode
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   160
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   161
compressWithLength :: BL.ByteString -> BL.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   162
compressWithLength b = BL.drop 8 . encode . runPut $ do
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   163
    put $ ((fromIntegral $ BL.length b)::Word32)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   164
    mapM_ putWord8 $ BW.unpack $ BL.toStrict $ Z.compress b
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   165
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   166
packDrawnMap :: BL.ByteString -> B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   167
packDrawnMap =
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   168
      Base64.encode
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   169
    . BL.toStrict
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   170
    . compressWithLength
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   171
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   172
prependGhostPoints :: [(Int16, Int16)] -> B.ByteString -> B.ByteString
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   173
prependGhostPoints pts dm = packDrawnMap $ (runPut $ forM_ pts $ \(x, y) -> put x >> put y >> putWord8 99) `BL.append` unpackDrawnMap dm
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   174
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   175
schemeParams :: [(B.ByteString, Int)]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   176
schemeParams = [
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   177
      ("e$damagepct", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   178
    , ("e$turntime", 1000)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   179
    , ("", 0)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   180
    , ("e$sd_turns", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   181
    , ("e$casefreq", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   182
    , ("e$minestime", 1000)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   183
    , ("e$minesnum", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   184
    , ("e$minedudpct", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   185
    , ("e$explosives", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   186
    , ("e$airmines", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   187
    , ("e$healthprob", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   188
    , ("e$hcaseamount", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   189
    , ("e$waterrise", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   190
    , ("e$healthdec", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   191
    , ("e$ropepct", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   192
    , ("e$getawaytime", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   193
    , ("e$worldedge", 1)
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   194
    ]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   195
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   196
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   197
gameFlagConsts :: [Word32]
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   198
gameFlagConsts = [
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   199
          0x00001000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   200
        , 0x00000010
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   201
        , 0x00000004
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   202
        , 0x00000008
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   203
        , 0x00000020
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   204
        , 0x00000040
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   205
        , 0x00000080
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   206
        , 0x00000100
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   207
        , 0x00000200
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   208
        , 0x00000400
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   209
        , 0x00000800
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   210
        , 0x00002000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   211
        , 0x00004000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   212
        , 0x00008000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   213
        , 0x00010000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   214
        , 0x00020000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   215
        , 0x00040000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   216
        , 0x00080000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   217
        , 0x00100000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   218
        , 0x00200000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   219
        , 0x00400000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   220
        , 0x00800000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   221
        , 0x01000000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   222
        , 0x02000000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   223
        , 0x04000000
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   224
        ]    
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   225
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   226
loadReplay :: String -> IO (Maybe ([TeamInfo], [(B.ByteString, B.ByteString)], [(B.ByteString, [B.ByteString])], [B.ByteString]))
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   227
loadReplay fileName = E.handle (\(e :: SomeException) -> return Nothing) $ do
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   228
            liftM (Just . read) $ readFile fileName
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   229
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   230
convert :: String -> IO ()
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   231
convert fileName = do
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   232
    Just (t, c1, c2, m) <- loadReplay fileName
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   233
    B.writeFile (fileName ++ ".hwd") $ replayToDemo t (Map.fromList c1) (Map.fromList c2) m
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   234
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   235
main = do
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   236
    args <- getArgs
e0ab70a90718 Add utility to convert server-side replay files into hwd binary format
unc0rr
parents:
diff changeset
   237
    when (length args == 1) $ (convert (head args))