StdGame.icl 14.6 KB
Newer Older
Peter Achten's avatar
Peter Achten committed
1
2
implementation module StdGame

3
import StdProcess
4
import	StdArray, StdBool, StdClass, StdFunc, StdInt, StdList, StdMisc
Peter Achten's avatar
Peter Achten committed
5
6
7
8
9
10
11
12
import	StdId
import	fixed, GameFunctions, gamehandle, gameutils, gst
from	gameobjectutils	import toBoundMapCode, fromBoundMapCode
from	StdPSt			import appPIO, accPIO
from	iostate			import setIOToolbox, getIOToolbox
import	windowcreate, windowdevice
from	windowaccess	import initWindowHandle
from	windowvalidate	import validateWindowId
Peter Achten's avatar
Peter Achten committed
13
from	ostypes			import OSNoWindowPtr
Peter Achten's avatar
Peter Achten committed
14
15
16


/* predefined bounds */
17
18
BND_MAP_CODES      :== 1 << 30
BND_STATIC_BOUNDS  :== 1 << 31
Peter Achten's avatar
Peter Achten committed
19
20

/* skipmove constant */
21
SK_FOREVER         :== -1
Peter Achten's avatar
Peter Achten committed
22
23


24
25
26
27
28
29
30
31
32
33
startGame :: .(Game a) a [.GameAttribute a] !*World -> .World
startGame gamedef initialstate options world
    = startIO SDI 0 init [ProcessClose closeProcess] world
where
    init ps
        #   (finalstate, _, ps) = openGame initialstate gamedef options ps
        =   closeProcess ps


openGame :: .gs !(Game .gs) ![GameAttribute .gs] !(PSt .l) -> (.gs, !ErrorReport, !PSt .l)
34
openGame gs gdef attr ps
Peter Achten's avatar
Peter Achten committed
35
36
37
38
39
40
41
42
    #   (wId, ps)       =   accPIO openId ps
    #   size            =   findSize attr {w=320,h=240}
    #   bpp             =   findBPP attr 8
    #   (_, ps)         =   OpenGameWindow wId size bpp True ps
    #   (tb,ps)         =   accPIO getIOToolbox ps
    #   gst             =   toGSt gs tb
    #   (initLevel,gst) =   gdef.nextlevel gst
    #   (gs,tb)         =   fromGSt gst
43
    #   (gs, _, tb)     =   PlayLevels initLevel gs gdef tb
Peter Achten's avatar
Peter Achten committed
44
    #   ps              =   appPIO (setIOToolbox tb) ps
45
    =   (gs, NoError, ps)
Peter Achten's avatar
Peter Achten committed
46
where
47
    findSize :: ![GameAttribute .gs] !Size -> Size
Peter Achten's avatar
Peter Achten committed
48
    findSize [] s = s
49
    findSize [ScreenSize x:xs] s = x
Peter Achten's avatar
Peter Achten committed
50
    findSize [x:xs] s = findSize xs s
51
    findBPP :: ![GameAttribute .gs] !Int -> Int
Peter Achten's avatar
Peter Achten committed
52
    findBPP [] s = s
53
    findBPP [ColorDepth x:xs] s = x
Peter Achten's avatar
Peter Achten committed
54
55
56
    findBPP [x:xs] s = findBPP xs s

    // always full screen, game in a window not implemented yet
57
	OpenGameWindow :: !Id !Size !Int !Bool !(PSt .l) -> (!ErrorReport, !PSt .l)
