Example #1
0
// add before delete the firm
void FirmDie::init(Firm *firmPtr, short dieFlag, int random)
{
	int x, y;
	firm_id = firmPtr->firm_id;
	firm_build_id = firmPtr->firm_build_id;
	nation_recno = firmPtr->nation_recno;
	loc_x1 = firmPtr->loc_x1;
	loc_y1 = firmPtr->loc_y1;
	loc_x2 = firmPtr->loc_x2;
	loc_y2 = firmPtr->loc_y2;

	parent_loc_x1 = firmPtr->loc_x1;
	parent_loc_y1 = firmPtr->loc_y1;
	parent_loc_x2 = firmPtr->loc_x2;
	parent_loc_y2 = firmPtr->loc_y2;

	altitude = firmPtr->altitude;
//	frame = 1;
	frame_delay_count = 0;
	die_flag = dieFlag;
	start_frame = 0;
	end_frame = 0;
	frame_looping = 0;

	if (die_flag == 6)
		se_res.sound(firmPtr->center_x, firmPtr->center_y, 1, 'F', 0, "DIE" );
	else
		se_res.sound(firmPtr->center_x, firmPtr->center_y, 1, 'F', 0, "A1" );
	
	FirmBuild *firmBuild = firm_die_res.get_build(firm_build_id);
	firmBuild->load_bitmap_res();

	int bitmapCount;
	int firstBitmap;
	FirmBitmap* firmBitmap;
	int frameCount = firmBuild->frame_count;
	for ( int k=1; k <= frameCount; k++ )
	{
		firstBitmap = firmBuild->first_bitmap(k);
		bitmapCount = firmBuild->bitmap_count(k);
		for ( int j=0; j <bitmapCount; j++ )
		{
			firmBitmap = firm_die_res.get_bitmap(firstBitmap + j);
			if( firmBitmap )
			{
				if (firmBitmap->ani_part == die_flag)
				{
					if (start_frame == 0)
					{
						start_frame = k;
						end_frame = k;
						frame_delay_count = firmBitmap->delay;
						if (firmBitmap->random_flag == 'R' || (firmBitmap->random_flag >'0' && firmBitmap->random_flag <='9'))
						{
							x = (loc_x2 - loc_x1 + 1)/firmBitmap->loc_width;
							y = (loc_y2 - loc_y1 + 1)/firmBitmap->loc_height;
							random = random % (x * y);
							loc_x1 = loc_x1 + random % x;
							loc_y1 = loc_y1 + random / x;
							loc_x2 = loc_x1 + firmBitmap->loc_width - 1;
							loc_y2 = loc_y1 + firmBitmap->loc_height - 1;
							//		altitude = world.get_corner( loc_x1, loc_y1 )->get_altitude();
							if (firmBitmap->random_flag != 'R')
								frame_looping = firmBitmap->random_flag - '0';
						}
					}
					else
						end_frame = k;
					break;
				}
			}
		}
	}

	err_when(end_frame < start_frame || end_frame == 0);
		
	frame = start_frame;
}
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();

}