Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 3bd5ca0891b08521f7913dfea5a3e75a2c45ead5 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
   base/npc.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "core/range.h"

#include "base/npc.h"
#include "base/game.h"
#include "base/patrol.h"

#include <cassert>

namespace game {
	
// NPC Wingman factory function
NPC *NPC::add_wingman(Ship *leader)
{
	if (!leader) {
		return 0;
	}
	
	if (leader->state() != Entity::Normal) {
		return 0;
	}

	NPC *npc = new NPC(leader->shipmodel());
	
	npc->set_leader(leader);
	//npc->set_owner(leader->owner());
	npc->set_name("Wingman");
	npc->set_mood(MoodFormation);
	
	npc->set_color(npc->leader()->color());
	npc->set_color_second(npc->leader()->color_second());
	
	npc->set_location(leader->location() - leader->axis().forward() *  2.0f * (leader->radius() + npc->radius()));
	npc->nudge();
	
	npc->set_axis(leader->axis());
	npc->set_zone(leader->zone());
	
	// copy weapon layout
	
	if (!npc->inventory()) {
		npc->add_inventory();
	}
	// TODO ship models should be identical, otherwise the slot loeading can fail in horrible ways
	assert (npc->shipmodel() == leader->shipmodel());
	
	for (size_t i = 0; i < leader->slots()->size(); ++i) {
		core::Slot *slot = leader->slots()->operator[](i);
		
		if (slot->item() && slot->has_flag(core::Slot::Mounted)) {
		
			core::Item *npc_item = new core::Item(slot->item()->info());
			npc_item->set_amount(slot->item()->amount());
			npc_item->set_flags(slot->item()->flags());
			npc_item->set_flag(core::Item::Mounted);
			npc->inventory()->add(npc_item);
		
			core::Slot *npc_slot = npc->slots()->operator[](i);
			npc_slot->set_item(npc_item);
			npc_slot->set_flag(core::Slot::Active);
			npc_slot->set_flag(core::Slot::Mounted);
		}
	}
	
	npc->calculate_weapon_range();
	
	npc->reset();
	
	return npc;
}

NPC::NPC(const ShipModel *shipmodel) : Ship(0, shipmodel)
{
	npc_mood = MoodWander;
	npc_destroyed_timestamp = 0;
	
	npc_patrol = 0;
	npc_leader = 0;
	
	npc_weapon_range = 0.0f;
}

NPC::~NPC()
{
	if (npc_patrol) {
		npc_patrol->erase_member(this);
	}
}

void NPC::calculate_weapon_range()
{
	npc_weapon_range = 0.0f;
	
	if (!slots()) {
		return;
	}
	
	for (core::Slots::const_iterator it = slots()->begin(); it != slots()->end(); it++) {
		core::Slot *slot = (*it);
		
		if (!slot->has_flag(core::Slot::Mounted)) {
			continue;
				
		} else if (!slot->item() || (slot->item()->info()->type() != Weapon::infotype())) {
			continue;
				
		} else {
			const Weapon *weapon = static_cast<const Weapon *>(slot->item()->info());
				
			if ((weapon->subtype() != Weapon::Cannon) && (weapon->subtype() != Weapon::Turret) ) {
				continue;
			}
			const float r = weapon->projectile_range();
			
			if (r > npc_weapon_range) {
				npc_weapon_range = r;
			}
		}
	}
}

void NPC::set_mood(const Mood mood)
{
	npc_mood = mood;
}

void NPC::set_leader(Ship *leader)
{
	npc_leader = leader;
}

void NPC::set_patrol(Patrol *patrol)
{
	npc_patrol = patrol;
}

Ship *NPC::target_closest_enemy()
{
	// scan for enemies
	Ship *current_enemy = 0;
	float current_distance = 0.0f;
	
	if (!npc_weapon_range) {
		return 0;
	}

	for (core::Zone::Content::iterator zit = zone()->content().begin(); zit != zone()->content().end(); ++zit) {
		if (((*zit)->moduletype() == ship_enttype) && ((*zit) != this)) {
			Ship *other_ship = static_cast<Ship *>((*zit));
			
			if ((other_ship->state() != Normal) && (other_ship->state() != ImpulseInitiate) && (other_ship->state() != Impulse)) {
				continue;
			}
			
			const float d = math::distance(location(), other_ship->location());
			const float r = radius() + other_ship->radius();
			
			// too far
			if (d > r + core::range::fxdistance) {
				continue;
			}
			
			// further than current target
			if ((current_distance > 0.0f) && (d > current_distance)) {
				continue;
			}
			
			float reputation = 0.0f;
			if (other_ship->owner()) {
					// check owner reputation for the faction this NPC belongs to
					reputation = other_ship->owner()->reputation(faction());
					
				} else if (other_ship->faction()) {
					// check other ships's faction for the faction this NPC belongs to
					assert(other_ship->faction()->type() == Faction::infotype());
					reputation = static_cast<const Faction *>(other_ship->faction())->reputation(faction());
				}
			
			// reputation threshold to get attacked
			if (reputation > core::range::reputation_hostile) {
				continue;
			}
		
			current_enemy = other_ship;
			current_distance = d;
		}
	}
	
	// set aim
	if ((state() == Normal) && current_enemy && (current_distance <= radius() + npc_weapon_range)) {
		// activate weapons
		set_target_controlflag(core::EntityControlable::ControlFlagFire);
		// rudimentary aim currention
		target_aim.assign(current_enemy->location() + current_enemy->axis().forward() * (current_enemy->radius() * 0.25f) );
	} else {
		// deactivate weapons
		unset_target_controlflag(core::EntityControlable::ControlFlagFire);
	}
	
	return current_enemy;
}

void NPC::frame(const unsigned long elapsed)
{
	if (state() == core::Entity::Destroyed) {
		
		if (!npc_destroyed_timestamp) {
			npc_destroyed_timestamp = core::game()->time();
			
		} else if (npc_destroyed_timestamp + 10.0f < core::game()->time()) {
			// stay alive for 10 more seconds while explosion particles are drawn		
			die();
		}

	} else {
		// TODO pilot magic and mood witchcraft		
		if (leader()) {
			
			// verify leader still exists
			if (!core::Entity::find(leader())) {
				
				set_leader(0);
				explode();
				npc_destroyed_timestamp = core::game()->time();
			
			} else if (leader()->zone() == zone()) {
				
				// leader is in this zone
				if (leader()->state() == Docked) {
					
					if (state() != core::Entity::Docked) {
						
						set_autopilot_target(leader()->dock());
						
						set_autopilot_flag(Ship::AutoPilotEnabled);
						set_autopilot_flag(Ship::AutoPilotDock);
						unset_autopilot_flag(Ship::AutoPilotFormation);
						unset_autopilot_flag(Ship::AutoPilotCombat);
						
					} else {
						unset_autopilot_flag(Ship::AutoPilotEnabled);
					}

				} else if ((leader()->state() == JumpInitiate) || (leader()->state() == Jump)) {
					
					// goto wherever the leader is jumping
					set_autopilot_target(leader()->jumpdepart());
					set_autopilot_flag(Ship::AutoPilotEnabled);
					set_autopilot_flag(Ship::AutoPilotDock);
					unset_autopilot_flag(Ship::AutoPilotFormation);
					unset_autopilot_flag(Ship::AutoPilotCombat);
					
				} else {
					
					if (state() == core::Entity::Docked) {
						// FIXME check launch conditions when docked at another player's ship
						launch();
						
					} else {
						Ship *enemy = target_closest_enemy();
						
						if (enemy && leader()->has_autopilot_flag(Ship::AutoPilotCombat)) {
							set_autopilot_target(enemy);
							
							set_autopilot_flag(Ship::AutoPilotEnabled);
							set_autopilot_flag(Ship::AutoPilotCombat);
							unset_autopilot_flag(Ship::AutoPilotDock);
							unset_autopilot_flag(Ship::AutoPilotFormation);
						} else {
							set_autopilot_target(leader());
					
							set_autopilot_flag(Ship::AutoPilotEnabled);
							unset_autopilot_flag(Ship::AutoPilotCombat);
							unset_autopilot_flag(Ship::AutoPilotDock);
							set_autopilot_flag(Ship::AutoPilotFormation);
						}
					}
				}
				
			} else {
				
				// leader is not in this zone
				if (!autopilot_target()) {
					
					target_direction = 0;
					target_pitch = 0;
					target_roll = 0;
					target_strafe = 0.0f;
					target_vstrafe = 0.0f;
					
					target_afterburner = 0.0f;
					target_thrust = 0;
					
					if (state() == core::Entity::Impulse) {

						func_impulse();
					}
				} else {
					set_autopilot_flag(Ship::AutoPilotEnabled);
					set_autopilot_flag(Ship::AutoPilotDock);
					unset_autopilot_flag(Ship::AutoPilotFormation);
					unset_autopilot_flag(Ship::AutoPilotCombat);
				}
			}
			
		} else if (patrol()) {
			// patrol leader behaviour
			
			// autopilot_target() is set by patrol
			if (!autopilot_target()) {
				
				target_direction = 0;
				target_pitch = 0;
				target_roll = 0;
				target_strafe = 0.0f;
				target_vstrafe = 0.0f;
				
				target_afterburner = 0.0f;
				target_thrust = 0;
				
				if (state() == core::Entity::Impulse) {
					func_impulse();
				}

			}	
		} else {
			die();
		}
	}
	
	// run a ship game frame
	Ship::frame(elapsed);
}

} // namespace game