tools/hwmap.hs
author unc0rr
Thu, 10 Jul 2014 13:40:49 +0400
changeset 10365 d5786d4230fd
parent 10335 d56b4c109abb
child 10366 b451b8f69f44
permissions -rw-r--r--
Racer challenge map #10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
     1
module Main where
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
     2
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
     3
import qualified Data.ByteString.Char8 as B
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
     4
import qualified Data.ByteString as BW
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
     5
import qualified Data.ByteString.Lazy as BL
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
     6
import qualified Codec.Binary.Base64 as Base64
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
     7
import Data.Word
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
     8
import Data.Int
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
     9
import Data.Binary
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    10
import Data.Binary.Put
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    11
import Data.Bits
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    12
import Control.Monad
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    13
import qualified Codec.Compression.Zlib as Z
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    14
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    15
data LineType = Solid | Erasing
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    16
    deriving Eq
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    17
10335
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    18
data Chunk = SpecialPoints [(Int16, Int16)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    19
    | Line LineType Word8 [(Int16, Int16)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    20
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    21
transform :: ((Int16, Int16) -> (Int16, Int16)) -> [Chunk] -> [Chunk]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    22
transform f = map tf
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    23
    where
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    24
    tf (SpecialPoints p) = SpecialPoints $ map f p
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    25
    tf (Line t r p) = Line t r $ map f p
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    26
    
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    27
scale f = transform (\(a, b) -> (a * f, b * f))
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    28
mirror = transform (\(a, b) -> (4095 - a, b))
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    29
translate dx dy = transform (\(a, b) -> (a + dx, b + dy))
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    30
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    31
instance Binary Chunk where
10335
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    32
    put (SpecialPoints p) = do
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    33
        forM_ p $ \(x, y) -> do
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    34
            put x
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    35
            put y
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    36
            putWord8 0
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    37
    put (Line lt r ((x1, y1):ps)) = do
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    38
        let flags = r .|. (if lt == Solid then 0 else (1 `shift` 6))
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    39
        put x1
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    40
        put y1
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    41
        putWord8 $ flags .|. (1 `shift` 7)
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    42
        forM_ ps $ \(x, y) -> do
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    43
            put x
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    44
            put y
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    45
            putWord8 flags
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    46
    get = undefined
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    47
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    48
compressWithLength :: BL.ByteString -> BL.ByteString
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    49
compressWithLength b = BL.drop 8 . encode . runPut $ do
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    50
    put $ ((fromIntegral $ BL.length b)::Word32)
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    51
    mapM_ putWord8 $ BW.unpack $ BL.toStrict $ Z.compress b
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    52
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    53
mapString :: B.ByteString
10365
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    54
mapString = B.pack . Base64.encode . BW.unpack . BL.toStrict . compressWithLength . BL.drop 8 . encode $ drawnMap02
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    55
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    56
main = B.writeFile "out.hwmap" mapString
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    57
10335
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    58
drawnMap01 = translate (-3) (-3) $ sp ++ mirror sp ++ base ++ mirror base
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    59
    where
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    60
    sp = translate 128 128 . scale 256 $ [SpecialPoints [
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    61
        (6, 0)
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    62
        , (1, 4)
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    63
        , (4, 7)
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    64
        , (7, 5)
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    65
        ]]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    66
    base = scale 256 $ [
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    67
        l [(5, 0), (5, 1)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    68
        , l [(7, 0), (7, 1)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    69
        , l [(8, 1), (6, 1), (6, 4)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    70
        , l [(8, 1), (8, 6), (6, 6), (6, 7), (8, 7)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    71
        , l [(7, 2), (7, 5), (5, 5)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    72
        , l [(5, 3), (5, 8)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    73
        , l [(6, 2), (4, 2)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    74
        , l [(1, 1), (4, 1), (4, 7)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    75
        , l [(3, 5), (3, 7), (2, 7), (2, 8)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    76
        , l [(2, 1), (2, 2)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    77
        , l [(0, 2), (1, 2), (1, 3), (3, 3), (3, 2)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    78
        , l [(0, 5), (1, 5)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    79
        , l [(1, 4), (4, 4)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    80
        , l [(2, 4), (2, 6), (1, 6), (1, 7)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    81
        , l [(0, 8), (8, 8)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    82
        ]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    83
    l = Line Solid 0
10365
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    84
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    85
drawnMap02 = translate (-3) (-3) $ sp ++ mirror sp ++ base ++ mirror base
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    86
    where
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    87
    sp = translate 128 128 . scale 256 $ [SpecialPoints [
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    88
        (7, 0)
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    89
        , (7, 7)
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    90
        ]]
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    91
    base = scale 256 $ [
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    92
        l [(8, 0), (8, 1), (1, 1)]
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    93
        , l [(2, 1), (2, 2), (3, 2), (3, 3), (4, 3), (4, 4), (5, 4), (5, 5), (6, 5), (6, 6), (7, 6), (7, 7), (7, 1)]
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    94
        , l [(0, 2), (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 5), (4, 6), (5, 6), (5, 7), (6, 7), (6, 8), (8, 8), (8, 2)]
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    95
        ]
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    96
    l = Line Solid 0