//***************************************************************
// insertPOP()
//
// Takes the list POP and inserts these
// systems into the GRIDSPC
// Comment:
// Additional possible images will only be shown either via the ainp()-Function
// or by a special VIEWER-Client (which is not yet part of this software project)
function [GRIDSPC]=insertPOP(GRIDSPC,POP)
[r,c]=size(POP)
for i=1:r //for every element of the list
Y=eval(POP(i,3))
X=eval(POP(i,4))
GRIDSPC(Y,X)=POP(i,2)
end
endfunction
//*************************************************
// RANDOM Actions
// Generates random actionsa based on uniform random numbers from 0 ... 9
//
// NACT := new action
function [NACT] =actRand()
NACT=floor(10*rand())
//printf("action:RANDOM NUMBER NACT= %d\n\n",NACT)
endfunction
//*************************************************
// RANDOM WIB
// (In test cases)
//
// Generates a random WIB for test cases
//
// MINMAL ASSUMPTIONS ABOUT RANDOM LSS1
//
// ENERGY-STATUS := Amount of available energy
// ENERGY-CONSUMPTION := Amount of energy decreasing the status in one cycle
// ENERGY-INPUT := amount of energy to increase status (from POP)
// ACTION-NEW := either a move or an eat = random action {0, ..., 9}
// POPRAND := list of dummy systems PRESET with ENERGY-STATUS AND ENERGY-CONSUMPTION
//
// FORMAT POPRAND:
// [[ID, ENERGY-STATUS, ENERGY-CONSUMPTION], ...]
function [WIB, POPRAND]=randomWIB(WIB,POP,POPRAND,SHOW)
[r,c]=size(POP) //assumes that POP and WIB have the same systems
//in the same order !
for i=1:r //for every element of the list POP
if SHOW==1 then disp('BEFORE POPRAND'), disp(POPRAND),disp(WIB), end
ENERGY=eval(POPRAND(i,2))
if (ENERGY>0)
//Update ENERGY-STATUS
NEWENERGY=eval(POP(i,8)) //Use collected energy
ENERGYCONSUMPTION=eval(POPRAND(i,3))
POPRAND(i,2)=sci2exp(ENERGY+NEWENERGY-ENERGYCONSUMPTION)
//Generate new action
[NACT]=actRand()
if SHOW==1 then disp('BEFORE WIB NEW'), disp(WIB), end
if NACT == 9 then WIB(i,3)=sci2exp(NACT) //case non-move
WIB(i,2)='0' //set move to zero
else
WIB(i,2)=dec2bin(NACT) //case move
WIB(i,3)='0' //set nmove to zero
end
end
end
endfunction
//*************************************************
// newPOS()
//
// Having the OLD position from POP and the intended movement as DIR from WIB
// (which in a special test case will be written by a random function)
// the function computes the NEW Pos
//
// XO, YO := old X-Y-Position
// DIR := direction of move
// XN, XN := new X-Y-Position
//
// The results will be stored in POP as NEWPOS
//
// ATTENTION : scilab counts Y from 'top = north' downwards to 'down = south'
// Thus to move to the 'north' with '01' you have to subtract '1'
// The same with a move from 'right = east' to the 'left = west'
//
function [POP] =newPOS(POP, WIB)
[r,c]=size(POP) //assumes that POP and WIB have the same systems
//in the same order !
for i=1:r
//Get new move
DIR=bin2dec(WIB(i,2))
//Get old position
YO = eval(POP(i,3))
XO =eval(POP(i,4))
//Compute coordinates for possible new position
if DIR == 0 then XN=XO, YN=YO // no move
elseif DIR == 1 then YN=YO-1, XN=XO
elseif DIR==2 then YN=YO-1, XN=XO+1
elseif DIR==3 then YN=YO, XN=XO+1
elseif DIR==4 then YN=YO+1, XN=XO+1
elseif DIR==5 then YN=YO+1, XN=XO
elseif DIR==6 then YN=YO+1, XN=XO-1
elseif DIR==7 then YN=YO, XN=XO-1
elseif DIR==8 then YN=YO-1, XN=XO-1
else printf("mover:ERROR WITH MOVEMENT AT = (YO,XO) = ( %d,%d)\n",YO,XO)
end// End of one element of POP list
//Write possible new position
POP(i,5)=sci2exp(YN)
POP(i,6)=sci2exp(XN)
end
endfunction
//****************************************
// findFOOD()
//
// This is a helper function for random test cases
// It checks for a random system whether there is some food around
// If yes this food is consumed
// It checks in clockwise order all positions
// But does only one uptake out of all
//
// i := index of system in POP
// YO, XO := actual position
// POP := list of systems in the world
// GRIDSPC := actual grid space of the world
function [POP]=updateENERGY(i,YO,XO,POP,GRIDSPC,SHOW)
if SHOW==1 then printf('POSITION (Y,X) = %d %d\n',YO,XO), end
//Compute coordinates for possible new position with food
// Proceeds in clockwise order
for DIR=1:8
if DIR == 1 then YN=YO-1, XN=XO,
if isequal(GRIDSPC(YN,XN),'F') then POP(i,8)='100',end
elseif DIR==2 then YN=YO-1, XN=XO+1
if isequal(GRIDSPC(YN,XN),'F') then POP(i,8)='100',end
elseif DIR==3 then YN=YO, XN=XO+1
if isequal(GRIDSPC(YN,XN),'F') then POP(i,8)='100',end
elseif DIR==4 then YN=YO+1, XN=XO+1
if isequal(GRIDSPC(YN,XN),'F') then POP(i,8)='100',end
elseif DIR==5 then YN=YO+1, XN=XO
if isequal(GRIDSPC(YN,XN),'F') then POP(i,8)='100',end
elseif DIR==6 then YN=YO+1, XN=XO-1
if isequal(GRIDSPC(YN,XN),'F') then POP(i,8)='100',end
elseif DIR==7 then YN=YO, XN=XO-1
if isequal(GRIDSPC(YN,XN),'F') then POP(i,8)='100',end
elseif DIR==8 then YN=YO-1, XN=XO-1
if isequal(GRIDSPC(YN,XN),'F') then POP(i,8)='100',end
end// End of one element of POP list
end// End of search
endfunction
//*************************************************
// newPOSPOS()
//
// Having the OLD position from POP and the intended NEW position
//
// Proceed in random order
//
// If system intends 'eat'-action check for food
//
// If system intends a move one has to check
// whether the new position is free, then move system to new position
// otherwise do nothing
function [POP,GRIDSPC] =newPOSPOS(POP,GRIDSPC,SHOW)
[r,c]=size(POP) //assumes that POP and WIB have the same systems
//in the same order !
if SHOW==1 then disp('BEFORE FLAGS TO ZERO'), end
for i=1:r, POP(i,7)='0', end //Set flag to zero
while (sum(eval(POP(:,7))) < r), //as long as not all systems have been processed
if SHOW==1 then disp('BEFORE SYSRND'), disp(GRIDSPC),end
//Select randomly a system
[SYSRND]=ceil((r)*rand())
if SHOW==1 then printf('SYSRND = %d ',SYSRND), end
if (POP(SYSRND,7) == '0') then POP(SYSRND,7)='1', //if not yet processed
if SHOW==1 then disp('BEFORE YN,XN'), end
YN=eval(POP(SYSRND,5)),
XN=eval(POP(SYSRND,6)),
YO=eval(POP(SYSRND,3)),
XO=eval(POP(SYSRND,4)),
//Check 'eat'-action
//Check food
//If food intake has been done set back to '0' in WIB
if eval(WIB(SYSRND,3)) == 9 then [POP]=updateENERGY(SYSRND,YO,XO,POP,GRIDSPC,SHOW), end
if SHOW==1 then disp('BEFORE isequal'), printf('YN = %d ',YN), printf('XN= %d ',XN),end
if isequal(GRIDSPC(YN, XN),'.') then
if SHOW==1 then disp('BEFORE DELETE SYSTEM'), printf('YO = %d ',YO), printf('XO= %d ',XO),end
GRIDSPC(YO,XO)='.' //Delete old system
if SHOW==1 then disp(GRIDSPC), end
if SHOW==1 then disp('BEFORE UPDATE OLD YO XO'), end
POP(SYSRND,3)=sci2exp(YN) //Update Y-X-coordinates
POP(SYSRND,4)=sci2exp(XN)
if SHOW==1 then disp('BEFORE NEW PLACEMENT OF SYSTEM'), end
GRIDSPC(YN,XN)=POP(SYSRND,2),//Place system at new position
end
end
end
endfunction