"use strict";

DATA_SDS_SCRIPTED_EVENTS["Raviene_checkin"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["Raviene_checkin"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// this is a multi-dialog event; we want to lock down movement between them
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
					cacheVars.meli.controls.held.A=true;

					cacheVars.ravi = new GO_Raviene();
					cacheVars.ravi.sprite.x = (34<<4);
					cacheVars.ravi.sprite.y = (1<<4);
					cacheVars.ravi.direction = 1;
					//cacheVars.meli.sprite.map_room.addSprite(cacheVars.ravi.sprite);
					cacheVars.ravi.addToRoom(cacheVars.meli.sprite.map_room);
					cacheVars.objMgr.add(cacheVars.ravi);

					//cacheVars.ravi.spell = cacheVars.ravi.SPELLS.KILL;
					//cacheVars.ravi.spell_target_name = "Hedgeboar";
				}

				cacheVars.bgmName = SDS.bgmName;
				SDS.switchBgm("A-minor Waltz in 4",true);
			},
			controls : {
				"Raviene":{right:true},
				"Meli":{A:true}
			},
			customPost : noop
		};
evt[t+=30] = {
			customPre : noop,
			controls : {
				"Raviene":{
					A:true,
					up:true
				},
				"Meli":{A:false}
			},
			customPost : noop
		};
evt[t+=5] = {
			customPre : noop,
			controls : {
				"Raviene":{
					right:false,
					up:false
				}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.direction=-1;
				}
				if (DATA_SDS_DIALOGUE["Raviene_c4_checkin_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_checkin_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				SDS.switchBgm(null,false);

				if (DATA_SDS_DIALOGUE["Raviene_c4_checkin_1"]){
					//cacheVars.wait=true; // need the event to proceed briefly so Meli can react
					cacheVars.waitPending = true;
					cacheVars.dlgMgr.open("Raviene_c4_checkin_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							cacheVars.waitPending = false;
							cacheVars.dlgPath = retval ? "B" : "A";
						}
					});
				}
			},
			controls : {
				"Meli":{A:true}
			},
			customPost : noop
		};
evt[t+=5] = {
			customPre : noop,
			controls : {
				"Meli":{A:false}
			},
			customPost : function(cacheVars){
				cacheVars.wait = cacheVars.waitPending;
				cacheVars.waitPending = false;
			}
		};
evt[++t] = {
			customPre : function(cacheVars){
				var next = "Raviene_c4_checkin_"+cacheVars.dlgPath+"2";
				
				// music swap based on path
				if (SDS.bgm) SDS.bgm.stop();
				if (cacheVars.dlgPath==="A"){
					SDS.switchBgm("Take On Them!",true);
				}
				else{
					SDS.switchBgm("Self; Pity",true);
				}
				
				if (DATA_SDS_DIALOGUE[next]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open(next,function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				var next = "Raviene_c4_checkin_"+cacheVars.dlgPath+"3";

				if (cacheVars.dlgPath==="A"){
					cacheVars.frameCounter+=3; // skip Meli movement
				}
				else if (cacheVars.ravi) cacheVars.ravi.hurt=true;
				
				if (DATA_SDS_DIALOGUE[next]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open(next,function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Meli":{
					left:true
				}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Meli":{
					left:false
				}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				var next = "Raviene_c4_checkin_"+cacheVars.dlgPath+"4";
				
				if (DATA_SDS_DIALOGUE[next]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open(next,function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				var next = "Raviene_c4_checkin_"+cacheVars.dlgPath+"5";
				
				if (DATA_SDS_DIALOGUE[next]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open(next,function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.ravi) cacheVars.ravi.hurt=false;
			},
			controls : {
				"Raviene":{
					right:true,
					A:false
				}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Raviene":{
					right:false,
					left:true
				}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Raviene":{
					A:true,
					up:true
				}
			},
			customPost : noop
		};
evt[t+=60] = {
			customPre : function(cacheVars){
				if (cacheVars.ravi){
					//cacheVars.ravi.sprite.map_room.removeSprite(cacheVars.ravi.sprite);
					cacheVars.ravi.clearFromRoom(cacheVars.ravi.sprite.map_room);
					cacheVars.objMgr.remove(cacheVars.ravi);
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				var next = "Raviene_c4_checkin_"+cacheVars.dlgPath+"6";
				if (cacheVars.dlgPath==="B" && cacheVars.meli){
					cacheVars.meli.overrideFrame = 27;
					cacheVars.meli.overrideFrame += cacheVars.meli.direction>0 ? 0 : 32;
				}
				
				if (DATA_SDS_DIALOGUE[next]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open(next,function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							cacheVars.waitPending = false;
						}
					});
				}
			},
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.dlgPath==="A" && SDS.bgm) SDS.bgm.stop();

				//if (cacheVars.meli && cacheVars.controls){
				//	cacheVars.meli.controls = cacheVars.controls;
				//	delete cacheVars.controls;
				//}
			}
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				GLOBAL_DIARY.note(4,"MET_RAVIENE");
				GLOBAL_DIARY.note(4,cacheVars.dlgPath==="A" ? "RAVIENE_NEMESIS" : "RAVIENE_EMPATH");

				if (cacheVars.meli){
					cacheVars.meli.overrideFrame=null;
				}

				SDS.switchBgm(cacheVars.bgmName,true);

				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Meli_shrine4_pray"] = {
		0 : {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// we'll be controlling Meli here, so...
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();

					cacheVars.meli.overrideFrame = 27;
					cacheVars.meli.overrideFrame += cacheVars.meli.direction>0 ? 0 : 32;
				}
			},
			controls : {},
			customPost : noop
		},
		
		60 : {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame=null;
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
				if (!GLOBAL_SCRATCHPAD.check("PRAYED") && !GLOBAL_DIARY.hasTear("S4_ALTAR")){
					
					cacheVars.altar = cacheVars.objMgr.getNamedObject("Altar");
					if (cacheVars.altar){
						
						cacheVars.tear = new GO_Tear("S4_ALTAR");
						cacheVars.tear.spawnOnSprite(cacheVars.altar.sprite,cacheVars.objMgr);
						cacheVars.tear.sprite.y-=32;
						
						cacheVars.poof = new GO_Poof();
						cacheVars.poof.spawnOnSprite(cacheVars.altar.sprite,cacheVars.objMgr);
						cacheVars.poof.sprite.y-=32;
					}
				}
				GLOBAL_SCRATCHPAD.note("PRAYED",true)
			}
		}
	};

