local cond_wmc_result= not GAMESTATE:IsPlayerEnabled(PLAYER_1)

local cond_xmc_result= not GAMESTATE:IsPlayerEnabled(PLAYER_2)

local function optional_actor(cond, actor)
	if cond then return actor end
	return Def.Actor{}
end

return Def.ActorFrame{
  Name= "actor_ymc",
  Def.ActorFrame{
    Name= "actor_zmc",
    Def.Quad{
      Name= "actor_Amc",
      InitCommand= function(self)
		
			--this is where variables go when the simfile starts up
			--function declarations, and things that will never change even
			--if this particular minigame is restarted after being played once before
		
			--for storing if a button is held down
			mawaru_sbahj_held = {0,0,0,0,0,0,0,0};
			
			mawaru_sbahj_players = {}
			mawaru_sbahj_bro = {}
			mawaru_sbahj_speed = 4
			mawaru_sbahj_grav = {0,0}
			mawaru_sbahj_fallspd = {0.5,0.5};
			mawaru_sbahj_maxfall = {5,5};
			mawaru_sbahj_floor = {180,180}
			mawaru_sbahj_bottom = 420
			
			mawaru_sbahj_bouncesleft = {3,3}
			
			mawaru_sbahj_dead = {false,false}
			mawaru_sbahj_ang = {0,0}
			mawaru_sbahj_bxspd = {0.3,0.3};
			mawaru_sbahj_brotimer = {0,0};
			mawaru_sbahj_brostate = {0,0};
			
			mawaru_sbahj_falls = {0.2,0.3,0.4,0.5,0.6,0.8}
			mawaru_sbahj_mfalls = {5,6,7,8,9,12}
			
		end,
      KillCommand= function(self)
			--any cleanup, if necessary
		end,
      StartCommand= function(self)
			
			--variables set up when a game is started
			--wait 2 seconds (for the curtain or whatever) before accepting any inputs
			mawaru_sbahj_lockinput = {.3,.3};
			
			for pn=1,2 do
				if mawaru_difficulty[pn] > 3 then
					MESSAGEMAN:Broadcast('StairsBG2P'..pn);
				else
					MESSAGEMAN:Broadcast('StairsBG1P'..pn);
				end
				
				if GAMESTATE:IsPlayerEnabled(pn-1) then
					mawaru_sbahj_fallspd[pn] = mawaru_sbahj_falls[ mawaru_difficulty[pn]+1 ]
					mawaru_sbahj_maxfall[pn] = mawaru_sbahj_mfalls[ mawaru_difficulty[pn]+1 ]
				end
			end
			
			self:sleep(0.02);
			self:queuecommand('Update');

		end,
      UpdateCommand= function(self)
			
			--game logic
			
			for pn=1,2 do
				if GAMESTATE:IsPlayerEnabled(pn-1) then
				
					if GAMESTATE:GetSongBeat() > 271 then
						if mawaru_sbahj_floor[pn] < mawaru_sbahj_bottom then
							mawaru_sbahj_floor[pn] = mawaru_sbahj_floor[pn]+1
							mawaru_sbahj_bro[pn]:addy(1);
						end
					end
				
					if not mawaru_sbahj_dead[pn] then
						--mawaru_sbahj_ang[pn] = mawaru_sbahj_ang[pn]+20;
						--mawaru_sbahj_bro[pn]:rotationz(mawaru_sbahj_ang[pn])
						mawaru_sbahj_brotimer[pn] = mawaru_sbahj_brotimer[pn]-0.02;
						if mawaru_sbahj_brotimer[pn] <= 0 then
							mawaru_sbahj_ang[pn] = mawaru_sbahj_ang[pn]+40+math.random(0,40);
							mawaru_sbahj_bro[pn]:rotationz(mawaru_sbahj_ang[pn])
							local bzr = 0.6+math.random(1,20)/100;
							mawaru_sbahj_bro[pn]:basezoomx(bzr)
							mawaru_sbahj_bro[pn]:basezoomy(bzr)
							mawaru_sbahj_bro[pn]:setstate(mawaru_sbahj_brostate[pn])
							if mawaru_sbahj_brostate[pn] == 0 then
								mawaru_sbahj_brostate[pn] = 1;
							else
								mawaru_sbahj_brostate[pn] = 0;
							end
							mawaru_sbahj_brotimer[pn] = 0.1;
						end
						
						mawaru_sbahj_bro[pn]:addy(mawaru_sbahj_grav[pn]);
						mawaru_sbahj_bro[pn]:addx(mawaru_sbahj_bxspd[pn]);
						if mawaru_sbahj_grav[pn] < mawaru_sbahj_maxfall[pn] then
							mawaru_sbahj_grav[pn] = mawaru_sbahj_grav[pn]+mawaru_sbahj_fallspd[pn]
						end
						
						if mawaru_sbahj_bro[pn]:GetY()+50 > mawaru_sbahj_floor[pn] or
							mawaru_sbahj_bro[pn]:GetY()+50 > mawaru_sbahj_floor[pn]-30 and
							math.abs(mawaru_sbahj_players[pn]:GetX()-mawaru_sbahj_bro[pn]:GetX()) <= 112*0.8+150*0.6 and mawaru_difficulty[pn] < 4 then
							mawaru_sbahj_grav[pn] = math.min(-math.abs(mawaru_sbahj_grav[pn]),-5)
							mawaru_sbahj_bro[pn]:y(mawaru_sbahj_floor[pn]-50)
							mawaru_sbahj_bouncesleft[pn] = mawaru_sbahj_bouncesleft[pn]-1
							Trace(mawaru_sbahj_bouncesleft[pn]);
							if mawaru_sbahj_bouncesleft[pn] < 0 then
								mawaru_sbahj_bouncesleft[pn] = 9999;
								mawaru_sbahj_floor[pn] = mawaru_sbahj_bottom;
								mawaru_sbahj_grav[pn] = mawaru_sbahj_grav[pn]-2;
								mawaru_sbahj_bxspd[pn] = 2.4
							elseif mawaru_sbahj_bouncesleft[pn] > 100 then
								if math.abs(mawaru_sbahj_players[pn]:GetX()-mawaru_sbahj_bro[pn]:GetX()) > (112*0.8*0.35)+(150*0.6*0.35) then
									mawaru_sbahj_dead[pn] = true;
									MESSAGEMAN:Broadcast('StairsDieP'..pn);
									if math.random(1,100) < 30 then
										Sound('wilhelm');
									else
										Sound('oww');
									end
									Sound('heavyfall');
								else
									MESSAGEMAN:Broadcast('StairsBounceP'..pn);
									Sound('bounce');
									mawaru_sbahj_grav[pn] = -12;
									mawaru_sbahj_bxspd[pn] = mawaru_sbahj_bxspd[pn]*1.1
									mawaru_sbahj_bro[pn]:y(mawaru_sbahj_floor[pn]-50-20)
								end
							end
						end
						
					end
					
					if mawaru_sbahj_lockinput[pn]>0 then
						mawaru_sbahj_lockinput[pn] = mawaru_sbahj_lockinput[pn]-0.02
					end
					
					if mawaru_sbahj_bro[pn]:GetX() > 320-50 then
						mawaru_sbahj_bro[pn]:x(320-50);
						mawaru_sbahj_bxspd[pn] = -math.abs(mawaru_sbahj_bxspd[pn])
					end
					
					if mawaru_sbahj_bro[pn]:GetX() < 50 then
						mawaru_sbahj_bro[pn]:x(50);
						mawaru_sbahj_bxspd[pn] = math.abs(mawaru_sbahj_bxspd[pn])
					end
				
					local bpn = pn-1;
						
					----------------------handle button presses-------------------
					if mawaru_sbahj_lockinput[pn] <= 0 then
						local d = 1;
						if pn == 2 then d = -1 end
						if mawaru_keys[1+(bpn*4)] == 1 then
							if mawaru_sbahj_players[pn]:GetX() > 50 and d == 1 or mawaru_sbahj_players[pn]:GetX() < 270 and d == -1 then
								mawaru_sbahj_players[pn]:addx(-mawaru_sbahj_speed*d);
							end
						end
						if mawaru_keys[4+(bpn*4)] == 1 then
							if mawaru_sbahj_players[pn]:GetX() < 270 and d == 1 or mawaru_sbahj_players[pn]:GetX() > 50 and d == -1 then
								mawaru_sbahj_players[pn]:addx(mawaru_sbahj_speed*d);
							end
						end
					end
					
				end	
			end
			
			self:sleep(0.02);
			if mawaru_curgame == 7 and mawaru_gamestate == 1 then
				self:queuecommand('Update');
			end
			
		end,
    },
    Def.Quad{
      Name= "actor_Bmc",
      InitCommand= function(self) mawaru_sbahj_controller = self end,
    },
    Def.ActorFrame{
      Name= "actor_Cmc",--Unconverted x pos. -Kid
      OnCommand= cmd(x,0;zoomx,1),
      Def.Sprite{
        Name= "actor_Dmc",--Unconverted x & y pos. -Kid
        OnCommand= cmd(x,160;y,240;visible,false),
        StairsBG1P1MessageCommand= cmd(visible,true),
        Texture= "bg1.png",
      },
      Def.Sprite{
        Name= "actor_Emc",--Unconverted x & y pos. -Kid
        OnCommand= cmd(x,160;y,240;visible,false),
        StairsBG2P1MessageCommand= cmd(visible,true),
        Texture= "bg2.png",
      },
      Def.Sprite{
        Name= "actor_Fmc",
        Frames= {
          {Frame= 3, Delay= 999},
          {Frame= 0, Delay= 0.05},
          {Frame= 1, Delay= 0.05},
          {Frame= 2, Delay= 0.05},
      },--Unconverted x & y pos. -Kid
        OnCommand= cmd(zoom,0.8;x,160;y,390;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_sbahj_players,self) end,
        StairsBounceP1MessageCommand= cmd(setstate,1),
        Texture= "./_trampo 2x2.png",
      },
      Def.Sprite{
        Name= "actor_anc",--Unconverted x & y pos. -Kid
        OnCommand= cmd(animate,0;x,50;y,100;basezoomx,0.6;basezoomy,0.6;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_sbahj_bro,self) end,
        StairsDieP1MessageCommand= cmd(animate,0;setstate,0;rotationz,0;sleep,0.03;vibrate;effectmagnitude,10,10,0;queuecommand,"StopVib"),
        StopVibCommand= cmd(stopeffect),
        Texture= "bro 2x1.png",
      },
    },
    Def.ActorFrame{
      Name= "actor_bnc",--Unconverted x & y pos. -Kid
      OnCommand= cmd(x,640;zoomx,-1),
      Def.Sprite{
        Name= "actor_cnc",--Unconverted x & y pos. -Kid
        OnCommand= cmd(x,160;y,240;visible,false),
        StairsBG1P2MessageCommand= cmd(visible,true),
        Texture= "bg1.png",
      },
      Def.Sprite{
        Name= "actor_dnc",--Unconverted x & y pos. -Kid
        OnCommand= cmd(x,160;y,240;visible,false),
        StairsBG2P2MessageCommand= cmd(visible,true),
        Texture= "bg2.png",
      },
      Def.Sprite{
        Name= "actor_enc",
        Frames= {
          {Frame= 3, Delay= 999},
          {Frame= 0, Delay= 0.05},
          {Frame= 1, Delay= 0.05},
          {Frame= 2, Delay= 0.05},
      },--Unconverted x & y pos. -Kid
        OnCommand= cmd(zoom,0.8;x,160;y,390;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_sbahj_players,self) end,
        StairsBounceP2MessageCommand= cmd(setstate,1),
        Texture= "./_trampo 2x2.png",
      },
      Def.Sprite{
        Name= "actor_fnc",--Unconverted x & y pos. -Kid
        OnCommand= cmd(animate,0;x,50;y,100;basezoomx,0.6;basezoomy,0.6;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_sbahj_bro,self) end,
        StairsDieP2MessageCommand= cmd(animate,0;setstate,0;rotationz,0;sleep,0.03;vibrate;effectmagnitude,10,10,0;queuecommand,"StopVib"),
        StopVibCommand= cmd(stopeffect),
        Texture= "bro 2x1.png",
      },
    },
    Def.Quad{
      Name= "actor_gnc",--Unconverted x & y pos. -Kid
      OnCommand= cmd(x,320;y,240;zoomto,6,480;diffuse,color('0,0,0,1')),
    },
    optional_actor(cond_wmc_result,
    Def.Quad{
      Name= "actor_hnc",--Unconverted x & y pos. -Kid
      OnCommand= cmd(x,640*0.25;y,240;zoomto,640/2,480;diffuse,color('0,0,0,1')),
    }),
    optional_actor(cond_xmc_result,
    Def.Quad{
      Name= "actor_inc",--Unconverted x & y pos. -Kid
      OnCommand= cmd(x,640*0.75;y,240;zoomto,640/2,480;diffuse,color('0,0,0,1')),
    }),
  },
}
