% 17 Sep 2003 notes
% Add max pause before automatic spin
% Version 3, will have index of maybe 100 images and sounds to use.

% 10 Sep 2003 notes.
% 1. Ensure that at least six spins between asterisks,
% but natural randomness aftre sixth.
% 2. Adjust to key and mouse click.
% 3. Initial screen for investigator control (backtick key).
% 4. Versions 2 always ends on 10 except asterisk every eighth trial.
% Number reward screen will simply count "Trial = 1", "Trial = 2", "Trial = 3"...
% until asterisk and then reset, no $$$ indicator.
% 5. Version 3.

% Wheel graphics Numbers themselves white, number backgrounds light blue
% and red, 0 black, * green

% myIMROTATEMMDD.m
% by Daniel Shima, Vanderbilt Vision Research Center.

clear all;
close all;

warning off MATLAB:DeprecatedLogicalAPI;
GetSecs;

% *********** USER DEFINED VARIABLES ***********

versionnumber = 3;

multb = 2; % Starting value of multiplier for number hits.
multbincrement = 3; % When asterisk hit, multb will increase by this value.
totalplaytime = 60; % Number of seconds subject plays roulette, starts as soon as wheel appears for first time.
minspintime = 2; % Lower limit of randomized wheel spin time.
maxspintime = 4; % Upper limit of randmoized wheel spin time.
wheelstopperiod = 1; % Number of seconds wheel visible after it stops and before reward screen appears.
rewardscreenperiod = 2; % Number of seconds reward screen visible.
interasteriskspins = 3; % Forced minimum hits on number between asterisk hits.
startresult = '10';
% **********************************************

spinsnoasterisk = 0;
totalmoney = 0;
j = 0; % Variable j is index for output array.

stamp0 = datestr(now,6); % 'mm/dd' (e.g., 03/01)
stamp1 = datestr(now,13); % ' 'HH:MM:SS' (e.g., 15:13:40)
stamp3 = datestr(now,10); % 'yyyy' (e.g., 2003)
stamp2 = [stamp3 stamp0(1:2) stamp0(4:5) stamp1(1:2) stamp1(4:5) stamp1(7:8)]; % 0301151340
outputfid = fopen([stamp2 'output.txt'],'wt');

pixelSize = 32;
backgroundcolor = [63 63 63];
[w, screenRect] = SCREEN(0,'OpenWindow',backgroundcolor,[],pixelSize); % Define your output screen as your monitor which is screen 0, give it an RGB backgroundcolor.
HideCursor;

framerate = SCREEN(w,'FrameRate');

load mywheel; % Variables mywheelarray, valueA, and characters.
load invalidpoints0818; % Variables invalidpoints, height, and width.
load rotatedimage001; % Variables rotatedimage and frames.

bottomwheelPoint = [screenRect(3)/2 screenRect(4)/2 screenRect(3)/2 screenRect(4)/2] + [0 height/2 0 height/2];

% Create scr for each angle of wheel.
for i = 1:1:frames
    scr(i) = SCREEN(w,'OpenOffscreenWindow',backgroundcolor,[0 0 width height]);
    SCREEN(scr(i),'PutImage',rotatedimage(:,:,:,i),[0 0 width height]);
end

