"use strict";

var DlgEvent = function(dlg){ // <3 js scoping
		return {
					customPre : function(cacheVars){
						if (DATA_SDS_DIALOGUE[dlg]){
							cacheVars.wait=true;
							cacheVars.dlgMgr.open(dlg,function(index,retval){
								if (retval!=null){
									cacheVars.wait=false; // yay js scoping
								}
							});
						}
					},
					controls : {},
					customPost : noop
				};
			};

DATA_SDS_SCRIPTED_EVENTS["Raviene_intro"] = {};
var evt = DATA_SDS_SCRIPTED_EVENTS["Raviene_intro"];
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 = new GO_Raviene();
					cacheVars.ravi.sprite.x = (19<<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);
				
				if (DATA_SDS_DIALOGUE["Raviene_c3_introduction_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c3_introduction_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["Raviene_c3_introduction_0cast"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c3_introduction_0cast",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+=31] = {
			customPre : function(cacheVars){
				if (cacheVars.ravi){
					cacheVars.ravi.spell = cacheVars.ravi.SPELLS.KILL;
					cacheVars.ravi.spell_target_name = "Meli";
				}
				if (DATA_SDS_DIALOGUE["Raviene_c3_introduction_1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c3_introduction_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Raviene":{B:true}
			},
			customPost : noop
		};
evt[t+=150] = {
			customPre : noop,
			controls : {
				"Raviene":{B:false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c3_introduction_2"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c3_introduction_2",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Raviene":{A:true, right: true, up: true}
			},
			customPost : noop
		};
evt[t+=19] = {
			customPre : noop,
			controls : {
				"Raviene":{A:false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Raviene":{A: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 : function(cacheVars){
				SDS.switchBgm(cacheVars.bgmName,true);				
			}
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Raviene_c3_introduction_3"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Raviene_c3_introduction_3",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : function(cacheVars){
				GLOBAL_DIARY.note(3,"MET_RAVIENE");

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

DATA_SDS_SCRIPTED_EVENTS["Bobo_meetbobo"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Bobo_meetbobo"];
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["Bobo_c3_meetbobo_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Bobo_c3_meetbobo_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
for (var l=1; l<10; ++l){
	evt[++t] = DlgEvent("Bobo_c3_meetbobo_"+l);
}
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Bobo_c3_meetbobo_10"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Bobo_c3_meetbobo_10",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : function(cacheVars){
				GLOBAL_DIARY.note(3,"MET_BOBO");

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

DATA_SDS_SCRIPTED_EVENTS["c3_shrinekeeper"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["c3_shrinekeeper"];
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.yorick = cacheVars.objMgr.getNamedObject("Yorick");
				
				if (DATA_SDS_DIALOGUE["Shrine3_Keeper_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Shrine3_Keeper_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.yorick){
					cacheVars.yorick.sprite.visible = !cacheVars.yorick.sprite.visible;
				}
			},
			controls : {},
			customPost : noop
		};
for (var l=0; l<60; ++l){
	evt[++t] = evt[t-1];
}
evt[t+=15] = {
			customPre : function(cacheVars){
				if (cacheVars.yorick){
					cacheVars.yorick.sprite.map_room.removeSprite(cacheVars.yorick.sprite);
					cacheVars.objMgr.remove(cacheVars.yorick);
				}
				if (DATA_SDS_DIALOGUE["Shrine3_Keeper_1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Shrine3_Keeper_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : function(cacheVars){
				GLOBAL_DIARY.note(3,"FOUND_KEEPER");

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

// events can't take parameters, so we need a few of these...
DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_1-2"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_1-2"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				if (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR1")==GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR2")){
					if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_nop"]){
						cacheVars.wait=true;
						cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch");
						cacheVars.dlgMgr.open("Switch_shrine3_basic_nop",function(index,retval){
							if (retval!=null){
								cacheVars.wait=false; // yay js scoping
								cacheVars.frameCounter+=91; // skip the woosh
							}
						});
					}	
				}

				else if (DATA_SDS_DIALOGUE["Switch_shrine3_basic"]){
					cacheVars.wait=true;
					cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch");
					cacheVars.dlgMgr.open("Switch_shrine3_basic",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==1){
								if (cacheVars.waterswitch){
									cacheVars.waterswitch.playSound("FX_waterrush");
									cacheVars.waterswitch.pressed=true;
								}
								cacheVars.level = (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR1")+GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR2"))>>1;
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR1",cacheVars.level);
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR2",cacheVars.level);
							}
							else{
								cacheVars.frameCounter+=91; // skip the woosh
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=90] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_complete"]){
					cacheVars.dlgMgr.open("Switch_shrine3_basic_complete",function(index,retval){});
				}
			},
			controls : {},
			customPost : noop
		};
DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_1-3"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_1-3"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				if (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR1")==GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR3")){
					if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_nop"]){
						cacheVars.wait=true;
						cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch");
						cacheVars.dlgMgr.open("Switch_shrine3_basic_nop",function(index,retval){
							if (retval!=null){
								cacheVars.wait=false; // yay js scoping
								cacheVars.frameCounter+=91; // skip the woosh
							}
						});
					}	
				}

				else if (DATA_SDS_DIALOGUE["Switch_shrine3_basic"]){
					cacheVars.wait=true;
					cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch");
					cacheVars.dlgMgr.open("Switch_shrine3_basic",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==1){
								if (cacheVars.waterswitch){
									cacheVars.waterswitch.playSound("FX_waterrush");
									cacheVars.waterswitch.pressed=true;
								}
								cacheVars.level = (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR1")+GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR3"))>>1;
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR1",cacheVars.level);
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR3",cacheVars.level);
							}
							else{
								cacheVars.frameCounter+=91; // skip the woosh
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=90] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_complete"]){
					cacheVars.dlgMgr.open("Switch_shrine3_basic_complete",function(index,retval){});
				}
			},
			controls : {},
			customPost : noop
		};
DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_2-3"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_2-3"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				if (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR3")==GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR2")){
					if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_nop"]){
						cacheVars.wait=true;
						cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch");
						cacheVars.dlgMgr.open("Switch_shrine3_basic_nop",function(index,retval){
							if (retval!=null){
								cacheVars.wait=false; // yay js scoping
								cacheVars.frameCounter+=91; // skip the woosh
							}
						});
					}	
				}

				else if (DATA_SDS_DIALOGUE["Switch_shrine3_basic"]){
					cacheVars.wait=true;
					cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch");
					cacheVars.dlgMgr.open("Switch_shrine3_basic",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==1){
								if (cacheVars.waterswitch){
									cacheVars.waterswitch.playSound("FX_waterrush");
									cacheVars.waterswitch.pressed=true;
								}
								cacheVars.level = (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR3")+GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR2"))>>1;
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR3",cacheVars.level);
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR2",cacheVars.level);
							}
							else{
								cacheVars.frameCounter+=91; // skip the woosh
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=90] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_complete"]){
					cacheVars.dlgMgr.open("Switch_shrine3_basic_complete",function(index,retval){});
				}
			},
			controls : {},
			customPost : noop
		};
DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_1-C"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_1-C"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				if (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR1")==GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR_CENTER")){
					if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_nop"]){
						cacheVars.wait=true;
						cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch_1C");
						cacheVars.dlgMgr.open("Switch_shrine3_basic_nop",function(index,retval){
							if (retval!=null){
								cacheVars.wait=false; // yay js scoping
								cacheVars.frameCounter+=91; // skip the woosh
							}
						});
					}	
				}

				else if (DATA_SDS_DIALOGUE["Switch_shrine3_1C"]){
					cacheVars.wait=true;
					cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch_1C");
					cacheVars.dlgMgr.open("Switch_shrine3_1C",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==1){
								if (cacheVars.waterswitch){
									cacheVars.waterswitch.playSound("FX_waterrush");
									cacheVars.waterswitch.pressed=true;
								}
								cacheVars.level = (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR1")+GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR_CENTER"))>>1;
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR1",cacheVars.level);
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR_CENTER",cacheVars.level);
							}
							else{
								cacheVars.frameCounter+=91; // skip the woosh
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=90] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_complete"]){
					cacheVars.dlgMgr.open("Switch_shrine3_basic_complete",function(index,retval){});
				}
			},
			controls : {},
			customPost : noop
		};
DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_2-C"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_2-C"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				if (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR2")==GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR_CENTER")){
					if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_nop"]){
						cacheVars.wait=true;
						cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch_2C");
						cacheVars.dlgMgr.open("Switch_shrine3_basic_nop",function(index,retval){
							if (retval!=null){
								cacheVars.wait=false; // yay js scoping
								cacheVars.frameCounter+=91; // skip the woosh
							}
						});
					}	
				}

				else if (DATA_SDS_DIALOGUE["Switch_shrine3_2C"]){
					cacheVars.wait=true;
					cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch_2C");
					cacheVars.dlgMgr.open("Switch_shrine3_2C",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==1){
								if (cacheVars.waterswitch){
									cacheVars.waterswitch.playSound("FX_waterrush");
									cacheVars.waterswitch.pressed=true;
								}
								cacheVars.level = (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR2")+GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR_CENTER"))>>1;
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR2",cacheVars.level);
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR_CENTER",cacheVars.level);
							}
							else{
								cacheVars.frameCounter+=91; // skip the woosh
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=90] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_complete"]){
					cacheVars.dlgMgr.open("Switch_shrine3_basic_complete",function(index,retval){});
				}
			},
			controls : {},
			customPost : noop
		};
DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_3-C"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterbalance_3-C"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				if (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR3")==GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR_CENTER")){
					if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_nop"]){
						cacheVars.wait=true;
						cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch_3C");
						cacheVars.dlgMgr.open("Switch_shrine3_basic_nop",function(index,retval){
							if (retval!=null){
								cacheVars.wait=false; // yay js scoping
								cacheVars.frameCounter+=91; // skip the woosh
							}
						});
					}	
				}

				else if (DATA_SDS_DIALOGUE["Switch_shrine3_3C"]){
					cacheVars.wait=true;
					cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch_3C");
					cacheVars.dlgMgr.open("Switch_shrine3_3C",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==1){
								if (cacheVars.waterswitch){
									cacheVars.waterswitch.playSound("FX_waterrush");
									cacheVars.waterswitch.pressed=true;
								}
								cacheVars.level = (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR3")+GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR_CENTER"))>>1;
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR3",cacheVars.level);
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR_CENTER",cacheVars.level);
							}
							else{
								cacheVars.frameCounter+=91; // skip the woosh
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=90] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Switch_shrine3_basic_complete"]){
					cacheVars.dlgMgr.open("Switch_shrine3_basic_complete",function(index,retval){});
				}
			},
			controls : {},
			customPost : noop
		};
DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterdrain"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Shrine3_waterdrain"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				if (GLOBAL_SCRATCHPAD.check("SHRINE3_RESERVOIR_CENTER")==0){
					if (DATA_SDS_DIALOGUE["Switch_shrine3_drain_nop"]){
						cacheVars.wait=true;
						cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch");
						cacheVars.dlgMgr.open("Switch_shrine3_drain_nop",function(index,retval){
							if (retval!=null){
								cacheVars.wait=false; // yay js scoping
								cacheVars.frameCounter+=151; // skip the woosh
							}
						});
					}	
				}

				else if (DATA_SDS_DIALOGUE["Switch_shrine3_drain"]){
					cacheVars.wait=true;
					cacheVars.waterswitch = cacheVars.objMgr.getNamedObject("WaterSwitch");
					cacheVars.dlgMgr.open("Switch_shrine3_drain",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
							if (retval==1){
								if (cacheVars.waterswitch){
									cacheVars.waterswitch.playSound("FX_waterrush");
									cacheVars.waterswitch.pressed=true;
								}
								GLOBAL_SCRATCHPAD.note("SHRINE3_RESERVOIR_CENTER",0);

								if (!GLOBAL_DIARY.check(3,"DRAINED_SEED") && 
									GLOBAL_SCRATCHPAD.check("SHRINE3_SEED_X")>=(14<<4) && GLOBAL_SCRATCHPAD.check("SHRINE3_SEED_X")<=(17<<4) &&
									GLOBAL_SCRATCHPAD.check("SHRINE3_SEED_Y")>=(33<<4)){ // would be 34, but buoyancy box pushes top of sprite proper just above the 544px line (540)

									GLOBAL_DIARY.note(3,"DRAINED_SEED")
									cacheVars.seedAway = true;
								}
								else{
									cacheVars.seedAway = false;
								}
							}
							else{
								cacheVars.frameCounter+=151; // skip the woosh
							}
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=90] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Switch_shrine3_drain_complete"]){
					cacheVars.dlgMgr.open("Switch_shrine3_drain_complete",function(index,retval){});
				}
				if (!cacheVars.seedAway){
					cacheVars.frameCounter+=61; // skip the one-time completion event
				}
			},
			controls : {},
			customPost : noop
		};
