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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
/*
base/ship.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 <cmath>
#include <iostream>
#include "auxiliary/functions.h"
#include "core/gameserver.h"
#include "core/entity.h"
#include "base/game.h"
#include "base/ship.h"
#include "math/functions.h"
using math::degrees360f;
using math::degrees180f;
namespace game
{
const float MIN_DELTA = 0.000001f;
Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable()
{
entity_moduletypeid = ship_enttype;
set_modelname(shipmodel->modelname());
set_name(shipmodel->name());
set_info(shipmodel);
ship_shipmodel = shipmodel;
ship_jumpdrive = shipmodel->jumpdrive();
ship_impulsedrive_timer = 0;
ship_jumpdrive_timer = 0;
ship_jumpdepart = 0;
if (owner) {
// this ship is owned by a player
set_owner(owner);
get_color().assign(owner->color());
get_color_second().assign(owner->color_second());
std::string str(aux::text_strip(owner->name()));
aux::to_label(str);
set_label(str);
// add an inventory
set_inventory(new core::Inventory());
inventory()->set_capacity(shipmodel->maxcargo());
} else {
set_name(shipmodel->name());
set_label(shipmodel->label());
}
if (shipmodel->dock()) {
using core::MenuDescription;
using core::ButtonDescription;
MenuDescription *menu_main = new MenuDescription();
menu_main->set_label("main");
menu_main->set_text("Launch area");
add_menu(menu_main);
ButtonDescription *button = new ButtonDescription();
button->set_text("Launch");
button->set_command("launch", ButtonDescription::CommandGame);
button->set_alignment(ButtonDescription::Center);
menu_main->add_button(button);
set_flag(core::Entity::Dockable);
}
set_mass(shipmodel->mass());
set_impulse_force(shipmodel->impulse_force());
set_thrust_force(shipmodel->thrust_force());
set_strafe_force(shipmodel->strafe_force());
set_turn_force(shipmodel->turn_force());
set_roll_force(shipmodel->roll_force());
reset();
const float linear_damp = 0.8f;
const float angular_damp = 0.8f;
body()->setDamping(linear_damp, angular_damp);
}
Ship::~Ship()
{
}
void Ship::reset()
{
current_target_direction = 0.0f;
current_target_pitch = 0.0f;;
current_target_roll = 0.0f;
current_target_strafe = 0.0f;
current_target_vstrafe = 0.0f;
current_target_afterburner = 0.0f;
EntityControlable::reset();
}
void Ship::func_impulse()
{
if (entity_state == core::Entity::Impulse) {
entity_state = core::Entity::Normal;
target_thrust = 1.0f;
entity_thrust = 0.0f;
set_dirty();
return;
}
if (entity_state == core::Entity::ImpulseInitiate) {
entity_state = core::Entity::Normal;
set_dirty();
return;
}
if (entity_state == core::Entity::JumpInitiate) {
entity_state = core::Entity::Normal;
ship_jumpdrive_timer = 0;
entity_timer = 0;
set_dirty();
}
if (entity_state != core::Entity::Normal)
return;
entity_state = core::Entity::ImpulseInitiate;
entity_timer = impulse_timer_delay;
ship_impulsedrive_timer = core::server()->time();
}
void Ship::initiate_jump(JumpPoint *depart)
{
ship_jumpdepart = 0;
if (!depart)
return;
if (!depart->target())
return;
ship_jumpdepart = depart;
entity_state = core::Entity::JumpInitiate;
entity_timer = jump_timer_delay;
ship_jumpdrive_timer = core::server()->time();
set_dirty();
}
void Ship::func_jump(std::string const &args)
{
if (entity_state == core::Entity::Docked) {
return;
}
// devel mode provides instant jump access to arbitrary systems
if (Game::g_devel->value() && (args.size())) {
core::Zone *jumptargetzone = core::Zone::search(args);
if (!jumptargetzone) {
std::string helpstr;
for (core::Zone::Registry::iterator it = core::Zone::registry().begin(); it != core::Zone::registry().end(); it++) {
if (helpstr.size())
helpstr.append("^N|^B");
helpstr.append((*it).second->label());
}
owner()->send("Usage: jump [^B" + helpstr + "^N]");
return;
}
if (jumptargetzone == zone()) {
owner()->send("Already in the " + jumptargetzone->name());
return;
}
owner()->send("Jumping to the " + jumptargetzone->name());
set_zone(jumptargetzone);
ship_jumpdrive_timer = 0;
entity_timer = 0;
set_state(core::Entity::Jump);
set_dirty();
return;
} else {
if (!jumpdrive() && !Game::g_devel->value()) {
owner()->send("This ship is not equiped with a hyperspace drive!");
return;
} else if (entity_state == core::Entity::Jump) {
return;
} else if (entity_state == core::Entity::JumpInitiate) {
owner()->send("Jump aborted, hyperspace drive deactivated");
ship_jumpdrive_timer = 0;
entity_timer = 0;
entity_state = core::Entity::Normal;
return;
}
initiate_jump(find_closest_jumppoint());
}
}
JumpPoint * Ship::find_closest_jumppoint()
{
// find closest jumpgate or jumppoint
float d = -1;
JumpPoint *jumppoint = 0;
for (core::Zone::Content::iterator it = zone()->content().begin(); it != zone()->content().end(); it++) {
core::Entity *entity = (*it);
if ((entity->moduletype() == jumppoint_enttype) || (entity->moduletype() == jumpgate_enttype)) {
JumpPoint *te = static_cast<JumpPoint *>(entity);
float d1 = math::distance(location(), te->location());
if ((d < 0) || (d1 < d)) {
d = d1;
jumppoint = te;
}
}
}
if (jumppoint && jumppoint->target()) {
if (Game::g_jumppointrange->value() < d) {
owner()->send("Jumppoint out of range!");
return 0;
} else {
owner()->send("Jumping to the " + jumppoint->target()->zone()->name());
return jumppoint;
}
} else {
owner()->send("No jumppoints found!");
return 0;
}
return 0;
}
void Ship::explode()
{
set_state(core::Entity::Destroyed);
target_direction = 0;
target_pitch = 0;
target_roll = 0;
target_strafe = 0.0f;
target_vstrafe = 0.0f;
target_afterburner = 0.0f;
target_thrust = 0;
entity_thrust = 0;
if (owner()) {
if (owner()->control() == this)
owner()->set_view(this);
}
};
void Ship::set_zone(core::Zone *zone)
{
core::EntityControlable::set_zone(zone);
if (owner() && (owner()->control() == (EntityControlable*) this))
owner()->set_zone(zone);
}
void Ship::action (btScalar seconds)
{
float maxspeed = 0;
float engine_force = 0;
float turn_force = ship_turn_force;
float roll_force = ship_roll_force;
btTransform t;
switch (entity_state) {
case core::Entity::Normal:
engine_force = ship_thrust_force * entity_thrust;
maxspeed = ship_shipmodel->maxspeed() * entity_thrust;
break;
case core::Entity::ImpulseInitiate:
case core::Entity::Impulse:
engine_force = ship_impulse_force;
maxspeed = (Game::g_impulsespeed ? Game::g_impulsespeed->value() * 0.01f : 0.0f);
turn_force *= .5f;
roll_force *= .5f;
break;
case core::Entity::JumpInitiate:
case core::Entity::Jump:
maxspeed = 0.0f;
engine_force = 0.0f;
break;
case core::Entity::Docked:
maxspeed = 0.0f;
engine_force = 0.0f;
t.setIdentity();
t.setOrigin(to_btVector3(location()));
t.setBasis(to_btMatrix3x3(axis()));
body()->setWorldTransform(t);
zone()->physics()->synchronizeSingleMotionState(entity_body);
return;
break;
default:
maxspeed = 0.0f;
engine_force = 0.0f;
break;
}
t.setIdentity();
entity_motionstate->getWorldTransform(t);
get_location().assign(t.getOrigin());
get_axis().assign(t.getBasis());
const float torque_scale = 0.001f;
// apply strafe
body()->applyCentralImpulse(math::to_btVector3(axis().up() * current_target_vstrafe * ship_strafe_force));
body()->applyCentralImpulse(math::to_btVector3(axis().left() * current_target_strafe * ship_strafe_force));
// apply afterburner
body()->applyCentralImpulse(math::to_btVector3(axis().forward() * current_target_afterburner * ship_strafe_force));
// apply main thruster
if (current_target_afterburner >= 0) {
body()->applyCentralImpulse(math::to_btVector3(axis().forward() * engine_force));
}
// apply direction
body()->applyTorqueImpulse(math::to_btVector3(axis().up() * current_target_direction * turn_force * torque_scale));
// apply pitch
body()->applyTorqueImpulse(math::to_btVector3(axis().left() * -current_target_pitch * turn_force * torque_scale));
// apply roll
body()->applyTorqueImpulse(math::to_btVector3(axis().forward() * -current_target_roll * roll_force* torque_scale));
// limit speed
entity_speed = (float) entity_body->getLinearVelocity().length();
if (entity_speed > Game::g_impulsespeed->value() * 0.01f) {
entity_speed = Game::g_impulsespeed->value() * 0.01f;
body()->setLinearVelocity(math::to_btVector3(axis().forward() * entity_speed));
}
}
void Ship::frame(float seconds)
{
const float direction_reaction = 2.0f; // directional control reaction time
const float thrust_reaction = 0.5f; // thrust control reaction time
const float strafe_reaction = 1.5f; // strafe control reaction time
const float afterburner_reaction = 1.0f; // afterburner control reaction time
math::Vector3f n; // normal of a plane
math::Axis target_axis(axis()); // target axis
entity_movement = 0;
/* -- update state ----------------------------------------- */
// speed might be set to 0 on this update
if (entity_speed != 0.0f)
set_dirty();
if (entity_state == core::Entity::Docked) {
target_direction = 0;
target_pitch = 0;
target_roll = 0;
target_strafe = 0.0f;
target_vstrafe = 0.0f;
target_afterburner = 0.0f;
target_thrust = 0;
entity_thrust = 0;
entity_speed = 0.0f;
} else if (entity_state == core::Entity::JumpInitiate) {
if (ship_jumpdrive_timer + 1.0f <= core::server()->time()) {
entity_timer -= 1.0f;
if (entity_timer <= 0) {
if (ship_jumpdepart && ship_jumpdepart->target()) {
set_state(core::Entity::Jump);
entity_speed = Game::g_impulsespeed->value() * 0.01f;
if (ship_jumpdepart->moduletype() == jumpgate_enttype) {
get_axis().assign(ship_jumpdepart->target()->axis());
get_location().assign(ship_jumpdepart->target()->location());
get_location() += axis().forward() * (radius() + ship_jumpdepart->target()->radius());
} else {
get_location().assign(ship_jumpdepart->target()->location() + location() - ship_jumpdepart->location());
}
set_zone(ship_jumpdepart->target()->zone());
if (owner() && owner()->view() && (owner()->view()->zone() != ship_jumpdepart->target()->zone()))
owner()->set_view(0);
owner()->send("^BJumping to the " + ship_jumpdepart->target()->zone()->name());
} else {
set_state(core::Entity::Normal);
}
ship_jumpdrive_timer = 0;
entity_timer = 0;
set_dirty();
return;
} else {
ship_jumpdrive_timer = core::server()->time();
set_dirty();
}
}
// control is disabled while the jumpdrive is activated
target_direction = 0;
target_pitch = 0;
target_roll = 0;
target_strafe = 0.0f;
target_vstrafe = 0.0f;
target_afterburner = 0.0f;
target_thrust = 0.1f;
} else if (entity_state == core::Entity::Jump) {
// control is disabled while the jumpdrive is activated
target_direction = 0;
target_pitch = 0;
target_roll = 0;
target_strafe = 0.0f;
target_vstrafe = 0.0f;
target_afterburner = 0.0f;
target_thrust = 0.0f;
// FIXME 5 second cooldown
entity_state = core::Entity::Normal;
set_dirty();
if (owner() && owner()->view() && owner()->control() == (EntityControlable*) this)
owner()->set_view(0);
return;
} else if (entity_state == core::Entity::ImpulseInitiate) {
// cancel impulse drive if afterburner goes reverse
if (target_afterburner < 0.0f) {
set_state(core::Entity::Normal);
set_dirty();
entity_timer = 0;
} else {
if (ship_impulsedrive_timer + 1.0f <= core::server()->time()) {
entity_timer -= 1.0f;
if (entity_timer <= 0) {
entity_state = core::Entity::Impulse;
entity_timer = 0;
set_dirty();
} else {
ship_impulsedrive_timer = core::server()->time();
set_dirty();
}
}
// clamp input values
target_thrust = 0.0f;
target_afterburner = 0.0f;
math::clamp(target_pitch, -1.0f, 1.0f);
math::clamp(target_roll, -1.0f, 1.0f);
math::clamp(target_direction, -1.0f, 1.0f);
}
} else if (entity_state == core::Entity::Impulse) {
// clamp input values
math::clamp(target_pitch, -1.0f, 1.0f);
math::clamp(target_roll, -1.0f, 1.0f);
math::clamp(target_direction, -1.0f, 1.0f);
// cancel impulse drive if afterburner goes reverse
if (target_afterburner < 0.0f) {
set_state(core::Entity::Normal);
set_dirty();
entity_timer = 0;
target_thrust = 1.0f;
entity_thrust = 1.0f;
} else {
target_afterburner = 0.0f;
target_thrust = 0.0f;
}
} else if (entity_state == core::Entity::Normal) {
// clamp input values
math::clamp(target_thrust, 0.0f, 1.0f);
math::clamp(target_pitch, -1.0f, 1.0f);
math::clamp(target_roll, -1.0f, 1.0f);
math::clamp(target_direction, -1.0f, 1.0f);
math::clamp(target_afterburner, -1.0f, 1.0f);
} else if (entity_state == core::Entity::Destroyed) {
target_direction = 0;
target_pitch = 0;
target_roll = 0;
target_strafe = 0.0f;
target_vstrafe = 0.0f;
target_afterburner = 0.0f;
target_thrust = 0;
entity_thrust = 0;
}
// update afterburner control target
if (current_target_afterburner < target_afterburner) {
current_target_afterburner += afterburner_reaction * seconds;
if (current_target_afterburner > target_afterburner)
current_target_afterburner = target_afterburner;
} else if (current_target_afterburner > target_afterburner) {
current_target_afterburner -= afterburner_reaction * seconds;
if (current_target_afterburner < target_afterburner)
current_target_afterburner = target_afterburner;
}
// update thrust control target
if (current_target_afterburner < 0.0f) {
target_thrust = 0;
}
if (entity_thrust < target_thrust) {
entity_thrust += thrust_reaction * seconds;
if (entity_thrust > target_thrust)
entity_thrust = target_thrust;
} else if (entity_thrust > target_thrust) {
entity_thrust -= thrust_reaction * seconds;
if (entity_thrust < target_thrust)
entity_thrust = target_thrust;
}
math::clamp(entity_thrust, 0.0f, 1.0f);
// update strafe control target
if (current_target_strafe < target_strafe) {
current_target_strafe += strafe_reaction * seconds;
if (current_target_strafe > target_strafe)
current_target_strafe = target_strafe;
} else if (current_target_strafe > target_strafe) {
current_target_strafe -= strafe_reaction * seconds;
if (current_target_strafe < target_strafe)
current_target_strafe = target_strafe;
}
// update vstrafe control target
if (current_target_vstrafe < target_vstrafe) {
current_target_vstrafe += strafe_reaction * seconds;
if (current_target_vstrafe > target_vstrafe)
current_target_vstrafe = target_vstrafe;
} else if (current_target_vstrafe > target_vstrafe) {
current_target_vstrafe -= strafe_reaction * seconds;
if (current_target_vstrafe < target_vstrafe)
current_target_vstrafe = target_vstrafe;
}
// update direction control target
if (current_target_direction < target_direction) {
current_target_direction += direction_reaction * seconds;
if (current_target_direction > target_direction) {
current_target_direction = target_direction;
}
} else if (current_target_direction > target_direction) {
current_target_direction -= direction_reaction * seconds;
if (current_target_direction < target_direction) {
current_target_direction = target_direction;
}
}
// update pitch control target
if (current_target_pitch < target_pitch) {
current_target_pitch += direction_reaction * seconds;
if (current_target_pitch > target_pitch)
current_target_pitch = target_pitch;
} else if (current_target_pitch > target_pitch) {
current_target_pitch -= direction_reaction * seconds;
if (current_target_pitch < target_pitch)
current_target_pitch = target_pitch;
}
// update roll control target
if (current_target_roll < target_roll) {
current_target_roll += direction_reaction * seconds;
if (current_target_roll > target_roll)
current_target_roll = target_roll;
} else if (current_target_roll > target_roll) {
current_target_roll -= direction_reaction * seconds;
if (current_target_roll < target_roll)
current_target_roll = target_roll;
}
/*
// -- BULLET
// apply thrust
body()->applyCentralForce(math::to_btVector3(axis().forward() * (actual_thrust * actual_acceleration)));
// apply strafe
body()->applyCentralForce(math::to_btVector3(axis().left() * (current_target_strafe * 0.15f * actual_acceleration)));
body()->applyCentralForce(math::to_btVector3(axis().up() * (current_target_vstrafe * 0.15f * actual_acceleration)));
// FIXME get movement state from linear/angular velocity
entity_movement = target_thrust;
entity_movement = math::max(entity_movement, fabs(current_target_pitch));
entity_movement = math::max(entity_movement, fabs(current_target_direction));
entity_movement = math::max(entity_movement, fabs(current_target_roll));
entity_movement = math::max(entity_movement, fabs(current_target_afterburner));
entity_movement = math::max(entity_movement, fabs(current_target_strafe));
entity_movement = math::max(entity_movement, fabs(current_target_vstrafe));
if (entity_movement > 0) {
set_dirty();
}
EntityDynamic::frame(seconds);
// apply direction rotation to target axis
if (fabs(current_target_direction) > MIN_DELTA) {
math::clamp(current_target_direction, -1.0f, 1.0f);
target_axis.change_direction(actual_turnspeed * current_target_direction);
} else {
current_target_direction = 0.0f;
}
// apply pitch rotation to target axis
if (fabs(current_target_pitch) > MIN_DELTA) {
math::clamp(current_target_pitch, -1.0f, 1.0f);
target_axis.change_pitch(actual_turnspeed * current_target_pitch);
} else {
current_target_pitch = 0.0f;
}
// apply roll rotation to axis
if (fabs(current_target_roll) > MIN_DELTA) {
math::clamp(current_target_roll, -1.0f, 1.0f);
get_axis().change_roll(actual_turnspeed * current_target_roll * seconds);
} else {
current_target_roll = 0.0f;
}
// update axis
float cosangle; // cosine of an angle
float angle; // angle in radians
n.assign(math::crossproduct(axis().forward(), target_axis.forward()));
if (!(n.length() < MIN_DELTA)) {
n.normalize();
cosangle = math::dotproduct(axis().forward(), target_axis.forward());
angle = acos(cosangle) * seconds; // * 180.0f / M_PI;
if (angle > MIN_DELTA)
get_axis().rotate(n, -angle);
}
// update speed
if ((entity_state == core::Entity::ImpulseInitiate) || (entity_state == core::Entity::Impulse)) {
actual_thrust = 1.0f;
} else {
actual_thrust = entity_thrust + current_target_afterburner * 0.15f;
}
const float max = actual_thrust * actual_maxspeed;
if (entity_speed < max) {
entity_speed += actual_acceleration * seconds;
if (entity_speed > max) {
entity_speed = max;
}
} else if (entity_speed > max) {
entity_speed -= actual_acceleration * seconds;
if (entity_speed < max) {
entity_speed = max;
}
}
// apply strafe to location
if (fabs(current_target_strafe) > MIN_DELTA) {
get_location() += axis().left() * (current_target_strafe * 0.15f * actual_maxspeed) * seconds;
}
// apply vstrafe to location
if (fabs(current_target_vstrafe) > MIN_DELTA) {
get_location() += axis().up() * (current_target_vstrafe * 0.15f * actual_maxspeed) * seconds;
}
// apply speed to location
if (fabs(speed()) > MIN_DELTA) {
get_location() += axis().forward() * speed() * seconds;
}
entity_movement = target_thrust;
entity_movement = math::max(entity_movement, fabs(current_target_pitch));
entity_movement = math::max(entity_movement, fabs(current_target_direction));
entity_movement = math::max(entity_movement, fabs(current_target_roll));
entity_movement = math::max(entity_movement, fabs(current_target_afterburner));
entity_movement = math::max(entity_movement, fabs(current_target_strafe));
entity_movement = math::max(entity_movement, fabs(current_target_vstrafe));
if ((entity_movement > 0) || (entity_speed > 0)) {
set_dirty();
}
// transfer entity location to motion state
btTransform t;
t.setIdentity();
t.setOrigin(math::to_btVector3(location()));
t.setBasis(math::to_btMatrix3x3(axis()));
entity_body->setWorldTransform(t);
if (zone())
zone()->physics()->synchronizeSingleMotionState(entity_body);
*/
EntityControlable::frame(seconds);
}
} // namespace game
|