Peter Achten's avatar
Peter Achten committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
	OpenGameWindow id gamewindowsize bitsperpixel fullscreen pState
		# pState				= WindowFunctions.dOpen pState
		# (isZero,pState)		= accPIO checkZeroWindowBound pState
		| isZero
			= (ErrorViolateDI,pState)
		# maybe_id				= Just id
		# (maybe_okId,ioState)	= validateWindowId maybe_id pState.io
		| isNothing maybe_okId
			= (ErrorIdsInUse,{pState & io=ioState})
		| otherwise
			# pState			= {pState & io=ioState}
			  info				= {	gamewindowDDPtr      = OSNoWindowPtr
			  					  ,	gamewindowCDepth     = bitsperpixel
			  					  ,	gamewindowSize       = gamewindowsize
			  					  ,	gamewindowFullScreen = fullscreen
			  					  }
			  okId				= fromJust maybe_okId
			# wH				= initWindowHandle "" Modeless IsGameWindow (GameWindowInfo info) [] [WindowId okId]
			# pState			= openwindow okId {wlsState=undef, wlsHandle=wH} pState
			# pState			= appPIO decreaseWindowBound pState
			= (NoError,pState)

80
PlayLevels :: !Int .gs !(Game .gs) !*OSToolbox -> (.gs, !ErrorReport, !*OSToolbox)
Peter Achten's avatar
Peter Achten committed
81
82
PlayLevels level gs gdef tb
    |   level == 0
83
    =   (gs, NoError, tb)
Peter Achten's avatar
Peter Achten committed
84
85
86
87
88
89
90
    #   ghnd             =  createGameHandle gdef
    #   (_, gs, tb)      =  PlayLevel level gs ghnd tb
    #   gst              =  toGSt gs tb
    #   (nextlevel, gst) =  gdef.nextlevel gst
    #   (gs,tb)          =  fromGSt gst
    =   PlayLevels nextlevel gs gdef tb

91
FindMaxID :: a ![a] -> a  | < a
Peter Achten's avatar
Peter Achten committed
92
93
94
95
96
FindMaxID x [] = x
FindMaxID x [y:ys]
    |   y > x       =   FindMaxID y ys
    |   otherwise   =   FindMaxID x ys

97
InitLayers :: ![Layer] ![BID] ![MAPID] !*OSToolbox -> (![BID], ![MAPID], !*OSToolbox)
Peter Achten's avatar
Peter Achten committed
98
99
100
101
102
103
InitLayers [] bids mapids tb    =   (bids, mapids, tb)
InitLayers [l:ls] bids mapids tb
    #   (bids, mapids, tb)      =   InitLayer l bids mapids tb
    =   InitLayers ls bids mapids tb


104
MaybeSetTransparentColor :: !BID !(Maybe Point2) !*OSToolbox -> (!GRESULT, !*OSToolbox)
Peter Achten's avatar
Peter Achten committed
105
106
107
MaybeSetTransparentColor _ Nothing tb = (GR_OK, tb)
MaybeSetTransparentColor bid (Just p) tb = SetTransparentColor bid p tb

108
MovementFunctions :: ![Layer] -> [Movement]
Peter Achten's avatar
Peter Achten committed
109
MovementFunctions [] = []
110
MovementFunctions [l:ls] = [l.movement] ++ MovementFunctions ls
Peter Achten's avatar
Peter Achten committed
111

112
InitLayer :: !Layer ![BID] ![MAPID] !*OSToolbox -> (![BID], ![MAPID], !*OSToolbox)
Peter Achten's avatar
Peter Achten committed
113
114
115
116
117
118
119
120
121
122
InitLayer l bids mapids tb
    #   (newbid, tb)    =   InitGameBitmap 0 b.bitmapname (us.w * nh) (us.h * nv) us.w us.h tb  // newbid
    #   (_, tb)         =   MaybeSetTransparentColor newbid b.transparent tb
    #   tb              =   InitBlockSequences newbid l.sequences tb
    #   (_, tb)         =   InitGameLayerMap newmapid newbid l.layermap True tb     // l.tile
    =   (bids++[newbid], mapids++[newmapid], tb)
where
    b           =   l.bmp
    us          =   b.unitsize
    (nh, nv)    =   b.dimensions
123
    newmapid    =   (FindMaxID 0 mapids) + 1