evt[t+=60] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Switch_shrine3_seed_flushed"]){
					cacheVars.dlgMgr.open("Switch_shrine3_seed_flushed",function(index,retval){});
				}
			},
			controls : {},
			customPost : noop
		};

DATA_SDS_SCRIPTED_EVENTS["Meli_shrine3_pray"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Meli_shrine3_pray"];
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.meli.overrideFrame = 27;
					cacheVars.meli.overrideFrame += cacheVars.meli.direction>0 ? 0 : 32;
				}
			},
			controls : {},
			customPost : noop
		};
		
evt[t+=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("S3_ALTAR")){
					cacheVars.altar = cacheVars.objMgr.getNamedObject("WaterAltar");
					if (cacheVars.altar){
						//var tear = new GO_Tear("S1_ALTAR");
						cacheVars.tear = new GO_Tear("S3_ALTAR");
						cacheVars.tear.spawnOnSprite(cacheVars.altar.sprite,cacheVars.objMgr);
						cacheVars.tear.sprite.y-=32;
						//var poof = new GO_Poof();
						cacheVars.poof = new GO_Poof();
						cacheVars.poof.spawnOnSprite(cacheVars.altar.sprite,cacheVars.objMgr);
						cacheVars.poof.sprite.y-=32;
					}
				}
				if (!GLOBAL_SCRATCHPAD.check("PRAYED")){
					if (DATA_SDS_DIALOGUE["Altar_shrine3_reset"]){
						cacheVars.wait=true;
						
						cacheVars.dlgMgr.open("Altar_shrine3_reset",function(index,retval){
							if (retval!=null){
								cacheVars.wait=false; // yay js scoping
							}
						});
					}
					ASSISTS["CHAPTER3"]["Shrine3Water"](true);
				}
				GLOBAL_SCRATCHPAD.note("PRAYED",true)
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Bobo_boss_start"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Bobo_boss_start"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					// this is a multi-screen event; we want to lock down movement to keep Meli in place
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
					cacheVars.meli.direction = -1;
				}
				
				if (DATA_SDS_DIALOGUE["Bobo_bossfight_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Bobo_bossfight_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"BossBobo":{A:true}
			},
			customPost : noop
		};
evt[t+=45] = {
			customPre : function(cacheVars){
				cacheVars.uncacheObjects.push("BossBobo");
			},
			controls : {
				"BossBobo":{A:false} // jic, but screen should have changed, so...
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				cacheVars.uncacheObjects.push("BossBobo");
			},
			controls : {
				"BossBobo":{A:true} // this will be a different bobo instance
			},
			customPost : noop
		};