for (i =         1:           9) result(i,:) = characters( 1,:); end;
for (i = 9+1+ 0*18:9+1+ 0*18+17) result(i,:) = characters(20,:); end;
for (i = 9+1+ 1*18:9+1+ 1*18+17) result(i,:) = characters(19,:); end;
for (i = 9+1+ 2*18:9+1+ 2*18+17) result(i,:) = characters(18,:); end;
for (i = 9+1+ 3*18:9+1+ 3*18+17) result(i,:) = characters(17,:); end;
for (i = 9+1+ 4*18:9+1+ 4*18+17) result(i,:) = characters(16,:); end;
for (i = 9+1+ 5*18:9+1+ 5*18+17) result(i,:) = characters(15,:); end;
for (i = 9+1+ 6*18:9+1+ 6*18+17) result(i,:) = characters(14,:); end;
for (i = 9+1+ 7*18:9+1+ 7*18+17) result(i,:) = characters(13,:); end;
for (i = 9+1+ 8*18:9+1+ 8*18+17) result(i,:) = characters(12,:); end;
for (i = 9+1+ 9*18:9+1+ 9*18+17) result(i,:) = characters(11,:); end;
for (i = 9+1+10*18:9+1+10*18+17) result(i,:) = characters(10,:); end;
for (i = 9+1+11*18:9+1+11*18+17) result(i,:) = characters( 9,:); end;
for (i = 9+1+12*18:9+1+12*18+17) result(i,:) = characters( 8,:); end;
for (i = 9+1+13*18:9+1+13*18+17) result(i,:) = characters( 7,:); end;
for (i = 9+1+14*18:9+1+14*18+17) result(i,:) = characters( 6,:); end;
for (i = 9+1+15*18:9+1+15*18+17) result(i,:) = characters( 5,:); end;
for (i = 9+1+16*18:9+1+16*18+17) result(i,:) = characters( 4,:); end;
for (i = 9+1+17*18:9+1+17*18+17) result(i,:) = characters( 3,:); end;
for (i = 9+1+18*18:9+1+18*18+17) result(i,:) = characters( 2,:); end;
for (i = 9+1+19*18:9+1+19*18+ 8) result(i,:) = characters( 1,:); end;

numberscr = SCREEN(w,'OpenOffscreenWindow');
SCREEN(numberscr,'TextSize',24);
cashwidth = SCREEN(numberscr,'TextWidth','$$$ CASH $$$');

asteriskscr = SCREEN(w,'OpenOffscreenWindow');
SCREEN(asteriskscr,'TextSize',24);

wheelscr = SCREEN(w,'OpenOffscreenWindow',backgroundcolor);
spinwidth = SCREEN(wheelscr,'TextWidth','SPIN');

startscr = SCREEN(w,'OpenOffscreenWindow',backgroundcolor);
SCREEN(startscr,'TextSize',24);
backtickstartwidth = SCREEN(startscr,'TextWidth','Hit the backtick to start.');
SCREEN(startscr,'DrawText','Hit the backtick to start.',screenRect(3)/2-backtickstartwidth/2,screenRect(4)/2,[0 0 0]);

i = 1;
while (1)
    if (result(i,:) == startresult)
        starti = i;
        initialstarti = i;
        break;
    else
        i = i + 18;
    end
end

endflag = 0;
textendx1 = 0;
textendy1 = 0;
textendx2 = 0;
textendy2 = 0;
textendx3 = 0;
textendy3 = 0;
spinhit = WAVREAD('SPINHIT');
asterisk = WAVREAD('ASTERISK');
cashreg = WAVREAD('CASHREG');

% Wait for investigator to hit backtick key.
SCREEN('CopyWindow',startscr,w);
[keyIsDown,secs,keyCode] = KbCheck;
while (1)
    [keyIsDown,secs,keyCode] = KbCheck;
    if (keyIsDown)
        if (KbName(find(keyCode)) == '`')
            SCREEN(w,'FillRect',backgroundcolor);
            break;
        end
    end
end
while (KbCheck) end;

% Create initial wheel, yellow arrow indicator, and button graphic at bottom.
SCREEN(w,'FillPoly',[255 255 0],[screenRect(3)/2,screenRect(4)/2+height/2; screenRect(3)/2-10/2,screenRect(4)/2+height/2+10; screenRect(3)/2+10/2,screenRect(4)/2+height/2+10]);
SCREEN(w,'FillRect',[0 0 0],[-150 -20+50 150 20+50] + bottomwheelPoint);
SCREEN(w,'FillRect',[255 255 255],[-150+5 -20+50+5 150-5 20+50-5] + bottomwheelPoint);
SCREEN(w,'DrawText','SPIN',bottomwheelPoint(1)-spinwidth/2,bottomwheelPoint(2)+55,[0 0 0]);
SCREEN('CopyWindow',scr(starti),w,[0 0 width height],CenterRect([0 0 height width],screenRect));
wheeltime = GetSecs;
startplay = wheeltime;

