StdGame.icl 14.6 KB
Newer Older
Peter Achten's avatar
Peter Achten committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
implementation module StdGame

import	StdArray, StdBool, StdClass, StdInt, StdList, StdMisc
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
from	oswindow		import OSNoWindowPtr


/* predefined bounds */
BND_MAP_CODES      :==  (1 << 30)
BND_STATIC_BOUNDS  :==  (1 << 31)

/* skipmove constant */
SK_FOREVER  :==  (~1)

:: NoState
   = NoState


26
OpenGame :: gs (Game gs) [GameAttribute gs] !(PSt .l) -> (ErrorReport, !(PSt .l))
Peter Achten's avatar
Peter Achten committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
OpenGame gs gdef attr ps
    #   (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
    #   (_, tb)         =   PlayLevels initLevel gs gdef tb
    #   ps              =   appPIO (setIOToolbox tb) ps
    =   (NoError, ps)
where
    findSize :: [GameAttribute gs] Size -> Size
    findSize [] s = s
    findSize [(ScreenSize x):xs] s = x
    findSize [x:xs] s = findSize xs s
    findBPP :: [GameAttribute gs] Int -> Int
    findBPP [] s = s
    findBPP [(ColorDepth x):xs] s = x
    findBPP [x:xs] s = findBPP xs s

    // always full screen, game in a window not implemented yet
50
	OpenGameWindow :: !Id !Size !Int !Bool !(PSt .l) -> (!ErrorReport, !PSt .l)
Peter Achten's avatar
Peter Achten committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
	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)

PlayLevels :: Int gs (Game gs) !*OSToolbox -> (ErrorReport, !*OSToolbox)
PlayLevels level gs gdef tb
    |   level == 0
    =   (NoError, tb)
    #   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

FindMaxID :: a [a] -> a  | < a
FindMaxID x [] = x
FindMaxID x [y:ys]
    |   y > x       =   FindMaxID y ys
    |   otherwise   =   FindMaxID x ys

InitLayers :: [Layer] [BID] [MAPID] !*OSToolbox -> ([BID], [MAPID], !*OSToolbox)
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


MaybeSetTransparentColor :: BID (Maybe Point2) !*OSToolbox -> (GRESULT, !*OSToolbox)
MaybeSetTransparentColor _ Nothing tb = (GR_OK, tb)
MaybeSetTransparentColor bid (Just p) tb = SetTransparentColor bid p tb

MovementFunctions :: [Layer] -> [(Movement)]
MovementFunctions [] = []
MovementFunctions [l:ls] = [l.movement] ++ (MovementFunctions ls)

InitLayer :: Layer [BID] [MAPID] !*OSToolbox -> ([BID], [MAPID], !*OSToolbox)
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
    newmapid    =   ((FindMaxID 0 mapids) + 1)

InitBlockSequences :: BID [TileSequence] !*OSToolbox -> !*OSToolbox
InitBlockSequences bid [] tb = tb
InitBlockSequences bid [s:ss] tb
    #   (_, tb) =   InitBlockSequence bid s tb
    =   InitBlockSequences bid ss tb

LayersDone :: [BID] [MAPID] !*OSToolbox -> !*OSToolbox
LayersDone bids mapids tb
    #   tb      =   MapsDone mapids tb
    #   tb      =   BitmapsDone bids tb
    =   tb

MapsDone :: [MAPID] !*OSToolbox -> !*OSToolbox
MapsDone [] tb = tb
MapsDone [m:ms] tb
    #   (_, tb)     =   GameLayerMapDone m tb
    =   MapsDone ms tb

BitmapsDone :: [BID] !*OSToolbox -> !*OSToolbox
BitmapsDone [] tb = tb
BitmapsDone [b:bs] tb
    #   (_, tb)     =   GameBitmapDone b tb
    =   BitmapsDone bs tb

