Example #1
0
File: ofirm.cpp Project: 112212/7k2
//---------- Begin of function Firm::process_animation --------//
//
void Firm::process_animation()
{
   //-------- process animation ----------//

   FirmBuild* firmBuild  = firm_res.get_build(firm_build_id);
	FirmBitmap* firmBitmap;
   int frameCount = firmBuild->frame_count;
	int firstBitmap;
	int bitmapCount;

	if( hit_points < (max_hit_points() * 0.1))
	{
		if (misc.random(100) > 65)
		{
			int effectId = sprite_res.search_sprite( "FIRMDIE" );
			if( effectId )
			{
				Effect::create(effectId, 
					(loc_x1 + misc.random(20)%(loc_x2 -loc_x1 +1)) *LOCATE_WIDTH,
					(loc_y1 + misc.random(20)%(loc_y2 -loc_y1 +1)) *LOCATE_HEIGHT,
					SPRITE_DIE, 1, 2, 0); 
			}
		}
	}
	else
	if( hit_points < (max_hit_points() * 0.3))
	{
		if (misc.random(100) > 80)
		{	
			int effectId = sprite_res.search_sprite( "FIRMDIE" );
			if( effectId )
			{
				Effect::create(effectId, 
					(loc_x1 + misc.random(20)%(loc_x2 -loc_x1 +1)) *LOCATE_WIDTH,
					(loc_y1 + misc.random(20)%(loc_y2 -loc_y1 +1)) *LOCATE_HEIGHT,
					SPRITE_DIE, 1, 2, 0);
			}
		}
	}

   if( frameCount==1 )     // no animation for this firm
      return;

   //---------- next frame -----------//
	int aniPartCount = firmBuild->ani_part_count;
	for (int i = 0; i < aniPartCount ; i++)
	{
		firm_remain_frame_delay[i] --;
		if (firm_remain_frame_delay[i] < 0)
		{
			// animate_full_size only supports animation of looping
			// it does not support random animation and in fact
			// it is now only used for no animation buildings
			// but it support different frame different delay

			if( firmBuild->animate_full_size )
			{
				if( ++firm_cur_frame[i] > frameCount )
					firm_cur_frame[i] = 1;
				firm_remain_frame_delay[i] = (char) firmBuild->frame_delay(firm_cur_frame[i]);
			}
			else
			{
			// not animate_full_size supports animation of looping and
			// random animation and partial animation
			// however different frames of each parts must has same delay
		
				for ( int k=1; k <frameCount; k++ )
				{
					firstBitmap = firmBuild->first_bitmap(k+1);
					bitmapCount = firmBuild->bitmap_count(k+1);

					int j;
					for ( j=0; j <bitmapCount; j++ )
					{
						firmBitmap = firm_res.get_bitmap(firstBitmap + j);
						if( firmBitmap )
						{
							if (firmBitmap->ani_part == (i + 1))
							{
								// unlike the animate_full_size animation, it search starting
								// from the 2nd frame for bitmaps (since now 1 frame may has more 
								// than 1 bitmap) with same animation part no. of the currently
								// searching part no., i. This has no relationship with the 
								// current_frame. Then restore this animation part's delay and 
								// set its current_frame either randomly or sequentially
			
								firm_remain_frame_delay[i] = firmBitmap->delay;
								if (firmBitmap->random_flag)
									firm_cur_frame[i] = misc.random(100) % frameCount +1;
								else
								if( ++firm_cur_frame[i] > frameCount )
									firm_cur_frame[i] = 1;
								
								if((firm_cur_frame[i] == 1) && !( firmBuild->use_first_frame ))
									firm_cur_frame[i] = 2;
								break;
							}
						}
					}
					if (j < bitmapCount)
						break;
				}
			}
		}
	}
}
Example #2
0
File: ofirm.cpp Project: 112212/7k2
//--------- Begin of function Firm::init --------//
//
// It will initialize vars, and set the world matrix.
// Before calling init(), firm_recno should be set
//
// Note : it will set world matrix regardless the existing location content,
//        so you must ensure that the location is clean by calling
//        world.zoom_matrix->add_firm_test()
//
// <int> xLoc, yLoc  = the location of firm in the world map
// <int> nationRecno = the recno of nation which build this firm
// <int> firmId      = id(type) of the firm
// [char*] buildCode = the build code of the firm, no need to give if the firm just have one build type
// [short] builderRecno = recno of the builder unit
//
void Firm::init(int xLoc, int yLoc, int nationRecno, int firmId, char* buildCode, short builderRecno)
{
	FirmInfo* firmInfo = firm_res[firmId];

	firm_id = firmId;

	if( buildCode )
		firm_build_id = firmInfo->get_build_id(buildCode);
	
	if( !buildCode || !firm_build_id )					// some buildings like human offensive structures do not have different buildings for different nationality, so firm_build_id will be 0 
		firm_build_id = firmInfo->first_build_id;

	//----------- set vars -------------//

	nation_recno   = nationRecno;
	setup_date     = info.game_date;

	//----- set the firm's absolute positions on the map -----//

	FirmBuild* firmBuild = firm_res.get_build(firm_build_id);
	FirmBitmap* firmBitmap;
	
	race_id = firmInfo->get_race_id(buildCode);

	if( !race_id )			// some firms like human offensive structures do not have race_id.
		race_id = nation_array[nation_recno]->race_id;

	err_when( is_human() == (firmId >= FIRM_LAIR && firmId <= FIRM_MAGIC) );		// error when a human attempts to build a monster firm or a mosnter attempts to build a human firm

	loc_x1 = xLoc;
	loc_y1 = yLoc;
	loc_x2 = loc_x1 + firmBuild->loc_width  - 1;
	loc_y2 = loc_y1 + firmBuild->loc_height - 1;

	center_x = (loc_x1 + loc_x2) / 2;
	center_y = (loc_y1 + loc_y2) / 2;

	region_id = world.get_region_id( center_x, center_y );

	// get the height of the top left corner
	// altitude = world.get_corner(xLoc, yLoc)->get_altitude();
	altitude = world.get_corner(loc_x2+1, loc_y2+1)->get_altitude();

	abs_x1 = world.zoom_matrix->calc_abs_x(xLoc * LOCATE_WIDTH,
		yLoc * LOCATE_HEIGHT, altitude) + firmBuild->min_offset_x;
	abs_y1 = world.zoom_matrix->calc_abs_y(xLoc * LOCATE_WIDTH,
		yLoc * LOCATE_HEIGHT, altitude) + firmBuild->min_offset_y;
   abs_x2 = abs_x1 + firmBuild->max_bitmap_width  - 1;
   abs_y2 = abs_y1 + firmBuild->max_bitmap_height - 1;

   //--------- set animation frame vars ---------//

	int bitmapCount;
	int firstBitmap;
	int frameCount = firmBuild->frame_count;
	int aniPartCount = firmBuild->ani_part_count;
	// ##### begin Gilbert 30/10 ######//
	err_when( aniPartCount < 0 );
	err_when( aniPartCount > MAX_FRAME_COUNTERS );
	// firm_cur_frame = (char*) mem_add( sizeof(char) *aniPartCount);
	// firm_remain_frame_delay = (char*) mem_add( sizeof(char) *aniPartCount);
	// ##### end Gilbert 30/10 ######//
	
	// even not animation_full_size will have aniPartCount = 1 (set in firmres.cpp)
	for (int i = 0; i < aniPartCount ; i++)
	{
		firm_cur_frame[i] = 1;
		
		if( firmBuild->animate_full_size )
			// animate_full_size only supports animation of looping
			// it does not support random animation and in fact
			// it is now only used for no animation buildings
			firm_remain_frame_delay[i] = (char) firmBuild->frame_delay(1);
		else
		{
			// not animate_full_size supports animation of looping and
			// random animation and partial animation
			// unlike the animate_full_size animation, it search starting
			// from the 2nd frame for bitmaps (since now 1 frame may has more 
			// than 1 bitmap) with same animation part no. of the currently
			// searching part no., i. Then assign this animation part's delay and 
			// set its current_frame to the first found frame
			for ( int k=1; k <frameCount; k++ )
			{
				firstBitmap = firmBuild->first_bitmap(k+1);
				bitmapCount = firmBuild->bitmap_count(k+1);

				int j;
				for ( j=0; j <bitmapCount; j++ )
				{
					firmBitmap = firm_res.get_bitmap(firstBitmap + j);
					if( firmBitmap )
					{
						if (firmBitmap->ani_part == (i + 1))
						{
							firm_remain_frame_delay[i] = firmBitmap->delay;
							firm_cur_frame[i] = k + 1;
							break;
						}
					}
				}
				if (j < bitmapCount)
					break;
			}
		}
	}
		 
   //--------- initialize gaming vars ----------//

   hit_points     = (float) 0;
   place_max_hit_points = firmInfo->max_hit_points;

	// #### begin Gilbert 6/11 ######//
	defense_attribute = firmBuild->defense_attribute;
	// #### end Gilbert 6/11 ######//

	//------ set construction and builder -------//

	under_construction = firmInfo->buildable;       // whether the firm is under construction, if the firm is not buildable it is completed in the first place

	if( !under_construction )			// if this firm doesn't been to be constructed, set its hit points to the maximum
		hit_points = place_max_hit_points;

	if( builderRecno )
		set_builder(builderRecno);
	else
		builder_recno = 0;

	//------ update firm counter -------//

   firmInfo->total_firm_count++;
	
	if( nation_recno )
		firmInfo->inc_nation_firm_count(nation_recno);

   //-------------------------------------------//

   if( nation_recno > 0 )
   {
		Nation* nationPtr = nation_array[nation_recno];

      is_ai = nationPtr->is_ai();
		ai_processed = 1;

      //--------- increase firm counter -----------//

      nationPtr->nation_firm_count++;

      //-------- update last build date ------------//

      nationPtr->last_build_firm_date = info.game_date;
   }
   else
   {
      is_ai = 0;
      ai_processed = 0;
   }

   ai_status = FIRM_WITHOUT_ACTION;
   ai_link_checked = 1;       // check the connected firms if ai_link_checked = 0;

   //--------------------------------------------//

	active_link_town_recno = 0;

	setup_link();

	set_world_matrix();

	init_name();

	//----------- init AI -----------//

	if( nation_recno )
		nation_array[nation_recno]->add_firm_info(firm_id, firm_recno);

	//-------- init derived ---------//

	init_derived();         // init_derived() before set_world_matrix() so that init_derived has access to the original land info.

	
	//-------- dynamic loading of firm bitmaps ---------//
	firmBuild->load_bitmap_res();

}