gameServer/EngineInteraction.hs
author unc0rr
Sat, 21 Nov 2015 17:07:06 +0300
branchqmlfrontend
changeset 11423 e045dc60c37e
parent 11355 d94f65b66c60
child 11547 2bbbe9cb2980
permissions -rw-r--r--
- Warnings/errors message box - Send JOIN_ROOM on double click on rooms list - Some small iprovements more
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10460
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
     1
{-
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
     2
 * Hedgewars, a free turn based strategy game
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 11031
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
10460
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
     4
 *
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
     8
 *
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
    12
 * GNU General Public License for more details.
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
    13
 *
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
    14
 * You should have received a copy of the GNU General Public License
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
    15
 * along with this program; if not, write to the Free Software
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
    16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
    17
 \-}
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 10343
diff changeset
    18
10983
6aba07ea908b Lazy fix for server build
unc0rr
parents: 10779
diff changeset
    19
{-# LANGUAGE CPP, OverloadedStrings #-}
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
    20
10983
6aba07ea908b Lazy fix for server build
unc0rr
parents: 10779
diff changeset
    21
#if defined(OFFICIAL_SERVER)
10027
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
    22
module EngineInteraction(replayToDemo, checkNetCmd, toEngineMsg, drawnMapData) where
10983
6aba07ea908b Lazy fix for server build
unc0rr
parents: 10779
diff changeset
    23
#else
10984
c828cabe33af More ifdefs \o/ (fix build even more)
unc0rr
parents: 10983
diff changeset
    24
module EngineInteraction(checkNetCmd, toEngineMsg) where
10983
6aba07ea908b Lazy fix for server build
unc0rr
parents: 10779
diff changeset
    25
#endif
6068
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    26
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    27
import qualified Data.Set as Set
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    28
import Control.Monad
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    29
import qualified Codec.Binary.Base64 as Base64
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    30
import qualified Data.ByteString.Char8 as B
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    31
import qualified Data.ByteString as BW
10027
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
    32
import qualified Data.ByteString.Lazy as BL
8479
8d71109b04d2 Some work on loading replay and interaction with checker
unc0rr
parents: 7766
diff changeset
    33
import qualified Data.Map as Map
8484
99c14f14f788 New checker of engine messages which is aware of glued together messages
unc0rr
parents: 8483
diff changeset
    34
import qualified Data.List as L
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
    35
import Data.Word
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
    36
import Data.Bits
8481
692ff6468b63 - Handle mapgen-specific parameters
unc0rr
parents: 8480
diff changeset
    37
import Control.Arrow
9690
6a1748b71df2 Handle absence of "SCRIPT" in replay
unc0rr
parents: 9401
diff changeset
    38
import Data.Maybe
6069
d59745e525ec GameInfo needs room params copy
unc0rr
parents: 6068
diff changeset
    39
-------------
d59745e525ec GameInfo needs room params copy
unc0rr
parents: 6068
diff changeset
    40
import CoreTypes
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
    41
import Utils
6068
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    42
10984
c828cabe33af More ifdefs \o/ (fix build even more)
unc0rr
parents: 10983
diff changeset
    43
#if defined(OFFICIAL_SERVER)
10055
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    44
{-
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    45
    this is snippet from http://stackoverflow.com/questions/10043102/how-to-catch-the-decompress-ioerror
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    46
    because standard 'catch' doesn't seem to catch decompression errors for some reason
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    47
-}
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    48
import qualified Codec.Compression.Zlib.Internal as Z
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    49
10060
bcf2e1ca2971 Use zlibFormat
unc0rr
parents: 10058
diff changeset
    50
decompressWithoutExceptions :: BL.ByteString -> Either String BL.ByteString
10055
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    51
decompressWithoutExceptions = finalise
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    52
                            . Z.foldDecompressStream cons nil err
10060
bcf2e1ca2971 Use zlibFormat
unc0rr
parents: 10058
diff changeset
    53
                            . Z.decompressWithErrors Z.zlibFormat Z.defaultDecompressParams
10065
35bd559c1973 Some basic sanity tests for replay files
unc0rr
parents: 10060
diff changeset
    54
  where err _ msg = Left msg
10055
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    55
        nil = Right []
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    56
        cons chunk = right (chunk :)
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    57
        finalise = right BL.fromChunks
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
    58
{- end snippet  -}
10984
c828cabe33af More ifdefs \o/ (fix build even more)
unc0rr
parents: 10983
diff changeset
    59
#endif
6068
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    60
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    61
toEngineMsg :: B.ByteString -> B.ByteString
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    62
toEngineMsg msg = B.pack $ Base64.encode (fromIntegral (BW.length msg) : BW.unpack msg)
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    63
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    64
10058
4ed428389c4e - Implement /callvote
unc0rr
parents: 10055
diff changeset
    65
{-fromEngineMsg :: B.ByteString -> Maybe B.ByteString
6068
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    66
fromEngineMsg msg = liftM BW.pack (Base64.decode (B.unpack msg) >>= removeLength)
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    67
    where
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    68
        removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing
10058
4ed428389c4e - Implement /callvote
unc0rr
parents: 10055
diff changeset
    69
        removeLength _ = Nothing-}
6068
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    70
10027
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
    71
em :: B.ByteString -> B.ByteString
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
    72
em = toEngineMsg
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
    73
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
    74
eml :: [B.ByteString] -> B.ByteString
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
    75
eml = em . B.concat
6068
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    76
8484
99c14f14f788 New checker of engine messages which is aware of glued together messages
unc0rr
parents: 8483
diff changeset
    77
splitMessages :: B.ByteString -> [B.ByteString]
99c14f14f788 New checker of engine messages which is aware of glued together messages
unc0rr
parents: 8483
diff changeset
    78
splitMessages = L.unfoldr (\b -> if B.null b then Nothing else Just $ B.splitAt (1 + fromIntegral (BW.head b)) b)
99c14f14f788 New checker of engine messages which is aware of glued together messages
unc0rr
parents: 8483
diff changeset
    79
99c14f14f788 New checker of engine messages which is aware of glued together messages
unc0rr
parents: 8483
diff changeset
    80
9304
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
    81
checkNetCmd :: B.ByteString -> (B.ByteString, B.ByteString, Maybe (Maybe B.ByteString))
6068
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    82
checkNetCmd msg = check decoded
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    83
    where
8485
7cae79214537 Fix the checker after some testing
unc0rr
parents: 8484
diff changeset
    84
        decoded = liftM (splitMessages . BW.pack) $ Base64.decode $ B.unpack msg
9304
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
    85
        check Nothing = (B.empty, B.empty, Nothing)
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
    86
        check (Just msgs) = let (a, b) = (filter isLegal msgs, filter isNonEmpty a) in (encode a, encode b, lft a)
8484
99c14f14f788 New checker of engine messages which is aware of glued together messages
unc0rr
parents: 8483
diff changeset
    87
        encode = B.pack . Base64.encode . BW.unpack . B.concat
8485
7cae79214537 Fix the checker after some testing
unc0rr
parents: 8484
diff changeset
    88
        isLegal m = (B.length m > 1) && (flip Set.member legalMessages . B.head . B.tail $ m)
9304
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
    89
        lft = foldr l Nothing
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9690
diff changeset
    90
        l m n = let m' = B.head $ B.tail m; tst = flip Set.member in
9304
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
    91
                      if not $ tst timedMessages m' then n
9401
2af7bea32e5e - Some fixes to official server build
unc0rr
parents: 9304
diff changeset
    92
                        else if '+' /= m' then Just Nothing else Just . Just . B.pack . Base64.encode . BW.unpack $ m
8506
3889dab021b8 - Fix check for void message
unc0rr
parents: 8503
diff changeset
    93
        isNonEmpty = (/=) '+' . B.head . B.tail
10343
fe9853dea6c4 Finish implementation of ability to take control over your team after being disconnected. Completely untested.
unc0rr
parents: 10067
diff changeset
    94
        legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sNpPwtgfhbc12345" ++ slotMessages
6068
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    95
        slotMessages = "\128\129\130\131\132\133\134\135\136\137\138"
10343
fe9853dea6c4 Finish implementation of ability to take control over your team after being disconnected. Completely untested.
unc0rr
parents: 10067
diff changeset
    96
        timedMessages = Set.fromList $ "+LlRrUuDdZzAaSjJ,NpPwtgfc12345" ++ slotMessages
6068
e18713ecf1e0 Introduce EngineInteraction module
unc0rr
parents:
diff changeset
    97
10983
6aba07ea908b Lazy fix for server build
unc0rr
parents: 10779
diff changeset
    98
#if defined(OFFICIAL_SERVER)
8479
8d71109b04d2 Some work on loading replay and interaction with checker
unc0rr
parents: 7766
diff changeset
    99
replayToDemo :: [TeamInfo]
8d71109b04d2 Some work on loading replay and interaction with checker
unc0rr
parents: 7766
diff changeset
   100
        -> Map.Map B.ByteString B.ByteString
8d71109b04d2 Some work on loading replay and interaction with checker
unc0rr
parents: 7766
diff changeset
   101
        -> Map.Map B.ByteString [B.ByteString]
8d71109b04d2 Some work on loading replay and interaction with checker
unc0rr
parents: 7766
diff changeset
   102
        -> [B.ByteString]
11320
556eafd1443a Store some more details on game config in the database
unc0rr
parents: 11246
diff changeset
   103
        -> (Maybe GameDetails, [B.ByteString])
556eafd1443a Store some more details on game config in the database
unc0rr
parents: 11246
diff changeset
   104
replayToDemo ti mParams prms msgs = if not sane then (Nothing, []) else (Just $ GameDetails scriptName infRopes vamp infattacks, concat [
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   105
        [em "TD"]
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   106
        , maybeScript
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   107
        , maybeMap
9304
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
   108
        , [eml ["etheme ", head $ prms Map.! "THEME"]]
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
   109
        , [eml ["eseed ", mParams Map.! "SEED"]]
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   110
        , [eml ["e$gmflags ", showB gameFlags]]
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   111
        , schemeFlags
10740
c9db53de9c3b Generate config for 'world edge' and 'script param' scheme options
unc0rr
parents: 10460
diff changeset
   112
        , schemeAdditional
9304
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
   113
        , [eml ["e$template_filter ", mParams Map.! "TEMPLATE"]]
10755
f78a5ca6e0af Include feature_size to demos for checker
unc0rr
parents: 10740
diff changeset
   114
        , [eml ["e$feature_size ", mParams Map.! "FEATURE_SIZE"]]
8481
692ff6468b63 - Handle mapgen-specific parameters
unc0rr
parents: 8480
diff changeset
   115
        , [eml ["e$mapgen ", mapgen]]
692ff6468b63 - Handle mapgen-specific parameters
unc0rr
parents: 8480
diff changeset
   116
        , mapgenSpecific
9304
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
   117
        , concatMap teamSetup ti
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   118
        , msgs
8483
d5fd4d7a0bcc Also convert teams to complete game config.
unc0rr
parents: 8481
diff changeset
   119
        , [em "!"]
11246
09a2d3988569 Also pass script information alongwith winner/achievements info, so that we could potentially have per mode ratings
unc0rr
parents: 11046
diff changeset
   120
        ])
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   121
    where
10065
35bd559c1973 Some basic sanity tests for replay files
unc0rr
parents: 10060
diff changeset
   122
        keys1, keys2 :: Set.Set B.ByteString
10755
f78a5ca6e0af Include feature_size to demos for checker
unc0rr
parents: 10740
diff changeset
   123
        keys1 = Set.fromList ["FEATURE_SIZE", "MAP", "MAPGEN", "MAZE_SIZE", "SEED", "TEMPLATE"]
10066
1a6e1aad58d6 - Add SCHEME which is needed in room config
unc0rr
parents: 10065
diff changeset
   124
        keys2 = Set.fromList ["AMMO", "SCHEME", "SCRIPT", "THEME"]
10067
8df84ec62600 Must not code at late night (fix sanity check)
unc0rr
parents: 10066
diff changeset
   125
        sane = Set.null (keys1 Set.\\ Map.keysSet mParams)
8df84ec62600 Must not code at late night (fix sanity check)
unc0rr
parents: 10066
diff changeset
   126
            && Set.null (keys2 Set.\\ Map.keysSet prms)
10760
fcef7d62d3c4 Eh, faulty check
unc0rr
parents: 10755
diff changeset
   127
            && (not . null . drop 41 $ scheme)
11321
71a46315c2d4 Fix replay sanity check
unc0rr
parents: 11320
diff changeset
   128
            && (not . null . tail $ prms Map.! "AMMO")
11328
2400c9496288 Oops, fix the fix
unc0rr
parents: 11321
diff changeset
   129
            && ((B.length . head . tail $ prms Map.! "AMMO") > 200)
10779
d85fe0cb5df2 Fix handling of perlin mapgen
unc0rr
parents: 10777
diff changeset
   130
        mapGenTypes = ["+rnd+", "+maze+", "+drawn+", "+perlin+"]
11246
09a2d3988569 Also pass script information alongwith winner/achievements info, so that we could potentially have per mode ratings
unc0rr
parents: 11046
diff changeset
   131
        scriptName = head . fromMaybe ["Normal"] $ Map.lookup "SCRIPT" prms
11337
41ca5f8ace18 Pass correct script name for scripts with spaces in name
unc0rr
parents: 11328
diff changeset
   132
        maybeScript = let s = scriptName in if s == "Normal" then [] else [eml ["escript Scripts/Multiplayer/", spaces2Underlining s, ".lua"]]
9304
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
   133
        maybeMap = let m = mParams Map.! "MAP" in if m `elem` mapGenTypes then [] else [eml ["emap ", m]]
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
   134
        scheme = tail $ prms Map.! "SCHEME"
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
   135
        mapgen = mParams Map.! "MAPGEN"
11031
4ad0252470dd Fixes to 10760 to reduce desyncs on checkers and better heat my room during long dark winter nights
unc0rr
parents: 10984
diff changeset
   136
        mazeSizeMsg = eml ["e$maze_size ", mParams Map.! "MAZE_SIZE"]
8481
692ff6468b63 - Handle mapgen-specific parameters
unc0rr
parents: 8480
diff changeset
   137
        mapgenSpecific = case mapgen of
11031
4ad0252470dd Fixes to 10760 to reduce desyncs on checkers and better heat my room during long dark winter nights
unc0rr
parents: 10984
diff changeset
   138
            "1" -> [mazeSizeMsg]
4ad0252470dd Fixes to 10760 to reduce desyncs on checkers and better heat my room during long dark winter nights
unc0rr
parents: 10984
diff changeset
   139
            "2" -> [mazeSizeMsg]
10777
18afcfaa4d5a - Send maze_size command in other mapgen modes too
unc0rr
parents: 10760
diff changeset
   140
            "3" -> let d = head . fromMaybe [""] $ Map.lookup "DRAWNMAP" prms in if BW.length d <= 4 then [] else drawnMapData d
11031
4ad0252470dd Fixes to 10760 to reduce desyncs on checkers and better heat my room during long dark winter nights
unc0rr
parents: 10984
diff changeset
   141
            _ -> []
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   142
        gameFlags :: Word32
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   143
        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
   144
        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
   145
            $ filter (\(_, (n, _)) -> not $ B.null n)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   146
            $ zip (drop (length gameFlagConsts) scheme) schemeParams
10740
c9db53de9c3b Generate config for 'world edge' and 'script param' scheme options
unc0rr
parents: 10460
diff changeset
   147
        schemeAdditional = let scriptParam = B.tail $ scheme !! 41 in [eml ["e$scriptparam ", scriptParam] | not $ B.null scriptParam]
8481
692ff6468b63 - Handle mapgen-specific parameters
unc0rr
parents: 8480
diff changeset
   148
        ammoStr :: B.ByteString
9304
3f4c3fc146c2 Fix spectator desync in rare conditions (there was team which left during its turn, and last command with timestamp from it was '+')
unc0rr
parents: 8541
diff changeset
   149
        ammoStr = head . tail $ prms Map.! "AMMO"
8481
692ff6468b63 - Handle mapgen-specific parameters
unc0rr
parents: 8480
diff changeset
   150
        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
d5fd4d7a0bcc Also convert teams to complete game config.
unc0rr
parents: 8481
diff changeset
   151
                   (map (\(x, y) -> eml [x, " ", y]) $ zip ["eammloadt", "eammprob", "eammdelay", "eammreinf"] [a, b, c, d])
d5fd4d7a0bcc Also convert teams to complete game config.
unc0rr
parents: 8481
diff changeset
   152
                   ++ [em "eammstore" | scheme !! 14 == "true" || scheme !! 20 == "false"]
d5fd4d7a0bcc Also convert teams to complete game config.
unc0rr
parents: 8481
diff changeset
   153
        initHealth = scheme !! 27
8481
692ff6468b63 - Handle mapgen-specific parameters
unc0rr
parents: 8480
diff changeset
   154
        teamSetup :: TeamInfo -> [B.ByteString]
8499
da5394a3eb0e - Add forgotten ammo definitions
unc0rr
parents: 8496
diff changeset
   155
        teamSetup t = (++) ammo $
8541
0cd63b963330 Try to prevent hedgehogs number in team desync
unc0rr
parents: 8527
diff changeset
   156
                eml ["eaddteam <hash> ", showB $ (1 + (readInt_ $ teamcolor t) :: Int) * 2113696, " ", teamname t]
8483
d5fd4d7a0bcc Also convert teams to complete game config.
unc0rr
parents: 8481
diff changeset
   157
                : em "erdriven"
d5fd4d7a0bcc Also convert teams to complete game config.
unc0rr
parents: 8481
diff changeset
   158
                : eml ["efort ", teamfort t]
8527
bf671ddf467c Fix stupid mistake which made checker desync almost always
unc0rr
parents: 8506
diff changeset
   159
                : take (2 * hhnum t) (
8503
e60c84b42f4d Pass hogs names and hats
unc0rr
parents: 8499
diff changeset
   160
                    concatMap (\(HedgehogInfo hname hhat) -> [
e60c84b42f4d Pass hogs names and hats
unc0rr
parents: 8499
diff changeset
   161
                            eml ["eaddhh ", showB $ difficulty t, " ", initHealth, " ", hname]
e60c84b42f4d Pass hogs names and hats
unc0rr
parents: 8499
diff changeset
   162
                            , eml ["ehat ", hhat]
e60c84b42f4d Pass hogs names and hats
unc0rr
parents: 8499
diff changeset
   163
                            ])
e60c84b42f4d Pass hogs names and hats
unc0rr
parents: 8499
diff changeset
   164
                        $ hedgehogs t
e60c84b42f4d Pass hogs names and hats
unc0rr
parents: 8499
diff changeset
   165
                        )
11320
556eafd1443a Store some more details on game config in the database
unc0rr
parents: 11246
diff changeset
   166
        infRopes = ammoStr `B.index` 7  == '9'
556eafd1443a Store some more details on game config in the database
unc0rr
parents: 11246
diff changeset
   167
        vamp = gameFlags .&. 0x00000200 /= 0
556eafd1443a Store some more details on game config in the database
unc0rr
parents: 11246
diff changeset
   168
        infattacks = gameFlags .&. 0x00100000 /= 0
11337
41ca5f8ace18 Pass correct script name for scripts with spaces in name
unc0rr
parents: 11328
diff changeset
   169
        spaces2Underlining = B.map (\c -> if c == ' ' then '_' else c)
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   170
8481
692ff6468b63 - Handle mapgen-specific parameters
unc0rr
parents: 8480
diff changeset
   171
drawnMapData :: B.ByteString -> [B.ByteString]
10040
koda
parents: 10017 10034
diff changeset
   172
drawnMapData =
10027
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   173
          L.map (\m -> eml ["edraw ", BW.pack m])
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   174
        . L.unfoldr by200
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   175
        . BL.unpack
10055
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
   176
        . either (const BL.empty) id
f738693be9be More robust implementation of decompression
unc0rr
parents: 10053
diff changeset
   177
        . decompressWithoutExceptions
10027
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   178
        . BL.pack
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   179
        . L.drop 4
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   180
        . fromMaybe []
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   181
        . Base64.decode
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   182
        . B.unpack
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   183
    where
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   184
        by200 :: [a] -> Maybe ([a], [a])
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   185
        by200 [] = Nothing
403b86a1d05f Implement drawnMapData function so checker could check replays with drawn maps
unc0rr
parents: 9690
diff changeset
   186
        by200 m = Just $ L.splitAt 200 m
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   187
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   188
schemeParams :: [(B.ByteString, Int)]
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   189
schemeParams = [
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   190
      ("e$damagepct", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   191
    , ("e$turntime", 1000)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   192
    , ("", 0)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   193
    , ("e$sd_turns", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   194
    , ("e$casefreq", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   195
    , ("e$minestime", 1000)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   196
    , ("e$minesnum", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   197
    , ("e$minedudpct", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   198
    , ("e$explosives", 1)
11355
d94f65b66c60 Oops, zero multiplier
unc0rr
parents: 11345
diff changeset
   199
    , ("e$airmines", 1)
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   200
    , ("e$healthprob", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   201
    , ("e$hcaseamount", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   202
    , ("e$waterrise", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   203
    , ("e$healthdec", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   204
    , ("e$ropepct", 1)
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   205
    , ("e$getawaytime", 1)
10740
c9db53de9c3b Generate config for 'world edge' and 'script param' scheme options
unc0rr
parents: 10460
diff changeset
   206
    , ("e$worldedge", 1)
8480
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   207
    ]
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   208
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   209
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   210
gameFlagConsts :: [Word32]
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   211
gameFlagConsts = [
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   212
          0x00001000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   213
        , 0x00000010
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   214
        , 0x00000004
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   215
        , 0x00000008
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   216
        , 0x00000020
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   217
        , 0x00000040
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   218
        , 0x00000080
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   219
        , 0x00000100
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   220
        , 0x00000200
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   221
        , 0x00000400
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   222
        , 0x00000800
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   223
        , 0x00002000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   224
        , 0x00004000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   225
        , 0x00008000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   226
        , 0x00010000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   227
        , 0x00020000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   228
        , 0x00040000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   229
        , 0x00080000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   230
        , 0x00100000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   231
        , 0x00200000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   232
        , 0x00400000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   233
        , 0x00800000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   234
        , 0x01000000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   235
        , 0x02000000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   236
        , 0x04000000
42d2565b5700 Converter from game parameters to engine commands, not finished yet
unc0rr
parents: 8479
diff changeset
   237
        ]
10984
c828cabe33af More ifdefs \o/ (fix build even more)
unc0rr
parents: 10983
diff changeset
   238
#endif