PlayLevel :: Int gs (GameHandle gs) !*OSToolbox -> (ErrorReport, gs,!*OSToolbox)
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
    #   (gs, tb)            =   RunGameEngine { scroll    = movements
                                              , gamestate = gs
                                              , gamehnd   = curgamehnd} tb
    #   tb                  =   maybeStopMusic curLevelHnd.music` tb
    #   (_, tb)             =   OSInitSoundSample (~1) "" 0 tb  // remove samples
    #   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
    maybePlayMusic :: (Maybe Music) !*OSToolbox -> !*OSToolbox
    maybePlayMusic Nothing tb = tb
    maybePlayMusic (Just m) tb
        #   (_, tb)     =   PlayMusic m.musicfile m.restart tb
        = tb
    maybeStopMusic :: (Maybe Music) !*OSToolbox -> !*OSToolbox
    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
    rgb :: Colour
    rgb = if fill
             (fromJust options.fillbackground)
             (RGB {r= ~1, g= ~1, b= ~1})
    fill = (isJust options.fillbackground)

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)

//convertsprites :: ![Sprite] !.(GSt .gs) -> (![SpriteID], !.(GSt .gs))
convertsprites spr gst
    # (idlst, gst) = map2 CreateAnimation spr gst
    # idlst        = map (~) idlst
    = (idlst, gst)


CreateGameBitmap :: !GameBitmap !(GSt .gs) -> (!GRESULT, !GSt .gs)
CreateGameBitmap bitmap=:{bitmapname, unitsize, dimensions, transparent} gst
    #   (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


CreateAnimation :: !Sprite !(GSt .gs) -> (!GRESULT, !GSt .gs)
CreateAnimation sprite=:{bitmap, sequence, loop} gst
    #   (bid, gst)   = CreateGameBitmap bitmap gst
    #   (sprid, gst) = accGStTb (InitSpriteAnimation bid sequence loop) gst
    = (~sprid, gst)

CreateNewGameObject :: !ObjectType !SubType !Point2 !(GSt .gs) -> (!GRESULT, !GSt .gs)
CreateNewGameObject ot st p gst
    =   accGStTb (InitGameObject ot st p) gst

:: ObjectFocus
   = { scrollleft      :: Int
     , scrollup        :: Int
     , scrollright     :: Int
     , scrolldown      :: Int
     , maxxscrollspeed :: Int
     , maxyscrollspeed :: Int
     }

instance zero ObjectFocus
where
    zero = { scrollleft      = 0
           , scrollup        = 0
           , scrollright     = 0
           , scrolldown      = 0
           , maxxscrollspeed = 0
           , maxyscrollspeed = 0
           }

CreateObjectFocus :: !ObjectFocus !(GSt .gs) -> (!GRESULT, !GSt .gs)
CreateObjectFocus o gst
    =   accGStTb (SetObjectFocus o.scrollleft o.scrollup o.scrollright
                      o.scrolldown o.maxxscrollspeed o.maxyscrollspeed) gst


MAX_VOLUME :==  10000
MIN_VOLUME :==      0

:: Volume
   :== Int

PAN_LEFT   :== ~10000
PAN_CENTER :==      0
PAN_RIGHT  :==  10000

:: Pan
   :== Int

DEFAULT_FREQUENCY :== 0

:: Frequency
   :== Int

PlaySoundSample :: !SoundID !Volume !Pan !Frequency !GameTime !(GSt .gs) -> (!GRESULT, !GSt .gs)
PlaySoundSample id vol pan freq delay gst
    =   accGStTb (OSPlaySoundSample id (vol - 10000) pan freq delay) gst

:: EventTarget
   = Self | AllObjects | BoundType Bounds

EventTargetToInt :: EventTarget -> Int
EventTargetToInt Self = 0
EventTargetToInt AllObjects = -1
EventTargetToInt (BoundType b) = b

