blob: 15c7576dfd27eae03a7a7222377abcd51109ec10 (
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
|
/*
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/weapon.h"
#include "sys/sys.h"
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 *ship = static_cast<Ship *>((*zit));
if ((ship->state() != Normal) && (ship->state() != ImpulseInitiate) && (ship->state() != Impulse)) {
continue;
}
const float d = math::distance(location(), ship->location());
const float r = radius() + ship->radius();
// too far
if (d > weapon_range + r) {
continue;
}
float reputation = 0.0f;
if (ship->owner()) {
// check owner reputation for the faction the NPC belongs to
reputation = ship->owner()->reputation(faction());
} else if (faction() && (faction()->type() == Faction::infotype())) {
//check my faction reputation for the other ships's faction
reputation = static_cast<const Faction *>(faction())->reputation(ship->faction());
}
// reputation threshold to get attacked
if (reputation > core::range::reputation_hostile) {
continue;
}
enemylist.push_back(ship);
}
}
// platforms do not need weapons in inventory to fire
if (enemylist.size()) {
const float modelscale = radius() / (model() ? model()->radius() : 1.0f);
for (core::Slots::iterator it = slots()->begin(); it != slots()->end(); it++) {
core::Slot *slot = (*it);
// found out if this slot is a cannon or a turret
const Weapon *weapon = 0;
if (slot->type() == model::Weapon::Cannon) {
weapon = cannon();
} else if (slot->type() == model::Weapon::Turret) {
weapon = turret();
}
if ((!weapon) || (weapon->projectile_interval() == 0)) {
continue;
}
if ((slot->last_fired() + weapon->projectile_interval() > core::server()->timestamp())) {
continue;
}
// location of the slot in world coordinates
const math::Vector3f slot_location(location() + (axis() * slot->location() * modelscale));
// find a target for this slot
Ship *current_enemy = 0;
float current_distance = 0.0f;
const float projectile_radius = core::PROJECTILE_RADIUS;
math::Axis projectile_axis(axis() * slot->axis());
const math::Vector3f projectile_location(slot_location + projectile_axis.forward() * projectile_radius);
math::Vector3f projectile_direction;
math::Vector3f aim_location;
// 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++) {
const float d = math::distance((*enemy_it)->location(), projectile_location);
if (d > weapon->projectile_range() + (*enemy_it)->radius()) {
continue;
}
if ((current_distance > 0) && (d > current_distance)) {
continue;
}
aim_location.assign((*enemy_it)->location() + (*enemy_it)->axis().forward() * ( (*enemy_it)->radius() * 0.25f));
projectile_direction.assign(aim_location - projectile_location);
projectile_direction.normalize();
const float cosa = math::dotproduct(projectile_direction, projectile_axis.forward());
// check if the ship is in the slot's cone if fire
if (cosa >= conecos) {
current_distance = d;
current_enemy = (*enemy_it);
}
}
if (current_enemy) {
aim_location.assign(current_enemy->location() + current_enemy->axis().forward() * (current_enemy->radius() * 0.25f));
projectile_direction.assign(aim_location - 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());
}
}
}
}
}
}
|