while (~endflag & GetSecs - startplay < totalplaytime)
% while (~endflag)
    
    [mousex,mousey,mousebutton] = GetMouse;
    while (~KbCheck & ~mousebutton)
        [mousex,mousey,mousebutton] = GetMouse;
    end
    j = j + 1;
    SCREEN(w,'FillRect',[255 255 255],[-150 -20+50 150 20+50] + bottomwheelPoint);
    SCREEN(w,'FillRect',[0 0 0],[-150+5 -20+50+5 150-5 20+50-5] + bottomwheelPoint);
    SCREEN(w,'DrawText','SPIN',bottomwheelPoint(1)-spinwidth/2,bottomwheelPoint(2)+55,[255 255 255]);
    [mousex,mousey,mousebutton] = GetMouse;
    while (KbCheck | mousebutton)
        [mousex,mousey,mousebutton] = GetMouse;
    end
    subjectpresstime = GetSecs;
    SCREEN(w,'FillRect',[0 0 0],[-150 -20+50 150 20+50] + bottomwheelPoint);
    SCREEN(w,'FillRect',[255 255 255],[-150+5 -20+50+5 150-5 20+50-5] + bottomwheelPoint);
    SCREEN(w,'DrawText','SPIN',bottomwheelPoint(1)-spinwidth/2,bottomwheelPoint(2)+55,[0 0 0]);
    SCREEN(w,'FillRect',backgroundcolor,[0 0 max([textendx1 textendx2 textendx3]) max([textendy1 textendx2 textendx3])+10]); % Plus 10 for text comma removal.
    WAVPLAY(spinhit,'async');
    if (versionnumber == 1 | versionnumber == 3)
        spintime = RandSample([minspintime:maxspintime]); % Randomly spin between minspintime and maxspintime seconds.
        requestedspintime = spintime;
        spinframes = spintime*framerate;
        if (result(mod(starti-1+spinframes,frames)+1,:) == ' *' & spinsnoasterisk < interasteriskspins)
            spinframes = spinframes + RandSample([-4 -3 -2 -1 1 2 3 4])*18;
            fprintf('Asterisk result avoided because spinsnoasterisk was %d.\n',spinsnoasterisk);
        end
    elseif (versionnumber == 2)
        spintime = RandSample([minspintime:maxspintime]); % Randomly spin between minspintime and maxspintime seconds.
        requestedspintime = spintime;
        spinframes = round(spintime*framerate/frames)*frames;
