11015
+ − 1
module Main where
+ − 2
+ − 3
import qualified Data.ByteString.Char8 as B
+ − 4
import qualified Data.ByteString as BW
+ − 5
import qualified Data.ByteString.Lazy as BL
+ − 6
import qualified Codec.Binary.Base64 as Base64
+ − 7
import Data.Word
+ − 8
import Data.Int
+ − 9
import Data.Binary
+ − 10
import Data.Binary.Put
+ − 11
import Data.Bits
+ − 12
import Control.Monad
+ − 13
import qualified Codec.Compression.Zlib as Z
+ − 14
+ − 15
data LineType = Solid | Erasing
+ − 16
deriving Eq
+ − 17
+ − 18
data Chunk = SpecialPoints [(Int16, Int16)]
+ − 19
| Line LineType Word8 [(Int16, Int16)]
+ − 20
+ − 21
transform :: ((Int16, Int16) -> (Int16, Int16)) -> [Chunk] -> [Chunk]
+ − 22
transform f = map tf
+ − 23
where
+ − 24
tf (SpecialPoints p) = SpecialPoints $ map f p
+ − 25
tf (Line t r p) = Line t r $ map f p
+ − 26
+ − 27
scale f = transform (\(a, b) -> (a * f, b * f))
+ − 28
mirror = transform (\(a, b) -> (4095 - a, b))
+ − 29
flip' = transform (\(a, b) -> (a, 2047 - b))
+ − 30
translate dx dy = transform (\(a, b) -> (a + dx, b + dy))
+ − 31
+ − 32
instance Binary Chunk where
+ − 33
put (SpecialPoints p) = do
+ − 34
forM_ p $ \(x, y) -> do
+ − 35
put x
+ − 36
put y
+ − 37
putWord8 0
+ − 38
put (Line lt r ((x1, y1):ps)) = do
+ − 39
let flags = r .|. (if lt == Solid then 0 else (1 `shift` 6))
+ − 40
put x1
+ − 41
put y1
+ − 42
putWord8 $ flags .|. (1 `shift` 7)
+ − 43
forM_ ps $ \(x, y) -> do
+ − 44
put x
+ − 45
put y
+ − 46
putWord8 flags
+ − 47
get = undefined
+ − 48
+ − 49
compressWithLength :: BL.ByteString -> BL.ByteString
+ − 50
compressWithLength b = BL.drop 8 . encode . runPut $ do
+ − 51
put $ ((fromIntegral $ BL.length b)::Word32)
+ − 52
mapM_ putWord8 $ BW.unpack $ BL.toStrict $ Z.compress b
+ − 53
+ − 54
mapString :: B.ByteString
+ − 55
mapString = B.pack . Base64.encode . BW.unpack . BL.toStrict . compressWithLength . BL.drop 8 . encode $ drawnMap05
+ − 56
+ − 57
main = B.writeFile "out.hwmap" mapString
+ − 58
+ − 59
drawnMap01 = translate (-3) (-3) $ sp ++ mirror sp ++ base ++ mirror base
+ − 60
where
+ − 61
sp = translate 128 128 . scale 256 $ [SpecialPoints [
+ − 62
(6, 0)
+ − 63
, (1, 4)
+ − 64
, (4, 7)
+ − 65
, (7, 5)
+ − 66
]]
+ − 67
base = scale 256 $ [
+ − 68
l [(5, 0), (5, 1)]
+ − 69
, l [(7, 0), (7, 1)]
+ − 70
, l [(8, 1), (6, 1), (6, 4)]
+ − 71
, l [(8, 1), (8, 6), (6, 6), (6, 7), (8, 7)]
+ − 72
, l [(7, 2), (7, 5), (5, 5)]
+ − 73
, l [(5, 3), (5, 8)]
+ − 74
, l [(6, 2), (4, 2)]
+ − 75
, l [(1, 1), (4, 1), (4, 7)]
+ − 76
, l [(3, 5), (3, 7), (2, 7), (2, 8)]
+ − 77
, l [(2, 1), (2, 2)]
+ − 78
, l [(0, 2), (1, 2), (1, 3), (3, 3), (3, 2)]
+ − 79
, l [(0, 5), (1, 5)]
+ − 80
, l [(1, 4), (4, 4)]
+ − 81
, l [(2, 4), (2, 6), (1, 6), (1, 7)]
+ − 82
, l [(0, 8), (8, 8)]
+ − 83
]
+ − 84
l = Line Solid 0
+ − 85
+ − 86
drawnMap02 = translate (-3) (-3) $ sp ++ mirror sp ++ base ++ mirror base
+ − 87
where
+ − 88
sp = translate 128 128 . scale 256 $ [SpecialPoints [
+ − 89
(7, 0)
+ − 90
, (7, 7)
+ − 91
]]
+ − 92
base = scale 256 $ [
+ − 93
l [(8, 0), (8, 1), (1, 1)]
+ − 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)]
+ − 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)]
+ − 96
]
+ − 97
l = Line Solid 0
+ − 98
+ − 99
+ − 100
drawnMap03 = translate (-3) (-3) $ sp ++ mirror sp ++ base ++ mirror base
+ − 101
where
+ − 102
sp = translate 128 128 . scale 256 $ [SpecialPoints [
+ − 103
(3, 1)
+ − 104
, (2, 4)
+ − 105
]]
+ − 106
base = scale 256 $ [
+ − 107
l [(6, 0), (6, 1)]
+ − 108
, l [(1, 1), (5, 1)]
+ − 109
, l [(4, 1), (4, 2), (3, 2)]
+ − 110
, l [(0, 2), (1, 2), (1, 4)]
+ − 111
, l [(0, 4), (3, 4), (3, 3), (5, 3), (5, 2), (7, 2)]
+ − 112
, l [(7, 1), (7, 3)]
+ − 113
, l [(8, 0), (8, 4), (4, 4), (4, 5), (1, 5), (1, 6)]
+ − 114
, l [(6, 3), (6, 4)]
+ − 115
, l [(0, 8), (8, 8)]
+ − 116
, l [(1, 7), (1, 8)]
+ − 117
, l [(2, 7), (2, 5)]
+ − 118
, l [(3, 6), (3, 5)]
+ − 119
, l [(3, 7), (3, 8)]
+ − 120
, l [(4, 6), (4, 8)]
+ − 121
, l [(5, 4), (5, 6)]
+ − 122
, l [(5, 7), (5, 8)]
+ − 123
, l [(6, 5), (6, 8)]
+ − 124
, l [(7, 4), (7, 6)]
+ − 125
, l [(7, 7), (7, 8)]
+ − 126
, l [(8, 5), (8, 8)]
+ − 127
]
+ − 128
l = Line Solid 0
+ − 129
+ − 130
drawnMap04 = translate (-3) (-3) $ sp ++ fm sp ++ base ++ fm base
+ − 131
where
+ − 132
sp = translate 128 128 . scale 256 $ [SpecialPoints [
+ − 133
(7, 7)
+ − 134
-- , (6, 6)
+ − 135
, (3, 3)
+ − 136
, (0, 6)
+ − 137
, (3, 6)
+ − 138
]]
+ − 139
base = scale 256 $ [
+ − 140
l [(1, 2), (3, 2), (3, 1), (4, 1), (4, 2), (6, 2), (6, 4), (7, 4), (7, 5), (8, 5), (8, 8)]
+ − 141
, l [(0, 0), (16, 0)]
+ − 142
, l [(1, 5), (3, 5), (3, 7), (1, 7), (1, 5)]
+ − 143
, l [(4, 5), (6, 5), (6, 7), (4, 7), (4, 5)]
+ − 144
, l [(0, 4), (2, 4), (2, 3), (5, 3), (5, 4)]
+ − 145
, l [(6, 1), (6, 2), (7, 2)]
+ − 146
, l [(7, 1), (8, 1)]
+ − 147
, l [(7, 3), (8, 3)]
+ − 148
, l [(3, 4), (4, 4)]
+ − 149
, l [(7, 6), (7, 8)]
+ − 150
, l [(2, 0), (2, 1)]
+ − 151
, l [(5, 0), (5, 1)]
+ − 152
]
+ − 153
l = Line Solid 0
+ − 154
fm = flip' . mirror
+ − 155
+ − 156
drawnMap05 = sp ++ fullFill ++ lW
+ − 157
where
+ − 158
w = 320
+ − 159
sh = 420
+ − 160
basePoints = [(w, w), (1024 + w `div` 2, 2048 - w), (2048, w), (3072 - w `div` 2, 2048 - w), (4096 - w, w)]
+ − 161
lW = [Line Erasing 60 basePoints]
+ − 162
sp = [SpecialPoints $ basePoints ++ [(1024 + w `div` 2, 2048 - w - sh), (3072 - w `div` 2, 2048 - w - sh), (2048, w + sh)]]
+ − 163
+ − 164
fullFill = scale 256 $ [Line Solid 63 [(0, 1), (16, 1), (16, 3), (0, 3), (0, 5), (16, 5), (16, 7), (0, 7)]]