// modified 01/11/99
CreateUserGameEvent :: !EventType !EventPar !EventPar !EventTarget !SubType !GameTime !(GSt .gs) -> (!GRESULT, !GSt .gs)
CreateUserGameEvent ev evpar1 evpar2 dest subdest time gst
    =   accGStTb (CreateUserEvent ev evpar1 evpar2 (EventTargetToInt dest) subdest time) gst

// added 01/11/99
ANY_SUBTYPE :== (~1)

GetBoundMap :: !Int !Int !(GSt .gs) -> (!GRESULT, !(!Int, !DirectionSet), !GSt .gs)
GetBoundMap x y gst
    #   (result, gst) =   accGStTb (OSGetBoundMap x y) gst
    #   (gr, val)     =   result
    =   (gr, fromBoundMapCode val, gst)
    

SetBoundMap :: !Int !Int (!Int, !DirectionSet) !(GSt .gs) -> (!GRESULT, !GSt .gs)
SetBoundMap x y newvalue gst
    =   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
   }

defaultObjectRec :: .Int .Point2 .Size .GameTime !*(GSt .a) -> (!.ObjectRec,!*GSt .a)
defaultObjectRec objsubtype position size time gs
  = ( { active          = True
      , subtype         = objsubtype
      , size            = size
      , pos             = position
      , offset          = {x = 0, y = 0}
      , 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}
      , maxspeed        = {rx = (fxr EVERYTHING), ry = (fxr EVERYTHING)}
      , slowdown        = {fvx = Value 0.0, fvy = Value 0.0}
      , skipmove        = SK_FOREVER
      , options         = defaultObjectOptions
      }
    , gs)

defaultInitObject :: .Size .a .Int .Point2 .GameTime !*(GSt .b) -> (!(.a,!.ObjectRec),!*GSt .b)
defaultInitObject size state subtype pos time gs
    # (newobjrec, gs) = defaultObjectRec subtype pos size time gs
    = ((state, newobjrec), gs)

defaultGameObject :: .Int .Size b -> .Object *(GSt .c);
defaultGameObject objtype size state
  = { objecttype = objtype
    , sprites    = []
    , init       = (defaultInitObject size state)
   // , done       = \(state, r=:{subtype}) gs -> ((objtype, subtype), gs)
    , done       = \(state, r) gs            -> gs
    , move       = \(state, r) gs            -> ((state, r), gs)
    , animation  = \(state, r) gs            -> ((state, r), gs)
    , touchbound = \(state, r) _ _ gs        -> ((state, r), gs)
    , collide    = \(state, r) _ _ _ gs      -> ((state, r), gs)
    , frametimer = \(state, r) gs            -> ((state, r), gs)
    , keydown    = \(state, r) a gs          -> ((state, r), gs)
    , keyup      = \(state, r) a gs          -> ((state, r), gs)
    , userevent  = \(state, r) a b c gs      -> ((state, r), gs)
    }


BlankScreen :: Level (GSt gs)
BlankScreen
  = { boundmap     = { map = [{0}]
                     , blocksize = {w = 10000, h = 10000}
                     , objstart  = 1
                     , startobjx = 1
                     , startobjy = 1
                     }
    , initpos      = {x = 0, y = 0}
    , layers       = []
    , objects      = []
    , music        = Nothing
    , soundsamples = []
    , leveloptions = { fillbackground = Just Black
                     , escquit        = True
                     , debugscroll    = False
                     , fadein         = False
                     , fadeout        = False
                     }
    }


defaultShadow :: Int -> Shadow
defaultShadow n =
    { shadowpos   = {x = n, y = n}
    , shadowcolor = Black
    }

defaultMovement :: Movement
defaultMovement = \p t -> p

defaultScrollMovement :: Int -> Movement
defaultScrollMovement n = \p t -> {x = p.x / n, y = p.y / n}


alignCentered :: Alignment
alignCentered = { xyfromscreencenter = (True, True)
                , xycentered = (True, True)
                }