DATA_SDS_SCRIPTED_EVENTS["Meli_shrine5_pray"] = {
		0 : {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// we'll be controlling Meli here, so...
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();

					cacheVars.meli.overrideFrame = 27;
					cacheVars.meli.overrideFrame += cacheVars.meli.direction>0 ? 0 : 32;
				}
			},
			controls : {},
			customPost : noop
		},
		
		60 : {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame=null;
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
				if (!GLOBAL_SCRATCHPAD.check("PRAYED") && !GLOBAL_DIARY.hasTear("S5_ALTAR")){
					
					cacheVars.altar = cacheVars.objMgr.getNamedObject("Altar");
					if (cacheVars.altar){
						
						cacheVars.tear = new GO_Tear("S5_ALTAR");
						cacheVars.tear.spawnOnSprite(cacheVars.altar.sprite,cacheVars.objMgr);
						cacheVars.tear.sprite.y-=32;
						
						cacheVars.poof = new GO_Poof();
						cacheVars.poof.spawnOnSprite(cacheVars.altar.sprite,cacheVars.objMgr);
						cacheVars.poof.sprite.y-=32;
					}
				}
				GLOBAL_SCRATCHPAD.note("PRAYED",true)
			}
		}
	};

DATA_SDS_SCRIPTED_EVENTS["Ground_Ham"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Ground_Ham"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// we'll be controlling Meli here, so...
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				cacheVars.pork = cacheVars.objMgr.getNamedObject("CretaceousHam");

				if (DATA_SDS_DIALOGUE["Ground_Ham_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Ground_Ham_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){

				if (DATA_SDS_DIALOGUE["Ground_Ham_1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Ground_Ham_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				if (cacheVars.pork) cacheVars.pork.hp = 0; // prompt cleanup

				cacheVars.bgmName = SDS.bgmName;
				SDS.switchBgm("Found A Shiny Thing",false);
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);
				GLOBAL_DIARY.note(4,"CRETACEOUS_HAM");

				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Ham_Loaf"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Ham_Loaf"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// we'll be controlling Meli here, so...
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				if (DATA_SDS_DIALOGUE["Brill_ham_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Brill_ham_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = DlgEvent("Brill_ham_1");
evt[++t] = {
			customPre : function(cacheVars){

				GLOBAL_SCRATCHPAD.note("fade",true);
			},
			controls : {},
			customPost : noop
		};
evt[t+=16] = {
			customPre : function(cacheVars){
				GLOBAL_SCRATCHPAD.unpersist("fade");
				GLOBAL_SCRATCHPAD.note("fade",false);
				GLOBAL_SCRATCHPAD.note("unfade",true);
			},
			controls : {},
			customPost : noop
		};
evt[t+=16] = DlgEvent("Brill_ham_2");
evt[++t] = {
			customPre : function(cacheVars){

				if (DATA_SDS_DIALOGUE["Brill_ham_3"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Brill_ham_3",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				cacheVars.bgmName = SDS.bgmName;
				SDS.switchBgm("Found A Shiny Thing",false);
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);
				GLOBAL_DIARY.note(4,"CRETACEOUS_HAM_LOAF");
				GLOBAL_SCRATCHPAD.note("Dlg_Brill_Line",4);

				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Life_Ring"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Life_Ring"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// we'll be controlling Meli here, so...
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				if (DATA_SDS_DIALOGUE["Zeke_ham_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Zeke_ham_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							//cacheVars.dlgPath = retval;
							if (!retval) cacheVars.frameCounter += 4;
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = DlgEvent("Zeke_ham_1");
evt[++t] = DlgEvent("Zeke_ham_2");
evt[++t] = {
			customPre : function(cacheVars){

				if (DATA_SDS_DIALOGUE["Get_Ring"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Get_Ring",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				cacheVars.bgmName = SDS.bgmName;
				SDS.switchBgm("Found A Shiny Thing",false);
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);
				GLOBAL_DIARY.note(4,"LIFE_RING");
				GLOBAL_DIARY.hasRing = true;
			}
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
			}
		};


DATA_SDS_SCRIPTED_EVENTS["Zeke_c4_handoff"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Zeke_c4_handoff"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// we'll be controlling Meli here, so...
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				cacheVars.zeke = cacheVars.objMgr.getNamedObject("Zeke");

				if (DATA_SDS_DIALOGUE["Zeke_c4_metravi_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Zeke_c4_metravi_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		},
evt[++t] = {
			customPre : noop,
			controls : {
				"Zeke":{
					B:true
				}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : function(cacheVars){
				if (cacheVars.zeke){
					cacheVars.circlet = new GO_Circlet();
					cacheVars.circlet.sprite.x = cacheVars.zeke.sprite.x + (cacheVars.zeke.direction*16);
					cacheVars.circlet.sprite.y = cacheVars.zeke.sprite.y - 16;
					cacheVars.zeke.sprite.map_room.addSprite(cacheVars.circlet.sprite);
					cacheVars.zeke.manager.add(cacheVars.circlet);

					cacheVars.poof = new GO_Poof();
					cacheVars.poof.spawnOnSprite(cacheVars.circlet.sprite,cacheVars.circlet.manager);
					cacheVars.poof=null;

					cacheVars.circlet=null;
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=20] = {
			customPre : noop,
			controls : {
				"Zeke":{
					B:false
				}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){

				if (DATA_SDS_DIALOGUE["Zeke_c4_metravi_1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Zeke_c4_metravi_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							GLOBAL_SCRATCHPAD.note("Dlg_Zeke_Line",2);
						}
					});
				}
			},
			controls : {},
			customPost : function(cacheVars){
				GLOBAL_SCRATCHPAD.note("ZEKE_HANDOFF",true);

				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Cafe_sample"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["Cafe_sample"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// this is a multi-dialog event; we want to lock down movement between them
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}
				if (DATA_SDS_DIALOGUE["Cafe_Sample0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Cafe_Sample0",function(index,retval){
						if (retval!==null){
							cacheVars.wait=false; // yay js scoping

							// if declining the sample, skip to end
							if (retval==1){
								cacheVars.frameCounter += 1;
							}
						}
					});
				}
			},
			controls : {
				
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Cafe_Sample1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Cafe_Sample1",function(index,retval){
						if (retval!==null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				GLOBAL_DIARY.life = GLOBAL_DIARY.max_life;
				GLOBAL_DIARY.chi = GLOBAL_DIARY.max_chi;
				GLOBAL_DIARY.subchi = 0;
				GLOBAL_DIARY.fastchi = 30*60; // 60 seconds regen boost
			},
			controls : {
				
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
			},
			controls : {},
			customPost : noop
		};


DATA_SDS_SCRIPTED_EVENTS["Gabe_greeting"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["Gabe_greeting"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// this is a multi-dialog event; we want to lock down movement between them
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				//cacheVars.gabe = cacheVars.objMgr.getNamedObject("Gabe");
				GLOBAL_SCRATCHPAD.note("Gabe_triggered",true);
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{
					left:true, 
					A: true
				}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Gabe":{
					left:false, 
					A: false,
					right: true
				}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{
					right: false
				}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.direction=-1;
				}
				if (DATA_SDS_DIALOGUE["Gabe_c4_treasure_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Gabe_c4_treasure_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{right:true}
			},
			customPost : noop
		};
evt[t+=6] = {
			customPre : noop,
			controls : {
				"Gabe":{right:false}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Gabe":{down:true}
			},
			customPost : noop
		};
evt[t+=30] = {
			customPre : noop,
			controls : {
				"Gabe":{down:false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Gabe_c4_treasure_1"]){
					cacheVars.wait=true; // need the event to proceed briefly so Meli can react
					cacheVars.dlgMgr.open("Gabe_c4_treasure_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							cacheVars.dlgPath = retval;
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{down:true}
			},
			customPost : noop
		};
evt[t+=20] = {
			customPre : noop,
			controls : {
				"Gabe":{down:false}
			},
			customPost : noop
		};
evt[t+=20] = {
			customPre : noop,
			controls : {
				"Gabe":{down:true}
			},
			customPost : noop
		};
evt[t+=20] = {
			customPre : noop,
			controls : {
				"Gabe":{down:false}
			},
			customPost : noop
		};
evt[t+=20] = {
			customPre : noop,
			controls : {
				"Gabe":{down:true}
			},
			customPost : noop
		};
evt[t+=30] = {
			customPre : noop,
			controls : {
				"Gabe":{down:false}
			},
			customPost : noop
		};
evt[t+=20] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Gabe_c4_treasure_2"]){
					cacheVars.wait=true; // need the event to proceed briefly so Meli can react
					cacheVars.dlgMgr.open("Gabe_c4_treasure_2",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Gabe_c4_treasure_3"]){
					cacheVars.wait=true; // need the event to proceed briefly so Meli can react
					cacheVars.dlgMgr.open("Gabe_c4_treasure_3",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							cacheVars.dlgPath = retval;

							if (retval==2) GLOBAL_DIARY.note(4,"DISLIKED_GABE");
							else if (retval==1) GLOBAL_DIARY.note(4,"LIKED_GABE");
							else GLOBAL_DIARY.note(4,"MET_GABE");
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{A: true}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Gabe":{A: false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				var next = "Gabe_c4_treasure_4_"+cacheVars.dlgPath;
				
				
				if (DATA_SDS_DIALOGUE[next]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open(next,function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{down:true}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Gabe":{down:false}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Gabe":{left:true}
			},
			customPost : noop
		};
evt[t+=60] = {
			customPre : noop,
			controls : {
				"Gabe":{
					left:false,
					right:true
				}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{right:false}
			},
			customPost : function(cacheVars){
				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}

				GLOBAL_SCRATCHPAD.note("Gabe_cb_trigger_active",true);
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Gabe_callback"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["Gabe_callback"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// this is a multi-dialog event; we want to lock down movement between them
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				//cacheVars.gabe = cacheVars.objMgr.getNamedObject("Gabe");

				GLOBAL_SCRATCHPAD.note("Gabe_cb_trigger_active",false);
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{A: true}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Gabe":{A: false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.direction=-1;
				}
				if (DATA_SDS_DIALOGUE["Gabe_c4_treasure_5"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Gabe_c4_treasure_5",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Meli":{left:true}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Meli":{left:false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
				GLOBAL_SCRATCHPAD.note("Gabe_cb_trigger_active",true);
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Gabe_regreet"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["Gabe_regreet"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// this is a multi-dialog event; we want to lock down movement between them
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				//cacheVars.gabe = cacheVars.objMgr.getNamedObject("Gabe");
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{A: true}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Gabe":{A: false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.direction=-1;
				}
				if (DATA_SDS_DIALOGUE["Gabe_c4_garbage_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Gabe_c4_garbage_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
				GLOBAL_SCRATCHPAD.note("Gabe_cb_trigger_active",true);
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Skyshrine_Alarm"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Skyshrine_Alarm"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				//cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				cacheVars.beams = [];
				cacheVars.objMgr.getAllNamedObjects("Beamwall",cacheVars.beams);
				for (cacheVars.i=0; cacheVars.i<cacheVars.beams.length; ++cacheVars.i){
					cacheVars.beams[cacheVars.i].Activate(true);
				}
				cacheVars.plat = cacheVars.objMgr.getNamedObject("RCPlatform");

				if (DATA_SDS_DIALOGUE["Skyshrine_Alarm"]){
					//cacheVars.wait=true; // delayed, see below
					cacheVars.dlgMgr.open("Skyshrine_Alarm",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				cacheVars.bgmName = SDS.bgmName;
				if (!GLOBAL_DIARY.check(4,"NOTREASURE",true)) SDS.switchBgm("Rumbl3",true);

			},
			controls : {},
			customPost : noop
		};
evt[t+=3] = {
			customPre : noop,
			controls : {
				"Gabe":{A: true}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Gabe":{A: false}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Gabe":{A: true}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Gabe":{A: false}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Gabe":{A: true}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Gabe":{A: false}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : function(cacheVars){
				// be slightly sneaky, knowing the dialogues consume time,
				// and delay Gabe's reaction and the cacheVars.wait until here.
				// If we're in speedrun mode AND Gabe's line is missing,
				// we may get into some trouble...
				if (DATA_SDS_DIALOGUE["Skyshrine_Alarm"]){
					cacheVars.wait=true;
				}

				var next = null;
				if (!GLOBAL_DIARY.check(4,"GARBAGE_CHUTE")){
					next = "Gabe_c4_treasure_6";
				}
				else if (!GLOBAL_DIARY.check(4,"NOTREASURE",true)){
					next = "Gabe_c4_garbage_1";
				}
				if (DATA_SDS_DIALOGUE[next]){
					cacheVars.wait=true; // jic we somehow have gabe's line but not the alarm
					cacheVars.dlgMgr.open(next,function(index,retval){
						if (retval!=null){
							// jic we somehow have gabe's line but not the alarm
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.plat){
					cacheVars.plat.down = true;
				}
			}
		};
evt[t+=3] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.plat){
					cacheVars.plat.clearFromRoom(cacheVars.plat.sprite.map_room);
					cacheVars.objMgr.remove(cacheVars.plat);
				}
				SDS.bgm.stop();
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Lab_Bookshelf"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Lab_Bookshelf"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Lab_Bookshelf"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Lab_Bookshelf",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (!GLOBAL_DIARY.check(4,"HAS_KEYS") && DATA_SDS_DIALOGUE["Lab_Keys?"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Lab_Keys?",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (!GLOBAL_DIARY.check(4,"HAS_KEYS") && DATA_SDS_DIALOGUE["Lab_Keys"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Lab_Keys",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping

							GLOBAL_DIARY.note(4,"HAS_KEYS");
						}
					});

					cacheVars.bgmName = SDS.bgmName;
					SDS.switchBgm("Found A Shiny Thing",false);
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,false);
			}
		};

DATA_SDS_SCRIPTED_EVENTS["BossRoom_Alarm"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["BossRoom_Alarm"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				//cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				cacheVars.boss = cacheVars.objMgr.getNamedObject("BossRoom");
				if (cacheVars.boss){
					cacheVars.boss.setBeams(true);
				}

				if (DATA_SDS_DIALOGUE["BossRoom_Alarm"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("BossRoom_Alarm",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				SDS.switchBgm("",true);

			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (!GLOBAL_SCRATCHPAD.check("BOSSROOM_AHA")){
					GLOBAL_SCRATCHPAD.note("BOSSROOM_AHA",1);
					GLOBAL_SCRATCHPAD.persist("BOSSROOM_AHA");
				}
				else{
					GLOBAL_SCRATCHPAD.note("BOSSROOM_AHA",GLOBAL_SCRATCHPAD.check("BOSSROOM_AHA")+1);
				}

				var aha = GLOBAL_SCRATCHPAD.check("BOSSROOM_AHA");
				while(aha>0 && !DATA_SDS_DIALOGUE["BossRoom_Aha"+aha]) --aha;

				if (DATA_SDS_DIALOGUE["BossRoom_Aha"+aha]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("BossRoom_Aha"+aha,function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				if (cacheVars.boss){
					cacheVars.boss.active = true;
					SDS.switchBgm("Rumbl3",true);
				}

				
			}
		};

DATA_SDS_SCRIPTED_EVENTS["BossRoom_end"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["BossRoom_end"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				//cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				
				if (DATA_SDS_DIALOGUE["BossRoom_Breakdown"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("BossRoom_Breakdown",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
				// because ofc yaga managed to die in such a way that he could have 
				// made it back to the fight trigger while the breakdown dialog was
				// still running.......
				GLOBAL_DIARY.note(4,"BOSS");
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				//GLOBAL_DIARY.note(4,"BOSS");
				GLOBAL_SCRATCHPAD.note("BOSS_DESTRUCT",true);
				SDS.switchBgm("",true);
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Raviene_c4_exposition"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["Raviene_c4_exposition"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// this is a multi-dialog event; we want to lock down movement between them
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();

					cacheVars.meli.direction = 1;
				}

				cacheVars.ravi = cacheVars.objMgr.getNamedObject("Raviene");
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{A: true}
			},
			customPost : noop
		};
evt[t+=5] = {
			customPre : noop,
			controls : {
				"Gabe":{A: false}
			},
			customPost : noop
		};
// meli ~= col 21
// ravi = col 33
// gabe = col 27
evt[t+=5] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c4_exposition_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_exposition_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				SDS.switchBgm("A-minor Waltz in 4",true);
			},
			controls : {
				"Raviene":{// col ~33
					left: true,
					A: true
				}, 
				"Gabe":{right: true} // col ~27
			},
			customPost : noop
		};
evt[t+=7] = {
			customPre : noop,
			controls : {
				"Raviene":{A: false} 
			},
			customPost : noop
		};
evt[t+=3] = {
			customPre : noop,
			controls : {
				"Raviene":{A: true} 
			},
			customPost : noop
		};
evt[t+=20] = {
			customPre : noop,
			controls : {
				"Gabe":{ // col 31
					right: false,
					left: true
				}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Gabe":{left: false} 
			},
			customPost : noop
		};
evt[t+=18] = {
			customPre : noop,
			controls : {
				"Raviene":{A: false}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Raviene":{left: false} // col ~25
			},
			customPost : noop
		};
evt[t+=20] = {
			customPre : noop,
			controls : {
				"Raviene":{right: true}
			},
			customPost : noop
		};
evt[t+=5] = {
			customPre : noop,
			controls : {
				"Raviene":{right: false} // col ~26
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c4_exposition_1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_exposition_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{A: true}
			},
			customPost : noop
		};
evt[t+=3] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c4_exposition_2"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_exposition_2",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {
				"Gabe":{A: false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Raviene":{A: true}
			},
			customPost : noop
		};
evt[t+=5] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c4_exposition_3"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_exposition_3",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {
				"Raviene":{A: false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Gabe":{right: true}, // col ~31
				"Raviene":{left: true}, // col ~26
				"Meli":{right: true} // col ~21
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Raviene":{left: false}, // col ~25
				"Meli":{right: false}, // col ~22
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Meli":{A: true}
			},
			customPost : noop
		};
evt[t+=3] = {
			customPre : noop,
			controls : {
				"Meli":{A: false}
			},
			customPost : noop
		};
evt[t+=5] = {
			customPre : noop,
			controls : {
				"Gabe":{ // col ~34
					right: false,
					left: true
				}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c4_exposition_4"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_exposition_4",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {
				"Gabe":{left: false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c4_exposition_5"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_exposition_5",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
				SDS.switchBgm("",true);
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c4_exposition_5b"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_exposition_5b",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
				SDS.switchBgm("Forced Fit",true);
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Raviene":{A: true},
				"Meli":{right: true} // col ~22
			},
			customPost : noop
		};
evt[t+=4] = {
			customPre : noop,
			controls : {
				"Raviene":{A: false}
			},
			customPost : noop
		};
evt[t+=2] = {
			customPre : noop,
			controls : {
				"Raviene":{A: true},
			},
			customPost : noop
		};
evt[t+=4] = {
			customPre : noop,
			controls : {
				"Meli":{right: false} // col ~24
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c4_exposition_6"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_exposition_6",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.ravi){
					cacheVars.ravi.spell = cacheVars.ravi.SPELLS.HOLD;
					cacheVars.ravi.spell_target_name = "Meli";
				}
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame = 27;
					cacheVars.meli.overrideFrame += cacheVars.meli.direction>0 ? 0 : 32;
				}
			},
			controls : {
				"Raviene":{B: true}
			},
			customPost : noop
		};
evt[t+=30] = {
			customPre : noop,
			controls : {
				"Raviene":{
					B: false,
					A: false
				}
			},
			customPost : noop
		};
evt[t+=15] = {
			customPre : noop,
			controls : {
				"Raviene":{left: true} // col ~25
			},
			customPost : noop
		};
evt[t+=5] = {
			customPre : noop,
			controls : {
				"Raviene":{left: false} // col ~24
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.ravi){
					cacheVars.ravi.spell = cacheVars.ravi.SPELLS.NONE;
				}
			},
			controls : {
				"Raviene":{B: true}
			},
			customPost : noop
		};
evt[t+=15] = {
			customPre : function(cacheVars){
				GLOBAL_DIARY.hasBracelets=false;
				GLOBAL_DIARY.hasCollar=false;
				GLOBAL_DIARY.hasBelt=false;
				GLOBAL_DIARY.hasCirclet=false;

				if (DATA_SDS_DIALOGUE["Lost_treasures"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Lost_treasures",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			customPost : noop
		};
evt[t+=15] = {
			customPre : noop,
			controls : {
				"Raviene":{B: false}
			},
			customPost : noop
		};
evt[t+=10] = {
			customPre : noop,
			controls : {
				"Raviene":{right: true} // col ~24
			},
			customPost : noop
		};
evt[t+=30] = {
			customPre : noop,
			controls : {
				"Raviene":{
					right: false,
					left: true
				}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Raviene":{left: false} // col ~27
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.ravi){
					// toggle holdshield off on meli
					cacheVars.ravi.spell = cacheVars.ravi.SPELLS.HOLD;
					cacheVars.ravi.spell_target_name = "Meli";

					cacheVars.ravi.treasuresOverlay.visible = true;
				}
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame = null;
				}

				if (DATA_SDS_DIALOGUE["Raviene_c4_exposition_7"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c4_exposition_7",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {
				"Raviene":{B: true}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Raviene":{B: false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
				GLOBAL_DIARY.note(4,"NOTREASURE");

				SDS.bgmOverride = "Shadows of the Past";
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Raviene_notreasure"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["Raviene_notreasure"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// this is a multi-dialog event; we want to lock down movement between them
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				cacheVars.ravi = cacheVars.objMgr.getNamedObject("Raviene");

				var dlg = (GLOBAL_DIARY.empathy()>0) ? "Raviene_c4_notreasure_0b" : "Raviene_c4_notreasure_0a";
				if (DATA_SDS_DIALOGUE[dlg]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open(dlg,function(index,retval){
						if (retval!=null){
							cacheVars.retval = retval;
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				var dlg = "Raviene_c4_notreasure_2_0";
				if (cacheVars.retval===0){
					// "I despise you" = bad karma
					GLOBAL_DIARY.clear(4,"NOTREASURE");
					GLOBAL_DIARY.note(4,"NOTREASURE_NEMESIS");
					dlg = "Raviene_c4_notreasure_1_0";
				}
				else if (GLOBAL_DIARY.empathy()>0){
					// option 1 in the no-empathy path is the neutral response.
					// but in the positive empathy path, it triggers... things.
					GLOBAL_DIARY.clear(4,"NOTREASURE");
					GLOBAL_DIARY.note(4,"NOTREASURE_EMPATH");
					dlg = "Raviene_c4_notreasure_1_1";

					if (cacheVars.ravi){
						cacheVars.ravi.hurt=true;
					}
				}

				if (DATA_SDS_DIALOGUE[dlg]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open(dlg,function(index,retval){
						if (retval!=null){
							cacheVars.retval = retval;
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Ravi_Diary"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Ravi_Diary"];
t=0;
// step 0: initialize tracking; prompt menu for where to begin
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.pages = GLOBAL_SCRATCHPAD.check("raviPages");
				if (!cacheVars.pages){
					cacheVars.pages = [];
					for (var i=0; i<=22; ++i){
						cacheVars.pages.push(false);
					}
					GLOBAL_SCRATCHPAD.note("raviPages",cacheVars.pages);
				}

				cacheVars.page = 0;

				if (DATA_SDS_DIALOGUE["Ravi_Diary"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Ravi_Diary",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping

							switch(retval){
								case -1:
									cacheVars.frameCounter=2;
								break;
								case 0:
									cacheVars.page=0;
								break;
								case 1:
									cacheVars.page=4;
								break;
								case 2:
									cacheVars.page=17;
								break;
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
// step 1: show the currently selected page, handle forward/again/back/out page changes
evt[++t] = {
			customPre : function(cacheVars){
				var page = ""+cacheVars.page;
				if (cacheVars.page===22){
					if (GLOBAL_DIARY.check(4,"RAVIENE_NEMESIS")) page+="A";
					if (GLOBAL_DIARY.check(4,"RAVIENE_EMPATH")) page+="B";
				}

				if (DATA_SDS_DIALOGUE["Ravi_Diary_"+page]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Ravi_Diary_"+page,function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping

							cacheVars.pages[cacheVars.page] = true;

							switch(retval){
								case -1:
									cacheVars.frameCounter=2;
								break;
								case 0:
									// TODO: block off later entries until plot happens
									++cacheVars.page;
									--cacheVars.frameCounter;
								break;
								case 1:
									--cacheVars.frameCounter;
								break;
								case 2:
									if (cacheVars.page>0) --cacheVars.page;
									--cacheVars.frameCounter;
								break;
							}
						}
					});
				}
				else if (DATA_SDS_DIALOGUE["Ravi_Diary_Blank"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Ravi_Diary_Blank",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping

							cacheVars.frameCounter=2;
						}
					});
				}
				else{
					cacheVars.frameCounter=2;
				}
			},
			controls : {},
			customPost : noop
		};
// step 2: close out, cache currently read pages, save the event if we read them all
evt[++t] = {
			customPre : function(cacheVars){
				GLOBAL_SCRATCHPAD.note("raviPages",cacheVars.pages);
				var pagecount = 0;
				for (var i=0; i<cacheVars.pages.length; ++i){
					if (cacheVars.pages[i]) ++pagecount;
				}

				// TODO: allow for the diary event to be set
				// if all _currently available_ pages are read.
				// 0-22 are enoguh to get the gist, if that's all that are unlocked, so don't hold out for the later ones
				// but don't let an incomplete read count if more are available
				if (pagecount===cacheVars.pages.length && !GLOBAL_DIARY.check(4,"RAVI_DIARY")){
					GLOBAL_DIARY.note(4,"RAVI_DIARY");
				}
			},
			controls : {},
			customPost : noop
		};



DATA_SDS_SCRIPTED_EVENTS["chapter4_end"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["chapter4_end"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// meli is "dead" here, so no controls
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();

					cacheVars.meli.overrideFrame = 30; // empty

					cacheVars.poof = new GO_Poof("FX_splat");
					cacheVars.poof.spawnOnSprite(cacheVars.meli.sprite,cacheVars.objMgr);
				}
				GLOBAL_DIARY.life=0;

				SDS.switchBgm("",true);
				SDS.screenFX = "color-burn";
				SDS.screenFXColor = "#f00";

				cacheVars.red = 256;

				GLOBAL_DIARY.note(4,"FALL");
			},
			controls : {},
			customPost : noop
		};

evt[t+=60] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Development_interstitial_4"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Development_interstitial_4",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
for (var l=0; l<16; ++l){
	evt[t+=10] = {
			customPre : function(cacheVars){
				SDS.screenFX = "multiply";
				cacheVars.red -= 16;
				SDS.screenFXColor = "rgb("+cacheVars.red+",0,0)";
			},
			controls : {},
			customPost : noop
		};
}

evt[t+=60] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Meli_c5_intro_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Meli_c5_intro_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=30] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Meli_c5_intro_1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Meli_c5_intro_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=30] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Meli_c5_intro_2"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Meli_c5_intro_2",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=45] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Meli_c5_intro_3"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Meli_c5_intro_3",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=60] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Meli_c5_intro_4"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Meli_c5_intro_4",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=150] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Meli_c5_intro_5"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Meli_c5_intro_5",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};

// autoport to bed, fade in to ~rgb(171,93,255) color-burn
// 0 2 4 8 a / 0 1 2 3 4 / 0 4 8 c f
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.pass = new GO_Passage(
						1<<4,
						1<<4,
						function(){return "village_caves";},
						function(sprite){
							sprite.x = 22<<4;
							sprite.y = (15<<4)-6;
							sprite.game_object.direction = -1; // since this should always be Meli, or at least a NPC
						});
					cacheVars.pass.spawnOnSprite(cacheVars.meli.sprite,cacheVars.meli.manager);	
				}

				SDS.bgmOverride = "Secondhand Feelings";
				GLOBAL_DIARY.note(5,"AWAKE");
			}
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame=59;
				}
				SDS.screenFX = "color-burn";
				cacheVars.red = 0;
				cacheVars.green = 0;
				cacheVars.blue = 0;
				SDS.screenFXColor = "#000";
			}
		};
for (var l=0; l<20; ++l){
	evt[t+=10] = {
			customPre : function(cacheVars){
				// MOOD_TO_BURN[0]/20
				cacheVars.red += 8;
				cacheVars.green += 4;
				cacheVars.blue += 12;
				SDS.screenFXColor = "rgb("+cacheVars.red+","+cacheVars.green+","+cacheVars.blue+")";
			},
			controls : {},
			customPost : noop
		};
}

evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame=null;
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}

				GLOBAL_DIARY.life=GLOBAL_DIARY.max_life;
				SDS.screenFX = "color-burn";
				SDS.screenFXColor = MOOD_TO_BURN["0"];//"#a5f";
			}
		};

DATA_SDS_SCRIPTED_EVENTS["chapter4_end_b"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["chapter4_end_b"];
var t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				
				if (cacheVars.meli){
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				GLOBAL_DIARY.life=0;

				SDS.switchBgm("",true);
				//SDS.screenFX = "color-burn";
				//SDS.screenFXColor = "#f00";

				//cacheVars.red = 256;

				GLOBAL_DIARY.note(4,"SLEEP");
				GLOBAL_SCRATCHPAD.note("fade",true);
			},
			controls : {},
			customPost : noop
		};
evt[t+=60] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Development_preinterstitial_4b"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Development_preinterstitial_4b",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}

				SDS.screenFX = "multiply";
				SDS.screenFXColor = "rgb(0,0,0)";
			},
			controls : {},
			customPost : noop
		};
evt[t+=30] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Development_interstitial_4b"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Development_interstitial_4b",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};


// autoport to bed, fade in to ~rgb(171,93,255) color-burn
// 0 2 4 8 a / 0 1 2 3 4 / 0 4 8 c f
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.pass = new GO_Passage(
						1<<4,
						1<<4,
						function(){return "village_caves";},
						function(sprite){
							sprite.x = 22<<4;
							sprite.y = (15<<4)-6;
							sprite.game_object.direction = -1; // since this should always be Meli, or at least a NPC
						});
					cacheVars.pass.spawnOnSprite(cacheVars.meli.sprite,cacheVars.meli.manager);	
				}

				SDS.bgmOverride = "Secondhand Feelings";
				GLOBAL_DIARY.note(5,"AWAKE");
			}
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame=59;
				}
				SDS.screenFX = "color-burn";
				cacheVars.red = 0;
				cacheVars.green = 0;
				cacheVars.blue = 0;
				SDS.screenFXColor = "#000";
			}
		};
for (var l=0; l<20; ++l){
	evt[t+=10] = {
			customPre : function(cacheVars){
				// MOOD_TO_BURN[0]/20
				cacheVars.red += 8;
				cacheVars.green += 4;
				cacheVars.blue += 12;
				SDS.screenFXColor = "rgb("+cacheVars.red+","+cacheVars.green+","+cacheVars.blue+")";
			},
			controls : {},
			customPost : noop
		};
}

evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				if (cacheVars.meli){
					cacheVars.meli.overrideFrame=null;
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}

				GLOBAL_DIARY.life=GLOBAL_DIARY.max_life;
				SDS.screenFX = "color-burn";
				SDS.screenFXColor = MOOD_TO_BURN["0"];//"#a5f";
			}
		};

evt = null;
t=null;

