Beispiel #1
0
//--------- Begin of function Unit::handle_blocked_attack_wall ---------//
// handle the case that the way of this unit to the target wall is blocked by
// another unit
//
// <Unit*> unitPtr	- the blocking unit
//
void Unit::handle_blocked_attack_wall(Unit *unitPtr)
{
	if(action_x_loc==unitPtr->action_x_loc && action_y_loc==unitPtr->action_y_loc && action_mode==unitPtr->action_mode)
	{
		//------------- both attacks the same wall ------------//
		Location *locPtr = world.get_loc(action_x_loc, action_y_loc);
		if(!locPtr->is_wall())
			stop2(KEEP_DEFENSE_MODE);	// stop since wall is deleted
		else if(space_for_attack(action_x_loc, action_y_loc, UNIT_LAND, 1, 1))
		{
			//------------ found surrounding place to attack the wall -------------//
			if(mobile_type==UNIT_LAND)
				set_move_to_surround(action_x_loc, action_y_loc, 1, 1, BUILDING_TYPE_WALL); // search for a unit only, not for a group
			else
				attack_wall(action_x_loc, action_y_loc);
		}
		else // no surrounding place found, stop now
			stop(KEEP_PRESERVE_ACTION); // no space available, so stop to wait for space to attack the wall
	}
	else
	{
		if(action_x_loc==-1 || action_y_loc==-1)
			stop();
		else
			set_wait();
	}
}
Beispiel #2
0
//--------- Begin of function Unit::handle_blocked_attack_town ---------//
// handle the case that the way of this unit to the target town is blocked by
// another unit
//
// <Unit*> unitPtr	- the blocking unit
//
void Unit::handle_blocked_attack_town(Unit *unitPtr)
{
	if(action_x_loc==unitPtr->action_x_loc && action_y_loc==unitPtr->action_y_loc && action_para==unitPtr->action_para &&
		action_mode==unitPtr->action_mode)
	{
		//---------------- both attacks the same town ----------------------//
		Location *locPtr = world.get_loc(action_x_loc, action_y_loc);
		if(!locPtr->is_town())
			stop2(KEEP_DEFENSE_MODE);	// stop since town is deleted
		else if(space_for_attack(action_x_loc, action_y_loc, UNIT_LAND, STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT))
		{
			//------------ found surrounding place to attack the town -------------//
			Town *townPtr = town_array[action_para];
			{
				if(mobile_type==UNIT_LAND)
					set_move_to_surround(townPtr->loc_x1, townPtr->loc_y1, STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT, BUILDING_TYPE_TOWN_MOVE_TO);
				else
					attack_town(townPtr->loc_x1, townPtr->loc_y1);
			}
		}
		else // no surrounding place found, stop now
			stop(KEEP_PRESERVE_ACTION);
	}
	else
		stop();
}
Beispiel #3
0
//--------- Begin of function Unit::handle_blocked_attack_firm ---------//
// handle the case that the way of this unit to the target firm is blocked by
// another unit
//
// <Unit*> unitPtr	- the blocking unit
//
void Unit::handle_blocked_attack_firm(Unit *unitPtr)
{
	if(action_x_loc==unitPtr->action_x_loc && action_y_loc==unitPtr->action_y_loc && action_para==unitPtr->action_para &&
		action_mode==unitPtr->action_mode)
	{
		//------------- both attacks the same firm ------------//
		Location *locPtr = world.get_loc(action_x_loc, action_y_loc);
		if(!locPtr->is_firm())
			stop2(KEEP_DEFENSE_MODE);	// stop since firm is deleted
		else
		{
			Firm *firmPtr = firm_array[action_para];
			FirmInfo *firmInfo = firm_res[firmPtr->firm_id];

			if(space_for_attack(action_x_loc, action_y_loc, UNIT_LAND, firmInfo->loc_width, firmInfo->loc_height))
			{
				//------------ found surrounding place to attack the firm -------------//
				if(mobile_type==UNIT_LAND)
					set_move_to_surround(firmPtr->loc_x1, firmPtr->loc_y1, firmInfo->loc_width, firmInfo->loc_height, BUILDING_TYPE_FIRM_MOVE_TO);
				else
					attack_firm(firmPtr->loc_x1, firmPtr->loc_y1);
			}
			else // no surrounding place found, stop now
				stop(KEEP_PRESERVE_ACTION);
		}
	}
	else // let process_idle() handle it
		stop();
}
Beispiel #4
0
//------ Begin of function Unit::handle_blocked_same_target_attack ---------//
// the target of the blocked unit and this unit are same
//
// <Unit*>	unitPtr		-	pointer to blocking uit
// <Unit*>	targetPtr	-	pointer to target unit
//
void Unit::handle_blocked_same_target_attack(Unit* unitPtr, Unit* targetPtr)
{
	//----------------------------------------------------------//
	// this unit is now waiting and the unit pointed by unitPtr
	// is attacking the unit pointed by targetPtr
	//----------------------------------------------------------//
	err_when(cur_x%ZOOM_LOC_WIDTH || cur_y%ZOOM_LOC_HEIGHT);
	err_when(cur_x!=next_x || cur_y!=next_y);
	err_when(cur_x==go_x && cur_y==go_y);

	if(space_for_attack(action_x_loc, action_y_loc, targetPtr->mobile_type, targetPtr->sprite_info->loc_width, targetPtr->sprite_info->loc_height))
	{
		err_when(action_x_loc!=action_x_loc2 || action_y_loc!=action_y_loc2);
		search_or_stop(move_to_x_loc, move_to_y_loc, 1, SEARCH_MODE_TO_ATTACK, targetPtr->sprite_recno);
		//search(move_to_x_loc, move_to_y_loc, 1, SEARCH_MODE_TO_ATTACK, targetPtr->sprite_recno);
	}
	else if(in_any_defense_mode())
	{
		err_when(action_mode2!=ACTION_AUTO_DEFENSE_ATTACK_TARGET && action_mode2!=ACTION_DEFEND_TOWN_ATTACK_TARGET &&
					action_mode2!=ACTION_MONSTER_DEFEND_ATTACK_TARGET);

		general_defend_mode_detect_target();
	}
	else if(m.points_distance(next_x_loc(), next_y_loc(), action_x_loc, action_y_loc)<ATTACK_DETECT_DISTANCE)
	{
		//------------------------------------------------------------------------//
		// if the target is within the detect range, stop the unit's action to detect
		// another target if any exist. In case, there is no other target, the unit
		// will still attack the original target since it is the only target in the
		// detect range
		//------------------------------------------------------------------------//
		stop2(KEEP_DEFENSE_MODE);
	}
	else
		set_wait(); // set wait to stop the movement
}
Beispiel #5
0
//--------- Begin of function Unit::general_defend_mode_process_attack_target ---------//
// process unit defense action. If target is dead, action_mode changes to detect_mode
//
// return 1 if action mode changes to detect mode
// return 0 otherwise
//
int Unit::general_defend_mode_process_attack_target()
{
	Location *locPtr;
	Unit *unitPtr;
	Town *townPtr;
	Firm *firmPtr;
	SpriteInfo	*spriteInfo;
	FirmInfo		*firmInfo;
	int clearToDetect = 0;

	//------------------------------------------------------------------------------//
	// if the unit's action mode is in defensive attack action, process the corresponding
	// checking.
	//------------------------------------------------------------------------------//
	switch(action_mode)
	{
		case ACTION_ATTACK_UNIT:
				if(unit_array.is_deleted(action_para2))
					clearToDetect++;
				else
				{
					err_when(unit_array.is_deleted(action_para2));
					unitPtr = unit_array[action_para2];
					
					//if(unitPtr->cur_action==SPRITE_IDLE)
					//	clearToDetect++;

					if(!nation_can_attack(unitPtr->nation_recno)) // cannot attack this nation
						clearToDetect++;
				}
				break;

		case ACTION_ATTACK_FIRM:
				if(firm_array.is_deleted(action_para2))
					clearToDetect++;
				else
				{
					err_when(firm_array.is_deleted(action_para2));
					firmPtr = firm_array[action_para2];
					
					if(!nation_can_attack(firmPtr->nation_recno)) // cannot attack this nation
						clearToDetect++;
				}
				break;

		case ACTION_ATTACK_TOWN:
				if(town_array.is_deleted(action_para2))
					clearToDetect++;
				else
				{
					err_when(town_array.is_deleted(action_para2));
					townPtr = town_array[action_para2];
					
					if(!nation_can_attack(townPtr->nation_recno)) // cannot attack this nation
						clearToDetect++;
				}
				break;

		case ACTION_ATTACK_WALL:
				locPtr = world.get_loc(action_x_loc2, action_y_loc2);
				
				if(!locPtr->is_wall() || !nation_can_attack(locPtr->power_nation_recno))
					clearToDetect++;
				break;

		default: clearToDetect++;
					break;
	}

	//------------------------------------------------------------------------------//
	// suitation changed to defensive detecting mode
	//------------------------------------------------------------------------------//
	if(clearToDetect)
	{
		//----------------------------------------------------------//
		// target is dead, change to detect state for another target
		//----------------------------------------------------------//
		reset_action_para();
		return 1;
	}
	else if(waiting_term<ATTACK_WAITING_TERM)
		waiting_term++;
	else
	{
		//------------------------------------------------------------------------------//
		// process the corresponding attacking procedure.
		//------------------------------------------------------------------------------//
		waiting_term = 0;
		switch(action_mode)
		{
			case ACTION_ATTACK_UNIT:
				err_when(unit_array.is_deleted(action_para2) || !unitPtr);
				spriteInfo = unitPtr->sprite_info;
				
				//-----------------------------------------------------------------//
				// attack the target if able to reach the target surrounding, otherwise
				// continue to wait
				//-----------------------------------------------------------------//
				action_x_loc2 = unitPtr->next_x_loc(); // update target location
				action_y_loc2 = unitPtr->next_y_loc();
				if(space_for_attack(action_x_loc2, action_y_loc2, unitPtr->mobile_type, spriteInfo->loc_width, spriteInfo->loc_height))
					attack_unit(unitPtr->sprite_recno);
				break;

			case ACTION_ATTACK_FIRM:
				err_when(firm_array.is_deleted(action_para2) || !firmPtr);
				firmInfo = firm_res[firmPtr->firm_id];
				
				//-----------------------------------------------------------------//
				// attack the target if able to reach the target surrounding, otherwise
				// continue to wait
				//-----------------------------------------------------------------//
				attack_firm(action_x_loc2, action_y_loc2);

				if(!is_in_surrounding(move_to_x_loc, move_to_y_loc, sprite_info->loc_width, action_x_loc2, action_y_loc2,
					firmInfo->loc_width, firmInfo->loc_height))
					waiting_term = 0;
				break;

			case ACTION_ATTACK_TOWN:
				err_when(town_array.is_deleted(action_para2) || !townPtr);

				//-----------------------------------------------------------------//
				// attack the target if able to reach the target surrounding, otherwise
				// continue to wait
				//-----------------------------------------------------------------//
				attack_town(action_x_loc2, action_y_loc2);

				if(!is_in_surrounding(move_to_x_loc, move_to_y_loc, sprite_info->loc_width, action_x_loc2, action_y_loc2,
					STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT))
					waiting_term = 0;
				break;

			case ACTION_ATTACK_WALL:
				err_when(action_para || action_para2);

				attack_wall(action_x_loc2, action_y_loc2);
				if(!is_in_surrounding(move_to_x_loc, move_to_y_loc, sprite_info->loc_width, action_x_loc2, action_y_loc2, 1, 1))
					waiting_term = 0;
				break;

			default:
				err_here();
				break;
		}
	}

	return 0;
}