tools/hwmap.hs
author unc0rr
Fri, 25 Jul 2014 14:16:33 +0400
changeset 10367 6e1bfa70ccb3
parent 10366 b451b8f69f44
child 10369 5adc765da26b
permissions -rw-r--r--
Oops
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
10366
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
    54
mapString = B.pack . Base64.encode . BW.unpack . BL.toStrict . compressWithLength . BL.drop 8 . encode $ drawnMap03
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
10366
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
    97
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
    98
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
    99
drawnMap03 = translate (-3) (-3) $ sp ++ mirror sp ++ base ++ mirror base
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   100
    where
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   101
    sp = translate 128 128 . scale 256 $ [SpecialPoints [
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   102
        (3, 1)
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   103
        , (2, 4)
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   104
        ]]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   105
    base = scale 256 $ [
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   106
        l [(6, 0), (6, 1)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   107
        , l [(1, 1), (5, 1)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   108
        , l [(4, 1), (4, 2), (3, 2)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   109
        , l [(0, 2), (1, 2), (1, 4)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   110
        , l [(0, 4), (3, 4), (3, 3), (5, 3), (5, 2), (7, 2)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   111
        , l [(7, 1), (7, 3)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   112
        , l [(8, 0), (8, 4), (4, 4), (4, 5), (1, 5), (1, 6)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   113
        , l [(6, 3), (6, 4)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   114
        , l [(0, 8), (8, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   115
        , l [(1, 7), (1, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   116
        , l [(2, 7), (2, 5)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   117
        , l [(3, 6), (3, 5)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   118
        , l [(3, 7), (3, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   119
        , l [(4, 6), (4, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   120
        , l [(5, 4), (5, 6)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   121
        , l [(5, 7), (5, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   122
        , l [(6, 5), (6, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   123
        , l [(7, 4), (7, 6)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   124
        , l [(7, 7), (7, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   125
        , l [(8, 5), (8, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   126
        ]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   127
    l = Line Solid 0