File: OUNITIND.cpp

package info (click to toggle)
7kaa 2.15.7%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,980 kB
  • sloc: cpp: 137,722; ansic: 3,599; asm: 3,523; perl: 1,665; makefile: 1,185; sh: 185; pascal: 27
file content (142 lines) | stat: -rw-r--r-- 3,908 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Seven Kingdoms: Ancient Adversaries
 *
 * Copyright 1997,1998 Enlight Software Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

//Filename    : OUNITIND.CPP
//Description : Object independent Unit AI

#include <OSYS.h>
#include <OSPY.h>
#include <OREBEL.h>
#include <OUNIT.h>
#include <OCONFIG.h>
#include <OF_CAMP.h>
#include <ONATION.h>

//--------- Begin of function Unit::think_independent_unit --------//
//
// Think about the action of an independent unit. It first tries to
// settle to a town. If not successful, it will disband itself.
//
void Unit::think_independent_unit()
{
	if( !is_ai_all_stop() )
		return;

	//--- don't process if it's a spy and the notify cloak flag is on ---//

	if( spy_recno )
	{
		//---------------------------------------------//
		//
		// If notify_cloaked_nation_flag is 0, the AI
		// won't control the unit.
		//
		// If notify_cloaked_nation_flag is 1, the AI
		// will control the unit. But not immediately,
		// it will do it once 5 days so the player can
		// have a chance to select the unit and set its
		// notify_cloaked_nation_flag back to 0 if the
		// player wants.
		//
		//---------------------------------------------//

		if( spy_array[spy_recno]->notify_cloaked_nation_flag==0 )
			return;

		if( info.game_date%5 != sprite_recno%5 )
			return;
	}

	//-------- if this is a rebel ----------//

	if( unit_mode == UNIT_MODE_REBEL )
	{
		Rebel* rebelPtr = rebel_array[unit_mode_para];

		//--- if the group this rebel belongs to already has a rebel town, assign to it now ---//

		if( rebelPtr->town_recno )
		{
			if( !town_array.is_deleted(rebelPtr->town_recno) )
			{
				Town* townPtr = town_array[rebelPtr->town_recno];

				err_when( townPtr->rebel_recno != rebelPtr->rebel_recno );

				assign(townPtr->loc_x1, townPtr->loc_y1);
			}
			
			return;			// don't do anything if the town has been destroyed, Rebel::next_day() will take care of it. 
		}
	}
	//---- look for towns to assign to -----//

	Town *townPtr, *bestTown=NULL;
	int  regionId = world.get_region_id( next_x_loc(), next_y_loc() );
	int  curRating, bestRating=0;
	int  curXLoc = next_x_loc(), curYLoc = next_y_loc();

	for( int i=town_array.size() ; i>0 ; i-- )
	{
		if( town_array.is_deleted(i) )
			continue;

		townPtr = town_array[i];

		if( townPtr->nation_recno ||
			 townPtr->population >= MAX_TOWN_POPULATION ||
			 townPtr->region_id != regionId )
		{
			continue;
		}

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

		curRating = world.distance_rating(curXLoc, curYLoc,
						townPtr->center_x, townPtr->center_y );

		curRating += 100 * townPtr->race_pop_array[race_id-1] / townPtr->population;

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

		if( curRating > bestRating )
		{
			bestRating = curRating;
			bestTown   = townPtr;
		}
	}

	if( bestTown )
	{
		err_when( unit_mode==UNIT_MODE_REBEL && rebel_array[unit_mode_para]->town_recno &&
					 rebel_array[unit_mode_para]->town_recno != bestTown->town_recno );

		//--- drop its rebel identity and becomes a normal unit if he decides to settle to a town ---//

		if( unit_mode == UNIT_MODE_REBEL )
			rebel_array.drop_rebel_identity(sprite_recno);		

		assign(bestTown->loc_x1, bestTown->loc_y1);
	}
	else
		resign(COMMAND_AI);
}
//---------- End of function Unit::think_independent_unit --------//