1package{
2	import flash.display.*;
3	import flash.geom.*;
4  import flash.events.*;
5  import flash.net.*;
6
7	public class dwgraphicsclass extends Sprite {
8		public var BUTTON_BLANK:int = 0;
9		public var BUTTON_MENU:int = 1;
10		public var BUTTON_BACK:int = 2;
11		public var BUTTON_TALK:int = 3;
12		public var BUTTON_USE:int = 4;
13		public var BUTTON_TELEPORT:int = 5;
14		public var BUTTON_GAMECENTER:int = 6;
15		public var BUTTON_CONTROLS:int = 7;
16		public var BUTTON_DPAD:int = 8;
17
18		public function init():void {
19			//We initialise a few things
20			updatebackground = true;
21			tiles_rect=new Rectangle(0,0,8,8);
22			sprites_rect=new Rectangle(0,0,32,32);
23			bfont_rect=new Rectangle(0,0,8,8);
24			bfontmask_rect=new Rectangle(0,0,9,9);
25			bg_rect = new Rectangle(0, 0, 320, 240);
26			footerrect = new Rectangle(0, 230, 320, 10);
27			prect = new Rectangle(0, 0, 4, 4);
28			line_rect = new Rectangle();
29			tl = new Point(0, 0); tpoint = new Point(0, 0); trect = new Rectangle();
30			ct = new ColorTransform(0, 0, 0, 1, 255, 255, 255, 1); //Set to white
31			madpoint = new Point;
32			madrect = new Rectangle;
33
34			tele_rect = new Rectangle(0, 0, 96, 96);
35			trinketcolset = false;
36
37			showcutscenebars = false;
38		  cutscenebarspos = 0;
39
40			flipmode = false; setflipmode = false;
41			flipmatrix.scale(1, -1);
42			flipmatrix.translate(0, 240);
43			flipfontmatrix.scale(1, -1);	flipfontmatrix.translate(0, 8);
44			flipfontmatrix2.scale(1, -1);	flipfontmatrix2.translate(0, 9);
45
46			//Background inits
47			for (i = 0; i < 50; i++) {
48				var s:Rectangle = new Rectangle(Math.random() * 320, Math.random() * 240, 2, 2);
49				var s2:int = 4+(Math.random()*4);
50				stars.push(s); starsspeed.push(s2);
51
52				var bb:Rectangle;
53				var bvx:int = 0;	var bvy:int = 0;
54				if(Math.random()*100 > 50){
55				  bvx = 9 - (Math.random() * 19); if (bvx > -6 && bvx < 6) bvx = 6;
56					 bvx = bvx * 1.5;
57					bb = new Rectangle(Math.random() * 320, Math.random() * 240, 32, 12);
58  			}else {
59				  bvy = 9 - (Math.random() * 19); if (bvy > -6 && bvy < 6) bvy = 6;
60					 bvy = bvy * 1.5;
61					bb = new Rectangle(Math.random() * 320, Math.random() * 240, 12, 32);
62				}
63				var bint:Number = 0.5 + ((Math.random() * 100) / 200);
64				backboxes.push(bb);
65				backboxvx.push(bvx); backboxvy.push(bvy);
66				backboxint.push(bint);
67			}
68			backboxrect = new Rectangle();
69			backoffset = 0; backgrounddrawn = false;
70
71			warpskip = 0;
72			warpfcol = 0x000000; warpbcol = 0x000000;
73		  warprect = new Rectangle(0, 0, 0, 0);
74
75			spcol = 0; spcoldel = 0;
76
77			crewframe = 0; crewframedelay = 4;
78			menuoffset = 0; resumegamemode = false;
79
80  		//Textboxes!
81			for (i = 0; i < 30; i++) {
82				var t:textboxclass = new textboxclass;
83				textbox.push(t);
84			}
85			ntextbox = 0;
86
87			//Fading stuff
88			for (i = 0; i < 15; i++) {
89				fadebars.push(0);
90			}
91			fadeamount = 0;
92			fademode = 0;
93
94		  bigbuffer = new BitmapData(320, 32, true, 0x000000);
95			bigbufferscreen = new Bitmap(bigbuffer);
96			bigbufferscreen.width = 640;
97      bigbufferscreen.height = 20;
98
99			towerbuffer = new BitmapData(320, 240, true, 0x000000);
100			frontbuffer = new BitmapData(320, 240, true, 0x000000);
101
102			menubuffer = new BitmapData(320, 240, false, 0x000000);  //yeah yeah I know this is lazy
103
104			backbuffer=new BitmapData(320, 240,false,0x000000);
105		  screenbuffer = new BitmapData(320,240,false,0x000000);
106
107		  temptile = new BitmapData(8, 8, false, 0x000000);
108		  tempsprite = new BitmapData(32, 32, false, 0x000000);
109			screen = new Bitmap(screenbuffer);
110
111			//screen.width = 640;//320;//;640;
112      //screen.height = 480;// 240;//480;
113			screen.smoothing = true;
114
115			addChild(screen);
116
117			buttonscreen.push(new Bitmap(buttonimg[0]));
118			buttonscreen.push(new Bitmap(buttonimg[1]));
119			buttonscreen.push(new Bitmap(buttonimg[2]));
120			buttonscreen.push(new Bitmap(buttonimg[3]));
121
122			/*
123			if (device.deviceresolution == device.IPAD) {
124				buttonsize = 90;
125				buttonyspacing = buttonsize / 3;
126				buttonxspacing = 40;
127				buttonscreen[0].width = 92*3; buttonscreen[0].height = buttonsize;
128				buttonscreen[1].width = 92*3; buttonscreen[1].height = buttonsize;
129				buttonscreen[2].width = 40*3; buttonscreen[2].height = 40*3;
130				buttonscreen[3].width = 40 * 3; buttonscreen[3].height = 40 * 3;
131				buttonydiff = buttonscreen[2].height / 2;
132			}else if (device.deviceresolution == device.ANDROID) {
133			*/
134
135				/*
136			}else {
137				buttonsize = 30;
138				buttonyspacing = buttonsize / 3;
139				buttonxspacing = 20;
140				buttonscreen[0].width = 92; buttonscreen[0].height = buttonsize;
141				buttonscreen[1].width = 92; buttonscreen[1].height = buttonsize;
142				buttonscreen[2].width = 40; buttonscreen[2].height = 40;
143				buttonscreen[3].width = 40; buttonscreen[3].height = 40;
144				buttonydiff = buttonscreen[2].height / 2;
145			}
146			*/
147
148			/*buttonscreen.push(new Bitmap(buttonimg[1]));
149			buttonscreen[1].width = buttonsize; buttonscreen[1].height = buttonsize;
150			buttonscreen.push(new Bitmap(buttonimg[2]));
151			buttonscreen[2].width = buttonsize; buttonscreen[2].height = buttonsize;
152			buttonscreen.push(new Bitmap(buttonimg[3]));
153			buttonscreen[3].width = buttonsize; buttonscreen[3].height = buttonsize;
154			*/
155
156			for (i = 0; i < 10; i++) {
157				buttonlerp.push(int(0));
158				buttonstate.push(int(0));
159				currentbuttontype.push(int(-1));
160				newbuttontype.push(int(-1));
161				buttonframe.push(int(-1));
162			}
163
164			buttonactive.push(false);
165			buttonactive.push(false);
166			buttonactive.push(false);
167			buttonactive.push(false);
168			buttonhighlight.push(0); buttonhighlight.push(0);	buttonhighlight.push(0); buttonhighlight.push(0);
169			buttonpos.push(new Point(0, 0)); buttonpos.push(new Point(0, 0));
170			buttonpos.push(new Point(0, 0)); buttonpos.push(new Point(0, 0));
171			//So, 0-4 are buffers that can be overwritten. 5+ contains graphics for each button.
172
173			initbuttonpositions();
174
175			/*
176			buttonscreen[0].x = buttonxspacing;
177			buttonscreen[0].y = device.yres - buttonsize-buttonyspacing-buttonydiff;
178			buttonpos.push(new Point(buttonscreen[0].x, buttonscreen[0].y));
179
180			buttonscreen[1].x = buttonxspacing + buttonsize + buttonxspacing;
181			buttonscreen[1].y = device.yres - buttonsize-buttonyspacing;
182			buttonpos.push(new Point(buttonscreen[1].x, buttonscreen[1].y));
183
184			buttonscreen[2].x = device.xres - buttonxspacing - buttonsize;
185			buttonscreen[2].y = buttonsize / 4;
186			buttonpos.push(new Point(buttonscreen[2].x, buttonscreen[2].y));
187
188			buttonscreen[3].x = device.xres - buttonxspacing - buttonsize - buttonxspacing - buttonsize;
189			buttonscreen[3].y = device.yres - buttonsize-buttonyspacing;
190			buttonpos.push(new Point(buttonscreen[3].x, buttonscreen[3].y));
191			*/
192			drawonscreenbutton(0, -1);
193			drawonscreenbutton(1, -1);
194			drawonscreenbutton(2, -1);
195			drawonscreenbutton(3, -1);
196			addChild(buttonscreen[0]);
197			addChild(buttonscreen[1]);
198			addChild(buttonscreen[2]);
199			addChild(buttonscreen[3]);
200			/*
201			drawonscreenbutton(0, 0);
202	    drawonscreenbutton(1, 0);
203	    drawonscreenbutton(2, 0);
204	    drawonscreenbutton(3, 0);
205			showarrows();
206			*/
207    }
208
209		public function initbuttonpositions():void {
210			devicex = device.xres;
211			devicey = device.yres;
212
213			buttonsize = device.yres / (32 / 3);
214			buttonyspacing = buttonsize / 3;
215			buttonxspacing = (buttonsize * 3) / 3;
216			buttonscreen[0].width = (buttonsize * 46) / 15; buttonscreen[0].height = buttonsize;
217			buttonscreen[1].width = (buttonsize * 46) / 15; buttonscreen[1].height = buttonsize;
218			buttonscreen[2].width = (buttonsize * 5) / 3; buttonscreen[2].height = (buttonsize * 5) / 3;
219			buttonscreen[3].width = (buttonsize * 5) / 3; buttonscreen[3].height = (buttonsize * 5) / 3;
220			buttonydiff = buttonscreen[2].height / 2;
221
222			buttonscreen[0].x = device.xres + 1;
223			buttonscreen[0].y = 0;
224			buttonpos[0].setTo(buttonscreen[0].x, buttonscreen[0].y);
225
226			buttonscreen[1].x = 0;
227			buttonscreen[1].y = 0;
228			buttonpos[1].setTo(buttonscreen[1].x, buttonscreen[1].y);
229
230			buttonscreen[2].x = (buttonxspacing / 2);
231			buttonscreen[2].y = device.yres - buttonsize-buttonyspacing - buttonydiff;
232			buttonpos[2].setTo(buttonscreen[2].x, buttonscreen[2].y);
233
234			buttonscreen[3].x = (buttonxspacing/2) + buttonsize + buttonxspacing;
235			buttonscreen[3].y = device.yres - buttonsize-buttonyspacing;
236			buttonpos[3].setTo(buttonscreen[3].x, buttonscreen[3].y);
237		}
238
239		public function mobile_changebutton(t:int):void {
240			if (newbuttontype[0] != t) {
241				if (currentbuttontype[0] != t) {
242					newbuttontype[0] = t;
243					buttonstate[0] = 1;
244				}
245			}
246		}
247
248		public function mobile_changeleftbutton(t:int):void {
249			if (newbuttontype[1] != t) {
250				if (currentbuttontype[1] != t) {
251					newbuttontype[1] = t;
252					buttonstate[1] = 1;
253				}
254			}
255		}
256
257		public function mobile_hidearrows():void {
258			if (newbuttontype[2] != BUTTON_BLANK) {
259				if (currentbuttontype[2] != BUTTON_BLANK) {
260					newbuttontype[2] = BUTTON_BLANK;
261					buttonstate[2] = 1;
262				}
263			}
264		}
265
266		public function mobile_showarrows(t:int):void {
267			if (t == 2) {
268				if (newbuttontype[2] != BUTTON_DPAD) {
269					if (currentbuttontype[2] != BUTTON_DPAD) {
270						newbuttontype[2] = BUTTON_DPAD;
271						buttonstate[2] = 1;
272					}
273				}
274			}
275		}
276
277		public function drawbutton(game:gameclass, help:helpclass):void {
278			//Called every frame, this function controls what buttons appear and when,
279			//and lerps them in and out in a nice way.
280			//buttonstate[0]:
281			// 0 - Normal, slide in if not in
282			// 1 - Sliding out, change to new button state
283			for (i = 0; i < 3; i++){
284				switch(buttonstate[i]) {
285					case 0:
286						if (buttonlerp[i] > 60) {
287							buttonlerp[i] = 60;
288						}
289						if (buttonlerp[i] > 0) {
290							buttonlerp[i] -= 20;
291							if (buttonlerp[i] < 0) {
292								buttonlerp[i] = 0;
293							}
294						}
295					break;
296					case 1:
297						if (buttonlerp[i] < 100) {
298							buttonlerp[i] += 20;
299						}
300						if (buttonlerp[i] >= 60) buttonlerp[i] = 100;
301
302						if (buttonlerp[i] >= 100) {
303							buttonlerp[i] = 100;
304							buttonstate[i] = 0;
305							currentbuttontype[i] = newbuttontype[i];
306							newbuttontype[i] = -1;
307						}
308					break;
309				}
310			}
311
312			switch(currentbuttontype[2]) {
313				case BUTTON_BLANK:
314					changebuttonframe(2, -1);
315					changebuttonframe(3, -1);
316				break;
317				case BUTTON_DPAD:
318					changebuttonframe(2, 11);
319					changebuttonframe(3, 12);
320
321					if (game.press_left) {
322						changebuttonpos(2, buttonxspacing/2, devicey - buttonsize-buttonyspacing - buttonydiff+10);
323					}else{
324						changebuttonpos(2, buttonxspacing/2, devicey - buttonsize-buttonyspacing - buttonydiff);
325					}
326
327					if (game.press_right) {
328						changebuttonpos(3, (buttonxspacing/2)+ buttonsize + buttonxspacing, devicey - buttonsize-buttonyspacing - buttonydiff+10);
329					}else{
330						changebuttonpos(3, (buttonxspacing/2) + buttonsize + buttonxspacing, devicey - buttonsize-buttonyspacing - buttonydiff);
331					}
332				break;
333			}
334
335			switch(currentbuttontype[1]) {
336				case BUTTON_BLANK:
337					//Blank
338					changebuttonframe(1, -1);
339					changebuttonpos(1, 0 - ((buttonscreen[1].width * (buttonlerp[1])) / 100), 0);
340				break;
341				case BUTTON_CONTROLS:
342					//Controls button
343					if (game.menupage >= 30) {
344						changebuttonframe(1, 9);
345					}else{
346						changebuttonframe(1, 8);
347					}
348					if (flipmode) {
349						changebuttonpos(1, 0 - ((buttonscreen[1].width * (buttonlerp[1])) / 100), devicey-buttonyspacing-buttonscreen[1].height);
350					}else {
351						changebuttonpos(1, 0 - ((buttonscreen[1].width * (buttonlerp[1])) / 100), buttonyspacing);
352					}
353
354				break;
355				case BUTTON_GAMECENTER:
356					changebuttonframe(1, 10);
357					changebuttonpos(1, 0 - ((buttonscreen[1].width * (buttonlerp[1])) / 100), 5);
358				break;
359			}
360
361			switch(currentbuttontype[0]) {
362				case BUTTON_BLANK:
363					//Blank
364					changebuttonframe(0, -1);
365					changebuttonpos(0, devicex - ((buttonscreen[0].width * (100-buttonlerp[0])) / 100), 0);
366				break;
367				case BUTTON_MENU:
368					//Menu button
369				  changebuttonframe(0, 0);
370					changebuttonpos(0, devicex - ((buttonscreen[0].width * (100-buttonlerp[0])) / 100), 0);
371				break;
372				case BUTTON_BACK:
373					//Back button
374					changebuttonframe(0, 1);
375					if (flipmode) {
376						changebuttonpos(0, devicex - ((buttonscreen[0].width * (100 - buttonlerp[0])) / 100), devicey - buttonyspacing - buttonscreen[0].height);
377					}else{
378						changebuttonpos(0, devicex - ((buttonscreen[0].width * (100 - buttonlerp[0])) / 100), buttonyspacing);
379					}
380				break;
381				case BUTTON_USE:
382					//Use button
383					if (help.slowsine % 16 > 8) {
384						changebuttonframe(0, 4);
385					}else {
386						changebuttonframe(0, 5);
387					}
388					changebuttonpos(0, devicex - ((buttonscreen[0].width * (100-buttonlerp[0])) / 100), buttonyspacing);
389				break;
390				case BUTTON_TALK:
391					//Talk button
392					if (help.slowsine % 16 > 8) {
393						changebuttonframe(0, 2);
394					}else {
395						changebuttonframe(0, 3);
396					}
397					changebuttonpos(0, devicex - ((buttonscreen[0].width * (100-buttonlerp[0])) / 100), buttonyspacing);
398				break;
399				case BUTTON_TELEPORT:
400					//Teleport button
401					if (help.slowsine % 16 > 8) {
402						changebuttonframe(0, 6);
403					}else {
404						changebuttonframe(0, 7);
405					}
406					changebuttonpos(0, devicex - ((buttonscreen[0].width * (100-buttonlerp[0])) / 100), buttonyspacing);
407				break;
408			}
409
410			/*
411			if (game.press_left) { drawonscreenbutton(0, 1);
412			}else{ drawonscreenbutton(0, 0);}
413
414			if (game.press_right) { drawonscreenbutton(1, 1);
415			}else { drawonscreenbutton(1, 0); }
416
417			if (game.press_map) { drawonscreenbutton(2, 1);
418			}else { drawonscreenbutton(2, 0); }
419			*/
420			//Draw circle pad!
421	//if (key.controlstick != -1) {
422	/*
423		backbuffer.copyPixels(dwgfx.buttonimg[13], dwgfx.buttonimg[13].rect, new Point(key.controlstick_x - 18+18, key.controlstick_y - 18+18));
424		for (i = 0; i < key.touchPoints; i++) {
425			if (key.touchid[i] == key.controlstick) {
426				dwgfx.backbuffer.copyPixels(dwgfx.buttonimg[14], dwgfx.buttonimg[14].rect, new Point(key.touchx[i] - 18, key.touchy[i] - 18));
427			}
428		}
429		*/
430	//}
431		}
432
433		public function addbutton():void {
434			var t:BitmapData = new BitmapData(buffer.width, buffer.height, true, 0x00000000);
435			t.copyPixels(buffer, new Rectangle(0,0,buffer.width, buffer.height), new Point(0,0));
436			buttonimg.push(t);
437		}
438
439		public function drawonscreenbutton(t:int, t2:int):void {
440			//Draw button t with frame t2
441			if (t2 == -1) {
442				buttonimg[t].fillRect(buttonimg[t].rect, 0x00000000);
443			}
444			if (buttonframe[t] != t2) {
445				buttonframe[t] = t2;
446				buttonimg[t].fillRect(buttonimg[t].rect, 0x00000000);
447				buttonimg[t].copyPixels(buttonimg[4 + t2], buttonimg[t].rect, tl);
448			}
449		}
450
451		public function changebuttonframe(t:int, t2:int):void {
452			//Draw button t with frame t2
453			if (buttonframe[t] != t2) {
454				buttonframe[t] = t2;
455				if (t2 == -1) {
456					buttonimg[t].fillRect(buttonimg[t].rect, 0x00000000);
457				}else{
458					buttonimg[t].fillRect(buttonimg[t].rect, 0x00000000);
459					buttonimg[t].copyPixels(buttonimg[4 + t2], buttonimg[t].rect, tl);
460				}
461			}
462		}
463
464		public function changebuttonpos(t:int, xp:int, yp:int):void {
465			buttonscreen[t].x = xp;
466			buttonscreen[t].y = yp;
467		}
468
469		public function changebuttonxpos(t:int, xp:int):void {
470			buttonscreen[t].x = xp;
471		}
472
473		public function showarrows():void {
474		  if (buttonactive[0] == false) {
475				buttonhighlight[0] = 120;
476				buttonactive[0] = true;
477				addChild(buttonscreen[0]);
478				addChild(buttonscreen[1]);
479				addChild(buttonscreen[2]);
480			}
481		}
482
483		public function hidearrows():void {
484		  if (buttonactive[0] == true) {
485				buttonactive[0] = false;
486				removeChild(buttonscreen[0]);
487				removeChild(buttonscreen[1]);
488				removeChild(buttonscreen[2]);
489			}
490		}
491
492
493		public function drawspritesetcol(x:int, y:int, t:int, c:int, help:helpclass):void {
494		  tpoint.x = x; tpoint.y = y;
495			setcol(c, help);
496			sprites[t].colorTransform(sprites_rect, ct);
497			backbuffer.copyPixels(sprites[t], sprites_rect, tpoint);
498		}
499
500
501		public function makebfont():void {
502			for (var j:Number = 0; j < 16; j++) {
503				for (var i:Number = 0; i < 16; i++) {
504					var t:BitmapData = new BitmapData(8, 8, true, 0x000000);
505					var t2:BitmapData = new BitmapData(8, 8, true, 0x000000);
506					var temprect:Rectangle = new Rectangle(i * 8, j * 8, 8, 8);
507					t.copyPixels(buffer, temprect, tl);
508					t2.draw(t, flipfontmatrix);
509					bfont.push(t);
510					flipbfont.push(t2);
511				}
512			}
513
514			//Ok, now we work out the lengths (this data string cortesy of a program I wrote!)
515			for (i = 0; i < 256; i++) bfontlen.push(6);
516			var maprow:Array;
517			var tstring:String="4,3,5,7,6,7,6,3,4,4,7,7,3,5,2,5,6,5,6,6,6,6,6,6,6,6,2,3,5,5,5,6,7,6,6,6,6,5,5,6,6,3,6,6,5,7,7,6,6,6,6,6,5,6,7,7,7,7,5,4,5,4,5,6,4,6,6,6,6,5,5,6,6,3,6,6,5,7,7,6,6,6,6,6,5,6,7,7,7,7,5,5,3,5,6,4";
518
519			maprow = new Array();
520			maprow = tstring.split(",");
521			for(var k:int = 0; k < 96; k++) {
522				bfontlen[k + 32] = 8;// int(maprow[k]);
523			}
524		}
525
526		public function makebfontmask():void {
527			for (var j:Number = 0; j < 16; j++) {
528				for (var i:Number = 0; i < 16; i++) {
529					var t:BitmapData = new BitmapData(9, 9, true, 0x000000);
530					var t2:BitmapData = new BitmapData(9, 9, true, 0x000000);
531					var temprect:Rectangle = new Rectangle(i * 9, j * 9, 9, 9);
532					t.copyPixels(buffer, temprect, tl);
533					t2.draw(t, flipfontmatrix2);
534					bfontmask.push(t);
535					flipbfontmask.push(t2);
536				}
537			}
538		}
539
540		public function makeentcolourarray():void {
541			for (var j:Number = 0; j < 60; j++) {
542				for (var i:Number = 0; i < 12; i++) {
543					var t:BitmapData = new BitmapData(8, 8, true, 0x000000);
544					var temprect:Rectangle = new Rectangle(i * 8, j * 8, 8, 8);
545					t.copyPixels(buffer, temprect, tl);
546					entcolours.push(t);
547				}
548			}
549		}
550
551		public function maketilearray():void {
552			for (var j:Number = 0; j < 30; j++) {
553				for (var i:Number = 0; i < 40; i++) {
554					var t:BitmapData = new BitmapData(8, 8, true, 0x000000);
555					var temprect:Rectangle = new Rectangle(i * 8, j * 8, 8, 8);
556					t.copyPixels(buffer, temprect, tl);
557					tiles.push(t);
558				}
559			}
560		}
561
562		public function maketile2array():void {
563			for (var j:Number = 0; j < 30; j++) {
564				for (var i:Number = 0; i < 40; i++) {
565					var t:BitmapData = new BitmapData(8, 8, true, 0x000000);
566					var temprect:Rectangle = new Rectangle(i * 8, j * 8, 8, 8);
567					t.copyPixels(buffer, temprect, tl);
568					tiles2.push(t);
569				}
570			}
571		}
572
573		public function maketile3array():void {
574			for (var j:Number = 0; j < 30; j++) {
575				for (var i:Number = 0; i < 30; i++) {
576					var t:BitmapData = new BitmapData(8, 8, true, 0x000000);
577					var temprect:Rectangle = new Rectangle(i * 8, j * 8, 8, 8);
578					t.copyPixels(buffer, temprect, tl);
579					tiles3.push(t);
580				}
581			}
582		}
583
584		public function makespritearray():void {
585			for (var j:Number = 0; j < 16; j++) {
586				for (var i:Number = 0; i < 12; i++) {
587					var t:BitmapData = new BitmapData(32, 32, true, 0x000000);
588					var temprect:Rectangle = new Rectangle(i * 32, j * 32, 32, 32);
589					t.copyPixels(buffer, temprect, tl);
590					sprites.push(t);
591				}
592			}
593		}
594
595		public function makeflipspritearray():void {
596			for (var j:Number = 0; j < 16; j++) {
597				for (var i:Number = 0; i < 12; i++) {
598					var t:BitmapData = new BitmapData(32, 32, true, 0x000000);
599					var temprect:Rectangle = new Rectangle(i * 32, j * 32, 32, 32);
600					t.copyPixels(buffer, temprect, tl);
601					flipsprites.push(t);
602				}
603			}
604		}
605
606		public function maketelearray():void {
607			for (var i:Number = 0; i < 10; i++) {
608			  var t:BitmapData = new BitmapData(96, 96, true, 0x000000);
609				var temprect:Rectangle = new Rectangle(i * 96, 0, 96, 96);
610				t.copyPixels(buffer, temprect, tl);
611				tele.push(t);
612			}
613		}
614
615		public function drawmobilebutton(game:gameclass, xp:int, yp:int, wp:int, hp:int, t:String, cr:int, cg:int, cb:int, xoff:int = -8, yoff:int = -10):void {
616			xp = xp + xoff;
617			yp = yp + yoff;
618			drawfillrect(xp + 4, yp + 4, wp, hp, cr * 0.25, cg * 0.25, cb * 0.25);
619			drawfillrect(xp, yp, wp, hp, cr, cg, cb);
620			drawfillrect(xp + 2, yp + 2, wp - 4, hp - 4, cr * 0.75, cg * 0.75, cb * 0.75);
621			xp = xp + (wp / 2) - (t.length * 4);
622			print(xp, yp + 9, t, 255, 255, 255);
623		}
624
625		public function drawmenu(game:gameclass, cr:int, cg:int, cb:int, help:helpclass, division:int = 30):void {
626			if (game.mobilemenu) {
627				for (i = 0; i < game.nummenuoptions; i++) {
628					/*
629					if (i == game.currentmenuoption) {
630						//Draw it highlighted
631						if (game.menuoptionsactive[i]) {
632							tempstring = game.menuoptions[i];	tempstring = "[ " + tempstring.toUpperCase() + " ]";
633							print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb);
634						}else {
635							tempstring = game.menuoptions[i];	tempstring = "[ " + tempstring + " ]";
636							//Draw it in gray
637							print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12)+game.menuyoff, tempstring, 128, 128, 128);
638						}
639					}else {
640					*/
641					//Draw it normally
642					if (game.menuoptionslayout[i] == 0) {
643						//Normal Buttons
644						if (game.menuoptionsactive[i]) {
645							drawfillrect(160 - 70 + 4, 140 + (i * b_gap) + game.menuyoff - 6 + 4, 140, b_size, cr*0.25, cg*0.25, cb*0.25);
646							drawfillrect(160 - 70, 140 + (i * b_gap) + game.menuyoff - 6, 140, b_size, cr, cg, cb);
647							drawfillrect(160 - 70 + 2, 140 + (i * b_gap) + game.menuyoff - 6 + 2, 140 - 4, b_size - 4, cr*0.75, cg*0.75, cb*0.75);
648							print(110 + (i * division) +game.menuxoff, 140 + (i * b_gap) + game.menuyoff+3, game.menuoptions[i], 255, 255, 255, true);
649						}else {
650							drawfillrect(160 - 70 + 4, 140 + (i * b_gap) + game.menuyoff - 6 + 4, 140, b_size, 128*0.25, 128*0.25, 128*0.25);
651							drawfillrect(160 - 70, 140 + (i * b_gap) + game.menuyoff - 6, 140, b_size, 128, 128, 128);
652							drawfillrect(160 - 70 + 2, 140 + (i * b_gap) + game.menuyoff - 6 + 2, 140 - 4, b_size - 4, 128*0.75, 128*0.75, 128*0.75);
653							print(110 + (i * division) +game.menuxoff, 140 + (i * b_gap) + game.menuyoff+3, "LOCKED", 255, 255, 255, true);
654						}
655					}else if (game.menuoptionslayout[i] == 1) {
656						//Super big button for main menu play
657						drawfillrect(160 - 70 + 4, 140 + (i * b_gap) -10+ game.menuyoff - 6 + 4, 140, b_size*2, cr*0.25, cg*0.25, cb*0.25);
658						drawfillrect(160 - 70, 140 + (i * b_gap)-10 + game.menuyoff - 6, 140, b_size*2, cr, cg, cb);
659						drawfillrect(160 - 70 + 2, 140 + (i * b_gap)-10 + game.menuyoff - 6 + 2, 140 - 4, (b_size*2) - 4, cr*0.75, cg*0.75, cb*0.75);
660						bigprint(110 + (i * division) +game.menuxoff, 140 + (i * b_gap) -10 + game.menuyoff + 10, game.menuoptions[i], 255, 255, 255, true);
661					}else if (game.menuoptionslayout[i] == 2) {
662						//Main menu: Player levels and super gravitron
663						if (game.menuoptionsactive[i]) {
664							drawfillrect(160 - 70 + 4, 140 + (i * b_gap)+20 + game.menuyoff - 6 + 4, 140, b_size, cr*0.25, cg*0.25, cb*0.25);
665							drawfillrect(160 - 70, 140 + (i * b_gap)+20 + game.menuyoff - 6, 140, b_size, cr, cg, cb);
666							drawfillrect(160 - 70 + 2, 140 + (i * b_gap) +20+ game.menuyoff - 6 + 2, 140 - 4, b_size - 4, cr*0.75, cg*0.75, cb*0.75);
667							print(110 + (i * division) +game.menuxoff, 140 + (i * b_gap) + 20 + game.menuyoff+3, game.menuoptions[i], 255, 255, 255, true);
668						}else {
669							drawfillrect(160 - 70 + 4, 140 + (i * b_gap)+20 + game.menuyoff - 6 + 4, 140, 20, 128*0.25, 128*0.25, 128*0.25);
670							drawfillrect(160 - 70, 140 + (i * b_gap)+20 + game.menuyoff - 6, 140, 20, 128, 128, 128);
671							drawfillrect(160 - 70 + 2, 140 + (i * b_gap) +20+ game.menuyoff - 6 + 2, 140 - 4, 20 - 4, 128*0.75, 128*0.75, 128*0.75);
672							print(110 + (i * division) +game.menuxoff, 140 + (i * b_gap) + 20 + game.menuyoff+3, "LOCKED", 255, 255, 255, true);
673						}
674					}else if (game.menuoptionslayout[i] == 3) {
675						//Back Button
676						drawfillrect(-4, 230 - 6 + 4, 70, b_size, cr*0.10, cg*0.10, cb*0.10);
677						drawfillrect(-8, 230 - 6, 70, b_size, cr*0.5, cg*0.5, cb*0.5);
678						drawfillrect(-8+2, 230 - 6 + 2, 70 - 4, b_size - 4, cr*0.4, cg*0.4, cb*0.4);
679						print(8, 230, game.menuoptions[i], 196, 196, 196);
680					}else if (game.menuoptionslayout[i] == 4) {
681						//Normal Buttons except wider for longer text
682						if (game.menuoptionsactive[i]) {
683							drawfillrect(160 - 90 + 4, 140 + (i * b_gap) + game.menuyoff - 6 + 4, 180, b_size, cr*0.25, cg*0.25, cb*0.25);
684							drawfillrect(160 - 90, 140 + (i * b_gap) + game.menuyoff - 6, 180, b_size, cr, cg, cb);
685							drawfillrect(160 - 90 + 2, 140 + (i * b_gap) + game.menuyoff - 6 + 2, 180 - 4, b_size - 4, cr*0.75, cg*0.75, cb*0.75);
686							print(110 + (i * division) +game.menuxoff, 140 + (i * b_gap) + game.menuyoff+3, game.menuoptions[i], 255, 255, 255, true);
687						}else {
688							drawfillrect(160 - 90 + 4, 140 + (i * b_gap) + game.menuyoff - 6 + 4, 180, b_size, 128*0.25, 128*0.25, 128*0.25);
689							drawfillrect(160 - 90, 140 + (i * b_gap) + game.menuyoff - 6, 180, b_size, 128, 128, 128);
690							drawfillrect(160 - 90 + 2, 140 + (i * b_gap) + game.menuyoff - 6 + 2, 180 - 4, b_size - 4, 128*0.75, 128*0.75, 128*0.75);
691							print(110 + (i * division) +game.menuxoff, 140 + (i * b_gap) + game.menuyoff+3, "LOCKED", 255, 255, 255, true);
692						}
693					}else if (game.menuoptionslayout[i] == 5) {
694						//Time trial button, left
695
696						if (game.menuoptionsactive[i]) {
697							drawfillrect(10, 140 + (i * 68) -10+ game.menuyoff - 6 + 4, 140, 40, cr*0.25, cg*0.25, cb*0.25);
698							drawfillrect(10, 140 + (i * 68)-10 + game.menuyoff - 6, 140, 40, cr, cg, cb);
699							drawfillrect(12, 140 + (i * 68)-10 + game.menuyoff - 6 + 2, 140 - 4, 40 - 4, cr*0.75, cg*0.75, cb*0.75);
700							print(18, 140 + (i * 68) -10 + game.menuyoff + 20, game.menuoptions[i], 255, 255, 255);
701
702							if (game.besttimes[i] == -1) {
703								print( 18, 140 + (i * 68) -10 + game.menuyoff + 3, "Not attempted", 196, 196, 196, false);
704							}else {
705								print( 18, 140 + (i * 68) -10 + game.menuyoff + 3, game.timetstring(game.besttimes[i], help), 196, 196, 196);
706
707								print( 18+72, 140 + (i * 68) -10 + game.menuyoff + 3, "Rank: ", 196, 196, 196);
708								switch(game.bestrank[i]) {
709									case 0:	print( 18+116, 140 + (i * 68) -10 + game.menuyoff + 3, "B", 255, 255, 255); break;
710									case 1:	print( 18+116, 140 + (i * 68) -10 + game.menuyoff + 3, "A", 255, 255, 255); break;
711									case 2:	print( 18+116, 140 + (i * 68) -10 + game.menuyoff + 3, "S", 255, 255, 255); break;
712									case 3:	print( 18+116, 140 + (i  * 68) -10 + game.menuyoff + 3, "V", 255, 255, 255); break;
713								}
714							}
715						}else {
716							drawfillrect(10, 140 + (i * 68) -10+ game.menuyoff - 6 + 4, 140, 40, 128*0.25, 128*0.25, 128*0.25);
717							drawfillrect(10, 140 + (i * 68)-10 + game.menuyoff - 6, 140, 40, 128, 128, 128);
718							drawfillrect(12, 140 + (i * 68)-10 + game.menuyoff - 6 + 2, 140 - 4, 40 - 4, 128*0.75, 128*0.75, 128*0.75);
719
720							print( 18, 140 + (i * 68) -10 + game.menuyoff + 12, "Locked...", 255, 255, 255, false);
721						}
722
723					}else if (game.menuoptionslayout[i] == 6) {
724						//Time trial button, right
725
726						if (game.menuoptionsactive[i]) {
727							drawfillrect(170, 140 + ((i-3) * 68) -10+ game.menuyoff - 6 + 4, 140, 40, cr*0.25, cg*0.25, cb*0.25);
728							drawfillrect(170, 140 + ((i-3) * 68)-10 + game.menuyoff - 6, 140, 40, cr, cg, cb);
729							drawfillrect(172, 140 + ((i-3) * 68)-10 + game.menuyoff - 6 + 2, 140 - 4, 40 - 4, cr*0.75, cg*0.75, cb*0.75);
730							print(178, 140 + ((i - 3) * 68) -10 + game.menuyoff + 20, game.menuoptions[i], 255, 255, 255);
731
732							if (game.besttimes[i] == -1) {
733								print( 178, 140 + ((i - 3) * 68) -10 + game.menuyoff + 3, "Not attempted", 196, 196, 196, false);
734							}else {
735								print( 178, 140 + ((i - 3) * 68) -10 + game.menuyoff + 3, game.timetstring(game.besttimes[i], help), 196, 196, 196);
736
737								print( 178+72, 140 + ((i - 3) * 68) -10 + game.menuyoff + 3, "Rank: ", 196, 196, 196);
738								switch(game.bestrank[i]) {
739									case 0:	print( 178+116, 140 + ((i - 3) * 68) -10 + game.menuyoff + 3, "B", 255, 255, 255); break;
740									case 1:	print( 178+116, 140 + ((i - 3) * 68) -10 + game.menuyoff + 3, "A", 255, 255, 255); break;
741									case 2:	print( 178+116, 140 + ((i - 3) * 68) -10 + game.menuyoff + 3, "S", 255, 255, 255); break;
742									case 3:	print( 178+116, 140 + ((i - 3) * 68) -10 + game.menuyoff + 3, "V", 255, 255, 255); break;
743								}
744							}
745						}else {
746							drawfillrect(170, 140 + ((i-3) * 68) -10+ game.menuyoff - 6 + 4, 140, 40, 128*0.25, 128*0.25, 128*0.25);
747							drawfillrect(170, 140 + ((i-3) * 68)-10 + game.menuyoff - 6, 140, 40, 128, 128, 128);
748							drawfillrect(172, 140 + ((i - 3) * 68) - 10 + game.menuyoff - 6 + 2, 140 - 4, 40 - 4, 128 * 0.75, 128 * 0.75, 128 * 0.75);
749
750							print( 178, 140 + ((i - 3) * 68) -10 + game.menuyoff + 12, "Locked...", 255, 255, 255, false);
751						}
752					}else if (game.menuoptionslayout[i] == 7) {
753						//Options button on the main menu - don't draw anything, just have a detection zone
754					}else if (game.menuoptionslayout[i] == 8) {
755						//credits button on the main menu - don't draw anything, just have a detection zone
756					}else if (game.menuoptionslayout[i] == 9) {
757						//For unlocking and relocking things
758						if (game.menuoptionsactive[i]) {
759							drawfillrect(160 - 90 + 4, 140 + (i * b_gap) + game.menuyoff - 6 + 4, 180, b_size, cr*0.25, cg*0.25, cb*0.25);
760							drawfillrect(160 - 90, 140 + (i * b_gap) + game.menuyoff - 6, 180, b_size, cr, cg, cb);
761							drawfillrect(160 - 90 + 2, 140 + (i * b_gap) + game.menuyoff - 6 + 2, 180 - 4, b_size - 4, cr*0.75, cg*0.75, cb*0.75);
762							print(110 + (i * division) +game.menuxoff, 140 + (i * b_gap) + game.menuyoff+3, game.menuoptions[i], 255, 255, 255, true);
763						}else {
764							print(110 + (i * division) +game.menuxoff, 140 + (i * b_gap) + game.menuyoff+3, game.menuoptions_alt[i], 255, 255, 255, true);
765						}
766					}else if (game.menuoptionslayout[i] == 10) {
767						//Player level menu next page button
768						if (game.menuoptionsactive[i]) {
769							drawfillrect(160 - 70 + 4, 240 - b_gap + 4, 140, b_size, cr*0.25, cg*0.25, cb*0.25);
770							drawfillrect(160 - 70, 240 - b_gap, 140, b_size, cr, cg, cb);
771							drawfillrect(160 - 70 + 2, 240 - b_gap + 2, 140 - 4, b_size - 4, cr*0.75, cg*0.75, cb*0.75);
772							print(110 + (i * division) +game.menuxoff, 240 - b_gap+3+6, game.menuoptions[i], 255, 255, 255, true);
773						}else {
774							drawfillrect(160 - 70 + 4, 240 - b_gap + 4, 140, 20, 128*0.25, 128*0.25, 128*0.25);
775							drawfillrect(160 - 70, 240 - b_gap, 140, 20, 128, 128, 128);
776							drawfillrect(160 - 70 + 2, 240 - b_gap + 2, 140 - 4, 20 - 4, 128*0.75, 128*0.75, 128*0.75);
777							print(110 + (i * division) +game.menuxoff, 240 - b_gap +3+6, "LOCKED", 255, 255, 255, true);
778						}
779					}else if (game.menuoptionslayout[i] == 11) {
780						//Player level
781						drawfillrect(160 - 140 + 4, 140 + (i * 65) -10+ game.menuyoff - 6 + 4, 280, (b_size*2)+5, cr*0.25, cg*0.25, cb*0.25);
782						drawfillrect(160 - 140, 140 + (i * 65)-10 + game.menuyoff - 6, 280, (b_size*2)+5, cr, cg, cb);
783						drawfillrect(160 - 140 + 2, 140 + (i * 65)-10 + game.menuyoff - 6 + 2, 280 - 4, (b_size*2) - 4+5, cr*0.75, cg*0.75, cb*0.75);
784						print(120, 140 + (i * 65) -10 + game.menuyoff, editor.ListOfMetaData[int(game.menuoptions[i])].title, 255, 255, 255);
785						print(120, 140 + (i * 65) -10 + game.menuyoff + 10, editor.ListOfMetaData[int(game.menuoptions[i])].creator, 196, 196, 196);
786
787						print(120, 140 + (i * 65) -10 + game.menuyoff + 35+3, "spikyness: ", 196, 196, 196);
788						for (var tiii:int = 0; tiii < editor.playerlevel_spikyness[int(game.menuoptions[i])]; tiii++){
789							drawtile(120 + (8 * 11)+(8*tiii), 140 + (i * 65) -10 + game.menuyoff + 35+3, 8);
790						}
791
792						if (int(game.menuoptions_alt[i]) == 3) {
793							drawsprite(280-20, 140 + (i * 65) -10 + game.menuyoff + 30-3, 188, 255, 255, 255);
794							drawsprite(280, 140 + (i * 65) -10 + game.menuyoff + 30, 184, 255, 255, 255);
795						}else	if (int(game.menuoptions_alt[i]) == 1) {
796							drawsprite(280, 140 + (i * 65) -10 + game.menuyoff + 30, 184, 255, 255, 255);
797						}
798
799						drawplayerlevelimage(int(game.menuoptions[i]), 160 - 140 + 4+2, 140 + (i * 65) -10 + game.menuyoff - 6 + 4+2);
800					}else if (game.menuoptionslayout[i] == 12) {
801						//more games button on the main menu - don't draw anything, just have a detection zone
802					}
803				}
804			}else {
805				for (i = 0; i < game.nummenuoptions; i++) {
806					if (i == game.currentmenuoption) {
807						//Draw it highlighted
808						if (game.menuoptionsactive[i]) {
809							tempstring = game.menuoptions[i];	tempstring = "[ " + tempstring.toUpperCase() + " ]";
810							print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb);
811						}else {
812							tempstring = game.menuoptions[i];	tempstring = "[ " + tempstring + " ]";
813							//Draw it in gray
814							print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12)+game.menuyoff, tempstring, 128, 128, 128);
815						}
816					}else {
817						//Draw it normally
818						if (game.menuoptionsactive[i]) {
819							print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i], cr, cg, cb);
820						}else {
821							//Draw it in gray
822							print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i], 128, 128, 128);
823						}
824					}
825				}
826			}
827		}
828
829		public function drawlevelmenu(game:gameclass, cr:int, cg:int, cb:int, division:int = 30):void {
830			for (var i:int = 0; i < game.nummenuoptions; i++){
831				if (i == game.currentmenuoption){
832					if(game.nummenuoptions-i<=2){
833						//Draw it highlighted
834						if (game.menuoptionsactive[i]) {
835							tempstring = game.menuoptions[i];	tempstring = "[ " + tempstring.toUpperCase() + " ]";
836					    print(110 + (i * division) - 16 +game.menuxoff, 140 + 8 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb);
837						}else{
838							tempstring = game.menuoptions[i];
839							tempstring = "[ " + tempstring + " ]";
840							//Draw it in gray
841							print(110 + (i * division) - 16 +game.menuxoff, 140 + 8 + (i * 12) + game.menuyoff, tempstring, 128, 128, 128);
842						}
843					}else{
844						//Draw it highlighted
845						if (game.menuoptionsactive[i]){
846							tempstring = game.menuoptions[i];	tempstring = "[ " + tempstring.toUpperCase() + " ]";
847					    print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb);
848						}else{
849							tempstring = game.menuoptions[i];
850							tempstring = "[ " + tempstring + " ]";
851							//Draw it in gray
852							print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12)+game.menuyoff, tempstring, 128, 128, 128);
853						}
854					}
855				}else{
856					if(game.nummenuoptions-i<=2){
857						//Draw it normally
858						if (game.menuoptionsactive[i]){
859							print(110 + (i * division) +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, game.menuoptions[i], cr, cg, cb);
860						}else{
861							//Draw it in gray
862							print(110 + (i * division) +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, game.menuoptions[i], 128, 128, 128);
863						}
864					}else{
865						//Draw it normally
866						if (game.menuoptionsactive[i]){
867							print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i], cr, cg, cb);
868						}else{
869							//Draw it in gray
870							print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i], 128, 128, 128);
871						}
872					}
873				}
874			}
875		}
876
877		//Fade functions
878		public function processfade():void {
879			if (fademode > 1) {
880				if (fademode == 2) {
881					//prepare fade out
882					for (i = 0; i < 15; i++) {
883						fadebars[i] = -int(Math.random() * 12) * 8;
884					}
885					fadeamount = 0;
886					fademode = 3;
887				}else if (fademode == 3) {
888					fadeamount += 24;
889					if (fadeamount > 416) {
890						fademode = 1; //faded
891					}
892				}else if (fademode == 4) {
893					//prepare fade in
894					for (i = 0; i < 15; i++) {
895						fadebars[i] = 320 + int(Math.random() * 12) * 8;
896					}
897					fadeamount = 416;
898					fademode = 5;
899				}else if (fademode == 5) {
900					fadeamount -= 24;
901					if (fadeamount <= 0) {
902						fademode = 0; //normal
903					}
904				}
905			}
906		}
907
908		public function drawfade():void {
909			if (fademode == 1) {
910				backbuffer.fillRect(backbuffer.rect, 0x000000);
911			}else if(fademode==3){
912			  for (i = 0; i < 15; i++) {
913					setmadrect(fadebars[i], i * 16, fadeamount, 16);
914				  backbuffer.fillRect(madrect, 0x000000);
915				}
916			}else if(fademode==5){
917			  for (i = 0; i < 15; i++) {
918					setmadrect(fadebars[i]-fadeamount, i * 16, 500, 16);
919				  backbuffer.fillRect(madrect, 0x000000);
920				}
921			}
922		}
923
924		public function setwarprect(a:int, b:int, c:int, d:int):void {
925			warprect.x = a; warprect.y = b;
926			warprect.width = c; warprect.height = d;
927		}
928
929		public function createtextbox(t:String, xp:int, yp:int, r:int = 255, g:int = 255, b:int = 255):void {
930			if(ntextbox == 0) {
931				//If there are no active textboxes, Z=0;
932				m = 0; ntextbox++;
933			}else {
934				/*i = 0; m = -1;
935				while (i < ntextbox) {
936					if (!textbox[i].active) {	m = i; i = ntextbox;}
937					i++;
938				}
939				if (m == -1) {m = ntextbox; ntextbox++;}
940				*/
941				m = ntextbox; ntextbox++;
942			}
943
944			if(m<20){
945				textbox[m].clear();
946				textbox[m].line[0] = t;
947				textbox[m].xp = xp;
948				if (xp == -1) textbox[m].xp = 160 - (((t.length / 2) + 1) * 8);
949				textbox[m].yp = yp;
950				textbox[m].initcol(r, g, b);
951				textbox[m].resize();
952			}
953		}
954
955		public function textboxcleanup():void {
956			i = ntextbox - 1; while (i >= 0 && !textbox[i].active) { ntextbox--; i--; }
957		}
958
959		public function textboxcenter():void {
960			textbox[m].centerx(); textbox[m].centery();
961		}
962
963		public function textboxcenterx():void {
964			textbox[m].centerx();
965		}
966
967		public function textboxwidth():int {
968			return textbox[m].w;
969		}
970
971		public function textboxmove(xo:int, yo:int):void {
972			textbox[m].xp += xo; textbox[m].yp += yo;
973		}
974
975		public function textboxmoveto(xo:int):void {
976			textbox[m].xp = xo;
977		}
978
979		public function textboxcentery():void {
980		  textbox[m].centery();
981		}
982
983		public function textboxadjust():void {
984		  textbox[m].adjust();
985		}
986
987		public function addline(t:String):void {
988		  textbox[m].addline(t);
989		}
990
991		public function textboxtimer(t:int):void {
992		  textbox[m].timer=t;
993		}
994
995		public function textboxremove():void {
996			//Remove all textboxes
997			for (i = 0; i < ntextbox; i++) {
998				textbox[i].remove();
999			}
1000		}
1001
1002		public function textboxremovefast():void {
1003			//Remove all textboxes
1004			for (i = 0; i < ntextbox; i++) {
1005				textbox[i].removefast();
1006			}
1007		}
1008
1009		public function textboxactive():void {
1010			//Remove all but the most recent textbox
1011			for (i = 0; i < ntextbox; i++) {
1012				if (m != i) textbox[i].remove();
1013			}
1014		}
1015
1016		public function setmadrect(x:int, y:int, w:int, h:int):void {
1017			madrect.x = x; madrect.y = y; madrect.width = w; madrect.height = h;
1018		}
1019
1020		public function drawtextbox(x:int, y:int, w:int, h:int, r:int, g:int, b:int):void {
1021			//given these parameters, draw a textbox
1022			madrect.x = x; madrect.y = y; madrect.width = w*8; madrect.height = h*8;
1023			backbuffer.fillRect(madrect, RGB(r / 6, g / 6, b / 6));
1024			drawcoloredtile(x, y, 40, r, g, b);
1025			drawcoloredtile(x + (w*8) - 8, y, 42, r, g, b);
1026			drawcoloredtile(x, y + (h*8) - 8, 45, r, g, b);
1027			drawcoloredtile(x + (w*8) - 8, y + (h*8) - 8, 47, r, g, b);
1028
1029			for (k = 0; k < w-2; k++) {
1030				drawcoloredtile(x + 8 + (k * 8), y, 41, r, g, b);
1031				drawcoloredtile(x + 8 + (k * 8), y + (h * 8) - 8, 46, r, g, b);
1032			}
1033
1034			for (k = 0; k < h-2; k++) {
1035				drawcoloredtile(x, y + 8 + (k * 8), 43, r, g, b);
1036				drawcoloredtile(x + (w * 8) - 8, y + 8 + (k * 8), 44, r, g, b);
1037			}
1038		}
1039
1040		public function drawpixeltextbox(x:int, y:int, w:int, h:int, w2:int, h2:int, r:int, g:int, b:int, xo:int=0, yo:int=0):void {
1041			//given these parameters, draw a textbox with a pixel width
1042			madrect.x = x; madrect.y = y; madrect.width = w; madrect.height = h;
1043			backbuffer.fillRect(madrect, RGB(r / 6, g / 6, b / 6));
1044
1045			for (k = 0; k < w2-2; k++) {
1046				drawcoloredtile(x + 8-xo + (k * 8), y, 41, r, g, b);
1047				drawcoloredtile(x + 8-xo + (k * 8), y + (h) - 8, 46, r, g, b);
1048			}
1049
1050			for (k = 0; k < h2-2; k++) {
1051				drawcoloredtile(x, y + 8-yo + (k * 8), 43, r, g, b);
1052				drawcoloredtile(x + (w) - 8, y + 8-yo + (k * 8), 44, r, g, b);
1053			}
1054
1055			drawcoloredtile(x, y, 40, r, g, b);
1056			drawcoloredtile(x + (w) - 8, y, 42, r, g, b);
1057			drawcoloredtile(x, y + (h) - 8, 45, r, g, b);
1058			drawcoloredtile(x + (w) - 8, y + (h) - 8, 47, r, g, b);
1059		}
1060
1061		public function drawcustompixeltextbox(x:int, y:int, w:int, h:int, w2:int, h2:int, r:int, g:int, b:int, xo:int=0, yo:int=0):void {
1062			//given these parameters, draw a textbox with a pixel width
1063
1064			//madrect.x = x; madrect.y = y; madrect.w = w; madrect.h = h;
1065			//backbuffer.fillRect(madrect, RGB(r / 6, g / 6, b / 6));
1066			madrect.x = x; madrect.y = y; madrect.width = w; madrect.height = h;
1067			backbuffer.fillRect(madrect, RGB(r / 6, g / 6, b / 6));
1068
1069			for (k = 0; k < w2-2; k++){
1070				drawcoloredtile(x + 8-xo + (k * 8), y, 41, r, g, b);
1071				drawcoloredtile(x + 8-xo + (k * 8), y + (h) - 8, 46, r, g, b);
1072			}
1073
1074			drawcoloredtile(x + (w) - 16, y, 41, r, g, b);
1075			drawcoloredtile(x + (w) - 16, y + (h) - 8, 46, r, g, b);
1076			drawcoloredtile(x + (w) - 24, y, 41, r, g, b);
1077			drawcoloredtile(x + (w) - 24, y + (h) - 8, 46, r, g, b);
1078
1079			for (k = 0; k < h2-2; k++){
1080				drawcoloredtile(x, y + 8-yo + (k * 8), 43, r, g, b);
1081				drawcoloredtile(x + (w) - 8, y + 8-yo + (k * 8), 44, r, g, b);
1082			}
1083
1084			drawcoloredtile(x, y + (h) - 16, 43, r, g, b);
1085			drawcoloredtile(x + (w) - 8, y + (h) - 16, 44, r, g, b);
1086			drawcoloredtile(x, y + (h) - 24, 43, r, g, b);
1087			drawcoloredtile(x + (w) - 8, y + (h) - 24, 44, r, g, b);
1088
1089			drawcoloredtile(x, y, 40, r, g, b);
1090			drawcoloredtile(x + (w) - 8, y, 42, r, g, b);
1091			drawcoloredtile(x, y + (h) - 8, 45, r, g, b);
1092			drawcoloredtile(x + (w) - 8, y + (h) - 8, 47, r, g, b);
1093		}
1094
1095		public function drawpartimage(t:int, xp:int, yp:int, wp:int, hp:int):void {
1096			tpoint.x = xp; tpoint.y = yp;
1097			madrect.x = 0; madrect.y = 0; madrect.width = wp; madrect.height = hp;
1098
1099			backbuffer.copyPixels(images[t], madrect, tpoint);
1100		}
1101
1102		public function cutscenebars():void {
1103			if (showcutscenebars) {
1104				cutscenebarspos += 25;
1105				if (cutscenebarspos >= 360) cutscenebarspos = 360;
1106				setmadrect(0, 0, cutscenebarspos, 16);
1107				backbuffer.fillRect(madrect, 0x000000);
1108				setmadrect(360-cutscenebarspos, 224, cutscenebarspos, 16);
1109				backbuffer.fillRect(madrect, 0x000000);
1110			}else {
1111				//disappearing
1112				if (cutscenebarspos > 0) {
1113					cutscenebarspos -= 25;
1114					//draw
1115					setmadrect(0, 0, cutscenebarspos, 16);
1116					backbuffer.fillRect(madrect, 0x000000);
1117					setmadrect(360-cutscenebarspos, 224, cutscenebarspos, 16);
1118				  backbuffer.fillRect(madrect, 0x000000);
1119				}
1120			}
1121		}
1122
1123		public function drawgui(help:helpclass):void {
1124			textboxcleanup();
1125			//Draw all the textboxes to the screen
1126			for (i = 0; i<ntextbox; i++) {
1127				//This routine also updates the textboxs
1128				textbox[i].update();
1129				if (textbox[i].active) {
1130					if (textbox[i].r == 0 && textbox[i].g == 0 && textbox[i].b == 0) {
1131						if(flipmode){
1132							for (j = 0; j < textbox[i].numlines; j++) {
1133								print(textbox[i].xp + 8, textbox[i].yp + (textbox[i].numlines*8) - (j * 8), textbox[i].line[j], 196, 196, 255 - help.glow);
1134							}
1135						}else {
1136							for (j = 0; j < textbox[i].numlines; j++) {
1137								print(textbox[i].xp + 8, textbox[i].yp + 8 + (j * 8), textbox[i].line[j], 196, 196, 255 - help.glow);
1138							}
1139						}
1140					}else{
1141						backbuffer.fillRect(textbox[i].textrect, RGB(textbox[i].r/6, textbox[i].g/6, textbox[i].b / 6));
1142
1143						drawcoloredtile(textbox[i].xp, textbox[i].yp, 40, textbox[i].r, textbox[i].g, textbox[i].b);
1144						drawcoloredtile(textbox[i].xp+textbox[i].w-8, textbox[i].yp, 42, textbox[i].r, textbox[i].g, textbox[i].b);
1145						drawcoloredtile(textbox[i].xp, textbox[i].yp+textbox[i].h-8, 45, textbox[i].r, textbox[i].g, textbox[i].b);
1146						drawcoloredtile(textbox[i].xp+textbox[i].w-8, textbox[i].yp+textbox[i].h-8, 47, textbox[i].r, textbox[i].g, textbox[i].b);
1147						for (k = 0; k < textbox[i].lw; k++) {
1148							drawcoloredtile(textbox[i].xp + 8 + (k * 8), textbox[i].yp, 41, textbox[i].r, textbox[i].g, textbox[i].b);
1149							drawcoloredtile(textbox[i].xp + 8 + (k * 8), textbox[i].yp+textbox[i].h-8, 46, textbox[i].r, textbox[i].g, textbox[i].b);
1150						}
1151						for (k = 0; k < textbox[i].numlines; k++) {
1152							drawcoloredtile(textbox[i].xp, textbox[i].yp + 8 + (k * 8), 43, textbox[i].r, textbox[i].g, textbox[i].b);
1153							drawcoloredtile(textbox[i].xp + textbox[i].w-8, textbox[i].yp + 8 + (k * 8), 44, textbox[i].r, textbox[i].g, textbox[i].b);
1154						}
1155
1156						if(flipmode){
1157							for (j = 0; j < textbox[i].numlines; j++) {
1158								print(textbox[i].xp + 8, textbox[i].yp  + (textbox[i].numlines*8) - (j * 8), textbox[i].line[j], textbox[i].r, textbox[i].g, textbox[i].b);
1159							}
1160						}else {
1161							for (j = 0; j < textbox[i].numlines; j++) {
1162								print(textbox[i].xp + 8, textbox[i].yp +8 + (j * 8), textbox[i].line[j], textbox[i].r, textbox[i].g, textbox[i].b);
1163							}
1164						}
1165					}
1166
1167					if ((textbox[i].yp == 12 || textbox[i].yp == 180) && textbox[i].r == 165) {
1168						if (flipmode) {	drawimage(5, 0, 180, true);
1169						}else{ drawimage(0, 0, 12, true);	}
1170					}else if ((textbox[i].yp == 12 || textbox[i].yp == 180) && textbox[i].g == 165) {
1171						if (flipmode) {	drawimage(6, 0, 180, true);
1172						}else{ drawimage(4, 0, 12, true);	}
1173					}
1174					if (flipmode) {
1175						if (textbox[i].r == 175 && textbox[i].g == 175) {
1176							//purple guy
1177							drawsprite(80 - 6, 64 + 48 + 4, 6, 220- help.glow/4 - (Math.random()*20), 120- help.glow/4, 210 - help.glow/4);
1178						}else if (textbox[i].r == 175 && textbox[i].b == 175) {
1179							//red guy
1180							drawsprite(80 - 6, 64 + 48+ 4, 6, 255 - help.glow/8, 70 - help.glow/4, 70 - help.glow / 4);
1181						}else if (textbox[i].r == 175) {
1182							//green guy
1183							drawsprite(80 - 6, 64 + 48 + 4, 6, 120 - help.glow / 4 - (Math.random() * 20), 220 - help.glow / 4, 120 - help.glow / 4);
1184						}else if (textbox[i].g == 175) {
1185							//yellow guy
1186							drawsprite(80 - 6, 64 + 48+ 4, 6, 220- help.glow/4 - (Math.random()*20), 210 - help.glow/4, 120- help.glow/4);
1187						}else if (textbox[i].b == 175) {
1188							//blue guy
1189							drawsprite(80 - 6, 64 + 48+ 4, 6, 75, 75, 255- help.glow/4 - (Math.random()*20));
1190						}
1191					}else{
1192						if (textbox[i].r == 175 && textbox[i].g == 175) {
1193							//purple guy
1194							drawsprite(80 - 6, 64 + 32 + 4, 0, 220- help.glow/4 - (Math.random()*20), 120- help.glow/4, 210 - help.glow/4);
1195						}else if (textbox[i].r == 175 && textbox[i].b == 175) {
1196							//red guy
1197							drawsprite(80 - 6, 64 + 32 + 4, 0, 255 - help.glow/8, 70 - help.glow/4, 70 - help.glow / 4);
1198						}else if (textbox[i].r == 175) {
1199							//green guy
1200							drawsprite(80 - 6, 64 + 32 + 4, 0, 120 - help.glow / 4 - (Math.random() * 20), 220 - help.glow / 4, 120 - help.glow / 4);
1201						}else if (textbox[i].g == 175) {
1202							//yellow guy
1203							drawsprite(80 - 6, 64 + 32 + 4, 0, 220- help.glow/4 - (Math.random()*20), 210 - help.glow/4, 120- help.glow/4);
1204						}else if (textbox[i].b == 175) {
1205							//blue guy
1206							drawsprite(80 - 6, 64 + 32 + 4, 0, 75, 75, 255- help.glow/4 - (Math.random()*20));
1207						}
1208					}
1209				}
1210			}
1211		}
1212
1213		public function RGBA(red:Number,green:Number,blue:Number):Number{
1214			return (blue | (green << 8) | (red << 16)) + 0xFF000000;
1215		}
1216
1217		public function RGB(red:Number,green:Number,blue:Number):Number{
1218			return (blue | (green << 8) | (red << 16))
1219		}
1220
1221	  public function RGBf(red:Number, green:Number, blue:Number):Number {
1222			red = (red+128) / 3; green = (green+128) / 3; blue = (blue+128) / 3;
1223			return (blue | (green << 8) | (red << 16))
1224		}
1225
1226		public function addmobileimage():void {
1227			var t:BitmapData = new BitmapData(buffer.width, buffer.height, true, 0x000000);
1228			setmadrect(0, 0, buffer.width, buffer.height);
1229			t.copyPixels(buffer, madrect, tl);
1230			mobileimages.push(t);
1231		}
1232
1233		public function addimage():void {
1234			var t:BitmapData = new BitmapData(buffer.width, buffer.height, true, 0x000000);
1235			setmadrect(0, 0, buffer.width, buffer.height);
1236			t.copyPixels(buffer, madrect, tl);
1237			images.push(t);
1238		}
1239
1240		public function addplayerlevelimage():void {
1241			var t:BitmapData = new BitmapData(buffer.width, buffer.height, true, 0x000000);
1242			setmadrect(0, 0, buffer.width, buffer.height);
1243			t.copyPixels(buffer, madrect, tl);
1244			playerlevelimages.push(t);
1245		}
1246
1247		public function addbackground():void {
1248			var t:BitmapData = new BitmapData(160, 144, true, 0x000000);
1249			t.copyPixels(buffer, bg_rect, tl);
1250			backgrounds.push(t);
1251		}
1252
1253		public function drawtowerbackground(map:mapclass):void {
1254			if (map.bypos < 0) map.bypos += 120 * 8;
1255
1256			if (map.scrolldir == 1) map.tdrawback = true;
1257
1258			if (map.tdrawback) {
1259				//Draw the whole thing; needed for every colour cycle!
1260				for (j = 0; j < 30; j++) {
1261					for (i = 0; i < 40; i++) {
1262						temp = map.tower.backat(i, j, map.bypos);
1263						drawtowertile3(i * 8, (j * 8) - (map.bypos % 8), temp, map.colstate);
1264					}
1265				}
1266
1267				backbuffer.copyPixels(towerbuffer, towerbuffer.rect, tl, null, null, false);
1268
1269				map.tdrawback = false;
1270			}else {
1271				//just update the bottom
1272				towerbuffer.scroll(0, -map.bscroll);
1273				for (i = 0; i < 40; i++) {
1274					temp = map.tower.backat(i, 0, map.bypos);
1275					drawtowertile3(i * 8, -(map.bypos % 8), temp, map.colstate);
1276				}
1277
1278				backbuffer.copyPixels(towerbuffer, towerbuffer.rect, tl, null, null, false);
1279			}
1280		}
1281
1282		public function drawtowerbackgroundsolo(map:mapclass):void {
1283			if (map.bypos < 0) map.bypos += 120 * 8;
1284
1285			if (map.tdrawback) {
1286				//Draw the whole thing; needed for every colour cycle!
1287				for (j = 0; j < 31; j++) {
1288					for (i = 0; i < 40; i++) {
1289						temp = map.tower.backat(i, j, map.bypos);
1290						drawtowertile3(i * 8, (j * 8) - (map.bypos % 8), temp, map.colstate);
1291					}
1292				}
1293
1294				backbuffer.copyPixels(towerbuffer, towerbuffer.rect, tl, null, null, false);
1295
1296				map.tdrawback = false;
1297			}else {
1298				//just update the bottom
1299				towerbuffer.scroll(0, -map.bscroll);
1300				for (i = 0; i < 40; i++) {
1301					temp = map.tower.backat(i, 0, map.bypos);
1302					drawtowertile3(i * 8, -(map.bypos % 8), temp, map.colstate);
1303				}
1304
1305				backbuffer.copyPixels(towerbuffer, towerbuffer.rect, tl, null, null, false);
1306			}
1307		}
1308
1309		public function drawtowermap(map:mapclass):void {
1310			for (j = 0; j < 30; j++) {
1311				for (i = 0; i < 40; i++) {
1312					temp = map.tower.at(i, j, map.ypos);
1313					if (temp > 0) drawtile3(i * 8, (j * 8) - (map.ypos % 8), temp, map.colstate);
1314				}
1315			}
1316		}
1317
1318		public function drawtowermap_nobackground(map:mapclass):void {
1319			for (j = 0; j < 30; j++) {
1320				for (i = 0; i < 40; i++) {
1321					temp = map.tower.at(i, j, map.ypos);
1322					if (temp > 0 && temp<28) drawtile3(i * 8, (j * 8) - (map.ypos % 8), temp, map.colstate);
1323				}
1324			}
1325		}
1326
1327		public function drawtowerspikes(map:mapclass):void {
1328			for (i = 0; i < 40; i++) {
1329				drawtile3(i * 8, -8+map.spikeleveltop, 9, map.colstate);
1330				drawtile3(i * 8, 230-map.spikelevelbottom, 8, map.colstate);
1331			}
1332		}
1333
1334		public function drawtowerentities(map:mapclass, obj:entityclass, help:helpclass):void {
1335			//Update line colours!
1336			if (linedelay <= 0) {
1337				linestate++;
1338				if (linestate >= 10) linestate = 0;
1339				linedelay = 2;
1340			}else {
1341				linedelay--;
1342			}
1343
1344			var j:int, k:int;
1345			for (var i:int = 0; i < obj.nentity; i++) {
1346				if (!obj.entities[i].invis && obj.entities[i].active) {
1347					if (obj.entities[i].size == 0) {      // Sprites
1348						tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp-map.ypos;
1349						setcol(obj.entities[i].colour, help);
1350						sprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
1351						backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
1352						//screenwrapping!
1353						if (!map.minitowermode) {
1354							if ( map.ypos >= 500 && map.ypos <= 5000) { //The "wrapping" area of the tower
1355								if (tpoint.x < 0) {
1356									tpoint.x += 320;
1357									backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
1358								}
1359								if (tpoint.x > 300) {
1360									tpoint.x -= 320;
1361									backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
1362								}
1363							}
1364						}
1365					}else if (obj.entities[i].size == 1) { // Tiles
1366					  tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp-map.ypos;
1367					  backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
1368					}else if (obj.entities[i].size == 2) { // Special: Moving platform, 4 tiles
1369					  tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp-map.ypos;
1370					  backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
1371						tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
1372						tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
1373						tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
1374					}else if (obj.entities[i].size == 3) { // Big chunky pixels!
1375					  prect.x = obj.entities[i].xp; prect.y = obj.entities[i].yp-map.ypos;
1376						//A seperate index of colours, for simplicity
1377						if(obj.entities[i].colour==1){
1378						  backbuffer.fillRect(prect, RGB(196 - (Math.random() * 64), 10, 10));
1379						}else if (obj.entities[i].colour == 2) {
1380							backbuffer.fillRect(prect, RGB(160- help.glow/2 - (Math.random()*20), 200- help.glow/2, 220 - help.glow));
1381						}
1382					}else if (obj.entities[i].size == 4) { // Small pickups
1383					  drawhuetile(obj.entities[i].xp, obj.entities[i].yp-map.ypos, obj.entities[i].tile, obj.entities[i].colour);
1384					}else if (obj.entities[i].size == 5) { //Horizontal Line
1385						line_rect.x = obj.entities[i].xp; line_rect.y = obj.entities[i].yp-map.ypos;
1386						line_rect.width = obj.entities[i].w; line_rect.height = 1;
1387						drawgravityline(i, obj);
1388					}else if (obj.entities[i].size == 6) { //Vertical Line
1389						line_rect.x = obj.entities[i].xp; line_rect.y = obj.entities[i].yp-map.ypos;
1390						line_rect.width = 1; line_rect.height = obj.entities[i].h;
1391						drawgravityline(i, obj);
1392					}
1393				}
1394			}
1395		}
1396
1397		public function drawbackground(t:int, map:mapclass):void {
1398			switch(t) {
1399				case 1:
1400				  //Starfield
1401				  backbuffer.fillRect(backbuffer.rect, 0x000000);
1402					for (i = 0; i < 50; i++) {
1403						if (starsspeed[i] <= 6) {
1404						  backbuffer.fillRect(stars[i], 0x222222);
1405						}else {
1406							backbuffer.fillRect(stars[i], 0x555555);
1407						}
1408						stars[i].x -= starsspeed[i];
1409						if (stars[i].x < -10) {
1410							stars[i].x += 340;
1411							stars[i].y = Math.random() * 240;
1412							starsspeed[i] = 4+(Math.random()*4);
1413						}
1414					}
1415				break;
1416				case 2:
1417				  //Lab
1418					switch(rcol) {
1419						//Akward ordering to match tileset
1420						case 0: bcol2 = RGB(0, 16*backboxint[i], 16*backboxint[i]);	break; //Cyan
1421						case 1: bcol2 = RGB(16*backboxint[i], 0, 0);	break;  //Red
1422						case 2: bcol2 = RGB(16*backboxint[i], 0, 16*backboxint[i]);	break; //Purple
1423						case 3: bcol2 = RGB(0, 0, 16*backboxint[i]);	break;  //Blue
1424						case 4: bcol2 = RGB(16*backboxint[i], 16*backboxint[i], 0);	break; //Yellow
1425						case 5: bcol2 = RGB(0, 16 * backboxint[i], 0);	break;  //Green
1426						case 6:
1427						  //crazy case
1428							spcoldel--;
1429							if (spcoldel <= 0) {
1430								spcoldel = 15;
1431								spcol++;
1432								if (spcol >= 12) spcol = 0;
1433							}
1434							switch(spcol) {
1435								case 0: bcol2 = RGB(0, 16*backboxint[i], 16*backboxint[i]);	break; //Cyan
1436								case 1: bcol2 = RGB(0, (spcoldel+1)*backboxint[i], 16*backboxint[i]);	break; //Cyan
1437								case 2: bcol2 = RGB(0, 0, 16*backboxint[i]);	break;  //Blue
1438								case 3: bcol2 = RGB((16-spcoldel)*backboxint[i], 0, 16*backboxint[i]);	break;  //Blue
1439								case 4: bcol2 = RGB(16*backboxint[i], 0, 16*backboxint[i]);	break; //Purple
1440								case 5: bcol2 = RGB(16*backboxint[i], 0, (spcoldel+1)*backboxint[i]);	break; //Purple
1441								case 6: bcol2 = RGB(16*backboxint[i], 0, 0);	break;  //Red
1442								case 7: bcol2 = RGB(16*backboxint[i], (16-spcoldel)*backboxint[i], 0);	break;  //Red
1443								case 8: bcol2 = RGB(16*backboxint[i], 16*backboxint[i], 0);	break; //Yellow
1444								case 9: bcol2 = RGB((spcoldel+1)*backboxint[i], 16*backboxint[i], 0);	break; //Yellow
1445								case 10: bcol2 = RGB(0, 16 * backboxint[i], 0);	break;  //Green
1446								case 11: bcol2 = RGB(0, 16 * backboxint[i], (16-spcoldel)*backboxint[i]);	break;  //Green
1447							}
1448						break;
1449					}
1450				  backbuffer.fillRect(backbuffer.rect, bcol2);
1451					for (i = 0; i < 18; i++) {
1452						switch(rcol) {
1453							//Akward ordering to match tileset
1454							case 0: bcol = RGB(16, 128*backboxint[i], 128*backboxint[i]);	break; //Cyan
1455							case 1: bcol = RGB(128*backboxint[i], 16, 16);	break;  //Red
1456							case 2: bcol = RGB(128*backboxint[i], 16, 128*backboxint[i]);	break; //Purple
1457							case 3: bcol = RGB(16, 16, 128*backboxint[i]);	break;  //Blue
1458							case 4: bcol = RGB(128*backboxint[i], 128*backboxint[i], 16);	break; //Yellow
1459							case 5: bcol = RGB(16, 128 * backboxint[i], 16);	break;  //Green
1460							case 6:
1461						  //crazy case
1462							switch(spcol) {
1463								case 0: bcol = RGB(16, 128*backboxint[i], 128*backboxint[i]);	break; //Cyan
1464								case 1: bcol = RGB(16, ((spcoldel+1)*8)*backboxint[i], 128*backboxint[i]);	break; //Cyan
1465								case 2: bcol = RGB(16, 16, 128*backboxint[i]);	break;  //Blue
1466								case 3: bcol = RGB((128-(spcoldel*8))*backboxint[i], 16, 128*backboxint[i]);	break;  //Blue
1467								case 4: bcol = RGB(128*backboxint[i], 16, 128*backboxint[i]);	break; //Purple
1468								case 5: bcol = RGB(128*backboxint[i], 16, ((spcoldel+1)*8)*backboxint[i]);	break; //Purple
1469								case 6: bcol = RGB(128*backboxint[i], 16, 16);	break;  //Red
1470								case 7: bcol = RGB(128*backboxint[i], (128-(spcoldel*8))*backboxint[i], 16);	break;  //Red
1471								case 8: bcol = RGB(128*backboxint[i], 128*backboxint[i], 16);	break; //Yellow
1472								case 9: bcol = RGB(((spcoldel+1)*8)*backboxint[i], 128*backboxint[i], 16);	break; //Yellow
1473								case 10: bcol = RGB(16, 128 * backboxint[i], 16);	break;  //Green
1474								case 11: bcol = RGB(16, 128 * backboxint[i], (128-(spcoldel*8))*backboxint[i]);	break;  //Green
1475							}
1476						break;
1477						}
1478						backbuffer.fillRect(backboxes[i], bcol);
1479
1480						backboxrect.x = backboxes[i].x + 1; backboxrect.y = backboxes[i].y + 1;
1481						backboxrect.width = backboxes[i].width - 2; backboxrect.height = backboxes[i].height - 2;
1482						backbuffer.fillRect(backboxrect, bcol2);
1483
1484						backboxes[i].x += backboxvx[i];
1485						backboxes[i].y += backboxvy[i];
1486						if (backboxes[i].x < -40) { backboxes[i].x = 320; backboxes[i].y = Math.random() * 240; }
1487					  if (backboxes[i].x > 320) { backboxes[i].x = -32; backboxes[i].y = Math.random() * 240; }
1488						if (backboxes[i].y < -40) { backboxes[i].y = 240; backboxes[i].x = Math.random() * 320; }
1489						if (backboxes[i].y > 260) { backboxes[i].y = -32; backboxes[i].x = Math.random() * 320; }
1490					}
1491				break;
1492				case 3: //Warp zone (horizontal)
1493				  backoffset+=3; if (backoffset >= 16) backoffset -= 16;
1494
1495				  if (backgrounddrawn) {
1496						towerbuffer.scroll( -3, 0);
1497						for (j = 0; j < 15; j++) {
1498							temp = 680 + (rcol * 3);
1499							drawtowertile(317 - backoffset, (j * 16), temp+40);  //20*16 = 320
1500							drawtowertile(317 - backoffset + 8, (j * 16), temp + 41);
1501							drawtowertile(317 - backoffset, (j * 16) + 8, temp + 80);
1502							drawtowertile(317 - backoffset + 8, (j * 16) + 8, temp + 81);
1503						}
1504					}else {
1505						//draw the whole thing for the first time!
1506						backoffset = 0;
1507						towerbuffer.fillRect(towerbuffer.rect, 0x000000);
1508						for (j = 0; j < 15; j++) {
1509							for (i = 0; i < 21; i++) {
1510								temp = 680 + (rcol * 3);
1511								drawtowertile((i * 16) - backoffset, (j * 16), temp+40);
1512								drawtowertile((i * 16) - backoffset + 8, (j * 16), temp + 41);
1513								drawtowertile((i * 16) - backoffset, (j * 16) + 8, temp + 80);
1514								drawtowertile((i * 16) - backoffset + 8, (j * 16) + 8, temp + 81);
1515							}
1516						}
1517						backgrounddrawn = true;
1518					}
1519
1520					backbuffer.copyPixels(towerbuffer, towerbuffer.rect, tl);
1521				break;
1522				case 4: //Warp zone (vertical)
1523				  backoffset+=3; if (backoffset >= 16) backoffset -= 16;
1524
1525				  if (backgrounddrawn) {
1526						towerbuffer.scroll(0, -3);
1527						for (i = 0; i < 21; i++) {
1528							temp = 760 + (rcol * 3);
1529							drawtowertile((i * 16), 237 - backoffset, temp + 40); //14*17=240 - 3
1530							drawtowertile((i * 16) + 8, 237 - backoffset, temp + 41);
1531							drawtowertile((i * 16), 237 - backoffset + 8, temp + 80);
1532							drawtowertile((i * 16) + 8, 237 - backoffset + 8, temp + 81);
1533						}
1534					}else {
1535						//draw the whole thing for the first time!
1536						backoffset = 0;
1537						towerbuffer.fillRect(towerbuffer.rect, 0x000000);
1538						for (j = 0; j < 15; j++) {
1539							for (i = 0; i < 21; i++) {
1540								temp = 760 + (rcol * 3);
1541								drawtowertile((i * 16), (j * 16)- backoffset, temp+40);
1542								drawtowertile((i * 16)+ 8, (j * 16)- backoffset, temp + 41);
1543								drawtowertile((i * 16), (j * 16)- backoffset + 8, temp + 80);
1544								drawtowertile((i * 16)+ 8, (j * 16)- backoffset + 8, temp + 81);
1545							}
1546						}
1547						backgrounddrawn = true;
1548					}
1549
1550					backbuffer.copyPixels(towerbuffer, towerbuffer.rect, tl);
1551				break;
1552				case 5:
1553				  //Warp zone, central
1554					switch(rcol) {
1555						//Akward ordering to match tileset
1556						case 0: warpbcol = 0x0A100E; warpfcol = 0x102221; break; //Cyan
1557						case 1: warpbcol = 0x11090B; warpfcol = 0x221011; break; //Red
1558						case 2: warpbcol = 0x0F0A10; warpfcol = 0x221022; break; //Purple
1559						case 3: warpbcol = 0x0A0B10; warpfcol = 0x101022; break; //Blue
1560						case 4: warpbcol = 0x100D0A; warpfcol = 0x221E10; break; //Yellow
1561						case 5: warpbcol = 0x0D100A; warpfcol = 0x142210; break; //Green
1562						case 6: warpbcol = 0x0A0A0A; warpfcol = 0x121212; break; //Gray
1563					}
1564
1565				  backoffset += 1;
1566					if (backoffset >= 16) {
1567						backoffset -= 16;
1568						warpskip = (warpskip + 1) % 2;
1569					}
1570
1571					for (i = 10 ; i >= 0; i--) {
1572						temp = (i << 4) + backoffset;
1573						setwarprect(160 - temp, 120 - temp, temp * 2, temp * 2);
1574						if (i % 2 == warpskip) {
1575							backbuffer.fillRect(warprect, warpbcol);
1576						}else {
1577							backbuffer.fillRect(warprect, warpfcol);
1578						}
1579					}
1580				break;
1581				case 6:
1582				  //Final Starfield
1583				  backbuffer.fillRect(backbuffer.rect, 0x000000);
1584					for (i = 0; i < 50; i++) {
1585						if (starsspeed[i] <= 8) {
1586						  backbuffer.fillRect(stars[i], 0x222222);
1587						}else {
1588							backbuffer.fillRect(stars[i], 0x555555);
1589						}
1590						stars[i].y -= starsspeed[i];
1591						if (stars[i].y < -10) {
1592							stars[i].y += 260;
1593							stars[i].x = Math.random() * 320;
1594							starsspeed[i] = 5+(Math.random()*5);
1595						}
1596					}
1597				break;
1598				case 7:
1599				  //Static, unscrolling section of the tower
1600					for (j = 0; j < 30; j++) { for (i = 0; i < 40; i++) {	drawtile3(i * 8, j * 8, map.tower.backat(i, j, 200), 15);	}	}
1601				break;
1602				case 8:
1603				  //Static, unscrolling section of the tower
1604					for (j = 0; j < 30; j++) { for (i = 0; i < 40; i++) {	drawtile3(i * 8, j * 8, map.tower.backat(i, j, 200), 10);	}	}
1605				break;
1606				case 9:
1607				  //Static, unscrolling section of the tower
1608					for (j = 0; j < 30; j++) { for (i = 0; i < 40; i++) {	drawtile3(i * 8, j * 8, map.tower.backat(i, j, 600), 0);	}	}
1609				break;
1610				default:
1611				  backbuffer.fillRect(backbuffer.rect, 0x000000);
1612					//backbuffer.copyPixels(backgrounds[t], bg_rect, tl);
1613				break;
1614			}
1615		}
1616
1617		public function drawimage(t:int, xp:int, yp:int, cent:Boolean=false):void {
1618			if (cent) {
1619				tpoint.x = 160 - int(images[t].width / 2); tpoint.y = yp;
1620				trect.x = 0; trect.y = 0; trect.width = images[t].width; trect.height = images[t].height;
1621				backbuffer.copyPixels(images[t], trect, tpoint);
1622			}else {
1623				tpoint.x = xp; tpoint.y = yp;
1624				trect.x = 0; trect.y = 0; trect.width = images[t].width; trect.height = images[t].height;
1625			  backbuffer.copyPixels(images[t], trect, tpoint);
1626			}
1627		}
1628
1629		public function drawmobileimage(t:int, xp:int, yp:int, cent:Boolean=false):void {
1630			if (cent) {
1631				tpoint.x = 160 - int(mobileimages[t].width / 2); tpoint.y = yp;
1632				trect.x = 0; trect.y = 0; trect.width = mobileimages[t].width; trect.height = mobileimages[t].height;
1633				backbuffer.copyPixels(mobileimages[t], trect, tpoint);
1634			}else {
1635				tpoint.x = xp; tpoint.y = yp;
1636				trect.x = 0; trect.y = 0; trect.width = mobileimages[t].width; trect.height = mobileimages[t].height;
1637			  backbuffer.copyPixels(mobileimages[t], trect, tpoint);
1638			}
1639		}
1640
1641		public function drawmobilehands(t:int, xp:int, yp:int):void {
1642			tpoint.x = xp; tpoint.y = yp;
1643			trect.x = 0; trect.y = 0; trect.width = mobileimages[t].width; trect.height = 70;
1644			backbuffer.copyPixels(mobileimages[t], trect, tpoint);
1645		}
1646
1647		public function drawplayerlevelimage(t:int, xp:int, yp:int):void {
1648			tpoint.x = xp; tpoint.y = yp;
1649			trect.x = 0; trect.y = 0; trect.width = playerlevelimages[t].width; trect.height = playerlevelimages[t].height;
1650			backbuffer.copyPixels(playerlevelimages[t], trect, tpoint);
1651		}
1652
1653		public function drawimagecol(t:int, xp:int, yp:int, r:int, g:int, b:int, cent:Boolean = false):void {
1654			setcolreal(RGB(r,g,b));
1655			if (cent) {
1656				tpoint.x = 160 - int(images[t].width / 2); tpoint.y = yp;
1657				trect.x = 0; trect.y = 0; trect.width = images[t].width; trect.height = images[t].height;
1658				images[t].colorTransform(trect, ct);
1659				backbuffer.copyPixels(images[t], trect, tpoint);
1660			}else {
1661				tpoint.x = xp; tpoint.y = yp;
1662				trect.x = 0; trect.y = 0; trect.width = images[t].width; trect.height = images[t].height;
1663			  images[t].colorTransform(trect, ct);
1664				backbuffer.copyPixels(images[t], trect, tpoint);
1665			}
1666		}
1667
1668		public function crewcolour(t:int):int {
1669			//given crewmate t, return colour in setcol
1670			if (t == 0) return 0;
1671			if (t == 1) return 20;
1672			if (t == 2) return 14;
1673			if (t == 3) return 15;
1674			if (t == 4) return 13;
1675			if (t == 5) return 16;
1676			return 0;
1677		}
1678
1679		public function setcol(t:int, help:helpclass):void {
1680		  //Setup predefinied colours as per our zany palette
1681			switch(t) {
1682				//Player Normal
1683				case 0: ct.color = RGB(160- help.glow/2 - (Math.random()*20), 200- help.glow/2, 220 - help.glow); break;
1684				//Player Hurt
1685				case 1:	ct.color = RGB(196 - (Math.random() * 64), 10, 10); break;
1686				//Enemies and stuff
1687				case 2: ct.color = RGB(225-(help.glow/2), 75, 30); break;
1688				case 3: //Trinket
1689					if (!trinketcolset) {
1690						trinketr = 200 - (Math.random() * 64);
1691						trinketg = 200 - (Math.random() * 128);
1692						trinketb = 164 + (Math.random() * 60);
1693						trinketcolset = true;
1694					}
1695				  ct.color = RGB(trinketr, trinketg, trinketb);
1696				break;
1697				case 4: //Inactive savepoint
1698				  temp = (help.glow/2) + (Math.random() * 8);
1699				  ct.color = RGB(80 + temp, 80 + temp, 80 + temp);
1700				break;
1701				case 5: //Active savepoint
1702				  ct.color = RGB(164+(Math.random()*64),164+(Math.random()*64), 255-(Math.random()*64))
1703				break;
1704				case 6: //Enemy : Red
1705				  ct.color = RGB(250 - help.glow/2, 60- help.glow/2, 60 - help.glow/2);
1706				break;
1707				case 7: //Enemy : Green
1708				  ct.color = RGB(100 - help.glow/2 - (Math.random()*30), 250 - help.glow/2, 100 - help.glow/2 - (Math.random()*30));
1709				break;
1710				case 8: //Enemy : Purple
1711				  ct.color = RGB(250 - help.glow/2, 20, 128 - help.glow/2 + (Math.random()*30));
1712				break;
1713				case 9: //Enemy : Yellow
1714				  ct.color = RGB(250 - help.glow/2, 250 - help.glow/2, 20);
1715				break;
1716				case 10: //Warp point (white)
1717				  ct.color = RGB(255 - (Math.random() * 64), 255 - (Math.random() * 64), 255 - (Math.random() * 64));
1718				break;
1719				case 11: //Enemy : Cyan
1720				  ct.color = RGB(20, 250 - help.glow/2, 250 - help.glow/2);
1721				break;
1722				case 12: //Enemy : Blue
1723				  ct.color = RGB(90- help.glow/2, 90 - help.glow/2, 250 - help.glow/2);
1724				break;
1725				//Crew Members
1726				//green
1727				case 13: ct.color = RGB(120- help.glow/4 - (Math.random()*20), 220 - help.glow/4, 120- help.glow/4); break;
1728				//Yellow
1729				case 14: ct.color = RGB(220- help.glow/4 - (Math.random()*20), 210 - help.glow/4, 120- help.glow/4); break;
1730				//pink
1731				case 15: ct.color = RGB(255 - help.glow/8, 70 - help.glow/4, 70 - help.glow / 4); break;
1732				//Blue
1733				case 16: ct.color = RGB(75, 75, 255- help.glow/4 - (Math.random()*20)); break;
1734
1735
1736				case 17: //Enemy : Orange
1737				  ct.color = RGB(250 - help.glow/2, 130 - help.glow/2, 20);
1738				break;
1739				case 18: //Enemy : Gray
1740				  ct.color = RGB(130- help.glow/2, 130 - help.glow/2, 130 - help.glow/2);
1741				break;
1742				case 19: //Enemy : Dark gray
1743				  ct.color = RGB(60- help.glow/8, 60 - help.glow/8, 60 - help.glow/8);
1744				break;
1745				//Purple
1746				case 20: ct.color = RGB(220 - help.glow / 4 - (Math.random() * 20), 120 - help.glow / 4, 210 - help.glow / 4); break;
1747
1748				case 21: //Enemy : Light Gray
1749				  ct.color = RGB(180- help.glow/2, 180 - help.glow/2, 180 - help.glow/2);
1750				break;
1751				case 22: //Enemy : Indicator Gray
1752				  ct.color = RGB(230- help.glow/2, 230- help.glow/2, 230- help.glow/2);
1753				break;
1754				case 23: //Enemy : Indicator Gray
1755				  ct.color = RGB(255- help.glow/2 - (Math.random() * 40) , 255- help.glow/2 - (Math.random() * 40), 255- help.glow/2 - (Math.random() * 40));
1756				break;
1757
1758				//Trophies
1759				//cyan
1760				case 30: ct.color = RGBf(160, 200, 220); break;
1761				//Purple
1762				case 31: ct.color = RGBf(220, 120, 210); break;
1763				//Yellow
1764				case 32: ct.color = RGBf(220, 210, 120); break;
1765				//red
1766				case 33: ct.color = RGBf(255, 70, 70); break;
1767				//green
1768				case 34: ct.color = RGBf(120, 220, 120); break;
1769				//Blue
1770				case 35: ct.color = RGBf(75, 75, 255); break;
1771				//Gold
1772				case 36:  ct.color = RGB(180, 120, 20); break;
1773				case 37: //Trinket
1774					if (!trinketcolset) {
1775						trinketr = 200 - (Math.random() * 64);
1776						trinketg = 200 - (Math.random() * 128);
1777						trinketb = 164 + (Math.random() * 60);
1778						trinketcolset = true;
1779					}
1780				  ct.color = RGBf(trinketr, trinketg, trinketb);
1781				break;
1782				//Silver
1783				case 38:  ct.color = RGBf(196, 196, 196); break;
1784				//Bronze
1785				case 39:  ct.color = RGBf(128, 64, 10); break;
1786				//Awesome
1787				case 40: //Teleporter in action!
1788				  temp = Math.random() * 150;
1789				  if(temp<33){
1790				    ct.color = RGBf(255 - (Math.random() * 64), 64 + (Math.random() * 64), 64 + (Math.random() * 64));
1791					}else if (temp < 66) {
1792						ct.color = RGBf(64 + (Math.random() * 64), 255 - (Math.random() * 64), 64 + (Math.random() * 64));
1793					}else if (temp < 100) {
1794						ct.color = RGBf(64 + (Math.random() * 64), 64 + (Math.random() * 64), 255 - (Math.random() * 64));
1795					}else {
1796						ct.color = RGBf(164+(Math.random()*64),164+(Math.random()*64), 255-(Math.random()*64))
1797					}
1798				break;
1799
1800				case 100: //Inactive Teleporter
1801				  temp = (help.glow/2) + (Math.random() * 8);
1802				  ct.color = RGB(42 + temp, 42 + temp, 42 + temp);
1803				break;
1804				case 101: //Active Teleporter
1805				  ct.color = RGB(164+(Math.random()*64),164+(Math.random()*64), 255-(Math.random()*64))
1806				break;
1807				case 102: //Teleporter in action!
1808				  temp = Math.random() * 150;
1809				  if(temp<33){
1810				    ct.color = RGB(255 - (Math.random() * 64), 64 + (Math.random() * 64), 64 + (Math.random() * 64));
1811					}else if (temp < 66) {
1812						ct.color = RGB(64 + (Math.random() * 64), 255 - (Math.random() * 64), 64 + (Math.random() * 64));
1813					}else if (temp < 100) {
1814						ct.color = RGB(64 + (Math.random() * 64), 64 + (Math.random() * 64), 255 - (Math.random() * 64));
1815					}else {
1816						ct.color = RGB(164+(Math.random()*64),164+(Math.random()*64), 255-(Math.random()*64))
1817					}
1818				break;
1819
1820				default: ct.color = 0xFFFFFF;	break;
1821			}
1822		}
1823
1824		public function setcolreal(t:int):void {
1825			ct.color = t;
1826		}
1827
1828		public function drawcoloredtile(x:int, y:int, t:int, r:int, g:int, b:int):void {
1829			tpoint.x = x; tpoint.y = y;
1830			setcolreal(RGB(r,g,b));
1831			tiles[t].colorTransform(tiles_rect, ct);
1832			backbuffer.copyPixels(tiles[t], tiles_rect, tpoint);
1833		}
1834
1835		public function drawhuetile(x:int, y:int, t:int, c:int):void {
1836			tpoint.x = x; tpoint.y = y;
1837			switch(c) {
1838				case 0:setcolreal(RGB(250-(Math.random()*32), 250-(Math.random()*32), 10)); break;
1839				case 1:setcolreal(RGB(250-(Math.random()*32), 250-(Math.random()*32), 10)); break;
1840
1841				default:setcolreal(RGB(250-(Math.random()*32), 250-(Math.random()*32), 10)); break;
1842			}
1843
1844			tiles[t].colorTransform(tiles_rect, ct);
1845			backbuffer.copyPixels(tiles[t], tiles_rect, tpoint);
1846		}
1847
1848		public function drawcrewman(x:int, y:int, t:int, act:Boolean, help:helpclass, noshift:Boolean=false):void {
1849			if (!act) {
1850				if (noshift) {
1851					if (flipmode) { drawspritesetcol(x, y, 14, 19, help);
1852					}else{ drawspritesetcol(x, y, 12, 19, help); }
1853				}else{
1854					if (flipmode) { drawspritesetcol(x - 8, y, 14, 19, help);
1855				  }else{ drawspritesetcol(x - 8, y, 12, 19, help); }
1856				}
1857			}else {
1858				if (flipmode) crewframe += 6;
1859
1860				switch(t) {
1861					case 0: drawspritesetcol(x, y, crewframe, 0, help);	break;
1862					case 1: drawspritesetcol(x, y, crewframe, 20, help);	break;
1863					case 2: drawspritesetcol(x, y, crewframe, 14, help);	break;
1864					case 3: drawspritesetcol(x, y, crewframe, 15, help);	break;
1865					case 4: drawspritesetcol(x, y, crewframe, 13, help);	break;
1866					case 5: drawspritesetcol(x, y, crewframe, 16, help);	break;
1867				}
1868
1869				if (flipmode) crewframe -= 6;
1870			}
1871		}
1872
1873		public function drawtrophytext(obj:entityclass, help:helpclass):void {
1874			if (obj.trophytext < 15) {
1875				temp = (196 * obj.trophytext) / 15;	temp2 = (196 * obj.trophytext) / 15; temp3 = ((255 - help.glow) * obj.trophytext) / 15;
1876			}else { temp = 196;	temp2 = 196; temp3 = 255 - help.glow; }
1877			switch(obj.trophytype) {
1878				case 1:
1879				  print( -1, 6, "SPACE STATION 1 MASTERED", temp, temp2, temp3, true);
1880					print( -1, 16, "Obtain a V Rank in this Time Trial", temp, temp2, temp3, true);
1881				break;
1882				case 2:
1883				  print( -1, 6, "LABORATORY MASTERED", temp, temp2, temp3, true);
1884					print( -1, 16, "Obtain a V Rank in this Time Trial", temp, temp2, temp3, true);
1885				break;
1886				case 3:
1887				  print( -1, 6, "THE TOWER MASTERED", temp, temp2, temp3, true);
1888					print( -1, 16, "Obtain a V Rank in this Time Trial", temp, temp2, temp3, true);
1889				break;
1890				case 4:
1891				  print( -1, 6, "SPACE STATION 2 MASTERED", temp, temp2, temp3, true);
1892					print( -1, 16, "Obtain a V Rank in this Time Trial", temp, temp2, temp3, true);
1893				break;
1894				case 5:
1895				  print( -1, 6, "WARP ZONE MASTERED", temp, temp2, temp3, true);
1896					print( -1, 16, "Obtain a V Rank in this Time Trial", temp, temp2, temp3, true);
1897				break;
1898				case 6:
1899				  print( -1, 6, "FINAL LEVEL MASTERED", temp, temp2, temp3, true);
1900					print( -1, 16, "Obtain a V Rank in this Time Trial", temp, temp2, temp3, true);
1901				break;
1902				case 7:
1903				  print( -1, 6, "GAME COMPLETE", temp, temp2, temp3, true);
1904					print( -1, 16, "Complete the game", temp, temp2, temp3, true);
1905				break;
1906				case 8:
1907				  print( -1, 6, "FLIP MODE COMPLETE", temp, temp2, temp3, true);
1908					print( -1, 16, "Complete the game in flip mode", temp, temp2, temp3, true);
1909				break;
1910				case 9:
1911					print( -1, 11, "Win with less than 50 deaths", temp, temp2, temp3, true);
1912				break;
1913				case 10:
1914					print( -1, 11, "Win with less than 100 deaths", temp, temp2, temp3, true);
1915				break;
1916				case 11:
1917					print( -1, 11, "Win with less than 250 deaths", temp, temp2, temp3, true);
1918				break;
1919				case 12:
1920					print( -1, 11, "Win with less than 500 deaths", temp, temp2, temp3, true);
1921				break;
1922				case 13:
1923					print( -1, 11, "Last 5 seconds on the Super Gravitron", temp, temp2, temp3, true);
1924				break;
1925				case 14:
1926					print( -1, 11, "Last 10 seconds on the Super Gravitron", temp, temp2, temp3, true);
1927				break;
1928				case 15:
1929					print( -1, 11, "Last 15 seconds on the Super Gravitron", temp, temp2, temp3, true);
1930				break;
1931				case 16:
1932					print( -1, 11, "Last 20 seconds on the Super Gravitron", temp, temp2, temp3, true);
1933				break;
1934				case 17:
1935					print( -1, 11, "Last 30 seconds on the Super Gravitron", temp, temp2, temp3, true);
1936				break;
1937				case 18:
1938					print( -1, 11, "Last 1 minute on the Super Gravitron", temp, temp2, temp3, true);
1939				break;
1940				case 20:
1941				  print( -1, 6, "MASTER OF THE UNIVERSE", temp, temp2, temp3, true);
1942					print( -1, 16, "Complete the game in no death mode", temp, temp2, temp3, true);
1943				break;
1944			}
1945		}
1946
1947		public function drawsprite(x:int, y:int, t:int, r:int, g:int, b:int):void {
1948			tpoint.x = x; tpoint.y = y;
1949			setcolreal(RGB(r,g,b));
1950			sprites[t].colorTransform(sprites_rect, ct);
1951			backbuffer.copyPixels(sprites[t], sprites_rect, tpoint);
1952		}
1953
1954		public function drawgravityline(t:int, obj:entityclass):void {
1955			if (obj.entities[t].life == 0) {
1956				/*
1957				switch(linestate) {
1958					case 0: backbuffer.fillRect(line_rect, RGB(0, 200, 0)); break;
1959					case 1: backbuffer.fillRect(line_rect, RGB(16, 245, 0)); break;
1960					case 2: backbuffer.fillRect(line_rect, RGB(0, 245, 16)); break;
1961					case 3: backbuffer.fillRect(line_rect, RGB(16, 200, 0)); break;
1962					case 4: backbuffer.fillRect(line_rect, RGB(24, 255, 16)); break;
1963					case 5: backbuffer.fillRect(line_rect, RGB(16, 235, 0)); break;
1964					case 6: backbuffer.fillRect(line_rect, RGB(0, 164, 16)); break;
1965					case 7: backbuffer.fillRect(line_rect, RGB(16, 245, 24)); break;
1966					case 8: backbuffer.fillRect(line_rect, RGB(0, 255, 16)); break;
1967					case 9: backbuffer.fillRect(line_rect, RGB(96, 245, 96)); break;
1968				}
1969				*/
1970				switch(linestate) {
1971					case 0: backbuffer.fillRect(line_rect, RGB(200 - 20, 200 - 20, 200 - 20)); break;
1972					case 1: backbuffer.fillRect(line_rect, RGB(225 - 30, 245 - 30, 245 - 30)); break;
1973					case 2: backbuffer.fillRect(line_rect, RGB(245 - 30, 245 - 30, 225 - 30)); break;
1974					case 3: backbuffer.fillRect(line_rect, RGB(164 - 10, 200 - 20, 200 - 20)); break;
1975					case 4: backbuffer.fillRect(line_rect, RGB(224 - 20, 255 - 30, 196 - 20)); break;
1976					case 5: backbuffer.fillRect(line_rect, RGB(205 - 20, 235 - 30, 196 - 20)); break;
1977					case 6: backbuffer.fillRect(line_rect, RGB(164 - 10, 164 - 10, 164 - 10)); break;
1978					case 7: backbuffer.fillRect(line_rect, RGB(225 - 30, 245 - 30, 205 - 20)); break;
1979					case 8: backbuffer.fillRect(line_rect, RGB(205 - 20, 255 - 30, 225 - 30)); break;
1980					case 9: backbuffer.fillRect(line_rect, RGB(245 - 30, 245 - 30, 245 - 30)); break;
1981				}
1982			}else{
1983				backbuffer.fillRect(line_rect, RGB(96, 96, 96));
1984			}
1985		}
1986
1987		public function drawentities(map:mapclass, obj:entityclass, help:helpclass):void {
1988			//Update line colours!
1989			if (linedelay <= 0) {
1990				linestate++;
1991				if (linestate >= 10) linestate = 0;
1992				linedelay = 2;
1993			}else {
1994				linedelay--;
1995			}
1996
1997			var j:int, k:int;
1998			for (var i:int = obj.nentity - 1; i >= 0; i--) {
1999				if (!obj.entities[i].invis && obj.entities[i].active) {
2000					if (obj.entities[i].size == 0) {      // Sprites
2001						if (flipmode) {
2002							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2003							setcol(obj.entities[i].colour, help);
2004							flipsprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2005							backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2006							if (map.warpx) {
2007								//screenwrapping!
2008								if (tpoint.x < 0) {
2009									tpoint.x += 320;
2010									backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2011								}
2012								if (tpoint.x > 300) {
2013									tpoint.x -= 320;
2014									backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2015								}
2016							}else if (map.warpy) {
2017								if (tpoint.y < 0) {
2018									tpoint.y += 230;
2019									backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2020								}
2021								if (tpoint.y > 210) {
2022									tpoint.y -= 230;
2023									backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2024								}
2025							}
2026						}else{
2027							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2028							setcol(obj.entities[i].colour, help);
2029							sprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2030							backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2031							if (map.warpx) {
2032								//screenwrapping!
2033								if (tpoint.x < 0) {
2034									tpoint.x += 320;
2035									backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2036								}
2037								if (tpoint.x > 300) {
2038									tpoint.x -= 320;
2039									backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2040								}
2041							}else if (map.warpy) {
2042								if (tpoint.y < 0) {
2043									tpoint.y += 230;
2044									backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2045								}
2046								if (tpoint.y > 210) {
2047									tpoint.y -= 230;
2048									backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2049								}
2050							}
2051						}
2052					}else if (obj.entities[i].size == 1) { // Tiles
2053					  tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2054					  backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2055					}else if (obj.entities[i].size == 2) { // Special: Moving platform, 4 tiles
2056					  tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2057						if(map.custommode){
2058							backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2059							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2060							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2061							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2062						}else{
2063							backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2064							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2065							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2066							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2067						}
2068					}else if (obj.entities[i].size == 3) { // Big chunky pixels!
2069					  prect.x = obj.entities[i].xp; prect.y = obj.entities[i].yp;
2070						//A seperate index of colours, for simplicity
2071						if(obj.entities[i].colour==1){
2072						  backbuffer.fillRect(prect, RGB(196 - (Math.random() * 64), 10, 10));
2073						}else if (obj.entities[i].colour == 2) {
2074							backbuffer.fillRect(prect, RGB(160- help.glow/2 - (Math.random()*20), 200- help.glow/2, 220 - help.glow));
2075						}
2076					}else if (obj.entities[i].size == 4) { // Small pickups
2077					  drawhuetile(obj.entities[i].xp, obj.entities[i].yp, obj.entities[i].tile, obj.entities[i].colour);
2078					}else if (obj.entities[i].size == 5) { //Horizontal Line
2079						line_rect.x = obj.entities[i].xp; line_rect.y = obj.entities[i].yp;
2080						line_rect.width = obj.entities[i].w; line_rect.height = 1;
2081						drawgravityline(i, obj);
2082					}else if (obj.entities[i].size == 6) { //Vertical Line
2083						line_rect.x = obj.entities[i].xp; line_rect.y = obj.entities[i].yp;
2084						line_rect.width = 1; line_rect.height = obj.entities[i].h;
2085						drawgravityline(i, obj);
2086					}else if (obj.entities[i].size == 7) { //Teleporter
2087					  drawtele(obj.entities[i].xp, obj.entities[i].yp, obj.entities[i].drawframe, obj.entities[i].colour, help);
2088					}else if (obj.entities[i].size == 8) { // Special: Moving platform, 8 tiles
2089					  tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2090						if (map.custommode) {
2091							backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2092							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2093							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2094							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2095							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2096							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2097							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2098							tpoint.x += 8; backbuffer.copyPixels(entcolours[obj.entities[i].drawframe], tiles_rect, tpoint);
2099						}else{
2100							backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2101							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2102							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2103							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2104							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2105							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2106							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2107							tpoint.x += 8; backbuffer.copyPixels(tiles[obj.entities[i].drawframe], tiles_rect, tpoint);
2108						}
2109					}else if (obj.entities[i].size == 9) {      // Really Big Sprite! (2x2)
2110						if (flipmode) {
2111							setcol(obj.entities[i].colour, help);
2112
2113							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2114							flipsprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2115							backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2116
2117							tpoint.x = obj.entities[i].xp+32; tpoint.y = obj.entities[i].yp;
2118							flipsprites[obj.entities[i].drawframe+1].colorTransform(sprites_rect, ct);
2119							backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe+1], sprites_rect, tpoint);
2120
2121							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp+32;
2122							flipsprites[obj.entities[i].drawframe+12].colorTransform(sprites_rect, ct);
2123							backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe+12], sprites_rect, tpoint);
2124
2125							tpoint.x = obj.entities[i].xp+32; tpoint.y = obj.entities[i].yp+32;
2126							flipsprites[obj.entities[i].drawframe+13].colorTransform(sprites_rect, ct);
2127							backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe + 13], sprites_rect, tpoint);
2128						}else{
2129							setcol(obj.entities[i].colour, help);
2130
2131							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2132							sprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2133							backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2134
2135							tpoint.x = obj.entities[i].xp+32; tpoint.y = obj.entities[i].yp;
2136							sprites[obj.entities[i].drawframe+1].colorTransform(sprites_rect, ct);
2137							backbuffer.copyPixels(sprites[obj.entities[i].drawframe+1], sprites_rect, tpoint);
2138
2139							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp+32;
2140							sprites[obj.entities[i].drawframe+12].colorTransform(sprites_rect, ct);
2141							backbuffer.copyPixels(sprites[obj.entities[i].drawframe+12], sprites_rect, tpoint);
2142
2143							tpoint.x = obj.entities[i].xp+32; tpoint.y = obj.entities[i].yp+32;
2144							sprites[obj.entities[i].drawframe+13].colorTransform(sprites_rect, ct);
2145							backbuffer.copyPixels(sprites[obj.entities[i].drawframe + 13], sprites_rect, tpoint);
2146						}
2147					}else if (obj.entities[i].size == 10) {      // 2x1 Sprite
2148						if (flipmode) {
2149							setcol(obj.entities[i].colour, help);
2150
2151							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2152							flipsprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2153							backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2154
2155							tpoint.x = obj.entities[i].xp+32; tpoint.y = obj.entities[i].yp;
2156							flipsprites[obj.entities[i].drawframe+1].colorTransform(sprites_rect, ct);
2157							backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe + 1], sprites_rect, tpoint);
2158						}else{
2159							setcol(obj.entities[i].colour, help);
2160
2161							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2162							sprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2163							backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2164
2165							tpoint.x = obj.entities[i].xp+32; tpoint.y = obj.entities[i].yp;
2166							sprites[obj.entities[i].drawframe+1].colorTransform(sprites_rect, ct);
2167							backbuffer.copyPixels(sprites[obj.entities[i].drawframe + 1], sprites_rect, tpoint);
2168						}
2169					}else if (obj.entities[i].size == 11) { //The fucking elephant
2170						setcol(obj.entities[i].colour, help);
2171						images[3].colorTransform(images[3].rect, ct);
2172					  drawimage(3, obj.entities[i].xp, obj.entities[i].yp);
2173					}else if (obj.entities[i].size == 12) {      // Regular sprites that don't wrap
2174						if (flipmode) {
2175							//forget this for a minute;
2176							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2177							setcol(obj.entities[i].colour, help);
2178							flipsprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2179							backbuffer.copyPixels(flipsprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2180
2181							//if we're outside the screen, we need to draw indicators
2182							if (obj.entities[i].xp < -20 && obj.entities[i].vx > 0) {
2183								if (obj.entities[i].xp < -100) {
2184									tpoint.x = -5 + (int(( -obj.entities[i].xp) / 10));
2185								}else{
2186								  tpoint.x = 5;
2187								}
2188
2189								tpoint.y = tpoint.y+4;
2190								setcol(23, help);
2191							  tiles[1167].colorTransform(tiles_rect, ct);
2192					      backbuffer.copyPixels(tiles[1167], tiles_rect, tpoint);
2193							}else if (obj.entities[i].xp > 340 && obj.entities[i].vx < 0) {
2194								if (obj.entities[i].xp > 420) {
2195									tpoint.x = 320 - (int(( obj.entities[i].xp-320) / 10));
2196								}else{
2197								  tpoint.x = 310;
2198								}
2199
2200								tpoint.y = tpoint.y+4;
2201								setcol(23, help);
2202							  tiles[1166].colorTransform(tiles_rect, ct);
2203					      backbuffer.copyPixels(tiles[1166], tiles_rect, tpoint);
2204							}
2205						}else{
2206							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2207							setcol(obj.entities[i].colour, help);
2208							sprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2209							backbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, tpoint);
2210
2211							//if we're outside the screen, we need to draw indicators
2212
2213							if (obj.entities[i].xp < -20 && obj.entities[i].vx > 0) {
2214								if (obj.entities[i].xp < -100) {
2215									tpoint.x = -5 + (int(( -obj.entities[i].xp) / 10));
2216								}else{
2217								  tpoint.x = 5;
2218								}
2219
2220								tpoint.y = tpoint.y+4;
2221								setcol(23, help);
2222							  tiles[1167].colorTransform(tiles_rect, ct);
2223					      backbuffer.copyPixels(tiles[1167], tiles_rect, tpoint);
2224							}else if (obj.entities[i].xp > 340 && obj.entities[i].vx < 0) {
2225								if (obj.entities[i].xp > 420) {
2226									tpoint.x = 320 - (int(( obj.entities[i].xp-320) / 10));
2227								}else{
2228								  tpoint.x = 310;
2229								}
2230
2231								tpoint.y = tpoint.y+4;
2232								setcol(23, help);
2233							  tiles[1166].colorTransform(tiles_rect, ct);
2234					      backbuffer.copyPixels(tiles[1166], tiles_rect, tpoint);
2235							}
2236						}
2237					}else if (obj.entities[i].size == 13) {      // Special for epilogue: huge hero!
2238						if (flipmode) {
2239							scaleMatrix = new Matrix();
2240			        scaleMatrix.scale(6, 6);
2241  		        bigbuffer.fillRect(bigbuffer.rect, 0x000000);
2242
2243							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2244							setcol(obj.entities[i].colour, help);
2245							flipsprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2246							bigbuffer.copyPixels(flipsprites[obj.entities[i].drawframe], sprites_rect, new Point(0, 0));
2247
2248							scaleMatrix.translate(obj.entities[i].xp, obj.entities[i].yp);
2249							backbuffer.draw(bigbufferscreen, scaleMatrix);
2250							scaleMatrix.translate(-obj.entities[i].xp, -obj.entities[i].yp);
2251						}else {
2252							scaleMatrix = new Matrix();
2253			        scaleMatrix.scale(6, 6);
2254  		        bigbuffer.fillRect(bigbuffer.rect, 0x000000);
2255
2256							tpoint.x = obj.entities[i].xp; tpoint.y = obj.entities[i].yp;
2257							setcol(obj.entities[i].colour, help);
2258							sprites[obj.entities[i].drawframe].colorTransform(sprites_rect, ct);
2259							bigbuffer.copyPixels(sprites[obj.entities[i].drawframe], sprites_rect, new Point(0, 0));
2260
2261							scaleMatrix.translate(obj.entities[i].xp, obj.entities[i].yp);
2262							backbuffer.draw(bigbufferscreen, scaleMatrix);
2263							scaleMatrix.translate(-obj.entities[i].xp, -obj.entities[i].yp);
2264						}
2265					}
2266				}
2267			}
2268		}
2269
2270		public function drawbuffertile(x:int, y:int, t:int):void {
2271			tpoint.x = x; tpoint.y = y;
2272			buffer.copyPixels(tiles[t], tiles_rect, tpoint);
2273		}
2274
2275		public function drawforetile(x:int, y:int, t:int):void {
2276			tpoint.x = x; tpoint.y = y;
2277			frontbuffer.copyPixels(tiles[t], tiles_rect, tpoint);
2278		}
2279
2280		public function drawforetile2(x:int, y:int, t:int):void {
2281			tpoint.x = x; tpoint.y = y;
2282			frontbuffer.copyPixels(tiles2[t], tiles_rect, tpoint);
2283		}
2284
2285		public function drawforetile3(x:int, y:int, t:int, off:int):void {
2286			tpoint.x = x; tpoint.y = y;
2287			frontbuffer.copyPixels(tiles3[t+(off*30)], tiles_rect, tpoint);
2288		}
2289
2290
2291		public function drawtele(x:int, y:int, t:int, c:int, help:helpclass):void {
2292			tpoint.x = x; tpoint.y = y;
2293
2294			setcolreal(RGB(16,16,16));
2295			tele[0].colorTransform(tele_rect, ct);
2296			backbuffer.copyPixels(tele[0], tele_rect, tpoint);
2297
2298			setcol(c, help);
2299			if (t > 9) t = 8; if (t < 0) t = 0;
2300			tele[t].colorTransform(tele_rect, ct);
2301			backbuffer.copyPixels(tele[t], tele_rect, tpoint);
2302		}
2303
2304		public function drawtile(x:int, y:int, t:int):void {
2305			tpoint.x = x; tpoint.y = y;
2306			backbuffer.copyPixels(tiles[t], tiles_rect, tpoint);
2307		}
2308
2309		public function drawtile2(x:int, y:int, t:int):void {
2310			tpoint.x = x; tpoint.y = y;
2311			backbuffer.copyPixels(tiles2[t], tiles_rect, tpoint);
2312		}
2313
2314		public function drawtile3(x:int, y:int, t:int, off:int):void {
2315			tpoint.x = x; tpoint.y = y;
2316			backbuffer.copyPixels(tiles3[t+(off*30)], tiles_rect, tpoint);
2317		}
2318
2319		public function drawtowertile3(x:int, y:int, t:int, off:int):void {
2320			tpoint.x = x; tpoint.y = y;
2321			towerbuffer.copyPixels(tiles3[t+(off*30)], tiles_rect, tpoint);
2322		}
2323
2324		public function drawtowertile(x:int, y:int, t:int):void {
2325			tpoint.x = x; tpoint.y = y;
2326			towerbuffer.copyPixels(tiles2[t], tiles_rect, tpoint, null, null, true);
2327		}
2328
2329		public function drawmap(map:mapclass):void {
2330			if (!foregrounddrawn) {
2331				frontbuffer.fillRect(frontbuffer.rect, 0x000000);
2332				if(map.tileset==0){
2333					for (j = 0; j < 29+map.extrarow; j++) {
2334						for (i = 0; i < 40; i++) {
2335							if(map.contents[i + map.vmult[j]]>0) drawforetile(i * 8, j * 8, map.contents[i + map.vmult[j]]);
2336						}
2337					}
2338				}else if (map.tileset == 1) {
2339					for (j = 0; j < 29+map.extrarow; j++) {
2340						for (i = 0; i < 40; i++) {
2341							if(map.contents[i + map.vmult[j]]>0) drawforetile2(i * 8, j * 8, map.contents[i + map.vmult[j]]);
2342						}
2343					}
2344				}else if (map.tileset == 2) {
2345					for (j = 0; j < 29+map.extrarow; j++) {
2346						for (i = 0; i < 40; i++) {
2347							if(map.contents[i + map.vmult[j]]>0) drawforetile3(i * 8, j * 8, map.contents[i + map.vmult[j]],map.rcol);
2348						}
2349					}
2350				}
2351				foregrounddrawn=true;
2352			}
2353
2354			backbuffer.copyPixels(frontbuffer, frontbuffer.rect, tl);
2355		}
2356
2357		public function drawfinalmap(map:mapclass):void {
2358			//Update colour cycling for final level
2359			if (map.final_colormode) {
2360				map.final_aniframedelay--;
2361				if(map.final_aniframedelay==0) foregrounddrawn=false;
2362				if (map.final_aniframedelay <= 0) {
2363					map.final_aniframedelay = 2;
2364					map.final_aniframe++;
2365					if (map.final_aniframe >= 4) map.final_aniframe = 0;
2366				}
2367			}
2368
2369			if (!foregrounddrawn) {
2370				frontbuffer.fillRect(frontbuffer.rect, 0x000000);
2371				if(map.tileset==0){
2372					for (j = 0; j < 29+map.extrarow; j++) {
2373						for (i = 0; i < 40; i++) {
2374							if(map.contents[i + map.vmult[j]]>0) drawforetile(i * 8, j * 8, map.finalat(i,j));
2375						}
2376					}
2377				}else if (map.tileset == 1) {
2378					for (j = 0; j < 29+map.extrarow; j++) {
2379						for (i = 0; i < 40; i++) {
2380							if(map.contents[i + map.vmult[j]]>0) drawforetile2(i * 8, j * 8, map.finalat(i,j));
2381						}
2382					}
2383				}
2384			  foregrounddrawn=true;
2385			}
2386
2387			backbuffer.copyPixels(frontbuffer, frontbuffer.rect, tl);
2388		}
2389
2390		public function drawmaptileat(xp:int, yp:int, mapx:int, mapy:int, map:mapclass):void {
2391			if (mapx < 100) mapx = 119;	if (mapy < 100) mapy = 119;
2392			if (mapx > 119) mapx = 100;	if (mapy > 119) mapy = 100;
2393		  if (map.explored[(mapx - 100) + ((mapy - 100) * 20)] == 1) {
2394				madrect.x = (mapx - 100)*12; madrect.y = (mapy - 100)*9; madrect.width = 12; madrect.height = 9;
2395				madpoint.x = xp; madpoint.y = yp;
2396				backbuffer.copyPixels(images[1], madrect, madpoint);
2397			}else {
2398				drawimage(2, xp, yp);
2399			}
2400		}
2401
2402		public function drawminimap(game:gameclass, map:mapclass):void {
2403			drawfillrect(272, 8, 40, 31, 196, 196, 196);
2404			drawfillrect(273, 9, 38, 29, 164,164,164);
2405			for (j = 0; j < 3; j++){
2406			  for (i = 0; i < 3; i++) {
2407					drawmaptileat(274 + (i * 12), 10 + (j * 9), game.roomx - 1 + i, game.roomy - 1 + j, map);
2408				}
2409			}
2410		}
2411
2412		public function drawrect(x:int, y:int, w:int, h:int, r:int, g:int, b:int):void {
2413			//Draw the retangle indicated by that object
2414			madrect.x = x; madrect.y = y;
2415			madrect.width = w; madrect.height = 1;
2416			backbuffer.fillRect(madrect, RGB(r,g,b));
2417
2418			madrect.width = 1; madrect.height = h;
2419			backbuffer.fillRect(madrect, RGB(r,g,b));
2420
2421			madrect.x = x + w - 1;
2422			madrect.width = 1; madrect.height = h;
2423			backbuffer.fillRect(madrect, RGB(r,g,b));
2424
2425			madrect.x = x; madrect.y = y + h - 1;
2426			madrect.width = w; madrect.height = 1;
2427			backbuffer.fillRect(madrect, RGB(r,g,b));
2428		}
2429
2430		public function drawfillrect(x:int, y:int, w:int, h:int, r:int, g:int, b:int):void {
2431			//Draw the retangle indicated by that object
2432			madrect.x = x; madrect.y = y;
2433			madrect.width = w; madrect.height = h;
2434			backbuffer.fillRect(madrect, RGB(r,g,b));
2435		}
2436
2437		public function printcrewname(x:int, y:int, t:int):void {
2438			//Print the name of crew member t in the right colour
2439			switch(t) {
2440				case 0: print(x, y, "Viridian", 16, 240, 240); break;
2441				case 1: print(x, y, "Violet", 240, 16, 240); break;
2442				case 2: print(x, y, "Vitellary", 240, 240, 16); break;
2443				case 3: print(x, y, "Vermilion", 240, 16, 16); break;
2444				case 4: print(x, y, "Verdigris", 16, 240, 16); break;
2445				case 5: print(x, y, "Victoria", 16, 16, 240); break;
2446			}
2447		}
2448
2449		public function printcrewnamestatus(x:int, y:int, t:int):void {
2450			//Print the status of crew member t in the right colour
2451			switch(t) {
2452				case 0: print(x, y, "(that's you!)", 12, 140, 140); break;
2453				case 1: print(x, y, "Rescued!", 140, 12, 140); break;
2454				case 2: print(x, y, "Rescued!", 140, 140, 12); break;
2455				case 3: print(x, y, "Rescued!", 140, 12, 12); break;
2456				case 4: print(x, y, "Rescued!", 12, 140, 12); break;
2457				case 5: print(x, y, "Rescued!", 12, 12, 140); break;
2458			}
2459		}
2460
2461		public function printcrewnamedark(x:int, y:int, t:int):void {
2462			//Print the name of crew member t as above, but in black and white
2463			switch(t) {
2464				case 0: print(x, y, "Viridian", 128,128,128); break;
2465				case 1: print(x, y, "Violet", 128,128,128); break;
2466				case 2: print(x, y, "Vitellary", 128,128,128); break;
2467				case 3: print(x, y, "Vermilion", 128,128,128); break;
2468				case 4: print(x, y, "Verdigris", 128,128,128); break;
2469				case 5: print(x, y, "Victoria", 128,128,128); break;
2470			}
2471		}
2472
2473		public function printmask(x:int, y:int, t:String, cen:Boolean = false):void {
2474			if (cen) x = 160 - (len(t) / 2);
2475			bfontpos = 0;
2476			for (var i:int = 0; i < t.length; i++) {
2477				cur = t.charCodeAt(i);
2478				tpoint.x = x + bfontpos-1; tpoint.y = y;
2479				if (flipmode) {
2480				  backbuffer.copyPixels(flipbfontmask[cur], bfont_rect, tpoint);
2481				}else{
2482				  backbuffer.copyPixels(bfontmask[cur], bfont_rect, tpoint);
2483				}
2484				bfontpos+=bfontlen[cur];
2485			}
2486		}
2487
2488		public function print(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean = false):void {
2489			if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
2490			if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
2491			ct.color = RGB(r, g, b);
2492			if (cen) x = 160 - (len(t) / 2);
2493			bfontpos = 0;
2494			for (var i:int = 0; i < t.length; i++) {
2495				cur = t.charCodeAt(i);
2496				tpoint.x = x + bfontpos; tpoint.y = y;
2497				if (flipmode) {
2498				  flipbfont[cur].colorTransform(bfont_rect, ct);
2499				  backbuffer.copyPixels(flipbfont[cur], bfont_rect, tpoint);
2500				}else {
2501				  bfont[cur].colorTransform(bfont_rect, ct);
2502				  backbuffer.copyPixels(bfont[cur], bfont_rect, tpoint);
2503				}
2504				bfontpos+=bfontlen[cur];
2505			}
2506		}
2507
2508		public function rprint(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean = false):void {
2509			if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
2510			if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
2511			ct.color = RGB(r, g, b);
2512			if (cen) x = 308 - (len(t));
2513			bfontpos = 0;
2514			for (var i:int = 0; i < t.length; i++) {
2515				cur = t.charCodeAt(i);
2516				tpoint.x = x + bfontpos; tpoint.y = y;
2517				if (flipmode) {
2518			   	flipbfont[cur].colorTransform(bfont_rect, ct);
2519				  backbuffer.copyPixels(flipbfont[cur], bfont_rect, tpoint);
2520				}else{
2521				  bfont[cur].colorTransform(bfont_rect, ct);
2522				  backbuffer.copyPixels(bfont[cur], bfont_rect, tpoint);
2523				}
2524				bfontpos+=bfontlen[cur];
2525			}
2526		}
2527
2528		public function printoff(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean = false):void {
2529			if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
2530			if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
2531			ct.color = RGB(r, g, b);
2532			if (cen) x = (160 - (len(t) / 2))+x;
2533			bfontpos = 0;
2534			for (var i:int = 0; i < t.length; i++) {
2535				cur = t.charCodeAt(i);
2536				tpoint.x = x + bfontpos; tpoint.y = y;
2537				if (flipmode) {
2538				  flipbfont[cur].colorTransform(bfont_rect, ct);
2539					backbuffer.copyPixels(flipbfont[cur], bfont_rect, tpoint);
2540				}else{
2541				  bfont[cur].colorTransform(bfont_rect, ct);
2542				  backbuffer.copyPixels(bfont[cur], bfont_rect, tpoint);
2543				}
2544				bfontpos+=bfontlen[cur];
2545			}
2546		}
2547
2548		public function bprint(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean=false):void {
2549			//printmask(x, y, t, cen);
2550			print(x, y - 1, t, 0, 0, 0, cen);
2551			if (cen) {
2552				printoff(-1, y, t, 0, 0, 0, cen); //kludge :i
2553				printoff(1, y, t, 0, 0, 0, cen);
2554			}else{
2555				print(x-1, y, t, 0, 0, 0, cen);
2556				print(x+1, y, t, 0, 0, 0, cen);
2557			}
2558			print(x, y+1, t, 0, 0, 0, cen);
2559
2560			print(x, y, t, r, g, b, cen);
2561		}
2562
2563		public function printmasktemptile(x:int, y:int, t:String, cen:Boolean = false):void {
2564			if (cen) x = 160- (len(t) / 2);
2565			bfontpos = 0;
2566			for (var i:int = 0; i < t.length; i++) {
2567				cur = t.charCodeAt(i);
2568				tpoint.x = x + bfontpos - 1; tpoint.y = y;
2569				if (flipmode) {
2570					temptile.copyPixels(flipbfontmask[cur], bfont_rect, tpoint);
2571				}else{
2572				  temptile.copyPixels(bfontmask[cur], bfont_rect, tpoint);
2573				}
2574				bfontpos+=bfontlen[cur];
2575			}
2576		}
2577
2578		public function printtemptile(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean = false):void {
2579			if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
2580			if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
2581			ct.color = RGB(r, g, b);
2582			if (cen) x = 160 - (len(t) / 2);
2583			bfontpos = 0;
2584			for (var i:int = 0; i < t.length; i++) {
2585				cur = t.charCodeAt(i);
2586				tpoint.x = x + bfontpos; tpoint.y = y;
2587				if (flipmode) {
2588				  flipbfont[cur].colorTransform(bfont_rect, ct);
2589					temptile.copyPixels(flipbfont[cur], bfont_rect, tpoint);
2590				}else{
2591				  bfont[cur].colorTransform(bfont_rect, ct);
2592				  temptile.copyPixels(bfont[cur], bfont_rect, tpoint);
2593				}
2594				bfontpos+=bfontlen[cur];
2595			}
2596		}
2597
2598		public function onscreen(t:int):Boolean {
2599			if (t >= -40 && t <= 280) return true;
2600			return false;
2601		}
2602
2603		public function bigrprint(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean = false, sc:Number = 2):void {
2604			if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
2605			if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
2606			ct.color = RGB(r, g, b);
2607
2608			scaleMatrix = new Matrix();
2609			scaleMatrix.scale(sc, sc);
2610
2611  		bigbuffer.fillRect(bigbuffer.rect, 0x000000);
2612
2613			x = x / sc;
2614
2615			x -= (len(t));
2616
2617			//if (r < -1) r = -1; if (g < 0) g = 0; if (b < 0) b = 0;?
2618			//if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
2619			ct.color = RGB(r, g, b);
2620			if(sc==2){
2621			  if (cen) x = 80 - (len(t)/2);
2622			}else if (sc == 3) {
2623				if (cen) x = 160/3 - (len(t) / 2);
2624			}else if (sc == 4) {
2625				if (cen) x = 40 - (len(t) / 2);
2626			}
2627			bfontpos = 0;
2628			for (i = 0; i < t.length; i++) {
2629				cur = t.charCodeAt(i);
2630				if (flipmode) {
2631				  flipbfont[cur].colorTransform(bfont_rect, ct);
2632				  bigbuffer.copyPixels(flipbfont[cur], bfont_rect, new Point(x + bfontpos, 0));
2633				}else {
2634				  bfont[cur].colorTransform(bfont_rect, ct);
2635				  bigbuffer.copyPixels(bfont[cur], bfont_rect, new Point(x + bfontpos, 0));
2636				}
2637				bfontpos+=bfontlen[cur];
2638			}
2639
2640			scaleMatrix.translate(0, y);
2641			backbuffer.draw(bigbufferscreen, scaleMatrix);
2642			scaleMatrix.translate(0, -y);
2643		}
2644
2645		public function bigprint(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean = false, sc:Number = 2):void {
2646			if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
2647			if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
2648			ct.color = RGB(r, g, b);
2649
2650			scaleMatrix = new Matrix();
2651			scaleMatrix.scale(sc, sc);
2652
2653  		bigbuffer.fillRect(bigbuffer.rect, 0x000000);
2654
2655			x = x / sc;
2656
2657			//if (r < -1) r = -1; if (g < 0) g = 0; if (b < 0) b = 0;?
2658			//if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
2659			ct.color = RGB(r, g, b);
2660			if(sc==2){
2661			  if (cen) x = 80 - (len(t)/2);
2662			}else if (sc == 3) {
2663				if (cen) x = 160/3 - (len(t) / 2);
2664			}else if (sc == 4) {
2665				if (cen) x = 40 - (len(t) / 2);
2666			}
2667			bfontpos = 0;
2668			for (var ti:int = 0; ti < t.length; ti++) {
2669				cur = t.charCodeAt(ti);
2670				if (flipmode) {
2671				  flipbfont[cur].colorTransform(bfont_rect, ct);
2672				  bigbuffer.copyPixels(flipbfont[cur], bfont_rect, new Point(x + bfontpos, 0));
2673				}else {
2674				  bfont[cur].colorTransform(bfont_rect, ct);
2675				  bigbuffer.copyPixels(bfont[cur], bfont_rect, new Point(x + bfontpos, 0));
2676				}
2677				bfontpos+=bfontlen[cur];
2678			}
2679
2680			scaleMatrix.translate(0, y);
2681			backbuffer.draw(bigbufferscreen, scaleMatrix);
2682			scaleMatrix.translate(0, -y);
2683		}
2684
2685		public function bprinttemptile(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean=false):void {
2686			printmasktemptile(x, y, t, cen);
2687			printtemptile(x, y, t, r, g, b, cen);
2688		}
2689
2690		public function len(t:String):int {
2691			bfontpos = 0;
2692		  for (var i:int = 0; i < t.length; i++) {
2693				cur = t.charCodeAt(i);
2694			  bfontpos+=bfontlen[cur];
2695			}
2696			return bfontpos;
2697		}
2698
2699		public function flashlight():void {
2700			backbuffer.fillRect(backbuffer.rect, 0xBBBBBB);
2701		}
2702
2703		public function screenshake():void {
2704			screenbuffer.lock();
2705			if(flipmode){
2706			  tpoint.x = int((Math.random() * 7) - 4); tpoint.y = int((Math.random() * 7) - 4);
2707			  flipmatrix.translate(tpoint.x, tpoint.y);
2708			  screenbuffer.draw(backbuffer, flipmatrix);
2709			  flipmatrix.translate(-tpoint.x, -tpoint.y);
2710			}else{
2711			  screenbuffer.copyPixels(backbuffer, backbuffer.rect, tl, null, null, false);
2712			  tpoint.x = (Math.random() * 7) - 4; tpoint.y = (Math.random() * 7) - 4;
2713			  screenbuffer.copyPixels(backbuffer, backbuffer.rect, tpoint, null, null, false);
2714			}
2715			screenbuffer.unlock();
2716
2717			backbuffer.lock();
2718			backbuffer.fillRect(backbuffer.rect, 0x000000);
2719			backbuffer.unlock();
2720		}
2721
2722		public function render():void {
2723			screenbuffer.lock();
2724			if(flipmode){
2725			  screenbuffer.draw(backbuffer, flipmatrix);
2726			}else{
2727			  screenbuffer.copyPixels(backbuffer, backbuffer.rect, tl, null, null, false);
2728			}
2729			screenbuffer.unlock();
2730
2731			backbuffer.lock();
2732			backbuffer.fillRect(backbuffer.rect, 0x000000);
2733			backbuffer.unlock();
2734		}
2735
2736		public function menuoffrender():void {
2737			screenbuffer.lock();
2738			screenbuffer.copyPixels(menubuffer, menubuffer.rect, tl, null, null, false);
2739
2740			if(flipmode){
2741			  flipmatrix.translate(0, menuoffset);
2742			  screenbuffer.draw(backbuffer, flipmatrix);
2743			  flipmatrix.translate(0, -menuoffset);
2744			}else {
2745				tpoint.x = 0;
2746				tpoint.y = menuoffset;
2747			  screenbuffer.copyPixels(backbuffer, backbuffer.rect, tpoint, null, null, false);
2748			}
2749
2750			screenbuffer.unlock();
2751
2752			backbuffer.lock();
2753			backbuffer.fillRect(backbuffer.rect, 0x000000);
2754			backbuffer.unlock();
2755		}
2756
2757		public var backgrounds:Vector.<BitmapData> = new Vector.<BitmapData>;
2758		public var images:Vector.<BitmapData> = new Vector.<BitmapData>;
2759		public var playerlevelimages:Vector.<BitmapData> = new Vector.<BitmapData>;
2760		public var mobileimages:Vector.<BitmapData> = new Vector.<BitmapData>;
2761		public var tele:Vector.<BitmapData> = new Vector.<BitmapData>;
2762	  public var tele_rect:Rectangle;
2763		public var tiles:Vector.<BitmapData> = new Vector.<BitmapData>;
2764		public var tiles2:Vector.<BitmapData> = new Vector.<BitmapData>;
2765		public var tiles3:Vector.<BitmapData> = new Vector.<BitmapData>;
2766		public var sprites:Vector.<BitmapData> = new Vector.<BitmapData>;
2767		public var flipsprites:Vector.<BitmapData> = new Vector.<BitmapData>;
2768		public var entcolours:Vector.<BitmapData> = new Vector.<BitmapData>;
2769		public var bfont:Vector.<BitmapData> = new Vector.<BitmapData>;
2770		public var bfontmask:Vector.<BitmapData> = new Vector.<BitmapData>;
2771		public var flipbfont:Vector.<BitmapData> = new Vector.<BitmapData>;
2772		public var flipbfontmask:Vector.<BitmapData> = new Vector.<BitmapData>;
2773		public var bfontlen:Vector.<int> = new Vector.<int>;
2774		public var bfontpos:int;
2775		public var cur:int;
2776		public var ct:ColorTransform;
2777	  public var tiles_rect:Rectangle;
2778	  public var sprites_rect:Rectangle;
2779	  public var bfont_rect:Rectangle;
2780	  public var bfontmask_rect:Rectangle;
2781		public var images_rect:Rectangle;
2782	  public var tl:Point; //The point at (0,0)
2783	  public var bg_rect:Rectangle, prect:Rectangle;
2784		public var line_rect:Rectangle;
2785
2786		public var temp:int;
2787		public var temp2:int;
2788		public var temp3:int;
2789		public var tempstring:String;
2790		public var alphamult:uint;
2791		public var stemp:String;
2792		public var buffer:BitmapData;
2793		public var i:int, j:int, k:int, m:int;
2794		public var tpoint:Point, trect:Rectangle;
2795		public var madrect:Rectangle, madpoint:Point;
2796
2797		public var temptile:BitmapData;
2798		public var tempsprite:BitmapData;
2799		public var footerrect:Rectangle;
2800		//Actual backgrounds
2801		public var backbuffer:BitmapData;
2802		public var frontbuffer:BitmapData;
2803		public var towerbuffer:BitmapData;
2804		public var screenbuffer:BitmapData;
2805		public var menubuffer:BitmapData;
2806		public var screen:Bitmap;
2807		public var updatebackground:Boolean;
2808		//Textbox Stuff
2809		public var ntextbox:int;
2810		public var textbox:Vector.<textboxclass> = new Vector.<textboxclass>;
2811		//Cutscene bars
2812		public var showcutscenebars:Boolean;
2813		public var cutscenebarspos:int;
2814		//Background Stuff
2815		public var stars:Vector.<Rectangle> = new Vector.<Rectangle>;
2816		public var starsspeed:Vector.<int> = new Vector.<int>;
2817
2818		//special for weird lab rooms
2819		public var spcol:int, spcoldel:int;
2820		//Menu stuff
2821		public var crewframe:int, crewframedelay:int;
2822		public var menuoffset:int, resumegamemode:Boolean;
2823		//More Background Stuff
2824		public var backboxes:Vector.<Rectangle> = new Vector.<Rectangle>;
2825		public var backboxvx:Vector.<int> = new Vector.<int>;
2826		public var backboxvy:Vector.<int> = new Vector.<int>;
2827		public var backboxint:Vector.<Number> = new Vector.<Number>;
2828		public var bcol:int, bcol2:int, rcol:int;
2829		public var backboxrect:Rectangle;
2830		public var linestate:int, linedelay:int;
2831		public var backoffset:int, backgrounddrawn:Boolean;
2832		public var foregrounddrawn:Boolean;
2833
2834		public var warpskip:int;
2835		public var warpfcol:int, warpbcol:int;
2836		public var warprect:Rectangle;
2837		//Fade effect
2838		public var fademode:int;
2839		public var fadeamount:int;
2840		public var fadebars:Vector.<int> = new Vector.<int>;
2841		//Single trinket colour
2842		public var trinketcolset:Boolean, trinketr:int, trinketg:int, trinketb:int;
2843		//Flipmode Stuff
2844		public var flipmode:Boolean, setflipmode:Boolean;
2845		public var flipmatrix:Matrix = new Matrix();
2846		public var flipfontmatrix:Matrix = new Matrix();
2847		public var flipfontmatrix2:Matrix = new Matrix();
2848
2849		public var scaleMatrix:Matrix = new Matrix();
2850		public var bigbuffer:BitmapData;
2851		public var bigbufferscreen:Bitmap;
2852
2853		//Mobile stuff
2854		public var screensizemultiplier:Number;
2855
2856		public var screenoffx:int, screenoffy:int;
2857
2858		public var buttonimg:Vector.<BitmapData> = new Vector.<BitmapData>;
2859	  public var buttonscreen:Vector.<Bitmap> = new Vector.<Bitmap>;
2860		public var buttonactive:Vector.<Boolean> = new Vector.<Boolean>;
2861		public var buttonhighlight:Vector.<int> = new Vector.<int>;
2862
2863		public var buttonpos:Vector.<Point> = new Vector.<Point>;
2864		public var currentbuttontype:Vector.<int> = new Vector.<int>;
2865		public var newbuttontype:Vector.<int> = new Vector.<int>;
2866		public var buttonstate:Vector.<int> = new Vector.<int>;
2867		public var buttonlerp:Vector.<int> = new Vector.<int>;
2868		public var buttonframe:Vector.<int> = new Vector.<int>;
2869		public var buttonsize:int, buttonyspacing:int, buttonxspacing:int, buttonydiff:int;
2870
2871		public var b_gap:int = 40, b_size:int = 26;
2872		public var devicex:int, devicey:int;
2873	}
2874}