author | unc0rr |
Sat, 09 Feb 2013 00:11:14 +0400 | |
changeset 8483 | d5fd4d7a0bcc |
parent 8481 | 692ff6468b63 |
child 8484 | 99c14f14f788 |
permissions | -rw-r--r-- |
8480
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
1 |
{-# LANGUAGE OverloadedStrings #-} |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
2 |
|
6068 | 3 |
module EngineInteraction where |
4 |
||
5 |
import qualified Data.Set as Set |
|
6 |
import Control.Monad |
|
7 |
import qualified Codec.Binary.Base64 as Base64 |
|
8 |
import qualified Data.ByteString.Char8 as B |
|
9 |
import qualified Data.ByteString as BW |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
7766
diff
changeset
|
10 |
import qualified Data.Map as Map |
8480
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
11 |
import Data.Word |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
12 |
import Data.Bits |
8481 | 13 |
import Control.Arrow |
6069 | 14 |
------------- |
15 |
import CoreTypes |
|
8480
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
16 |
import Utils |
6068 | 17 |
|
18 |
||
19 |
toEngineMsg :: B.ByteString -> B.ByteString |
|
20 |
toEngineMsg msg = B.pack $ Base64.encode (fromIntegral (BW.length msg) : BW.unpack msg) |
|
21 |
||
22 |
||
23 |
fromEngineMsg :: B.ByteString -> Maybe B.ByteString |
|
24 |
fromEngineMsg msg = liftM BW.pack (Base64.decode (B.unpack msg) >>= removeLength) |
|
25 |
where |
|
26 |
removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing |
|
27 |
removeLength _ = Nothing |
|
28 |
||
29 |
||
30 |
checkNetCmd :: B.ByteString -> (Bool, Bool) |
|
31 |
checkNetCmd msg = check decoded |
|
32 |
where |
|
33 |
decoded = fromEngineMsg msg |
|
34 |
check Nothing = (False, False) |
|
35 |
check (Just ms) | B.length ms > 0 = let m = B.head ms in (m `Set.member` legalMessages, m == '+') |
|
36 |
| otherwise = (False, False) |
|
6206
75e0d8169ba2
As sheepluva pointed out, allowing this message to be legal allows naughtiness. The server usage of this message does not seem to use this check.
nemo
parents:
6070
diff
changeset
|
37 |
legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sNpPwtghbc12345" ++ slotMessages |
6068 | 38 |
slotMessages = "\128\129\130\131\132\133\134\135\136\137\138" |
39 |
||
7766 | 40 |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
7766
diff
changeset
|
41 |
replayToDemo :: [TeamInfo] |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
7766
diff
changeset
|
42 |
-> Map.Map B.ByteString B.ByteString |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
7766
diff
changeset
|
43 |
-> Map.Map B.ByteString [B.ByteString] |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
7766
diff
changeset
|
44 |
-> [B.ByteString] |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
7766
diff
changeset
|
45 |
-> [B.ByteString] |
8480
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
46 |
replayToDemo teams mapParams params msgs = concat [ |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
47 |
[em "TD"] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
48 |
, maybeScript |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
49 |
, maybeMap |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
50 |
, [eml ["etheme ", head $ params Map.! "THEME"]] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
51 |
, [eml ["eseed ", mapParams Map.! "SEED"]] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
52 |
, [eml ["e$gmflags ", showB gameFlags]] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
53 |
, schemeFlags |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
54 |
, [eml ["e$template_filter ", mapParams Map.! "TEMPLATE"]] |
8481 | 55 |
, [eml ["e$mapgen ", mapgen]] |
56 |
, mapgenSpecific |
|
57 |
, concatMap teamSetup teams |
|
8480
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
58 |
, msgs |
8483 | 59 |
, [em "!"] |
8480
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
60 |
] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
61 |
where |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
62 |
em = toEngineMsg |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
63 |
eml = em . B.concat |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
64 |
mapGenTypes = ["+rnd+", "+maze+", "+drawn+"] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
65 |
maybeScript = let s = head $ params Map.! "SCRIPT" in if s == "Normal" then [] else [eml ["escript Scripts/Multiplayer/", s, ".lua"]] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
66 |
maybeMap = let m = mapParams Map.! "MAP" in if m `elem` mapGenTypes then [] else [eml ["emap ", m]] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
67 |
scheme = tail $ params Map.! "SCHEME" |
8481 | 68 |
mapgen = mapParams Map.! "MAPGEN" |
69 |
mapgenSpecific = case mapgen of |
|
70 |
"+maze+" -> [eml ["e$maze_size ", head $ params Map.! "MAZE_SIZE"]] |
|
71 |
"+drawn" -> drawnMapData . head $ params Map.! "DRAWNMAP" |
|
72 |
_ -> [] |
|
8480
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
73 |
gameFlags :: Word32 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
74 |
gameFlags = foldl (\r (b, f) -> if b == "false" then r else r .|. f) 0 $ zip scheme gameFlagConsts |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
75 |
schemeFlags = map (\(v, (n, m)) -> eml [n, " ", showB $ (readInt_ v) * m]) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
76 |
$ filter (\(_, (n, _)) -> not $ B.null n) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
77 |
$ zip (drop (length gameFlagConsts) scheme) schemeParams |
8481 | 78 |
ammoStr :: B.ByteString |
79 |
ammoStr = head . tail $ params Map.! "AMMO" |
|
80 |
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 |
|
8483 | 81 |
(map (\(x, y) -> eml [x, " ", y]) $ zip ["eammloadt", "eammprob", "eammdelay", "eammreinf"] [a, b, c, d]) |
82 |
++ [em "eammstore" | scheme !! 14 == "true" || scheme !! 20 == "false"] |
|
83 |
initHealth = scheme !! 27 |
|
8481 | 84 |
teamSetup :: TeamInfo -> [B.ByteString] |
8483 | 85 |
teamSetup t = |
86 |
eml ["eaddteam ", teamcolor t, " ", teamowner t, " <hash>"] |
|
87 |
: em "erdriven" |
|
88 |
: eml ["efort ", teamfort t] |
|
89 |
: replicate (hhnum t) (eml ["eaddhh 0 ", initHealth, " hedgehog"]) |
|
8480
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
90 |
|
8481 | 91 |
drawnMapData :: B.ByteString -> [B.ByteString] |
8483 | 92 |
drawnMapData = error "drawnMapData" |
8480
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
93 |
|
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
94 |
schemeParams :: [(B.ByteString, Int)] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
95 |
schemeParams = [ |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
96 |
("e$damagepct", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
97 |
, ("e$turntime", 1000) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
98 |
, ("", 0) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
99 |
, ("e$sd_turns", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
100 |
, ("e$casefreq", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
101 |
, ("e$minestime", 1000) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
102 |
, ("e$minesnum", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
103 |
, ("e$minedudpct", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
104 |
, ("e$explosives", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
105 |
, ("e$healthprob", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
106 |
, ("e$hcaseamount", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
107 |
, ("e$waterrise", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
108 |
, ("e$healthdec", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
109 |
, ("e$ropepct", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
110 |
, ("e$getawaytime", 1) |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
111 |
] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
112 |
|
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
113 |
|
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
114 |
gameFlagConsts :: [Word32] |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
115 |
gameFlagConsts = [ |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
116 |
0x00001000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
117 |
, 0x00000010 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
118 |
, 0x00000004 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
119 |
, 0x00000008 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
120 |
, 0x00000020 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
121 |
, 0x00000040 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
122 |
, 0x00000080 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
123 |
, 0x00000100 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
124 |
, 0x00000200 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
125 |
, 0x00000400 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
126 |
, 0x00000800 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
127 |
, 0x00002000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
128 |
, 0x00004000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
129 |
, 0x00008000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
130 |
, 0x00010000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
131 |
, 0x00020000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
132 |
, 0x00040000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
133 |
, 0x00080000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
134 |
, 0x00100000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
135 |
, 0x00200000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
136 |
, 0x00400000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
137 |
, 0x00800000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
138 |
, 0x01000000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
139 |
, 0x02000000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
140 |
, 0x04000000 |
42d2565b5700
Converter from game parameters to engine commands, not finished yet
unc0rr
parents:
8479
diff
changeset
|
141 |
] |
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
7766
diff
changeset
|
142 |
|
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
7766
diff
changeset
|
143 |
|
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
7766
diff
changeset
|
144 |