?? demo12_7_16b.cpp
字號(hào):
// load the ant bitmaps
Load_Bitmap_File(&bitmap16bit, "ANTIMG24.BMP");
// create master ant
Create_BOB(&ants[0],320,200, 24,24, 9, BOB_ATTR_MULTI_ANIM | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY,0,16);
// load the ants in
for (index=0; index < 9; index++)
Load_Frame_BOB16(&ants[0], &bitmap16bit, index, index, 0, BITMAP_EXTRACT_MODE_CELL);
// set the animations
int ant_anim_up[3] = {0,1,-1};
int ant_anim_right[3] = {2,3,-1};
int ant_anim_down[3] = {4,5,-1};
int ant_anim_left[3] = {6,7,-1};
int ant_anim_dead[2] = {8,-1};
Load_Animation_BOB(&ants[0],0,2, ant_anim_up);
Load_Animation_BOB(&ants[0],1,2, ant_anim_right);
Load_Animation_BOB(&ants[0],2,2, ant_anim_down);
Load_Animation_BOB(&ants[0],3,2, ant_anim_left);
Load_Animation_BOB(&ants[0],4,2, ant_anim_dead);
Set_Anim_Speed_BOB(&ants[0],3);
Set_Animation_BOB(&ants[0], ANT_ANIM_UP);
// clone the ants
for (index=1; index < NUM_ANTS; index++)
Clone_BOB(&ants[0], &ants[index]);
// initialize the ants
Init_Ants();
// create the mnm
Create_BOB(&mnm,0,0, 8,8, 1, BOB_ATTR_SINGLE_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY,0,16);
// load the mnm
Load_Frame_BOB16(&mnm, &bitmap16bit, 0, 1, 141, BITMAP_EXTRACT_MODE_ABS);
// position all the mnms
int num_piles = 3+rand()%3;
int curr_mnm = 0;
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(&bitmap16bit);
// 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);
// hide the mouse
if (!WINDOWED_APP)
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_BOB16(&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++)
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -