# File vw3.py # # Author: G.Doeben-Henisch # First date: July 21, 2019 # Last date: July 25, 2019 ''' ACTOR STORY 1. After completing a 2D virtual world with obstacles 'O', food objects 'F' and actors 'A', the general world cycling loop will be started. In this loop all possible changes will be managed. The original user has no active role during this cycling loop; he is now in the role of the administrator starting or stopping the cycling loop or interrupt to get some data. 2. The cycling loop can be viewed as structured in different phases. 1. In the first phase all general services will be managed, this is the management of the energy level of all objects: (i) food objects will be updated to grow and (ii) actor objects will be updated by reducing the energy by the standard depending from time. If energy level is below 0 then the avatar of the actor will be removed from the object list as well as from the grid. 2. In the second phase the world function wf will randomly select each action message one after the other and process these. There are the following cases: 1. In case of an eat-action it will be checked whether there is food at the position. If not nothing will happen,if yes then the energy level of the eating actor will be increased inside the actor and the amount of the food will be decreased. There is a correspondence between amount of food and consumed energy. 2. In case of a move-action it will be checked whether there is a path in the selected direction to move one cell. If not, then nothing will happen, if Yes, then the move will happen and the energy level will be decreased by a certain amount due to moving. 3. After finishing all services the world clock WCLCK will be increased by one (Remark: in later versions it can happen that there exist actions whose duration needs more than 1 cycle to become finished). ''' ############################ # IMPORTS import random as rnd import gridHelper as grh ############################ # SUPPORTING FUNCTION # # Now in a dedicated module gridHelper.py in folder 'code' # Complete path: C:\Users\gerd_2\code # This path has to be included for the python path search mechanism # # Show actual path values # # >>> import sys #>>> sys.path # ... # Extend the python path by appending the modul path: # # In my case the path is as follows: # #>>> sys.path.append('C:\\Users\\gerd_2\\code') ########################## # Main Programm ############### # MAIN # # The loop will work as long as the value of the variable 'loop' is different to 'N' # # SOME GLOBAL DICTIONARY # # These values are actually only for development. Later they should reflect more properties of a simulation environment objL={'F':[0,1000, 20], 'A':[0,1000,200,100]} ''' A food object 'F' has an initial value of '1000' and can grow if below 1000 by 20 every cycle An actor object 'A' has an ID, an initial energy level of 1000, loses every cycle 200 energy units if not moving and 100 energy units if moving, and can gain 500 energy units by eating ''' # MAIN OUTER LOOP loop='Y' while loop!='N': ################### # The user will be asked for some parameters needed for the editing of a virtual 2-dimensional world # Number of columns (m) and rows (n); by default equal. m=int(input('Number of columns (= equal to rows!) of 2D-grid ?')) n=m mx=grh.nmlist(n) # Generate a first empty matrix n*m # Percentage % of obstacles in the 2D-world?\n) obstacles=int(input('Percentage (as integer) of obstacles in the 2D-grid?')) no=grh.numberObj(obstacles,n) mx=grh.placeObj(no,n,mx,'O') # Percentage % of energy objects in the 2D-world?\n) energyObj=int(input('Percentage (as integer) of Energy Objects (= Food) in the 2D-grid ?')) no=grh.numberObj(energyObj,n) mx=grh.placeObj(no,n,mx,'F') # Percentage % of actor objects in the 2D-world?\n) actorObj=int(input('Percentage (as integer) of Actor Objects in the 2D-grid ?')) no=grh.numberObj(actorObj,n) if no<1: no=1 mx=grh.placeObj(no,n,mx,'A') # List of actual food objects in the grid: c='F' name='food' olF=grh.makeobjL(c,objL,mx,n) print('\n Objects as ',name,'\n') grh.printMX(olF) # List of actual actor objects in the grid: c='A' name='actor' olA=grh.makeobjL(c,objL,mx,n) print('\n Objects as ',name,'\n') grh.printMX(olA) print('\n END OF PREPARATIONA\n') # ############################## # MAIN WORLD CYCLING LOOP # print('WORLD CYCLE STARTS\n') CYC=int(input('How many CYCLES do you want?')) WCLCK=0 for t in range(CYC): WCLCK+=1 # Main reference point for time grh.printMX(mx) # UPDATING all FOOD objects # Take the list of all food objects olF # Check whether the energy level (objL index 1) is below Maximum. # If YES add some amount (objL Index 2). # If not do nothing olF=grh.foodUpdate(olF,objL) print('Updated food objects:\n') grh.printMX(olF) # UPDATING all ACTOR objects # Take the list of all actor objects olA # REDUCE the energy level according to the standard # CHECK whether the energy level (objL index 1) is below 0. # If YES add REMOVE AVATAR from grid. if len(olA)>1: olA=grh.actorUpdate(olA,objL,mx) print('Updated actor objects:\n') grh.printMX(olA) else: print('!!! MAIN: no more actors in the grid !!!') grh.printMX(mx) print('CYCle : ',CYC,' -------------------------') # Clarify how to continue (MAIN LOOP!) loop=input("\n MAIN LOOP: STOP = 'N', CONTINUE != 'N' \n")