?? demo12_7.cpp
字號:
} // end else
} break;
case ANT_SEARCH_FOOD_S2_WANDER:
{
// wander and look for food, when done wandering go back and scan
// burn food
ants[index].varsI[ANT_INDEX_HUNGER_LEVEL]+=1;
// move the ant
ants[index].x+=ant_movements_x[ants[index].varsI[ANT_INDEX_DIRECTION]];
ants[index].y+=ant_movements_y[ants[index].varsI[ANT_INDEX_DIRECTION]];
// test if ant is done with direction and needs a new one
if (--ants[index].counter_2 < 0)
{
// set direction
ants[index].varsI[ANT_INDEX_DIRECTION] = RAND_RANGE(ANT_ANIM_UP, ANT_ANIM_LEFT);
// time in this new direction
ants[index].counter_2 = RAND_RANGE(10, 100);
// start animation
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
} // end if new direction
// update memory with presence of food
int ant_cell_x = ants[index].x / 30;
int ant_cell_y = ants[index].y / 30;
// this updates the i,jth memory cell in ant with info about food
float food_near_ant = Food_Near_Ant(ant_cell_x, ant_cell_y);
ants_mem[index].cell[ant_cell_x][ant_cell_y] =
ANT_MEMORY_RESIDUAL_RATE*ants_mem[index].cell[ant_cell_x][ant_cell_y] +
(1-ANT_MEMORY_RESIDUAL_RATE)*food_near_ant;
// test if we just bumped into some food
if (food_near_ant > 0)
{
// find highest source of food
int food_x = -1;
int food_y = -1;
// find the highest food source in cell
int food_id = Max_Food_In_Cell(ant_cell_x, ant_cell_y, &food_x, &food_y);
// pre-empt into vector 2 food
ants[index].varsI[ANT_INDEX_AI_SUBSTATE] =
ANT_SEARCH_FOOD_S4_VECTOR_2FOOD;
// send to exact position
ants[index].varsI[ANT_INDEX_FOOD_TARGET_X] = food_x;
ants[index].varsI[ANT_INDEX_FOOD_TARGET_Y] = food_y;
// set target id of food
ants[index].varsI[ANT_INDEX_FOOD_TARGET_ID] = food_id;
// set counters to 0
ants[index].counter_1 = ants[index].counter_2 = 0;
} // end if
// test if we are done with this state and need to go back to scan
else
if (--ants[index].counter_1 < 0)
{
// go back to scan state
ants[index].varsI[ANT_INDEX_AI_SUBSTATE] = ANT_SEARCH_FOOD_S1_SCAN;
} // end if
} break;
case ANT_SEARCH_FOOD_S3_VECTOR_2CELL:
{
// this substate vectors the ant to the center of the cell, once
// there the ant "looks" to see if there actually is any food, if so
// vectors to it, else goes back and scans
// burn food
ants[index].varsI[ANT_INDEX_HUNGER_LEVEL]+=2;
// pick direction
int dx = ants[index].varsI[ANT_INDEX_FOOD_TARGET_X] - ants[index].x;
int dy = ants[index].varsI[ANT_INDEX_FOOD_TARGET_Y] - ants[index].y;
// pick maxium delta to move in
if (abs(dx) >= abs(dy))
{
// x dominate
if (dx > 0)
{
// move right
if (ants[index].varsI[ANT_INDEX_DIRECTION] != ANT_ANIM_RIGHT)
{
// start animation right
ants[index].varsI[ANT_INDEX_DIRECTION] = ANT_ANIM_RIGHT;
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
} // end if
ants[index].x+=ant_movements_x[ants[index].varsI[ANT_INDEX_DIRECTION]];
} // end if
else
if (dx < 0)
{
// move left
if (ants[index].varsI[ANT_INDEX_DIRECTION] != ANT_ANIM_LEFT)
{
// start animation left
ants[index].varsI[ANT_INDEX_DIRECTION] = ANT_ANIM_LEFT;
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
} // end if
ants[index].x+=ant_movements_x[ants[index].varsI[ANT_INDEX_DIRECTION]];
} // end else
} // end if x
else
{
// y dominate
if (dy > 0)
{
// move down
if (ants[index].varsI[ANT_INDEX_DIRECTION] != ANT_ANIM_DOWN)
{
// start animation down
ants[index].varsI[ANT_INDEX_DIRECTION] = ANT_ANIM_DOWN;
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
} // end if
ants[index].y+=ant_movements_y[ants[index].varsI[ANT_INDEX_DIRECTION]];
} // end if
else
if (dy < 0)
{
// move up
if (ants[index].varsI[ANT_INDEX_DIRECTION] != ANT_ANIM_UP)
{
// start animation down
ants[index].varsI[ANT_INDEX_DIRECTION] = ANT_ANIM_UP;
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
} // end if
ants[index].y+=ant_movements_y[ants[index].varsI[ANT_INDEX_DIRECTION]];
} // end else
} // end else
// update memory image ????
// update memory with presence of food
int ant_cell_x = ants[index].x / 30;
int ant_cell_y = ants[index].y / 30;
// this updates the i,jth memory cell in ant with info about food
float food_near_ant = Food_Near_Ant(ant_cell_x, ant_cell_y);
ants_mem[index].cell[ant_cell_x][ant_cell_y] =
ANT_MEMORY_RESIDUAL_RATE*ants_mem[index].cell[ant_cell_x][ant_cell_y] +
(1-ANT_MEMORY_RESIDUAL_RATE)*food_near_ant;
// now test if target reached
if (abs(ants[index].x - ants[index].varsI[ANT_INDEX_FOOD_TARGET_X]) < 4 &&
abs(ants[index].y - ants[index].varsI[ANT_INDEX_FOOD_TARGET_Y]) < 4)
{
// center of cell reached, now find the biggest piece of food and
// vector to it, if none exist then go back to scan
// compute cell position
int ant_cell_x = ants[index].x / 30;
int ant_cell_y = ants[index].y / 30;
// this updates the i,jth memory cell in ant with info about food
float food_near_ant = Food_Near_Ant(ant_cell_x, ant_cell_y);
// test if we just bumped into some food
if (food_near_ant > 0)
{
// find highest source of food
int food_x = -1;
int food_y = -1;
// find the highest food source in cell
int food_id = Max_Food_In_Cell(ant_cell_x, ant_cell_y, &food_x, &food_y);
// pre-empt into vector 2 food
ants[index].varsI[ANT_INDEX_AI_SUBSTATE] =
ANT_SEARCH_FOOD_S4_VECTOR_2FOOD;
// send to exact position
ants[index].varsI[ANT_INDEX_FOOD_TARGET_X] = food_x;
ants[index].varsI[ANT_INDEX_FOOD_TARGET_Y] = food_y;
// set target id of food
ants[index].varsI[ANT_INDEX_FOOD_TARGET_ID] = food_id;
// set counters to 0
ants[index].counter_1 = ants[index].counter_2 = 0;
} // end if
else
{
// set mode to scan
ants[index].varsI[ANT_INDEX_AI_SUBSTATE] = ANT_SEARCH_FOOD_S1_SCAN;
} // end else
} // end if
} break;
case ANT_SEARCH_FOOD_S4_VECTOR_2FOOD:
{
// this substate vectors the ant to the exact x,y of the food
// once there, tests to see if there is food there, if so eats it,
// else go to scan
// burn food
ants[index].varsI[ANT_INDEX_HUNGER_LEVEL]+=2;
// pick direction
int dx = ants[index].varsI[ANT_INDEX_FOOD_TARGET_X] - ants[index].x;
int dy = ants[index].varsI[ANT_INDEX_FOOD_TARGET_Y] - ants[index].y;
// pick maxium delta to move in
if (abs(dx) >= abs(dy))
{
// x dominate
if (dx > 0)
{
// move right
if (ants[index].varsI[ANT_INDEX_DIRECTION] != ANT_ANIM_RIGHT)
{
// start animation right
ants[index].varsI[ANT_INDEX_DIRECTION] = ANT_ANIM_RIGHT;
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
} // end if
ants[index].x+=ant_movements_x[ants[index].varsI[ANT_INDEX_DIRECTION]];
} // end if
else
if (dx < 0)
{
// move left
if (ants[index].varsI[ANT_INDEX_DIRECTION] != ANT_ANIM_LEFT)
{
// start animation left
ants[index].varsI[ANT_INDEX_DIRECTION] = ANT_ANIM_LEFT;
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
} // end if
ants[index].x+=ant_movements_x[ants[index].varsI[ANT_INDEX_DIRECTION]];
} // end else
} // end if x
else
{
// y dominate
if (dy > 0)
{
// move down
if (ants[index].varsI[ANT_INDEX_DIRECTION] != ANT_ANIM_DOWN)
{
// start animation down
ants[index].varsI[ANT_INDEX_DIRECTION] = ANT_ANIM_DOWN;
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
} // end if
ants[index].y+=ant_movements_y[ants[index].varsI[ANT_INDEX_DIRECTION]];
} // end if
else
if (dy < 0)
{
// move up
if (ants[index].varsI[ANT_INDEX_DIRECTION] != ANT_ANIM_UP)
{
// start animation down
ants[index].varsI[ANT_INDEX_DIRECTION] = ANT_ANIM_UP;
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
} // end if
ants[index].y+=ant_movements_y[ants[index].varsI[ANT_INDEX_DIRECTION]];
} // end else
} // end else
// update memory image ????
// update memory with presence of food
int ant_cell_x = ants[index].x / 30;
int ant_cell_y = ants[index].y / 30;
// this updates the i,jth memory cell in ant with info about food
float food_near_ant = Food_Near_Ant(ant_cell_x, ant_cell_y);
ants_mem[index].cell[ant_cell_x][ant_cell_y] =
ANT_MEMORY_RESIDUAL_RATE*ants_mem[index].cell[ant_cell_x][ant_cell_y] +
(1-ANT_MEMORY_RESIDUAL_RATE)*food_near_ant;
// now test if target reached
if (abs(ants[index].x - ants[index].varsI[ANT_INDEX_FOOD_TARGET_X]) < 4 &&
abs(ants[index].y - ants[index].varsI[ANT_INDEX_FOOD_TARGET_Y]) < 4)
{
// food reached, now find the biggest piece of food and
// vector to it, if none exist then go back to scan
// is there any food left?
if (ants[index].varsI[ANT_INDEX_FOOD_TARGET_ID] > 0)
{
// thank god!
Set_New_State(ANT_EATING, index, ants[index].varsI[ANT_INDEX_FOOD_TARGET_ID],0);
} // end if
else
{
// go back to scan
ants[index].varsI[ANT_INDEX_AI_SUBSTATE] = ANT_SEARCH_FOOD_S1_SCAN;
} // end if
} // end if
} break;
case ANT_SEARCH_FOOD_S5: break;
case ANT_SEARCH_FOOD_S6: break;
case ANT_SEARCH_FOOD_S7: break;
default: break;
} // end switch
} break;
case ANT_COMMUNICATING: // talking to another ant
{
// the ants sit and talk for a little bit, each ant picks
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -