function [ NEWLOC ] = nextroom (LOC, LL)
% Given current LOC and encoded value LL with conditions for this move,
% find NEWLOC for Adventure game, assuming all those conditions are met
% Method is from the FORTRAN source code, subroutine NEW1.FOR

% Starting from below label 10, the initial situation is that we have
% already decoded the entry in the TRAVEL array by taking the absolute value
% to remove the end-of-list marker (if any), and divided by 1000 to
% drop the action number. Thus, LL should now look exactly like the second
% field in the data file TEXT.TXT, and action number must have been one
% of the values from the remainder of the same row in the data file.

BigPart = floor(LL/1000);   % upper digits after splitting LL go here
SmallPart = mod (LL, 1000); % lower digits after splitting LL go here
if BigPart > 0 % if present, it ONLY adds restrictions to the move
    K = num2str(mod (BigPart, 100)); % lower 2 digits could be the object that enables magic
    if BigPart > 300  % eg. 8,303009,3,19,30,0,0,0
        disp(['BigPart says the PROP value for item ', K,' from section 5 of TEXT.TEX cannot = ', ...
            num2str(floor(BigPart/100)-3),' to continue']);
    elseif BigPart > 100 % eg. 100,159302,71,0,0,0,0
        disp(['BigPart says you must TOTING item ', K,' from section 5 of TEXT.TXT to continue']);
    else % eg. 5,50005,6,7,45,0,0,0,0
        disp(['BigPart says you have a ', K, ' percent probability of making this move']);
    end
end

% SmallPart ALWAYS contains the location of the next room, and possibly
% more restrictions. BigPart (if it exists) merely adds some restrictions.
% Thus, for the following examples from TEXT.TXT
% 8,303009,3,19,30,0,0,0 we must decode 009
% 100,159302,71,0,0,0,0  we must decode 302
% 7,595,60,14,30,0,0,0,0 we must decode 595
% 14,11,51,0,0,0,0       we must decode 11

if SmallPart > 500
    disp(['You must SPEAK item ', num2str(SmallPart-500), ...
        ' from section 6 of TEXT.TXT and stay where you are.']);
    NEWLOC=LOC;
    return;
elseif SmallPart > 300 % You found a SPECIAL CASE MOTION!!
    switch SmallPart-300
        case 1
            disp('The PLOVER-ALCOVE PASSAGE takes you (and possibly the emerald) between rooms 99 and 100.');
            NEWLOC=99+100-LOC;
            return;
        case 2
            disp('You drop the emerald by trying to move it without the plover and go nowhere.');
            NEWLOC=LOC;
            return;
        case 3
            disp('You will cross the TROLL BRIDGE connecting rooms 117 and 122 if you paid the toll.');
            NEWLOC=117+122-LOC;
            return;
    end
else  % normal case where entries <= 300 are just room numbers
    NEWLOC = SmallPart;
end
end