Peter Achten's avatar
Peter Achten committed
124

125
InitBlockSequences :: !BID ![TileSequence] !*OSToolbox -> !*OSToolbox
Peter Achten's avatar
Peter Achten committed
126
127
128
129
130
InitBlockSequences bid [] tb = tb
InitBlockSequences bid [s:ss] tb
    #   (_, tb) =   InitBlockSequence bid s tb
    =   InitBlockSequences bid ss tb

131
LayersDone :: ![BID] ![MAPID] !*OSToolbox -> !*OSToolbox
Peter Achten's avatar
Peter Achten committed
132
133
134
135
136
LayersDone bids mapids tb
    #   tb      =   MapsDone mapids tb
    #   tb      =   BitmapsDone bids tb
    =   tb

137
MapsDone :: ![MAPID] !*OSToolbox -> !*OSToolbox
Peter Achten's avatar
Peter Achten committed
138
139
140
141
142
MapsDone [] tb = tb
MapsDone [m:ms] tb
    #   (_, tb)     =   GameLayerMapDone m tb
    =   MapsDone ms tb

143
BitmapsDone :: ![BID] !*OSToolbox -> !*OSToolbox
Peter Achten's avatar
Peter Achten committed
144
145
146
147
148
BitmapsDone [] tb = tb
BitmapsDone [b:bs] tb
    #   (_, tb)     =   GameBitmapDone b tb
    =   BitmapsDone bs tb

149
PlayLevel :: !Int .gs !(GameHandle .gs) !*OSToolbox -> (!ErrorReport,.gs,!*OSToolbox)
Peter Achten's avatar
Peter Achten committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
PlayLevel levelnumber gs gamehnd tb
    #   (_, tb)             =   SetGameBoundMap wid ht bm os stx sty  tb
    #   (_, tb)             =   MoveScreenTo curLevelHnd.initpos` tb
    #   lyrs                =   curLevelHnd.layers`
    #   (bids, mapids, tb)  =   InitLayers lyrs [] [] tb
    #   movements           =   zip2 mapids (MovementFunctions lyrs)
    #   (_, tb)             =   initsoundsamples curLevelHnd.soundsamples` tb
    #   tb                  =   maybePlayMusic curLevelHnd.music` tb
    #   gst                 =   toGSt gs tb
    #   firstlevel          =   curLevelHnd
    #   (obj, gst)          =   convertallobjsprites firstlevel.objects` gst
    #   firstlevel          =   {firstlevel & objects` = obj}
    #   curgamehnd          =   {gamehnd & levels` = [firstlevel]}
    #   (gs,tb)             =   fromGSt gst
    #   (_, tb)             =   OSGameLevelOptions fill rgb esc dbg fdin fdout tb
165
166
167
    #   (gs, tb)            =   RunGameEngine { scroll  = movements
                                              , gamest  = gs
                                              , gamehnd = curgamehnd} tb
Peter Achten's avatar
Peter Achten committed
168
    #   tb                  =   maybeStopMusic curLevelHnd.music` tb
169
    #   (_, tb)             =   OSInitSoundSample (-1) "" 0 tb  // remove samples
Peter Achten's avatar
Peter Achten committed
170
171
172
173
174
175
176
177
178
179
180
181
    #   tb                  =   LayersDone bids mapids tb
    #   (_, tb)             =   ClearAllGameBitmaps tb
    =   (NoError, gs, tb)
