Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 4f183304e4b05bf1090339b865a9358f71d3dc58 (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
/*
   base/platform.cc
   This file is part of the Osirion project and is distributed under
   the terms and conditions of the GNU General Public License version 2
*/

#include "core/gameserver.h"
#include "core/entityprojectile.h"
#include "base/game.h"
#include "base/faction.h"
#include "base/platform.h"
#include "base/ship.h"
#include "base/weapon.h"
#include "sys/sys.h"

#include <cassert>

namespace game
{

Platform::Platform() : Entity()
{
	entity_moduletypeid = platform_enttype;
	
	platform_cannon = 0;
	platform_turret = 0;
}

Platform::~Platform()
{
	
}

void Platform::set_cannon(const Weapon *cannon)
{
	platform_cannon = cannon;
}

void Platform::set_turret(const Weapon *turret)
{
	platform_turret = turret;
}

void Platform::frame(const unsigned long elapsed)
{
	const float weapon_range = math::max((cannon() ? cannon()->projectile_range() : 0.0f), (turret() ? turret()->projectile_range() : 0.0f));
	
	if ((weapon_range > 0.0f) && slots() && zone()->keepalive_run()) {
		
		// create list of potential enemies
		std::list<Ship *> enemylist;
		
		for (core::Zone::Content::iterator zit = zone()->content().begin(); zit != zone()->content().end(); ++zit) {
			if ((*zit)->moduletype() == ship_enttype) {
				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 > weapon_range + r) {
					continue;
				}

				// reputation is interprated as "what others think of me"
				float reputation = 0.0f;
				if (other_ship->owner()) {
					// check owner reputation for the faction this platform belongs to
					reputation = other_ship->owner()->reputation(faction());
					
				} else if (other_ship->faction()) {
					// check other ships's faction for the faction this platform 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;
				}
				enemylist.push_back(other_ship);
			
			}
		}

		// platforms do not need weapons in inventory to fire
		if (enemylist.size()) {
			
			for (core::Slots::iterator slit = slots()->begin(); slit != slots()->end(); slit++) {
				core::Slot *slot = (*slit);
				
				// found out if this slot is a cannon or a turret
				const Weapon *weapon = 0;
				if (slot->type() == model::Slot::Cannon) {
					weapon = cannon();
				
				} else if (slot->type() == model::Slot::Turret) {
					weapon = turret();
				}
				
				if ((!weapon) || (weapon->projectile_interval() == 0)) {
					continue;
				}
				
				if ((slot->last_fired() + weapon->projectile_interval() >  core::server()->timestamp())) {
					continue;
				}
		
				// target location in world coordinates
				Ship *current_enemy = 0;		// The enemy ship the slot will be targetting
				float current_distance = 0.0f;		// Distance to the target location
				math::Vector3f current_aim;		// Location to aim at
				
				// slot and projectile location in world coordinates
				const math::Vector3f projectile_location(location() + (axis() * slot->location()));
				math::Vector3f projectile_direction;
				math::Axis projectile_axis(axis() * slot->axis());

				// we only need half the cone angle for the cosine calculation
				const float conecos = cosf(slot->cone() * 0.5f); 
				
				for (std::list<Ship *>::const_iterator enemy_it = enemylist.begin(); enemy_it != enemylist.end(); enemy_it++)
				{
					// apply aim correction
					// see https://www.reddit.com/r/gamedev/comments/16ceki/turret_aiming_formula/c7vbu2j
					
					const math::Vector3f v((*enemy_it)->axis().forward() * (*enemy_it)->speed());
					const math::Vector3f w((*enemy_it)->location() - projectile_location);
					
					const float a = v.lengthsquared() - weapon->projectile_speed() * weapon->projectile_speed();
					const float b = 2.0f * math::dotproduct(v, w);
					const float c = w.lengthsquared();
					
					const float D = b * b - 4.0f * a * c;
					if (D < 0.0f)
					{
						continue;
					} 					
					const float t = math::min(sqrtf(D) - b , -sqrtf(D) - b) / (2.0f * a);				
					const math::Vector3f hitpoint((*enemy_it)->location() + v * t);
					
					// verify the hitpoint is within weapon's range
					const float d = math::distance(hitpoint, projectile_location);
					if (d > weapon->projectile_range() + (*enemy_it)->radius())
					{
						continue;
					}
					if ((current_distance > 0.0f) && (d > current_distance))
					{
						continue;
					}
										
					projectile_direction.assign(hitpoint - projectile_location);
					projectile_direction.normalize();
					
					// verify the hitpoint is in the slot's cone-of-fire
					const float cosa = math::dotproduct(projectile_direction, projectile_axis.forward());
					if (cosa >= conecos)
					{
						current_distance = d;
						current_enemy = (*enemy_it);
						current_aim.assign(hitpoint);
					}
				}
				
				if (current_enemy)
				{
					projectile_direction.assign(current_aim - projectile_location);
					projectile_direction.normalize();
					
					const float cosa = math::dotproduct(projectile_direction, projectile_axis.forward());
					
					// point the projectile into the fire direction
					math::Vector3f normal(math::crossproduct(projectile_direction, projectile_axis.forward()));
					if (normal.length() > MIN_DELTA)
					{
						float sina = sqrt(1.0f - cosa * cosa);
						
						normal.normalize();
						projectile_axis.rotate(normal, cosa, sina);
					}

					// spawn a new projectile
					core::EntityProjectile *projectile = new core::EntityProjectile(this);

					projectile->set_damage(weapon->damage());
					projectile->set_lifespan(weapon->projectile_lifespan());
					projectile->set_projectile_modelname(weapon->projectile_modelname());
					projectile->set_projectile_soundname(weapon->projectile_soundname());
	
					projectile->set_axis(projectile_axis);
					projectile->set_location(projectile_location);
					projectile->set_speed(weapon->projectile_speed());
				
					projectile->reset();
					slot->set_last_fired(core::server()->timestamp());
				}
			}
			
		}
	}
}

} // namespace game