00001 00002 #include "common.h" 00003 #include "world.h" 00004 #include "instructions.h" 00005 #include "render.h" 00006 00008 uInt g_seed = 0; 00009 00011 world* g_pWorld = NULL; 00012 00013 00015 void initWorld() 00016 { 00017 srand(g_seed); 00018 00019 // create a new world 00020 g_pWorld = new world; 00021 00022 // set some initial parameters 00023 g_pWorld->setEnergyPerUnitMemory(2); 00024 g_pWorld->setEnergyPerAbsorb(5); 00025 g_pWorld->setMaxEnergyPerTile(100000); 00026 g_pWorld->setMaxCostPerTile(1.0f); 00027 g_pWorld->setMaxEnergyPerEntity(20000); 00028 g_pWorld->setMaxMemoryPerEntity(10000); 00029 00030 // create the instruction set 00031 00032 // memory manipulation 00033 g_pWorld->addInstruction(&instr_null); 00034 g_pWorld->addInstruction(&instr_copy); 00035 g_pWorld->addInstruction(&instr_increment); 00036 g_pWorld->addInstruction(&instr_decrement); 00037 g_pWorld->addInstruction(&instr_add); 00038 g_pWorld->addInstruction(&instr_sub); 00039 00040 // control 00041 g_pWorld->addInstruction(&instr_switch); 00042 00043 // threading 00044 g_pWorld->addInstruction(&instr_spawnThread); 00045 g_pWorld->addInstruction(&instr_killThread); 00046 00047 // movement 00048 g_pWorld->addInstruction(&instr_moveFoward); 00049 g_pWorld->addInstruction(&instr_rotate); 00050 00051 // energy transfer 00052 g_pWorld->addInstruction(&instr_transferEnergy); 00053 g_pWorld->addInstruction(&instr_absorbEnergy); 00054 00055 // memory transfer 00056 g_pWorld->addInstruction(&instr_giveMemory); 00057 g_pWorld->addInstruction(&instr_takeMemory); 00058 00059 // conversion 00060 g_pWorld->addInstruction(&instr_convertEnergyToMemory); 00061 g_pWorld->addInstruction(&instr_convertMemoryToEnergy); 00062 00063 // initialise the world 00064 g_pWorld->init(); 00065 00066 // populate the world 00067 for(int i=0;i<100;++i) 00068 for(int j=0;j<100;++j) 00069 if((float)rand()/RAND_MAX>0.95) 00070 g_pWorld->spawnEntity(1000, ((float)rand()/RAND_MAX)*255*255, position(i,j), g_north); 00071 00072 ++g_seed; 00073 } 00074 00075 00076 void draw() 00077 { 00078 g_pWorld->draw(); 00079 } 00080 00081 00082 void idle() 00083 { 00084 // if the world is dead or doesn't exist 00085 if(!g_pWorld || g_pWorld->isDead()) 00086 { 00087 // delete the world 00088 if(g_pWorld) delete g_pWorld; 00089 00090 // and create a new one 00091 initWorld(); 00092 } 00093 00094 // update the world 00095 g_pWorld->step(); 00096 } 00097 00098 00099 int main(int argc, char* argv[]) 00100 { 00101 // initialise the renderer 00102 initRenderingContext(argc, argv); 00103 00104 // set the callbacks 00105 setRenderCallback(draw); 00106 setMainLoopCallback(idle); 00107 00108 // run the app 00109 beginMainLoop(); 00110 }