?? demo12_7.cpp
字號:
for (int piles=0; piles < num_piles; num_piles++)
{
// plop down some mnms at the pile position
int pile_x = 32 + rand()%400;
int pile_y = rand()%480;
// compute number of mnms for pile
int num_mnms_pile = 5 + rand()%15;
// now find a position for each
for (index = 0; index < num_mnms_pile; index++)
{
// select random position and energy level for mnm
food[curr_mnm].x = pile_x + rand()%20;
food[curr_mnm].y = pile_y + rand()%20;
food[curr_mnm].energy = 600 + rand()%1000;
// increment total number of mnms thus far
if (++curr_mnm >= NUM_MNMS)
break;
} // end for index
if (++curr_mnm >= NUM_MNMS)
break;
} // end for pile
// unload ant imagery
Unload_Bitmap_File(&bitmap8bit);
// initialize directinput
DInput_Init();
// acquire the keyboard only
DInput_Init_Keyboard();
DInput_Init_Mouse();
// initilize DirectSound
DSound_Init();
// load background sounds
niceday_sound_id = DSound_Load_WAV("NICEDAY.WAV");
// start the sounds
DSound_Play(niceday_sound_id, DSBPLAY_LOOPING);
// set clipping rectangle to screen extents so objects dont
// mess up at edges
RECT screen_rect = {0,0,screen_width,screen_height};
lpddclipper = DDraw_Attach_Clipper(lpddsback,1,&screen_rect);
// hide the mouse
ShowCursor(FALSE);
// return success
return(1);
} // end Game_Init
///////////////////////////////////////////////////////////
int Game_Shutdown(void *parms)
{
// this function is where you shutdown your game and
// release all resources that you allocated
int index; // looping var
// shut everything down
// kill all the bobs
for (index = 0; index<NUM_ANTS; index++)
Destroy_BOB(&ants[index]);
// shutdown directdraw last
DDraw_Shutdown();
// now directsound
DSound_Stop_All_Sounds();
DSound_Shutdown();
// shut down directinput
DInput_Shutdown();
// return success
return(1);
} // end Game_Shutdown
/////////////////////////////////////////////////////////////////
#if 0
// defines for ants
#define NUM_ANTS 16
#define ANT_ANIM_UP 0
#define ANT_ANIM_RIGHT 1
#define ANT_ANIM_DOWN 2
#define ANT_ANIM_LEFT 3
// states of ant
#define ANT_WANDERING 0 // moving around randomly
#define ANT_EATING 1 // at a mnm eating it
#define ANT_RESTING 2 // sleeping :)
#define ANT_SEARCH_FOOD 3 // hungry and searching for food
#define ANT_SEARCH_FOOD_S1 31 // substate 1
#define ANT_SEARCH_FOOD_S2 32 // substate 2
#define ANT_COMMUNICATING 4 // talking to another ant
#define ANT_DEAD 5 // this guy is dead, got too hungry
#define ANT_INDEX_HUNGER_LEVEL 0
#define ANT_INDEX_HUNGER_TOLERANCE 1
#define ANT_INDEX_AI_STATE 2
#define ANT_INDEX_AI_SUBSTATE 3
#define ANT_INDEX_DIRECTION 4
#endif
void Init_Ants(void)
{
// this function initializes all the ant positions, states, etc.
int index;
for (index=0; index < NUM_ANTS; index++)
{
// set the position of ant
ants[index].x = rand()%472;
ants[index].y = rand()%screen_height;
// set the hunger level and tolerance of this guy
ants[index].varsI[ANT_INDEX_HUNGER_LEVEL] = 0;
// ant will die if hunger level reaches this
ants[index].varsI[ANT_INDEX_HUNGER_TOLERANCE] = 2000+rand()%2000;
// set the ai state of ant
ants[index].varsI[ANT_INDEX_AI_STATE] = ANT_WANDERING;
// set last ant talked to as self
ants[index].varsI[ANT_INDEX_LAST_TALKED_WITH] = index;
// set how long to wander
ants[index].counter_1 = RAND_RANGE(150, 300);
// set direction
ants[index].varsI[ANT_INDEX_DIRECTION] = RAND_RANGE(ANT_ANIM_UP, ANT_ANIM_LEFT);
// time in that direction
ants[index].counter_2 = RAND_RANGE(10, 100);
// start animation
Set_Animation_BOB(&ants[index], ants[index].varsI[ANT_INDEX_DIRECTION]);
// init ant memory
memset(&ants_mem[index], 0, sizeof(ANT_MEMORY));
} // end for index
} // end Init_Ants
//////////////////////////////////////////////////////////////////
void Draw_Ants(void)
{
// this function draws all the ants
int index;
for (index=0; index < NUM_ANTS; index++)
{
// draw the image
ants[index].x-=8; ants[index].y-=8; // center ant
Draw_BOB(&ants[index], lpddsback);
ants[index].x+=8; ants[index].y+=8; // fix center
// draw a little number above ant
sprintf(buffer,"%d", index);
Draw_Text_GDI(buffer,ants[index].x,ants[index].y-16,RGB(0,255,0),lpddsback);
// animate the ant
if (ants[index].varsI[ANT_INDEX_AI_STATE] == ANT_WANDERING ||
ants[index].varsI[ANT_INDEX_AI_STATE] == ANT_SEARCH_FOOD ||
ants[index].varsI[ANT_INDEX_AI_STATE] == ANT_DEAD)
Animate_BOB(&ants[index]);
} // end for index
} // end Draw_Ants
////////////////////////////////////////////////////////////////////
float Food_Near_Ant(int cell_x, int cell_y)
{
// this functions scan all the food in the universe and tests if any is
// close to ant in this cell, if so the energy level of the food is scaled
// and summed to the "memory" strength of that particular geographical location
// in the ants memory
// this algorithm is totally inefficient, in real life this was be called only
// once and all the cell based positions would be pre-computed since the mnms never
// move, but this is just to show you how you would do it if they could move...
int index; // looping var
float food_sum = 0; // used to tally up food in cell sector
// is this mnm in the current cell?
for (index = 0; index < NUM_MNMS; index++)
{
// is this mnm still there
if (food[index].energy > 0)
{
// compute cell position, this is dumb, but needed if mnms can move, which
// they could if you give the ants the ability to pick them up :)
int mnm_x = food[index].x / 30;
int mnm_y = food[index].y / 30;
// is this within the same cell
if (mnm_x == cell_x && mnm_y == cell_y)
food_sum+=(food[index].energy/5);
} // end if
} // end for index
// now send it back
return(food_sum);
} // end Food_Near_Ant
////////////////////////////////////////////////////////////////////////////////
int Max_Food_In_Cell(int cell_x, int cell_y, int *food_x, int *food_y)
{
// this function finds the exact location of the mnm with the highest energy in a cell
float max_food = 0; // used to tally up food in cell sector
int max_food_id = 0; // used to track winner
// is this mnm in the current cell?
for (int index = 0; index < NUM_MNMS; index++)
{
// is this mnm still there
if (food[index].energy > 0)
{
// compute cell position, this is dumb, but needed if mnms can move, which
// they could if you give the ants the ability to pick them up :)
int mnm_x = food[index].x / 30;
int mnm_y = food[index].y / 30;
// is this within the same cell
if (mnm_x == cell_x && mnm_y == cell_y)
{
// is this higher energy
if (food[index].energy > max_food)
{
// set this as new food
max_food_id = index;
max_food = food[index].energy;
*food_x = food[index].x;
*food_y = food[index].y;
} // end if
} // end if
} // end if
} // end for index
// now send it back
return(max_food_id);
} // end Max_Food_In_Cell
/////////////////////////////////////////////////////////////////////
int Select_State_Rand(int state1, int prob1,
int state2, int prob2,
int state3, int prob3,
int state4, int prob4,
int state5, int prob5,
int state6, int prob6)
{
// this function simply selects one of state1...state6 based on the probability
// of each state, if probi is 0 then the state is not considered
int index = 0, // looping variable
curr_elem = 0, // tracks next entry to place in table
state_prob[100]; // used to hold generated probability look up
// build probability table
for (index = 0; index < prob1; index++)
state_prob[curr_elem++] = state1;
for (index = 0; index < prob2; index++)
state_prob[curr_elem++] = state2;
for (index = 0; index < prob3; index++)
state_prob[curr_elem++] = state3;
for (index = 0; index < prob4; index++)
state_prob[curr_elem++] = state4;
for (index = 0; index < prob5; index++)
state_prob[curr_elem++] = state5;
for (index = 0; index < prob6; index++)
state_prob[curr_elem++] = state6;
// now select a state
return(state_prob[rand()%100]);
} // end Select_State_Rand
///////////////////////////////////////////////////////////////////////
void Set_New_State(int new_state, int index, int var1=0, int var2=0)
{
// this function sets the state of the ant to new_state
// reset all state info
ants[index].varsI[ANT_INDEX_AI_STATE] = 0;
ants[index].varsI[ANT_INDEX_AI_SUBSTATE] = 0;
ants[index].counter_1 = 0;
ants[index].counter_2 = 0;
// now set new state info
switch(new_state)
{
case ANT_WANDERING: // moving around randomly
{
// set the ai state of ant
ants[index].varsI[ANT_INDEX_AI_STATE] = ANT_WANDERING;
// set how long to wander
ants[index].counter_1 = RAND_RANGE(150, 300);
// set direction
ants[index].varsI[ANT_INDEX_DIRECTION] = RAND_RANGE(ANT_ANIM_UP, ANT_ANIM_LEFT);
// time in that direction
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -