local cond_hcb_result= GAMESTATE:IsPlayerEnabled(PLAYER_1)

local cond_icb_result= 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_jcb",
  Def.ActorFrame{
    Name= "actor_kcb",
    Def.Quad{
      Name= "actor_lcb",
      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_limit_held = {1,1,1,1,1,1,1,1};
			
			mawaru_limit_answers = {
				{0,1,'does not\nexist'},
				{340000,3400,34000},
				{480000,4800,48000},
				{2000,80000,8000},
				{693,810,729},
				{800,128,160},
				{32,128,64},
				{1000000,10000,100000}
			}
			
			mawaru_limit_dir = {90,180,270}		
			mawaru_limit_dirt = {'&LEFT;','&UP;','&RIGHT;'}
			mawaru_limit_myans = {-1,-1}
			mawaru_limit_myanst = {'',''}
			
			--[[
			
			]]
			
			mawaru_limit_done = false;
			
			mawaru_limit_correct = {
				'does not\nexist',
				34000,
				48000,
				8000,
				729,
				160,
				64,
				100000
			}
			
			mawaru_limit_won = {false,false}
			
		end,
      KillCommand= function(self)
			--any cleanup, if necessary
		end,
      StartCommand= function(self)
		
			for i=1,8 do
				mawaru_limit_answers[i] = mawaru_shuffle(mawaru_limit_answers[i])
			end
			
			--variables set up when a game is started
			--wait 2 seconds (for the curtain or whatever) before accepting any inputs
			mawaru_limit_lockinput = {.3,.3};
			
			mawaru_limit_question = 0;
			
			if not mawaru_nickplaying then
				mawaru_limit_question = math.random(1,7)
			end
			
			mawaru_limit_qs:setstate(mawaru_limit_question);
			
			for i=1,3 do
				_G['mawaru_limit_a'..i]:settext(mawaru_limit_dirt[i]..'\n\n'..mawaru_limit_answers[mawaru_limit_question+1][i])
			end
			
			self:sleep(0.02);
			self:queuecommand('Update');

		end,
      UpdateCommand= function(self)
		
			if GAMESTATE:GetSongBeat() > 666 and not mawaru_limit_done then
				mawaru_limit_done = true;
				
				local anywon = false
				for pn=1,2 do
					if GAMESTATE:IsPlayerEnabled(pn-1) then
						Trace('Check answer: P'..pn..' = '..tostring(mawaru_limit_myanst[pn]) )
						Trace('Expected: '..tostring(mawaru_limit_correct[mawaru_limit_question+1]) )
						if tostring(mawaru_limit_myanst[pn]) == tostring(mawaru_limit_correct[mawaru_limit_question+1]) then
							anywon = true;
							mawaru_limit_won[pn] = true;
							MESSAGEMAN:Broadcast('LimitRightP'..pn)
							
							mawaru_score[pn] = mawaru_score[pn]+5;
							mawaru_misc_score[pn] = mawaru_misc_score[pn]+5;
							
						else
							mawaru_limit_won[pn] = false;
							MESSAGEMAN:Broadcast('LimitWrongP'..pn)
						end
					end
				end
				
				if anywon then
					GoodSound()
				else
					BadSound()
				end
				
			end
			
			--game logic
			
			for pn=1,2 do
			
				if GAMESTATE:IsPlayerEnabled(pn-1) then
				
					if mawaru_limit_lockinput[pn]>0 then
						mawaru_limit_lockinput[pn] = mawaru_limit_lockinput[pn]-0.02
					end
				
					local bpn = pn-1;
					for i=1,4 do
						
						----------------------handle button presses-------------------
						if mawaru_limit_lockinput[pn] <= 0 and GAMESTATE:GetSongBeat() < 666 then
							if mawaru_keys[i+(bpn*4)] == 1 and mawaru_limit_held[i+(bpn*4)] == 0 then
								if i~= 2 then
									if i==1 then
										mawaru_limit_myans[pn] = i
									else
										mawaru_limit_myans[pn] = i-1
									end
									mawaru_limit_myanst[pn] = mawaru_limit_answers[mawaru_limit_question+1][mawaru_limit_myans[pn] ]
									MESSAGEMAN:Broadcast('LimitAnsP'..pn);
								end
								mawaru_limit_held[i+(bpn*4)] = 1
							end
							
						end
						
						----------------------don't touch this------------------------
						--release all buttons - used for detecting if a button is newly pressed, as opposed to held
						if mawaru_keys[i+(bpn*4)] == 0 then
							mawaru_limit_held[i+(bpn*4)] = 0
						end
						---------------------end don't touch this----------------------
						
					end
				end
				
			end
			
			self:sleep(0.02);
			if mawaru_curgame == 18 and mawaru_gamestate == 1 then
				self:queuecommand('Update');
			end
			
		end,
    },
    Def.Quad{
      Name= "actor_mcb",
      InitCommand= function(self) mawaru_limit_controller = self end,
    },
    --For all parts, Unconverted x & y pos. -Kid
    Def.Sprite{
      Name= "actor_ncb",
      OnCommand= cmd(x,320;y,240),
      Texture= "bg.png",
    },
    Def.Sprite{
      Name= "actor_ocb",
      InitCommand= function(self) mawaru_limit_qs = self end,
      OnCommand= cmd(animate,0;x,320;y,140;zoom,1.5),
      Texture= "ans 2x4.png",
    },
    Def.BitmapText{
      Name= "actor_pcb",
      Font= "_eurostile outline",
      InitCommand= function(self) mawaru_limit_a1=self end,
      OnCommand= cmd(x,120;y,240;shadowlength,0),
      Text= "",
    },
    Def.BitmapText{
      Name= "actor_qcb",
      Font= "_eurostile outline",
      InitCommand= function(self) mawaru_limit_a2=self end,
      OnCommand= cmd(x,320;y,240;shadowlength,0),
      Text= "",
    },
    Def.BitmapText{
      Name= "actor_rcb",
      Font= "_eurostile outline",
      InitCommand= function(self) mawaru_limit_a3=self end,
      OnCommand= cmd(x,520;y,240;shadowlength,0),
      Text= "",
    },
    optional_actor(cond_hcb_result,
    Def.ActorFrame{
      Name= "actor_scb",
      OnCommand= cmd(x,160;y,480),
      Def.Sprite{
        Name= "actor_tcb",
        LimitAnsP1MessageCommand= cmd(finishtweening;setstate,1;zoom,0.5;linear,0.2;zoom,0.45),
        OnCommand= cmd(vertalign,bottom;zoom,0.45;animate,0;y,4),
        Texture= "p1 2x1.png",
      },
      Def.Sprite{
        Name= "actor_ucb",
        LimitAnsP1MessageCommand= function(self) self:finishtweening(); self:visible(true); self:rotationz(mawaru_limit_dir[ mawaru_limit_myans[1] ])
	self:zoom(0.8); self:addy(-13); self:linear(0.2); self:zoom(0.7); self:addy(13); end,
        OnCommand= cmd(x,-6;y,-146;zoom,0.7;visible,false),
        Texture= "arrow1.png",
      },
    }),
    optional_actor(cond_icb_result,
    Def.ActorFrame{
      Name= "actor_vcb",
      OnCommand= cmd(x,480;y,480),
      Def.Sprite{
        Name= "actor_wcb",
        LimitAnsP2MessageCommand= cmd(finishtweening;setstate,1;zoom,0.5;linear,0.2;zoom,0.45),
        OnCommand= cmd(vertalign,bottom;zoom,0.45;animate,0;y,4),
        Texture= "p2 2x1.png",
      },
      Def.Sprite{
        Name= "actor_xcb",
        LimitAnsP2MessageCommand= function(self) self:finishtweening(); self:visible(true); self:rotationz(mawaru_limit_dir[ mawaru_limit_myans[2] ])
	self:zoom(0.8); self:addy(-13); self:linear(0.2); self:zoom(0.7); self:addy(13); end,
        OnCommand= cmd(x,-6;y,-146;zoom,0.7;visible,false),
        Texture= "arrow2.png",
      },
    }),
    Def.Sprite{
      Name= "actor_ycb",
      LimitRightP1MessageCommand= cmd(visible,true;setstate,1),
      LimitWrongP1MessageCommand= cmd(visible,true;setstate,0),
      OnCommand= cmd(x,160;y,320;visible,false;animate,0),
      Texture= "grade 2x1.png",
    },
    Def.Sprite{
      Name= "actor_zcb",
      LimitRightP2MessageCommand= cmd(visible,true;setstate,1),
      LimitWrongP2MessageCommand= cmd(visible,true;setstate,0),
      OnCommand= cmd(x,480;y,320;visible,false;animate,0),
      Texture= "grade 2x1.png",
    },
    Def.Sprite{
      Name= "actor_Acb",
      DoCommand= cmd(finishtweening;diffusealpha,0;zoom,3;linear,0.2;diffusealpha,1;zoom,0.6;y,100;sleep,2.5),
      OnCommand= cmd(diffusealpha,0;zoom,3;vibrate;effectmagnitude,1,1,1;x,320;y,240),
      StartCommand= function(self) if mawaru_nickplaying then self:playcommand('Do') end end,
      Texture= "inst1.png",
    },
    Def.Sprite{
      Name= "actor_Bcb",
      DoCommand= cmd(finishtweening;diffusealpha,0;zoom,3;linear,0.2;diffusealpha,1;zoom,0.6;y,100;sleep,2.5),
      OnCommand= cmd(diffusealpha,0;zoom,3;vibrate;effectmagnitude,1,1,1;x,320;y,240),
      StartCommand= function(self) if not mawaru_nickplaying then self:playcommand('Do') end end,
      Texture= "inst2.png",
    },
  },
}