where
    curLevelHnd         =   gamehnd.levels`!!(levelnumber-1)
    options             =   curLevelHnd.leveloptions`
    { map = bm,
      blocksize = bs,
      objstart = os,
      startobjx = stx,
      startobjy = sty}  =   curLevelHnd.boundmap`
    { w = wid, h = ht } =   bs
182
    maybePlayMusic :: !(Maybe Music) !*OSToolbox -> !*OSToolbox
Peter Achten's avatar
Peter Achten committed
183
184
185
186
    maybePlayMusic Nothing tb = tb
    maybePlayMusic (Just m) tb
        #   (_, tb)     =   PlayMusic m.musicfile m.restart tb
        = tb
187
    maybeStopMusic :: !(Maybe Music) !*OSToolbox -> !*OSToolbox
Peter Achten's avatar
Peter Achten committed
188
189
190
191
192
193
194
195
196
197
    maybeStopMusic Nothing tb = tb
    maybeStopMusic (Just m) tb
        |   m.continue
            = tb
        #   (_, tb)     =   StopMusic tb
        = tb
    esc = options.escquit
    dbg = options.debugscroll
    fdin = options.fadein
    fdout = options.fadeout
198
//  rgb :: Colour
Peter Achten's avatar
Peter Achten committed
199
200
    rgb = if fill
             (fromJust options.fillbackground)
201
202
             (RGB {r= -1, g= -1, b= -1})
    fill = isJust options.fillbackground
Peter Achten's avatar
Peter Achten committed
203
204
205
206
207
208
209
210
211
212
213
214
215

initsoundsamples sndlist gs
    = map2 initsoundsample sndlist gs

initsoundsample sample gs
    = OSInitSoundSample sample.soundid sample.soundfile sample.soundbuffers gs

convertallobjsprites obj gst = map2 convertobjsprites obj gst
where
    convertobjsprites obj gst
        # (sprids, gst) = convertsprites obj.sprites` gst
        = ({obj & spriteids` = sprids}, gst)

216
convertsprites :: ![Sprite] !(GSt .gs) -> (![SpriteID], !GSt .gs)
Peter Achten's avatar
Peter Achten committed
217
convertsprites spr gst
218
219
    # (idlst, gst) = map2 createAnimation spr gst
    # idlst        = map (\x->0-x)/*(~)*/ idlst
Peter Achten's avatar
Peter Achten committed
220
221
222
    = (idlst, gst)


223
224
createGameBitmap :: !GameBitmap !(GSt .gs) -> (!GRESULT, !GSt .gs)
createGameBitmap bitmap=:{bitmapname, unitsize, dimensions, transparent} gst
Peter Achten's avatar
Peter Achten committed
225
226
227
228
229
230
231
232
233
    #   (bid, gst) = accGStTb (InitGameBitmap 0 bitmapname (w * nh) (h * nv) w h) gst
    #   (_, gst)   = accGStTb (MaybeSetTransparentColor bid transparent) gst
    =   (bid, gst)
where
    w       = unitsize.w
    h       = unitsize.h
    (nh,nv) = dimensions


234
235
236
createAnimation :: !Sprite !(GSt .gs) -> (!GRESULT, !GSt .gs)
createAnimation sprite=:{bitmap, sequence, loop} gst
    #   (bid, gst)   = createGameBitmap bitmap gst
Peter Achten's avatar
Peter Achten committed
237
    #   (sprid, gst) = accGStTb (InitSpriteAnimation bid sequence loop) gst
238
    = (0-sprid, gst)
Peter Achten's avatar
Peter Achten committed
239

240
createNewGameObject :: !ObjectCode !SubCode !Point2 !(GSt .gs) -> (!GRESULT, !GSt .gs)
241
createNewGameObject ot st p gst
Peter Achten's avatar
Peter Achten committed
242
243
244
    =   accGStTb (InitGameObject ot st p) gst

:: ObjectFocus
245
246
247
248
249
250
   = { scrollleft      :: !Int
     , scrollup        :: !Int
     , scrollright     :: !Int
     , scrolldown      :: !Int
     , maxxscrollspeed :: !Int
     , maxyscrollspeed :: !Int
Peter Achten's avatar
Peter Achten committed
251
252
     }

253
instance zero ObjectFocus where
Peter Achten's avatar
Peter Achten committed
254
255
256
257
258
259
260
261
    zero = { scrollleft      = 0
           , scrollup        = 0
           , scrollright     = 0
           , scrolldown      = 0
           , maxxscrollspeed = 0
           , maxyscrollspeed = 0
           }

262
263
createObjectFocus :: !ObjectFocus !(GSt .gs) -> (!GRESULT, !GSt .gs)
createObjectFocus o gst
Peter Achten's avatar
Peter Achten committed
264
265
266
267
268
269
270
271
272
273
    =   accGStTb (SetObjectFocus o.scrollleft o.scrollup o.scrollright
                      o.scrolldown o.maxxscrollspeed o.maxyscrollspeed) gst


MAX_VOLUME :==  10000
MIN_VOLUME :==      0

:: Volume
   :== Int

274
PAN_LEFT   :== -10000
Peter Achten's avatar
Peter Achten committed
275
276
277
278
279
280
281
282
283
284
285
PAN_CENTER :==      0
PAN_RIGHT  :==  10000

:: Pan
   :== Int

DEFAULT_FREQUENCY :== 0

:: Frequency
   :== Int

286
287
playSoundSample :: !SoundID !Volume !Pan !Frequency !GameTime !(GSt .gs) -> (!GRESULT, !GSt .gs)
playSoundSample id vol pan freq delay gst
Peter Achten's avatar
Peter Achten committed
288
289
290
291
292
    =   accGStTb (OSPlaySoundSample id (vol - 10000) pan freq delay) gst

:: EventTarget
   = Self | AllObjects | BoundType Bounds

293
294
295
EventTargetToInt :: !EventTarget -> Int
EventTargetToInt Self          = 0
EventTargetToInt AllObjects    = -1
Peter Achten's avatar
Peter Achten committed
296
297
298
EventTargetToInt (BoundType b) = b

// modified 01/11/99
299
createUserGameEvent :: !EventCode !EventPar !EventPar !EventTarget !SubCode !GameTime !(GSt .gs) -> (!GRESULT, !GSt .gs)
300
createUserGameEvent ev evpar1 evpar2 dest subdest time gst
Peter Achten's avatar
Peter Achten committed
301
302
303
    =   accGStTb (CreateUserEvent ev evpar1 evpar2 (EventTargetToInt dest) subdest time) gst

// added 01/11/99
304
ANY_SUBTYPE :== -1
Peter Achten's avatar
Peter Achten committed
305

306
307
getBoundMap :: !Int !Int !(GSt .gs) -> (!GRESULT, !(!Int, !DirectionSet), !GSt .gs)
getBoundMap x y gst
Peter Achten's avatar
Peter Achten committed
308
309
310
311
312
    #   (result, gst) =   accGStTb (OSGetBoundMap x y) gst
    #   (gr, val)     =   result
    =   (gr, fromBoundMapCode val, gst)
    

313
314
setBoundMap :: !Int !Int !(!Int, !DirectionSet) !(GSt .gs) -> (!GRESULT, !GSt .gs)
setBoundMap x y newvalue gst
Peter Achten's avatar
Peter Achten committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
    =   accGStTb (OSSetBoundMap x y (toBoundMapCode newvalue)) gst



defaultObjectOptions :: ObjectOptions
defaultObjectOptions
 = { // fixed               = False
  // , ignorestaticbounds  = False
    ignorelevelbounds   = False
  // , bounceatcollisions  = False
  // , checkmapcodes       = False
   , checkkeyboard       = False
   , allowkeyboardrepeat = False
   , static              = False
   , hdirection          = DirRight
   , vdirection          = DirDown
   , automirrorleftright = False
   , automirrorupdown    = False
   , freeze              = False
   , removemapcode       = False
   }

337
338
defaultObjectRec :: SubCode Point2 Size GameTime !*(GSt .gs) -> (!GameObjectRec,!*GSt .gs)
defaultObjectRec objsubcode position size time gs
Peter Achten's avatar
Peter Achten committed
339
  = ( { active          = True
340
      , subcode         = objsubcode
Peter Achten's avatar
Peter Achten committed
341
342
      , size            = size
      , pos             = position
343
      , offset          = zero
Peter Achten's avatar
Peter Achten committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
      , currentsprite   = 1
      , displayoptions  = { blink           = False
                          , stretch         = False
                          , mirrorleftright = False
                          , mirrorupdown    = False
                          , rotation        = NoRotation }
      , ownbounds       = 0
      , bouncebounds    = 0
      , collidebounds   = 0
      , forgetdistance  = {x = 1, y = 1}
      , framecounter    = time
      , layer           = InFront
      , acceleration    = zero
      , speed           = zero
      , bounce          = {fvx = Value 0.0, fvy = Value 0.0}
359
      , maxspeed        = {rx  = fxr EVERYTHING,ry = fxr EVERYTHING}
Peter Achten's avatar
Peter Achten committed
360
361
362
363
364
365
      , slowdown        = {fvx = Value 0.0, fvy = Value 0.0}
      , skipmove        = SK_FOREVER
      , options         = defaultObjectOptions
      }
    , gs)

366
defaultInitObject :: Size state SubCode Point2 GameTime !*(GSt .gs) -> GameObjectState state *(GSt .gs)
Peter Achten's avatar
Peter Achten committed
367
368
defaultInitObject size state subtype pos time gs
    # (newobjrec, gs) = defaultObjectRec subtype pos size time gs
369
    = {st=state,gs=gs,or=newobjrec}
Peter Achten's avatar
Peter Achten committed
370

371
372
373
defaultGameObject :: !ObjectCode !Size state -> GameObject *(GSt .gs)
defaultGameObject objcode size state
  = { objectcode = objcode
Peter Achten's avatar
Peter Achten committed
374
    , sprites    = []
375
    , init       = defaultInitObject size state
376
    , done       = \{gs} -> gs
377
378
379
380
381
382
383
384
    , move       = id
    , animation  = id
    , touchbound = \_ _   -> id
    , collide    = \_ _ _ -> id
    , frametimer = id
    , keydown    = \_     -> id
    , keyup      = \_     -> id
    , userevent  = \_ _ _ -> id
Peter Achten's avatar
Peter Achten committed
385
386
387
    }


388
389
blankScreen :: Level (GSt .gs)
blankScreen
Peter Achten's avatar
Peter Achten committed
390
391
392
393
394
395
  = { boundmap     = { map = [{0}]
                     , blocksize = {w = 10000, h = 10000}
                     , objstart  = 1
                     , startobjx = 1
                     , startobjy = 1
                     }
396
    , initpos      = zero
Peter Achten's avatar
Peter Achten committed
397
398
399
400
401
402
403
404
405
406
407
408
409
    , layers       = []
    , objects      = []
    , music        = Nothing
    , soundsamples = []
    , leveloptions = { fillbackground = Just Black
                     , escquit        = True
                     , debugscroll    = False
                     , fadein         = False
                     , fadeout        = False
                     }
    }


410
defaultShadow :: !Int -> Shadow
Peter Achten's avatar
Peter Achten committed
411
412
413
414
415
416
defaultShadow n =
    { shadowpos   = {x = n, y = n}
    , shadowcolor = Black
    }

defaultMovement :: Movement
417
defaultMovement = const
Peter Achten's avatar
Peter Achten committed
418

419
420
defaultScrollMovement :: !Int -> Movement
defaultScrollMovement n = \p _ -> {x = p.x / n, y = p.y / n}
Peter Achten's avatar
Peter Achten committed
421
422
423
424


alignCentered :: Alignment
alignCentered = { xyfromscreencenter = (True, True)
425
                , xycentered         = (True, True)
Peter Achten's avatar
Peter Achten committed
426
                }