//*************************************************
// WORLD FUNCTION wf2()
//
// AUTHOR: G.Doeben-Henisch
// First: 25.Nov.2013
// Last: 11.Dez.2013
//*************************************************
//
// IDEA
//
// The world function as part of the WORLD computes all
// necessary changes in a world within one WORLD CYCLE.
//
// wf: W ---> W
//
// The wf() manages possible changes according to the actual GRID, the content
// of the WIB and according to some world laws.
//
// wf1 = newPOS o newACT o newSOUND
//
// The change of the images happens associated with the newMOVE or nmove operations
//
// - 'F' can 'disappear' by changing into '.'
// - '.' can change to 'F' by 'growing'
// - '*i' can change the 'positon' by a 'move'
//
// LAWS
//
// - Only one object at a position at one time. This means, if a system will move to a position (X,Y)
// and at this position is already an object 'O', 'F' or '*j', then this move is not possible.
// It will result in a 'collision message'.
//
// - If more than one system plans to move at a point of time t, then the order of moves will be 'random'
//
//**********************************************************
// wf1-HELPER FUNCTIONS
//
// insertPOP() := at start time this will place the systems from POP into the GRIDSPC once
//
// newPOS() := takes the move-command from WIB and computes a new position (as well as
// the non-move actions in random test cases)
//
// newPOSPOS() := checks randomly whether the new position is possible; if yes then this will be realized
// (as well as the non-move actions in random test cases)
//
// newSOUND() := realizes a SAY and a coded SOUND.
//
//***************************************************************
// 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
//*************************************************
// newPOS()
//
// Having the OLD position from POP and the intended movement as DIR from WIB
// 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 function 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==2 then printf('updateEnergy: 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
if SHOW==2 then NEWENERGY=eval(POP(i,8)),
printf('updateEnergy: NEWENERGY = %d\n',NEWENERGY), end
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 isequal(WIB(SYSRND,3),'1001') 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
//Check movement
//If free '.' go on
//Otherwise no move
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