evt[t+=45] = {
			customPre : function(cacheVars){
				cacheVars.uncacheObjects.push("BossBobo");
			},
			controls : {
				"BossBobo":{A:false} // and so will this, but jic again
			},
			customPost : function(cacheVars){
				if (cacheVars.meli && cacheVars.controls){
					cacheVars.meli.controls = cacheVars.controls;
					delete cacheVars.controls;
				}
				cacheVars.bobo = cacheVars.objMgr.getNamedObject("BossBobo");
				if (cacheVars.bobo){
					cacheVars.bobo.startFight();
				}
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Bobo_boss_end"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Bobo_boss_end"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				if (cacheVars.meli){
					cacheVars.meli.direction = -1;
				}
				if (DATA_SDS_DIALOGUE["Bobo_bossfight_1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Bobo_bossfight_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				GLOBAL_DIARY.note(3,"BOSS");

				cacheVars.bobo = cacheVars.objMgr.getNamedObject("BossBobo");
				if (cacheVars.bobo){
					cacheVars.bobo.cleanupFight();
				}
			}
		};

DATA_SDS_SCRIPTED_EVENTS["Lief_c3_treasure"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["Lief_c3_treasure"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				cacheVars.lief = cacheVars.objMgr.getNamedObject("Lief");
				if (cacheVars.meli){
					// this is a multi-screen event; we want to lock down movement to keep Meli in place
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				if (cacheVars.meli && cacheVars.lief){
					cacheVars.meli.direction = cacheVars.meli.sprite.x > cacheVars.lief.sprite.x ? -1 : 1;
					cacheVars.lief.direction = cacheVars.lief.sprite.x > cacheVars.meli.sprite.x ? -1 : 1;
				}
				if (DATA_SDS_DIALOGUE["Lief_c3_treasure_0"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Lief_c3_treasure_0",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {
				"Lief" : {A: true}
			},
			customPost : noop
		};
evt[t+=90] = {
			customPre : noop,
			controls : {
				"Lief" : {A: false}
			},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["Lief_c3_treasure_1"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("Lief_c3_treasure_1",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				GLOBAL_DIARY.note(3,"ACTIVATED_SEED");

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

DATA_SDS_SCRIPTED_EVENTS["SkySeed_Grow"] = {};
evt = DATA_SDS_SCRIPTED_EVENTS["SkySeed_Grow"];
t=0;
evt[t] = {
			customPre : function(cacheVars){
				cacheVars.meli = cacheVars.objMgr.getNamedObject("Meli");
				cacheVars.seed = cacheVars.objMgr.getNamedObject("SkySeed");
				if (cacheVars.meli){
					// this is a multi-screen event; we want to lock down movement to keep Meli in place
					cacheVars.controls = cacheVars.meli.controls;
					cacheVars.meli.controls = new Controls();
				}

				if (DATA_SDS_DIALOGUE["SkySeed_Sprout"]){
					//cacheVars.wait=true;
					cacheVars.dlgMgr.open("SkySeed_Sprout",function(index,retval){
						//if (retval!=null){
						//	cacheVars.wait=false; // yay js scoping
						//}
					});
				}
			},
			controls : {},
			customPost : noop
		};
for (var l=0; l<16; ++l){
	evt[t+=(16-l)] = {
			customPre : function(cacheVars){
				if (cacheVars.seed){
					++cacheVars.seed.sprite.frame;
					cacheVars.seed.sprite.frame&=3;
					--cacheVars.seed.sprite.y;
				}
			},
			controls : {},
			customPost : noop
		};
}
evt[++t] = {
			customPre : function(cacheVars){
				cacheVars.bgmName = SDS.bgmName;
				SDS.switchBgm("Earthquake",false);
			},
			controls : {},
			customPost : noop
		};
for (var l=0; l<256; ++l){
	evt[++t] = function(i){ return {
			customPre : function(cacheVars){
				if (cacheVars.seed){
					++cacheVars.seed.sprite.frame;
					cacheVars.seed.sprite.frame&=3;
					--cacheVars.seed.sprite.y;

					//var poof = new GO_Poof();
					cacheVars.poof = new GO_Poof();
					cacheVars.poof.spawnOnSprite(cacheVars.seed.sprite,cacheVars.objMgr); // basic registration
					//cacheVars.poof.sprite.y = (5<<4) + (((i>>4)*((i&63)>>2))>>(i&2)); // (0 to 15)*(0 to 15)/(1,2,4)
					cacheVars.poof.sprite.y = (5<<4) + ((i>>4)*(15 - (i&15)^((i&31)>>1))); // (0 to 16 over time)*(0 to 16 changing rapidly)
					// 16-block spread; hit all 256 x-positions, but nicely modulated
					// llllhhhh
					cacheVars.poof.sprite.x = (i>>4) + ((i&15)<<4); // 0-255 in modulated pattern
					cacheVars.poof.sprite.x *= ((i>>5)+1); // *=1 to *=8 over time
					cacheVars.poof.sprite.x = cacheVars.poof.sprite.x >> 3; // /=8
					cacheVars.poof.sprite.x -= ((255*((i>>5)+1))>>4); // left by half the maximum span: ((255*((i>>5)+1)) >> 3) >> 1
					cacheVars.poof.sprite.x += (14<<4); // center on center root
				}
			},
			controls : {},
			customPost : noop
		}}(l);
}
evt[++t] = {
			customPre : function(cacheVars){
				if (cacheVars.seed){
					cacheVars.seed.sprite.map_room.removeSprite(cacheVars.seed.sprite);
					cacheVars.objMgr.remove(cacheVars.seed);
				}
				SDS.switchBgm(cacheVars.bgmName,true);
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : function(cacheVars){
				if (DATA_SDS_DIALOGUE["SkySeed_Grow"]){
					cacheVars.wait=true;
					cacheVars.dlgMgr.open("SkySeed_Grow",function(index,retval){
						if (retval!=null){
							cacheVars.wait=false; // yay js scoping
						}
					});
				}
			},
			controls : {},
			customPost : noop
		};
evt[++t] = {
			customPre : noop,
			controls : {},
			customPost : function(cacheVars){
				GLOBAL_DIARY.note(3,"GROWN_SEED");

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

evt = null;
t=null;