%         while (1)
%             if (result(mod(starti-1+spinframes,frames)+1,:) == startresult & spinsnoasterisk < interasteriskspins)
%                 break;
%             elseif (result(mod(starti-1+spinframes,frames)+1,:) == ' *' & spinsnoasterisk == interasteriskspins)
%                 break;
%             else
%                 spinframes = spinframes + 18;
%                 % fprintf('Shifted to "%s".\n',result(mod(starti-1+spinframes,frames)+1,:));
%             end
%         end
        while (1)
            if (mod(starti-1+spinframes,frames)+1 == initialstarti & spinsnoasterisk < interasteriskspins)
                break;
            elseif (result(mod(starti-1+spinframes,frames)+1,:) == ' *' & spinsnoasterisk == interasteriskspins)
                break;
            else
                spinframes = spinframes + 18;
                % fprintf('Shifted to "%s".\n',result(mod(starti-1+spinframes,frames)+1,:));
            end
        end
        fprintf('\n');
    end
    startspin = GetSecs;
    for i = starti:starti-1+spinframes;
        SCREEN(w,'WaitBlanking',1);
        SCREEN('CopyWindow',scr(mod(i,frames)+1),w,[0 0 width height],CenterRect([0 0 height width],screenRect));
    end
    finishspin = GetSecs;
    actualspintime = finishspin - startspin;
    starti = mod(i,frames)+1;
    [textendx1 textendy1] = SCREEN(w,'DrawText',['FRAME ' num2str(mod(i,frames)+1) ', RESULT "' result(mod(i,frames)+1,:) '"'],15,15,[255 255 0]);

    if (result(mod(i,frames)+1,:) == ' *') % If asterisk hit.
        startwait = GetSecs;
        WAVPLAY(asterisk,'async');
        multb = multb + multbincrement;
        spinsnoasterisk = 0;

        SCREEN(asteriskscr,'FillRect',[63 0 0]);
        if (versionnumber == 1)
            totalwidth = SCREEN(asteriskscr,'TextWidth',['You have won a total of $' num2str(totalmoney,'%0.2f')]);
            SCREEN(asteriskscr,'DrawText',['You have won a total of $' num2str(totalmoney,'%0.2f')],screenRect(3)/2-totalwidth/2,screenRect(4)/2,[255 0 0]);
        end
        if (versionnumber == 1 | versionnumber == 2)
            multwidth = SCREEN(asteriskscr,'TextWidth',['The multiplier will be increased to ' num2str(multb)]);
            SCREEN(asteriskscr,'DrawText',['The multiplier will be increased to ' num2str(multb)],screenRect(3)/2-multwidth/2,screenRect(4)/2+24*2,[255 0 0]);
        end
        if (versionnumber == 3)
            getreadywidth = SCREEN(asteriskscr,'TextWidth',['Get ready for more spins!']);
            SCREEN(asteriskscr,'DrawText',['Get ready for more spins!'],screenRect(3)/2-getreadywidth/2,screenRect(4)/2,[255 0 0]);
        end
        
        while (GetSecs - startwait < wheelstopperiod) end
        SCREEN('CopyWindow',asteriskscr,w,screenRect,screenRect);
        rewardscreentime = GetSecs;
        output(j,1:8) = [1 subjectpresstime-wheeltime rewardscreentime-startplay 0 multb totalmoney requestedspintime actualspintime];
        
        SCREEN(wheelscr,'FillPoly',[255 255 0],[screenRect(3)/2,screenRect(4)/2+height/2; screenRect(3)/2-10/2,screenRect(4)/2+height/2+10; screenRect(3)/2+10/2,screenRect(4)/2+height/2+10]);
        SCREEN(wheelscr,'FillRect',[0 0 0],[-150 -20+50 150 20+50] + bottomwheelPoint);
        SCREEN(wheelscr,'FillRect',[255 255 255],[-150+5 -20+50+5 150-5 20+50-5] + bottomwheelPoint);
        SCREEN(wheelscr,'DrawText','SPIN',bottomwheelPoint(1)-spinwidth/2,bottomwheelPoint(2)+55,[0 0 0]);
        SCREEN('CopyWindow',scr(mod(i,frames)+1),wheelscr,[0 0 width height],CenterRect([0 0 height width],screenRect));
        
        while (GetSecs - startwait < wheelstopperiod + rewardscreenperiod) end
        SCREEN('CopyWindow',wheelscr,w);
        wheeltime = GetSecs;
    
    else % Else a number hit.
        startwait = GetSecs;
        WAVPLAY(cashreg,'async');
        totalmoney = totalmoney + str2num(result(mod(i,frames)+1,:))*multb/100;
        spinsnoasterisk = spinsnoasterisk + 1;

        [textendx2 textendy2] = SCREEN(w,'DrawText',['$' num2str(str2num(result(mod(i,frames)+1,:))/100,'%0.2f') ' x ' num2str(multb) ' = $' num2str(str2num(result(mod(i,frames)+1,:))*multb/100,'%0.2f')],15,30,[255 255 0]);
        [textendx3 textendy3] = SCREEN(w,'DrawText',['TOTAL MONEY $' num2str(totalmoney,'%0.2f')],15,60,[255 255 0]);

        SCREEN(numberscr,'FillRect',[0 63 0]);
        if (versionnumber == 1)
            winwidth = SCREEN(numberscr,'TextWidth',['$' num2str(str2num(result(mod(i,frames)+1,:))/100,'%0.2f') ' x ' num2str(multb) ' = $' num2str(str2num(result(mod(i,frames)+1,:))*multb/100,'%0.2f')]);
            SCREEN(numberscr,'DrawText','$$$ CASH $$$',screenRect(3)/2-cashwidth/2,screenRect(4)/2,[0 191 0]);
            SCREEN(numberscr,'DrawText',['$' num2str(str2num(result(mod(i,frames)+1,:))/100,'%0.2f') ' x ' num2str(multb) ' = $' num2str(str2num(result(mod(i,frames)+1,:))*multb/100,'%0.2f')],screenRect(3)/2-winwidth/2,screenRect(4)/2+24*2,[0 191 0]);
        end
        if (versionnumber == 2)
            trialwidth = SCREEN(numberscr,'TextWidth',['Trial ' num2str(j)]);
            SCREEN(numberscr,'DrawText',['Trial ' num2str(j)],screenRect(3)/2-trialwidth/2,screenRect(4)/2+24*2,[0 191 0]);
        end
        if (versionnumber == 3)
            for k = 1:360
                [novelx,novely] = POL2CART(k/360*2*pi,RandSample([1:screenRect(4)/2]));
                SCREEN(numberscr,'DrawLine',[0 191 0],screenRect(3)/2,screenRect(4)/2,screenRect(3)/2+novelx,screenRect(4)/2+novely);
            end
        end

        while (GetSecs - startwait < wheelstopperiod) end
        SCREEN('CopyWindow',numberscr,w,screenRect,screenRect);
        rewardscreentime = GetSecs;
        output(j,1:8) = [2 subjectpresstime-wheeltime rewardscreentime-startplay str2num(result(mod(i,frames)+1,:))/100 multb totalmoney requestedspintime actualspintime];
        
        SCREEN(wheelscr,'FillPoly',[255 255 0],[screenRect(3)/2,screenRect(4)/2+height/2; screenRect(3)/2-10/2,screenRect(4)/2+height/2+10; screenRect(3)/2+10/2,screenRect(4)/2+height/2+10]);
        SCREEN(wheelscr,'FillRect',[0 0 0],[-150 -20+50 150 20+50] + bottomwheelPoint);
        SCREEN(wheelscr,'FillRect',[255 255 255],[-150+5 -20+50+5 150-5 20+50-5] + bottomwheelPoint);
        SCREEN(wheelscr,'DrawText','SPIN',bottomwheelPoint(1)-spinwidth/2,bottomwheelPoint(2)+55,[0 0 0]);
        SCREEN('CopyWindow',scr(mod(i,frames)+1),wheelscr,[0 0 width height],CenterRect([0 0 height width],screenRect));
        
        while (GetSecs - startwait < wheelstopperiod + rewardscreenperiod) end
        SCREEN('CopyWindow',wheelscr,w);
        wheeltime = GetSecs;
        
        [textendx1 textendy1] = SCREEN(w,'DrawText',['FRAME ' num2str(mod(i,frames)+1) ', RESULT "' result(mod(i,frames)+1,:) '"'],15,15,[255 255 0]);
        [textendx2 textendy2] = SCREEN(w,'DrawText',['$' num2str(str2num(result(mod(i,frames)+1,:))/100,'%0.2f') ' x ' num2str(multb) ' = $' num2str(str2num(result(mod(i,frames)+1,:))*multb/100,'%0.2f')],15,30,[255 255 0]);
        [textendx3 textendy3] = SCREEN(w,'DrawText',['TOTAL MONEY $' num2str(totalmoney,'%0.2f')],15,60,[255 255 0]);
    end
    
    while (1)
        [keyIsDown,secs,keyCode] = KbCheck;
        if (keyIsDown)
            if (find(keyCode) == KbName('q'))
                endflag = 1;
            end
            break;
        end
    end
    
end

for i = 1:size(output,1)
    for j = 1:size(output,2)
        if (j == size(output,2))
            fprintf(outputfid,'%0.3f\n',output(i,j));
        else
            fprintf(outputfid,'%0.3f\t',output(i,j));
        end;
    end
end

st = fclose(outputfid);

FlushEvents('keyDown');
ShowCursor;
% Close all screens.
SCREEN('CloseAll');
