tools/hwmap.hs
author sheepluva
Wed, 04 Nov 2015 00:20:07 +0100
branch0.9.21
changeset 11345 0785ad226574
parent 10369 5adc765da26b
child 10885 3ea36d8d4636
permissions -rw-r--r--
closing 0.9.21 branch
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
10369
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
    26
10335
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))
10369
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
    29
flip' = transform (\(a, b) -> (a, 2047 - b))
10335
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    30
translate dx dy = transform (\(a, b) -> (a + dx, b + dy))
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    31
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    32
instance Binary Chunk where
10335
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    33
    put (SpecialPoints p) = do
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    34
        forM_ p $ \(x, y) -> do
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    35
            put x
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    36
            put y
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    37
            putWord8 0
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    38
    put (Line lt r ((x1, y1):ps)) = do
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    39
        let flags = r .|. (if lt == Solid then 0 else (1 `shift` 6))
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    40
        put x1
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    41
        put y1
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    42
        putWord8 $ flags .|. (1 `shift` 7)
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    43
        forM_ ps $ \(x, y) -> do
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    44
            put x
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    45
            put y
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    46
            putWord8 flags
10323
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    47
    get = undefined
72e6df962cb6 Code drawn map in haskell \o/
unc0rr
parents:
diff changeset
    48
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    49
compressWithLength :: BL.ByteString -> BL.ByteString
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    50
compressWithLength b = BL.drop 8 . encode . runPut $ do
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    51
    put $ ((fromIntegral $ BL.length b)::Word32)
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    52
    mapM_ putWord8 $ BW.unpack $ BL.toStrict $ Z.compress b
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    53
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    54
mapString :: B.ByteString
10369
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
    55
mapString = B.pack . Base64.encode . BW.unpack . BL.toStrict . compressWithLength . BL.drop 8 . encode $ drawnMap04
10329
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    56
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    57
main = B.writeFile "out.hwmap" mapString
e2dba215655a Compressing and encoding
unc0rr
parents: 10323
diff changeset
    58
10335
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    59
drawnMap01 = translate (-3) (-3) $ sp ++ mirror sp ++ base ++ mirror base
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    60
    where
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    61
    sp = translate 128 128 . scale 256 $ [SpecialPoints [
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    62
        (6, 0)
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    63
        , (1, 4)
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    64
        , (4, 7)
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    65
        , (7, 5)
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    66
        ]]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    67
    base = scale 256 $ [
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    68
        l [(5, 0), (5, 1)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    69
        , l [(7, 0), (7, 1)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    70
        , l [(8, 1), (6, 1), (6, 4)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    71
        , l [(8, 1), (8, 6), (6, 6), (6, 7), (8, 7)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    72
        , l [(7, 2), (7, 5), (5, 5)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    73
        , l [(5, 3), (5, 8)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    74
        , l [(6, 2), (4, 2)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    75
        , l [(1, 1), (4, 1), (4, 7)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    76
        , l [(3, 5), (3, 7), (2, 7), (2, 8)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    77
        , l [(2, 1), (2, 2)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    78
        , l [(0, 2), (1, 2), (1, 3), (3, 3), (3, 2)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    79
        , l [(0, 5), (1, 5)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    80
        , l [(1, 4), (4, 4)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    81
        , l [(2, 4), (2, 6), (1, 6), (1, 7)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    82
        , l [(0, 8), (8, 8)]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    83
        ]
d56b4c109abb Racer challenge map #9
unc0rr
parents: 10329
diff changeset
    84
    l = Line Solid 0
10365
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    85
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    86
drawnMap02 = translate (-3) (-3) $ sp ++ mirror sp ++ base ++ mirror base
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    87
    where
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    88
    sp = translate 128 128 . scale 256 $ [SpecialPoints [
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    89
        (7, 0)
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    90
        , (7, 7)
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    91
        ]]
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    92
    base = scale 256 $ [
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    93
        l [(8, 0), (8, 1), (1, 1)]
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    94
        , 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
    95
        , 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
    96
        ]
d5786d4230fd Racer challenge map #10
unc0rr
parents: 10335
diff changeset
    97
    l = Line Solid 0
10366
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
    98
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
    99
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   100
drawnMap03 = translate (-3) (-3) $ sp ++ mirror sp ++ base ++ mirror base
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   101
    where
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   102
    sp = translate 128 128 . scale 256 $ [SpecialPoints [
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   103
        (3, 1)
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   104
        , (2, 4)
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   105
        ]]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   106
    base = scale 256 $ [
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   107
        l [(6, 0), (6, 1)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   108
        , l [(1, 1), (5, 1)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   109
        , l [(4, 1), (4, 2), (3, 2)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   110
        , l [(0, 2), (1, 2), (1, 4)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   111
        , l [(0, 4), (3, 4), (3, 3), (5, 3), (5, 2), (7, 2)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   112
        , l [(7, 1), (7, 3)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   113
        , l [(8, 0), (8, 4), (4, 4), (4, 5), (1, 5), (1, 6)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   114
        , l [(6, 3), (6, 4)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   115
        , l [(0, 8), (8, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   116
        , l [(1, 7), (1, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   117
        , l [(2, 7), (2, 5)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   118
        , l [(3, 6), (3, 5)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   119
        , l [(3, 7), (3, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   120
        , l [(4, 6), (4, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   121
        , l [(5, 4), (5, 6)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   122
        , l [(5, 7), (5, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   123
        , l [(6, 5), (6, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   124
        , l [(7, 4), (7, 6)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   125
        , l [(7, 7), (7, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   126
        , l [(8, 5), (8, 8)]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   127
        ]
b451b8f69f44 Official racer map #11
unc0rr
parents: 10365
diff changeset
   128
    l = Line Solid 0
10369
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   129
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   130
drawnMap04 = translate (-3) (-3) $ sp ++ fm sp ++ base ++ fm base
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   131
    where
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   132
    sp = translate 128 128 . scale 256 $ [SpecialPoints [
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   133
        (7, 7)
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   134
--        , (6, 6)
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   135
        , (3, 3)
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   136
        , (0, 6)
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   137
        , (3, 6)
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   138
        ]]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   139
    base = scale 256 $ [
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   140
        l [(1, 2), (3, 2), (3, 1), (4, 1), (4, 2), (6, 2), (6, 4), (7, 4), (7, 5), (8, 5), (8, 8)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   141
        , l [(0, 0), (16, 0)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   142
        , l [(1, 5), (3, 5), (3, 7), (1, 7), (1, 5)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   143
        , l [(4, 5), (6, 5), (6, 7), (4, 7), (4, 5)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   144
        , l [(0, 4), (2, 4), (2, 3), (5, 3), (5, 4)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   145
        , l [(6, 1), (6, 2), (7, 2)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   146
        , l [(7, 1), (8, 1)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   147
        , l [(7, 3), (8, 3)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   148
        , l [(3, 4), (4, 4)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   149
        , l [(7, 6), (7, 8)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   150
        , l [(2, 0), (2, 1)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   151
        , l [(5, 0), (5, 1)]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   152
        ]
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   153
    l = Line Solid 0
5adc765da26b One more map
unc0rr
parents: 10366
diff changeset
   154
    fm = flip' . mirror