%%% ========= SETUP PARAMETERS ========= %%
clear all;
nsubs = 84; % total number of subjects we will test

% Stimuli paramaters
stim.streams    = 1:4; % number of streams
stim.shapes     = 9;   % number of shapes in each stream
stim.reps       = 50;  % number of times each shape is presented
stim.search     = 80;  % number of search trials

% Experiment parameters
stim.trialreps  = stim.shapes * stim.reps; % total number of shapes
stim.ntrials    = stim.trialreps + stim.search; % 450 shape presentations and 80 search trials

% Counterbalance parameters: the 'structured stream' has the nine shapes
% grouped arbitrarily for each participant into three triplets that remain
% constant throughout the experiment. The location of this stream is also
% counterbalanced.
counterbalanceStr = Shuffle(repmat(stim.streams,1,nsubs/length(stim.streams))); % chose which set is structured for each sub
counterbalanceLoc = Shuffle(repmat(stim.streams,1,nsubs/length(stim.streams))); % chose which location is structured for each sub

%%% ========= MAKE TRIALS ========= %%
maxIter = 1000; % max attempts at generating sequence

for iSub = 1:nsubs
     
    rand('state',sum(100*clock)); % Reseed the random-number generator for each sub.

    % Get chosen counterbalancing options
    trial(iSub).chosen.strStream     = counterbalanceStr(iSub);
    trial(iSub).chosen.strLocation   = counterbalanceLoc(iSub);
    trial(iSub).chosen.randStreams   = Shuffle(setdiff(stim.streams,counterbalanceStr(iSub))); % shuffle random streams
    trial(iSub).chosen.randLocations = Shuffle(setdiff(stim.streams,counterbalanceLoc(iSub))); % shuffle random streams
    
    %% MAKE REGULAR STREAM
    % Each triplet is repeated stim.reps times during the experiment. The
    % order of triplets is randomized, with the constraint that no triplet
    % can appear twice in a row.
    ii=0; 
    shuffleTrip = Shuffle(1:stim.shapes); % shuffle image order
    for tri = 1:3  % choose triplet combos for structured stream
        for img = 1:3, ii=ii+1;
            trial(iSub).chosen.triplets(tri,img) = shuffleTrip(ii); % 3 sets of 3 images each for structured condition
        end
    end
    
    for iBlock = 1:2  % double experiment length
        complete = 0; % reset for block 2
        while complete == 0 % while we haven't successfully made the sequence
            triplets     = repmat(1:3,1,stim.trialreps/9); % init triplet order for structured stream
            triplets     = triplets(randperm(numel(triplets))); % initial shuffle of triplet order
            strStream    = zeros(1,stim.trialreps/3); % init structured stream
            strStream(1) = triplets(1); % take first triplet
            triplets(1)  = []; % remove it from the triplet vector
            iter = 1; ii = 1;
            while any(~strStream) % reshuffle sequence if chosen triplet is the same as the previous one shown
                iter = iter+1; if iter > maxIter, break; end % stop if we maxed the allowed iterations
                current  = triplets(1);     % chose the next trial
                previous = strStream(ii);   % get the previous trial
                if current == previous      % if they are the same
                    triplets = triplets(randperm(numel(triplets))); % shuffle again
                    continue % shuffle and go back to start
                end
                ii = ii+1; strStream(ii) = current; % accept the chosen triplet
                triplets(1) = []; % drop it from the rep vector
            end
            if any(~strStream), complete = 0; else, complete = 1; end % check if we've successfully finished making the sequence
        end
        
        ii=0; % extract full sequence
        for kk = 1:length(strStream)
            chosenTrip = strStream(kk); % get next triplet
            for img = 1:3; ii=ii+1; % for each shape in chosen triplet
                trial(iSub).struct(iBlock,ii) = trial(iSub).chosen.triplets(chosenTrip,img); % get the images for that triplet
            end
        end
         
        %% MAKE RANDOM STREAM
        % In each random stream, the nine shapes are sequenced individually.
        % Each shape is repeated stim.reps times, and the order of shapes is
        % randomized, with the constraint that no shape could repeat back to
        % back.
        for iRand = 1:3 % for each of the 3 random streams
            complete = 0;
            while complete == 0 % while we haven't finished making the seq
                rands         = repmat(1:stim.shapes,1,stim.trialreps/stim.shapes); % init order for rand stream
                rands         = rands(randperm(numel(rands))); % initial shuffle of random stream
                randStream    = zeros(1,stim.trialreps); % init random stream
                randStream(1) = rands(1); % take first image
                rands(1) = []; % remove it from the vector
                
                iter = 1; ii = 1;
                while any(~randStream) % reshuffle sequence if chosen shape is the same as the previous one shown
                    iter = iter+1; if iter > maxIter, break, end % stop if we maxed the allowed iterations
                    previous = randStream(ii);  % get the previous trial
                    current  = rands(1);        % chose the next trial
                    if current == previous % if they are the same
                        rands = rands(randperm(length(rands))); continue % shuffle and go back to start
                    end
                    ii = ii+1; randStream(ii) = current; % accept the chosen shape
                    rands(1) = []; % drop it from the rep vector
                end
                
                if any(~randStream), complete = 0; else, complete = 1; % if successfully generated
                    trial(iSub).random(iBlock,iRand,:) = randStream; % save the sequence
                end
            end
        end
        
        %% MAKE SEARCH TRIALS
        % There are 80 search trials interspersed randomly in between the shape
        % trials, which resulted in an average of 1 search trial for every 5.6
        % shape trials.
        searchShuffle = sort(randsample(1:stim.ntrials,stim.search)); % chose target trials
        searchTrials  = zeros(1,stim.search); ii=1; % init search trials
        while any(~searchTrials) % while we haven't finished chosing the trials
            current  = searchShuffle(ii); % get the current trial
            if ii==1, previous = 0; else, previous = searchTrials(ii-1); end % if nothing to compare on the first trial
            if current == previous+1 % if next search trial immediately follows pick a different one
                searchShuffle(ii) = randsample(setdiff(1:stim.ntrials,searchShuffle),1); % chose a new trial
                searchShuffle = sort(searchShuffle); % resort the trials
                searchTrials  = zeros(1,stim.search); % start again
                ii=1; continue
            end
            searchTrials(ii) = searchShuffle(ii); ii=ii+1; % accept the value
        end
        
        % Get target parameters
        % The target was a T shape rotated 90° (i.e., pointing left) or
        % 90° (i.e., pointing right). The distractors were L shapes rotated 0°
        % (i.e., pointing right) or 180° (i.e., pointing left), with 1 rotated the
        % same direction as the T, and the other two in the opposite direction.
        trial(iSub).search(iBlock).trials       = searchTrials;
        trial(iSub).search(iBlock).orientation  = Shuffle(repmat(1:2,1,stim.search/2)); % counterbalance pointing direction of T
        trial(iSub).search(iBlock).location     = Shuffle(repmat(1:4,1,stim.search/4)); % location of target
    end 
  
end

%%% ========= DEFINE PATHS ========= %%
% Shapes
paths.sets      = {'.\images\shapes\SET1\','.\images\shapes\SET2\','.\images\shapes\SET3\','.\images\shapes\SET4\'}; % paths for shape set locations
paths.names     = {'1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg','9.jpg'}; % shape filenames

% Search
paths.Ltop      = '.\images\search\Ltop.png'; % path for 'L' target
paths.Lbottom   = '.\images\search\Lbottom.png'; % path for 'L' target
paths.T         = '.\images\search\T.png'; % path for 'T' target

% Data 
paths.prefix    = 'EXP1_REP_'; % save file prefix

% Instructions
paths.inMain    = '.\images\instructions\instructions_main.jpg';
paths.inMem     = '.\images\instructions\instructions_mem.jpg';

 
%% Get full paths for each shape
for ii = 1:4
    for kk = 1:9
        paths.dirs{ii,kk} = [paths.sets{ii} paths.names{kk}]; 
    end
end

%% Save everything
save('trialinfo_TEMP.mat','paths','stim','trial')
