-
Notifications
You must be signed in to change notification settings - Fork 96
/
Universe_control.cpp
executable file
·1370 lines (1122 loc) · 52.3 KB
/
Universe_control.cpp
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 3D World - Universe control - code that interfaces with ships and fre_objs
// by Frank Gennari
// 11/11/05
#include "universe.h"
#include "ship.h"
#include "ship_util.h"
#include "asteroid.h"
#include "timetest.h"
#include "openal_wrap.h"
#ifdef _OPENMP
#include <omp.h>
#endif
bool const TIMETEST = (GLOBAL_TIMETEST || 0);
bool const ORBITAL_REGEN = 0;
bool const PRINT_OWNERSHIP = 0;
bool const PLAYER_SLOW_PLANET_APPROACH = 1;
unsigned const GRAV_CHECK_MOD = 4; // must be a multiple of 2
float last_temp(-100.0);
string player_killer;
s_object clobj0; // closest object to player
pos_dir_up player_pdu;
cobj_vector_t const empty_cobjs; // always empty
unsigned owner_counts[NUM_ALIGNMENT] = {0};
float resource_counts[NUM_ALIGNMENT] = {0.0};
extern bool claim_planet, water_is_lava, no_shift_universe;
extern int uxyz[], window_width, window_height, do_run, fire_key, display_mode, DISABLE_WATER, frame_counter;
extern unsigned NUM_THREADS;
extern float zmax, zmin, fticks, univ_temp, temperature, atmosphere, vegetation, base_gravity, urm_static;
extern float water_h_off_rel, init_temperature, camera_shake;
extern double tfticks;
extern unsigned char **water_enabled;
extern unsigned team_credits[];
extern point universe_origin;
extern colorRGBA base_cloud_color, base_sky_color;
extern string user_text;
extern water_params_t water_params;
extern vector<free_obj *> uobjs;
extern vector<cached_obj> stat_objs;
extern vector<temp_source> temp_sources;
extern vector<hyper_inhibit_t> hyper_inhibits;
extern universe_t universe;
void process_univ_objects();
void check_shift_universe();
void draw_universe_sun_flare();
void sort_uobjects();
void setup_universe_fog(s_object const &closest);
void set_current_system_light(s_object const &clobj, point const &pspos, float a_scale, float d_scale);
void destroy_sobj(s_object const &target);
bool get_gravity(s_object &result, point pos, vector3d &gravity, int offset);
void set_universe_lighting_params(bool for_universe_draw);
point get_scaled_upt();
void add_player_ship_engine_light();
#ifdef _OPENMP
int omp_get_thread_num_3dw() {return omp_get_thread_num();} // where does this belong?
#else
int omp_get_thread_num_3dw() {return 0;}
#endif
void init_universe_display() {
setup_ships();
set_perspective(PERSP_ANGLE, UNIV_NCLIP_SCALE); // that's all
check_shift_universe();
}
void set_univ_pdu() {
u_ship const &player(player_ship());
player_pdu = pos_dir_up(player.get_pos(), player.get_dir(), player.get_up(), 0.0, 0.0, U_VIEW_DIST);
}
void check_asserts() {
assert(CELL_SIZE > 2*(GALAXY_MAX_SIZE + MAX_SYSTEM_EXTENT));
assert(SYSTEM_MIN_SPACING > 2*MAX_SYSTEM_EXTENT); // galaxies/systems don't overlap
assert(INTER_PLANET_MIN_SPACING > PLANET_MAX_SIZE/*2*MAX_PLANET_EXTENT*/); // planets don't overlap
assert(INTER_MOON_MIN_SPACING > MOON_MAX_SIZE); // moons don't overlap
assert(MOON_TO_PLANET_MAX_SPACING > PLANET_MAX_SIZE + MOON_TO_PLANET_MIN_SPACING);
assert(PLANET_TO_SUN_MAX_SPACING > STAR_MAX_SIZE + PLANET_TO_SUN_MIN_SPACING);
assert(MOON_TO_PLANET_MIN_SPACING > MOON_MAX_SIZE);
assert(INTER_PLANET_MIN_SPACING > PLANET_MAX_SIZE);
assert(PLANET_TO_SUN_MIN_SPACING > STAR_MAX_SIZE + PLANET_MAX_SIZE);
}
void do_univ_init() {
static bool univ_inited(0);
if (univ_inited) return;
setup_ships(); // just in case
univ_inited = 1;
set_rand2_state(1,1);
universe.init();
check_asserts();
import_default_modmap();
}
bool player_near_system() {return (clobj0.system >= 0);}
bool player_inside_system() {
if (!player_near_system()) return 0;
ussystem const &system(clobj0.get_system());
return (dist_less_than(player_ship().pos, system.pos, system.radius));
}
void setup_current_system(float sun_intensity) { // called in ground mode
int regen_mesh(0);
static bool last_is_lava(0);
static int inited(0);
static float last_water(-1.0);
do_univ_init();
if (!inited) {
inited = 1;
set_univ_pdu();
universe.draw_all_cells(clobj0, 1, 1, 2, 0, 1); // required to gen galaxies/systems
}
upos_point_type const &pos(get_player_pos2());
universe.get_object_closest_to_pos(clobj0, pos, 0, 4.0);
colorRGBA c1(ALPHA0), c2(ALPHA0);
float water(0.0);
atmosphere = 0.0;
vegetation = 0.0;
water_is_lava = 0;
base_cloud_color = WHITE;
base_sky_color = BACKGROUND_DAY;
if (clobj0.type == UTYPE_PLANET || clobj0.type == UTYPE_MOON) { // planet or moon
upos_point_type const &opos(clobj0.object->get_pos());
float const oradius(clobj0.object->get_radius());
if (dist_less_than(pos, opos, 10.0*oradius)) { // fairly close to the planet
if (!dist_less_than(pos, opos, 1.01*oradius)) { // not at the surface
upos_point_type const p_int(opos + (oradius + get_player_radius())*(pos - opos).get_norm());
player_ship().move_to(p_int); // move the player ship to the surface of the planet/moon
}
}
else {
// FIXME: what do we do here? still move the player ship? clear the closest planet? refuse to switch world mode? draw empty space?
}
}
if (clobj0.type == UTYPE_PLANET) { // determine atmosphere and cloud cover
uplanet const &planet(clobj0.get_planet());
atmosphere = planet.atmos;
water = planet.water;
vegetation = planet.get_vegetation();
base_cloud_color = planet.ai_color;
base_sky_color = planet.ao_color;
if (planet.lava > 0.0) {
assert(water == 0.0);
water = planet.lava;
water_is_lava = 1;
}
if (!planet.has_vegetation()) {
c1 = planet.colorA;
c2 = planet.colorB;
}
}
else if (clobj0.type == UTYPE_MOON) {
umoon const &moon(clobj0.get_moon());
atmosphere = max(0.1f, moon.atmos); // 0.1
water = moon.water; // 0.0
c1 = moon.colorA;
c2 = moon.colorB;
}
if (water == 0.0) {
if (DISABLE_WATER == 0) DISABLE_WATER = 2; // temporary disable
}
else {
if (DISABLE_WATER == 2) DISABLE_WATER = 0; // enable
}
if (water != last_water && water > 0.0 && water_enabled == NULL) { // only adjust water level if a custom water_enable mask hasn't been set
last_water = water;
float const water_level(0.5 + 0.5*(water - 0.5)), rel_wpz(get_rel_wpz());
if (fabs(water_level - rel_wpz) > 0.001) {
cout << TXT(water) << TXT(water_level) << TXT(rel_wpz) << TXT(water_h_off_rel) << TXT(water_is_lava) << endl;
change_water_level(water_level);
regen_mesh = 2; // regen texture (is this needed?)
}
}
if (water_is_lava != last_is_lava) {
last_is_lava = water_is_lava;
if (water_is_lava) {water_params.set_def_lava();} else {water_params.set_def_water();}
}
if (clobj0.type >= UTYPE_STAR) { // determine gravity
assert(clobj0.object != NULL);
base_gravity = 70.0*clobj0.object->gravity;
}
float const a_scale(0.5 + 1.0*atmosphere); // higher atmosphere = more clouds = more ambient lighting
set_current_system_light(clobj0, pos, a_scale*sun_intensity, 0.5*sun_intensity);
if (fabs(univ_temp - last_temp) > 0.1) { // update temperature
cout << "temperature: " << univ_temp << endl;
last_temp = univ_temp;
temperature = univ_temp;
regen_mesh = 1; // regen texture (is this needed?)
//init_temperature = univ_temp; // ???
}
//if (regen_mesh) *** regen mesh ***
setup_landscape_tex_colors(c1, c2);
if (regen_mesh) {
init_terrain_mesh();
clear_tiled_terrain(regen_mesh == 1);
}
}
void proc_uobjs_first_frame() {
for (unsigned i = 0; i < uobjs.size(); ++i) {
assert(uobjs[i]);
uobjs[i]->first_frame_hook();
}
}
void process_ships(int timer1) {
sort_uobjects();
if (TIMETEST) PRINT_TIME(" Sort uobjs");
add_player_ship_engine_light();
update_blasts();
if (TIMETEST) PRINT_TIME(" Process BRs");
process_univ_objects();
if (TIMETEST) PRINT_TIME(" Proc Univ Objs");
apply_explosions();
if (TIMETEST) PRINT_TIME(" Explosion");
u_ship &ps(player_ship());
//camera_origin = get_player_pos2();
bool burning(ps.is_burning()), damaged(ps.was_damaged());
ps.clear_damaged();
if (!ps.is_ok()) {
damaged = 1; // adds a red filter to the player's view while dying
destroy_player_ship(0);
}
if (damaged) {
if (camera_shake == 0.0) camera_shake = 1.0;
add_camera_filter(colorRGBA(1.0, 0.0, 0.0, 0.04), 5, -1, CAM_FILT_DAMAGE);
}
if (burning) {
float const tratio(ps.get_temp()/ps.specs().max_t);
if (tratio > 1.2) {add_camera_filter(colorRGBA(1.0, 0.0, 0.0, min(0.5, max(0.1, 0.1*tratio))), 3, -1, CAM_FILT_BURN);} // NOISE_TEX
}
}
void draw_universe_all(bool static_only, bool skip_closest, bool no_move, int no_distant, bool gen_only, bool no_asteroid_dust) {
set_universe_lighting_params(1); // for universe drawing
universe.get_object_closest_to_pos(clobj0, get_player_pos2(), 0, 4.0);
if (!static_only) {setup_universe_fog(clobj0);}
check_gl_error(120);
universe.draw_all_cells(clobj0, skip_closest, no_move, no_distant, gen_only, no_asteroid_dust);
check_gl_error(121);
}
void draw_universe(bool static_only, bool skip_closest, bool no_move, int no_distant, bool gen_only, bool no_asteroid_dust) { // should be process_universe()
RESET_TIME;
static int inited(0), first_frame_drawn(0);
do_univ_init();
if (!inited) {static_only = 0;} // force full universe init the first time
create_univ_cube_map();
if (!static_only && fire_key) {
fire_key = 0;
player_ship().try_fire_weapon(); // must be before process_univ_objects(), on master thread, since this can destroy objects and free VBOs
}
// clobj0 will not be set - need to draw cells before there are any sobjs
#ifdef _OPENMP
// disable multiple threads when the player is away from the starting galaxy center to avoid crashing when allocating/freeing galaxies, systems, and clusters
bool const near_init_galaxy(dist_less_than(get_player_pos2(), universe_origin, GALAXY_MIN_SIZE));
if (inited && !static_only && NUM_THREADS > 1 && !(display_mode & 0x40) && near_init_galaxy) {
// is this legal when a query object that tries to access a planet/moon/star through clobj as the uobject is being deleted?
#pragma omp parallel num_threads(2)
{
if (omp_get_thread_num_3dw() == 1) {process_ships(timer1);}
else {draw_universe_all(static_only, skip_closest, no_move, no_distant, gen_only, no_asteroid_dust);} // *must* be done by master thread
}
}
else
#endif
{
if (!static_only) {process_ships(timer1);}
draw_universe_all(static_only, skip_closest, no_move, no_distant, gen_only, no_asteroid_dust);
}
if (!gen_only && !first_frame_drawn) {
proc_uobjs_first_frame();
first_frame_drawn = 1;
}
if (!gen_only && !static_only) {
if (TIMETEST) PRINT_TIME(" Universe Draw");
check_gl_error(122);
draw_univ_objects(); // draw free objects
check_gl_error(123);
if (TIMETEST) PRINT_TIME(" Free Obj Draw");
}
check_shift_universe();
disable_light(get_universe_ambient_light(1)); // for universe draw
enable_light(0);
draw_universe_sun_flare(); // looks a bit odd, maybe it should be changed
inited = 1;
if (TIMETEST) PRINT_TIME(" Final Universe");
}
void proc_collision(free_obj *const uobj, upos_point_type const &cpos, point const &coll_pos, float radius, vector3d const &velocity, float mass, float elastic, int coll_tid) {
assert(mass > 0.0);
uobj->set_sobj_coll_tid(coll_tid);
uobj->move_to(cpos); // setup correct position for explode?
uobj->collision(coll_pos, velocity, S_BODY_DENSITY*mass, radius, NULL, elastic); // large mass
uobj->move_to(cpos); // more accurate since this takes into account the terrain
}
void process_univ_objects() {
vector<free_obj const*> stat_obj_query_res;
for (unsigned i = 0; i < uobjs.size(); ++i) { // can we use cached_objs?
free_obj *const uobj(uobjs[i]);
bool const no_coll(uobj->no_coll()), particle(uobj->is_particle()), projectile(uobj->is_proj());
if (no_coll && particle) continue; // no collisions, gravity, or temperature on this object
if (uobj->is_stationary()) continue;
bool const is_ship(uobj->is_ship()), orbiting(uobj->is_orbiting());
bool const calc_gravity(((uobj->get_time() + unsigned(size_t(uobj)>>8)) & (GRAV_CHECK_MOD-1)) == 0);
bool const lod_coll(PLAYER_SLOW_PLANET_APPROACH && is_ship && uobj->is_player_ship()); // enable if we want to do close planet flyby
float const radius(uobj->get_c_radius()*(no_coll ? 0.5 : 1.0));
upos_point_type const &obj_pos(uobj->get_pos());
vector3d gravity(zero_vector); // sum of gravity from sun, planets, possibly some moons, and possibly asteroids
point sun_pos(all_zeros);
// skip orbiting objects (no collisions or gravity effects, temperature is mostly constant)
s_object clobj; // closest object
bool const include_asteroids(!particle); // disable particle-asteroid collisions because they're too slow
int const found_close(orbiting ? 0 : universe.get_object_closest_to_pos(clobj, obj_pos, include_asteroids, 1.0, (no_coll ? 0.0 : radius)));
bool temp_known(0), has_rings(0);
float limit_speed_dist(clobj.dist);
if (found_close) {
if (clobj.type == UTYPE_ASTEROID) {
uasteroid const &asteroid(clobj.get_asteroid());
float const dist_to_cobj(clobj.dist - (asteroid.radius + radius));
uobj->set_sobj_dist(dist_to_cobj);
if (dist_to_cobj < 0.0) { // possible collision
upos_point_type norm(obj_pos, asteroid.pos);
vector3d const &ascale(asteroid.get_scale());
double const dist(norm.mag());
if (dist > TOLERANCE) {norm /= dist;} else {norm = plus_z;} // normalize
double const a_radius(asteroid.radius*(norm*upos_point_type(ascale)).mag()), rsum(a_radius + radius);
if (dist < rsum) {
// TODO: detailed collision?
if (projectile) {} // projectile explosions damage the asteroid (reduce its radius? what if it's instanced?)
float const elastic((lod_coll ? 0.1 : 1.0)*SBODY_COLL_ELASTIC);
upos_point_type const cpos(asteroid.pos + norm*min(rsum, 1.1*dist)); // move away from the asteroid, but limit the distance to smooth the response
proc_collision(uobj, cpos, asteroid.pos, asteroid.radius, asteroid.get_velocity(), 1.0, elastic, asteroid.get_fragment_tid(obj_pos));
if (is_ship && clobj.asteroid_field == AST_BELT_ID) { // ship collision with asteroid belt
//clobj.get_asteroid_belt().detach_asteroid(clobj.asteroid); // incomplete
}
}
}
}
else {
assert(clobj.object != NULL);
float const clobj_radius(clobj.object->get_radius());
point const clobj_pos(clobj.object->get_pos());
float const temperature(universe.get_point_temperature(clobj, obj_pos, sun_pos)*(FOBJ_TEMP_SCALE - uobj->get_shadow_val())); // shadow_val = 0-3
uobj->set_temp(temperature, sun_pos);
temp_known = 1;
float hmap_scale(0.0);
if (clobj.type == UTYPE_MOON ) {hmap_scale = MOON_HMAP_SCALE; }
if (clobj.type == UTYPE_PLANET) {hmap_scale = PLANET_HMAP_SCALE;}
float dist_to_cobj(clobj.dist - (hmap_scale*clobj_radius + radius)); // (1.0 + hmap_scale)*radius?
if (dist_to_cobj > 0.0 && is_ship && clobj.has_valid_system()) {
ussystem const &system(clobj.get_system());
if (system.asteroid_belt) {
// check distance to system asteroid fields (planet asteroid fields should be close enough to the planet already)
dist_to_cobj = min(dist_to_cobj, system.asteroid_belt->get_dist_to_boundary(obj_pos));
}
}
uobj->set_sobj_dist(dist_to_cobj);
if (clobj.type == UTYPE_PLANET || clobj.type == UTYPE_MOON) {
int coll(0);
if (dist_to_cobj < 0.0) { // collision (except for stars)
float coll_r;
upos_point_type cpos;
coll = 1;
// player_ship and possibly other ships need the more stable but less accurate algorithm
bool const simple_coll(!is_ship && !projectile);
float const radius_coll(lod_coll ? 1.25*NEAR_CLIP_SCALED : radius);
float const elastic((lod_coll ? 0.1 : 1.0)*SBODY_COLL_ELASTIC);
if (clobj.object->collision(obj_pos, radius_coll, uobj->get_velocity(), cpos, coll_r, simple_coll)) {
proc_collision(uobj, cpos, clobj_pos, coll_r, zero_vector, clobj.object->mass, elastic, clobj.object->get_fragment_tid(obj_pos));
coll = 2;
}
} // collision
if (is_ship) {uobj->near_sobj(clobj, coll);}
} // planet or moon
if (calc_gravity) {get_gravity(clobj, obj_pos, gravity, 1);}
if (clobj.type == UTYPE_PLANET) {
// when near a planet with rings, use the dist to the outer rings to limit speed so that we don't fly through the rings too quickly
uplanet const &planet(clobj.get_planet());
has_rings = (planet.ring_ro > 0.0);
if (has_rings) {limit_speed_dist = clobj.dist - (planet.ring_ro - planet.radius);} // can be negative
}
}
} // found_close
if (!temp_known) {
float temperature(0.0);
if (!particle && !projectile) {temperature = universe.get_point_temperature(clobj, obj_pos, sun_pos)*FOBJ_TEMP_SCALE;}
uobj->set_temp(temperature, sun_pos);
}
if (calc_gravity) {
bool near_b_hole(0);
vector3d swp_accel(zero_vector);
if (!stat_objs.empty()) {
all_query_data qdata(&stat_objs, obj_pos, 10.0, urm_static, uobj, stat_obj_query_res);
get_all_close_objects(qdata);
for (unsigned j = 0; j < stat_obj_query_res.size(); ++j) { // asteroid/black hole gravity
near_b_hole |= (stat_obj_query_res[j]->get_gravity(gravity, obj_pos) == 2);
}
}
if (clobj.has_valid_system()) {
swp_accel = clobj.get_star().get_solar_wind_accel(obj_pos, uobj->get_mass(), uobj->get_surf_area());
}
uobj->add_gravity_swp(gravity, swp_accel, float(GRAV_CHECK_MOD), near_b_hole);
}
if (is_ship) {
for (unsigned t = 0; t < temp_sources.size(); ++t) { // check for temperature of weapons - inefficient
temp_source const &ts(temp_sources[t]);
if (ts.source == uobj) continue; // no self damage
float const dist_sq(p2p_dist_sq(obj_pos, ts.pos)), rval(ts.radius + radius);
if (dist_sq > rval*rval) continue;
assert(ts.radius > TOLERANCE);
float const temp(ts.temp*min(1.0f, (rval - sqrt(dist_sq))/ts.radius)*min(1.0, 0.5*max(1.0f, ts.radius/radius)));
if (temp > uobj->get_temp()) {
uobj->set_temp(temp, ts.pos, ts.source); // source should be valid (and should register as an attacker)
}
} // for t
if (!orbiting) {
float const speed_factor(uobj->get_max_sf()); // SLOW_SPEED_FACTOR = 0.04, FAST_SPEED_FACTOR = 1.0
float speed_factor2(1.0);
if (clobj.val > 0) {
float min_sf(0.25*SLOW_SPEED_FACTOR);
if (lod_coll && (clobj.type == UTYPE_PLANET || clobj.type == UTYPE_MOON)) {
assert(clobj.object != nullptr);
if (dot_product_ptv(upos_point_type(uobj->get_velocity()), obj_pos, clobj.object->get_pos()) < 0.0) {min_sf = (has_rings ? 0.0025 : 0.001);} // only on approach
}
speed_factor2 = max(min_sf, min(1.0f, 0.7f*limit_speed_dist)); // clip to [0.01, 1.0]
}
if (min(speed_factor, speed_factor2) > SLOW_SPEED_FACTOR) { // faster than slow speed
for (auto h = hyper_inhibits.begin(); h != hyper_inhibits.end(); ++h) {
float const dist_sq(p2p_dist_sq(obj_pos, h->pos));
if (dist_sq > h->radius*h->radius) continue; // too far away to take effect
if (uobj == h->parent) continue; // don't inhibit self
if (h->parent->is_related(uobj)) continue; // don't inhibit our own fighters or parent
//if (h->parent->is_enemy(uobj)) continue; // should we only inhibit enemies?
//uobj->register_attacker(h->parent); // no attacker registration (yet)
float const val(sqrt(dist_sq)/h->radius), val2(val*val); // 0.0 - 1.0
min_eq(speed_factor2, ((1.0f - val2)*SLOW_SPEED_FACTOR + val2*speed_factor));
// WRITE
} // for h
}
uobj->set_speed_factor(min(speed_factor, speed_factor2));
}
}
} // for i
claim_planet = 0; // unset the flag - should have been used by this point
}
void reset_player_universe() {
change_speed_mode(do_run);
update_cpos();
reset_player_ship();
if (MOVE_PLAYER_RPOS) return; // ???
uxyz[0] = uxyz[1] = uxyz[2] = 0;
shift_univ_objs(get_scaled_upt(), 0);
universe.init();
}
void check_shift_universe() {
static bool had_init_shift(0);
if (no_shift_universe && had_init_shift) return;
point camera(get_player_pos2());
vector3d move(zero_vector);
bool moved(0);
for (unsigned d = 0; d < 3; ++d) { // max move distance is CELL_SIZE
int sh[3] = {0, 0, 0};
for (int sign = -1; sign <= 1; sign += 2) {
while ((camera[d] < sign*CELL_SIZEo2) ^ (sign > 0)) {
camera[d] -= sign*CELL_SIZE;
move[d] -= sign*CELL_SIZE;
uxyz[d] += sign;
sh[d] = sign;
moved = 1;
universe.shift_cells(sh[0], sh[1], sh[2]);
}
}
}
if (moved) {shift_univ_objs(move, 1);} // advance all free objects by a cell
had_init_shift = 1;
}
void fire_planet_killer(u_ship const *const ship, point const &ship_pos, vector3d const &fire_dir, float fire_range, int obj_types) {
point coll; // unused
s_object target;
// test for collisions with solid stellar objects
line_query_state lqs;
bool const sobj_coll((obj_types & OBJ_TYPE_UOBJ) ? universe.get_trajectory_collisions(lqs, target, coll, fire_dir, ship_pos, fire_range, 0.0) : 0);
bool sobjc(sobj_coll && target.is_solid());
// test for other ship collisions
int const itype(obj_types & ~OBJ_TYPE_UOBJ);
free_obj *fobj = NULL;
uobject const *obj = NULL;
float f_dist(0.0);
if (itype != 0) {
line_int_data li_data(ship_pos, fire_dir, fire_range, ship, NULL, 0, 0);
li_data.even_ncoll = 1;
obj = line_intersect_objects(li_data, fobj, itype);
f_dist = li_data.dist;
}
bool fobjc(obj != NULL);
if (sobjc && fobjc) { // two collisions - determine which collision was first
if (f_dist < target.dist) sobjc = 0; else fobjc = 0;
}
if (fobjc) { // hit a ship or projectile before the stellar object
assert(fobj != NULL);
vector3d const hit_pos(ship_pos, fobj->get_pos());
fobj->damage(1.0E6, DAMAGE_DESTROY, hit_pos.get_norm(), ship, UWEAP_DESTROY);
}
if (sobjc) { // destroy a stellar object
if (!ship->invalid() && (target.type == UTYPE_PLANET || target.type == UTYPE_MOON)) {
int const owner(target.get_world().owner);
if (owner != NO_OWNER) register_attack_from(ship, owner); // team takes offense
}
destroy_sobj(target);
}
}
// test for intersections with solid stellar objects
bool universe_intersection_test(line_query_state &lqs, point const &pos, vector3d const &dir, float range, bool include_asteroids) {
point coll; // unused
s_object target;
return (universe.get_trajectory_collisions(lqs, target, coll, dir, pos, range, 0.0, include_asteroids) && target.is_solid());
}
bool universe_ray_intersect(point const &start, point const &end, int obj_types, uobject const *cur=NULL, free_obj const *ignore=NULL) {
vector3d const dir((end - start).get_norm());
float const range(p2p_dist(start, end));
//if ((obj_types & OBJ_TYPE_UOBJ) && universe_intersection_test(lqs, start, dir, range)) return 1;
line_int_data li_data(start, dir, range, cur, ignore, 0, 0); // not sure how cur and ignore are used, or if it makes sense to pass them in here
li_data.even_ncoll = 1;
free_obj *fobj = NULL; // unused
uobject const *const uobj(line_intersect_objects(li_data, fobj, obj_types));
//if (uobj != NULL && uobj != cur && uobj != ignore) {cout << "intersects " << uobj->get_name() << endl;}
return (uobj != NULL && uobj != cur && uobj != ignore);
}
void draw_universe_sun_flare() {
if (clobj0.type < UTYPE_SYSTEM) return; // no star
ustar const &sun(clobj0.get_star());
if (!sun.is_ok()) return;
point const offset(clobj0.get_ucell().rel_center);
if (!univ_sphere_vis((sun.pos + offset), sun.radius)) return;
float intensity(1.0);
u_ship const &ps(player_ship());
point const viewer(ps.get_pos());
if (universe_ray_intersect(viewer, sun.pos, OBJ_TYPE_ALL, &sun, &ps)) { // center not visible, do detailed ray query
unsigned const npts = 32;
static point pts[npts];
static bool pts_valid(0);
unsigned nvis(0);
for (unsigned i = 0; i < npts; ++i) {
if (!pts_valid) {pts[i] = signed_rand_vector_norm();}
point const pos(sun.pos + pts[i]*(1.1*sun.radius)); // move slightly away from the sun so that it doesn't intersect
if (univ_sphere_vis((pos + offset), 0.0) && !universe_ray_intersect(viewer, pos, OBJ_TYPE_ALL, &sun, &ps)) {++nvis;}
}
pts_valid = 1;
if (nvis == 0) return;
intensity = 0.1 + 0.9*min(1.0, 2.0*nvis/float(npts)); // intensity starts to fall off when > 50% occluded
}
point const gv(make_pt_global(viewer));
DoFlares(gv, (gv + ps.get_dir()), make_pt_global(sun.pos), 0.01, 0.02, 0.5*intensity, 4); // draw only some flares with half intensity
}
void send_warning_message(string const &msg, bool no_duplicate) {
static int last_warning_tfticks(0);
static string last_msg;
if (no_duplicate) {
if (msg == last_msg) return;
last_msg = msg;
}
if ((tfticks - last_warning_tfticks) > 5.0*TICKS_PER_SECOND) {
print_text_onscreen(msg.c_str(), RED, 1.0, 1.0*TICKS_PER_SECOND, 1);
gen_sound(SOUND_ALERT, get_player_pos2(), 0.75);
last_warning_tfticks = tfticks;
}
}
void disable_player_ship() {
if (player_ship().is_resetting()) return; // already destroyed
print_text_onscreen("Ship Disabled", RED, 1.2, 2*TICKS_PER_SECOND, 2);
add_camera_filter(colorRGBA(1.0, 0.5, 0.0, 0.1), 8, -1, CAM_FILT_DAMAGE);
}
void destroy_player_ship(bool captured) {
if (player_ship().is_resetting()) return; // already destroyed
do_run = 0;
string msg(captured ? "Ship Captured" : "Ship Destroyed");
if (!player_killer.empty()) {msg += " by " + player_killer;}
print_text_onscreen(msg, RED, 1.2, 2*TICKS_PER_SECOND, 2);
add_camera_filter(colorRGBA(1.0, 0.0, 0.0, 0.6), 8, -1, CAM_FILT_DAMAGE);
if (!captured) gen_sound(SOUND_EXPLODE, get_player_pos2());
player_ship().reset_after(TICKS_PER_SECOND);
}
point get_universe_display_camera_pos() {
point const camera(get_player_pos2()), camera_scaled(camera/CELL_SIZE);
return point(uxyz[0]+camera_scaled.x, uxyz[1]+camera_scaled.y, uxyz[2]+camera_scaled.z);
}
void draw_universe_stats() {
char text[128];
float const aspect_ratio((float)window_width/(float)window_height);
u_ship const &ps(player_ship());
// draw position and orientation
point const cpos(get_universe_display_camera_pos());
vector3d const dir(ps.get_dir().get_norm());
float const player_temp(player_ship().get_true_temp());
//sprintf(text, "Loc: (%i: %3.3f, %i: %3.3f, %i: %3.3f) Dir: (%1.3f, %1.3f, %1.3f)", uxyz[0], camera_scaled.x, uxyz[1], camera_scaled.y, uxyz[2], camera_scaled.z, dir.x, dir.y, dir.z);
sprintf(text, "Loc: (%3.4f, %3.4f, %3.4f) Dir: (%1.3f, %1.3f, %1.3f) T: %3.1f", cpos.x, cpos.y, cpos.z, dir.x, dir.y, dir.z, player_temp);
draw_text(YELLOW, -0.009*aspect_ratio, -0.014, -0.028, text);
// draw shields, armor, weapon status, etc.
int const shields(int(100.0*ps.get_shields()/ps.get_max_shields()));
int const armor(int(100.0*ps.get_armor()/ps.get_max_armor()));
if (ps.need_ammo()) {
int const ammo(ps.get_ammo());
sprintf(text, "Shields %d Armor %d Ammo %d %s", shields, armor, ammo, ((ps.get_wnum() > 0) ? "" : "[No Weapon]"));
}
else {
sprintf(text, "Shields %d Armor %d", shields, armor);
}
draw_text(RED, -0.006*aspect_ratio, -0.011, -0.02, text);
draw_health_bar(armor, shields);
// draw credits, kills, total kills
sprintf(text, "Credits: %u", (ps.ncredits + team_credits[ALIGN_PLAYER]));
draw_text(WHITE, 0.0086*aspect_ratio, 0.0135, -0.025, text);
sprintf(text, "Kills: %u/%u", ps.get_num_kills(), ps.get_tot_kills());
draw_text(WHITE, 0.0086*aspect_ratio, 0.0126, -0.025, text);
sprintf(text, "Deaths: %u", ps.get_num_deaths());
draw_text(WHITE, 0.0086*aspect_ratio, 0.0117, -0.025, text);
// draw message text (user typed)
if (!user_text.empty()) {draw_text(WHITE, -0.010*aspect_ratio, 0.010, -0.02, user_text);} // x and z are scaled
}
void exec_universe_text(string const &text) {
if (text.empty()) return;
if (text[0] != '.') return; // not a command
if (text == ".self-destruct") {
player_killer = "Self Destruct";
destroy_player_ship(0);
return;
}
// *** WRITE ***
}
bool rename_obj(uobject *obj, unsigned alignment) { // a little difficult to use, have to enter the text first then click on an object
assert(obj);
int const owner(obj->get_owner());
if (/*owner != NO_OWNER &&*/ owner != (int)alignment) return 0;
if (user_text.empty()) return 0;
if (obj->rename(user_text)) {
string const str(string("Renaming ") + obj->get_name() + " to " + user_text);
print_text_onscreen(str, PURPLE, 0.8, TICKS_PER_SECOND);
return 1;
}
return 0;
}
bool sphere_intersect_uobject(point const &pos, float radius, bool include_asteroids) {
s_object result;
if (!universe.get_closest_object(result, pos, UTYPE_MOON, include_asteroids, 1, 1.0, 0, 1.0, radius)) return 0;
if (!result.object || !result.object->is_ok()) return 0; // can this happen?
if (!result.object->sphere_intersection(pos, radius)) return 0;
//cout << "intersects with " << result.object->get_name() << endl;
return 1;
}
bool get_closest_object(point const &pos, s_object &result, int obj_type, bool include_asteroids, bool get_destroyed=0) {
if (!universe.get_closest_object(result, pos, obj_type, include_asteroids, 1, 4.0, get_destroyed)) return 0;
if (result.type != obj_type) return 0; // incorrect type
if (obj_type <= UTYPE_SYSTEM) return 1; // no object to check
return (result.object != NULL && (get_destroyed || result.object->is_ok()));
}
uobject const *get_closest_world_ptr(point const &pos, int type) {
s_object result;
return (get_closest_object(pos, result, type, 0) ? result.object : NULL);
}
float urev_body::get_land_value(unsigned align, point const &cur_pos, float sradius) const {
float owner_val(0.5), value(2.0*rand_float());
if (!is_owned()) { // not yet owned
unsigned const res_c(sclasses[USC_COLONY].cost + max(sclasses[USC_DEFSAT].cost, sclasses[USC_ANTI_MISS].cost));
owner_val = ((team_credits[align] >= res_c) ? 2.5 : 0.75);
}
else if ((int)align == owner || align == ALIGN_PIRATE) { // owned by self or pirate
owner_val = 1.0;
}
else if (TEAM_ALIGNED(align) && TEAM_ALIGNED(owner)) { // owned by enemy team
if (resource_counts[align] > 100) {owner_val = 2.0;} // only target enemy planets once we have an economy going
if (have_excess_credits(align)) { // excess_credits case - more aggressively go after enemy colonies
value += 0.5*get_wealthy_value(align)*GALAXY_MIN_SIZE;
}
else {
float tot_resources(0.0);
for (unsigned i = 0; i < NUM_ALIGNMENT; ++i) {tot_resources += resource_counts[i];}
if (tot_resources > 100.0 && resource_counts[align] > 0.5*tot_resources) {value += 0.25*GALAXY_MIN_SIZE;} // dominant resources case - more aggressive
}
}
value += 2.0*liveable(); // bonus for colonies that can build ships
value += (0.1*resources + 0.5)*owner_val;
value += sradius/PLANET_TO_SUN_MAX_SPACING; // large system bonus
value -= 0.5*p2p_dist(cur_pos, pos);
return value;
}
bool line_intersect_sun(point const &p1, point const &p2, ussystem const &system, float halo) {
// avoid choosing a destination that requires flying through a star
return (system.sun.is_ok() && line_sphere_intersect(p1, p2, (point)system.sun.pos, halo*system.sun.radius));
}
uobject const *choose_dest_world(point const &pos, int exclude_id, unsigned align, float tmax) {
// use starting pos rather than ship pos for selecting the closest galaxy, which should return the home galaxy even when the ship is near the edge of an adjacent galaxy
point const galaxy_query_pos(universe_origin);
float const g_expand(CELL_SIZE/GALAXY_MIN_SIZE); // Note: can be in more than one galaxy, but should be OK
s_object result;
if (!universe.get_closest_object(result, galaxy_query_pos, UTYPE_GALAXY, 0, 1, 4.0, 0, g_expand) || result.type < UTYPE_GALAXY) return nullptr; // choose closest galaxy
ugalaxy const &galaxy(result.get_galaxy());
unsigned const nsystems(galaxy.sols.size());
if (nsystems == 0) return nullptr; // no systems generated (can this happen?)
uobject const *dest = NULL;
unsigned const nqueries(8), max_queries(nsystems/2);
float max_pvalue(0.0);
static rand_gen_t rgen;
for (unsigned n = 0, ngood = 0; (ngood < nqueries && n < max_queries); ++n) {
unsigned const six(rgen.rand()%nsystems);
ussystem const &system(galaxy.sols[six]); // chose a random system
float const sradius(system.get_radius());
bool sol_good(0);
for (auto p = system.planets.begin(); p != system.planets.end(); ++p) {
if (p->colonizable() && p->get_id() != exclude_id && p->temp < tmax) { // planet is acceptable
float const pvalue(p->get_land_value(align, pos, sradius));
sol_good = 1;
if ((max_pvalue == 0.0 || pvalue > max_pvalue) && !line_intersect_sun(pos, p->pos, system, 2.0)) { // best planet/moon so far
max_pvalue = pvalue;
dest = &(*p); // choose this planet as a candidate
}
}
for (auto m = p->moons.begin(); m != p->moons.end(); ++m) {
if (!m->colonizable() || m->get_id() == exclude_id || m->temp >= tmax) continue;
float const mvalue(m->get_land_value(align, pos, sradius));
sol_good = 1;
if ((max_pvalue == 0.0 || mvalue > max_pvalue) && !line_intersect_sun(pos, m->pos, system, 2.0)) { // best planet/moon so far
max_pvalue = mvalue;
dest = &(*m); // choose this moon as a candidate
}
} // for m
} // for p
if (sol_good) {++ngood;}
} // for n
//if (dest) {cout << "align: " << align << ", name: " << dest->get_name() << ", value: " << max_pvalue << endl;}
if (!dest) {cout << "no dest" << endl;} // testing
return dest;
}
inline string get_owner_name(unsigned owner) {
assert(owner < NUM_ALIGNMENT);
return align_names[owner];
}
bool check_dest_ownership(int uobj_id, point const &pos, free_obj *own, bool check_for_land, bool homeworld) {
assert(uobj_id >= 0 && own); // obj may be an invalid pointer though
unsigned const owner(own->get_align());
s_object result;
int const world_types[2] = {UTYPE_PLANET, UTYPE_MOON};
for (unsigned t = 0; t < 2; ++t) {
if (!get_closest_object(pos, result, world_types[t], 0)) continue;
if (result.object->get_id() != uobj_id) continue;
urev_body &world(result.get_world());
if (world.is_owned() || !world.colonizable()) continue;
if (check_for_land && !world.can_land_at(pos)) continue; // currently player only
if (PRINT_OWNERSHIP) {cout << world.get_name() << " is claimed by " << get_owner_name(owner) << "." << endl;}
float defend(world.resources);
bool owned(0);
while (defend > 8.0) {
if (add_orbiting_ship(USC_DEFSAT, 0, 0, 0, own, result) == NULL) break; // try to put a defense satellite in orbit
owned = 1;
defend -= 12.0;
}
if (defend > 0.0 || !owned) {
owned |= (add_orbiting_ship(USC_ANTI_MISS, 0, 0, 0, own, result) != NULL); // try to put an anti-missile drone in orbit
}
float const rsc_val(world.resources - (homeworld ? 0.0 : 20.0));
if (rsc_val > 0.0 && t == 0) { // planets only - colonies (first homeworld only?)
unsigned const colony_types[5] = {USC_COLONY, USC_ARMED_COL, USC_HW_COL, USC_STARPORT, USC_HW_SPORT};
unsigned start_val(0);
for (start_val = 0; start_val < 4 && world.resources > 10.0f*(start_val+1); ++start_val) {}
if (start_val == 3 || (start_val == 2 && (rand()&1))) {++start_val;}
orbiting_ship const *oship(NULL);
for (int i = start_val; i >= 0; --i) {
oship = add_orbiting_ship(colony_types[i], 0, 1, 1, own, result); // put a colony on world
if (oship != NULL) {
upos_point_type const dir((own->pos - oship->pos).get_norm());
own->move_to(oship->get_pos() + dir*(1.1*((double)own->get_c_radius() + oship->get_c_radius()))); // move away from object
break;
}
}
owned |= (oship != NULL);
}
if (owned) {world.set_owner(result, owner);} // only if inhabitable?
else {assert(!world.is_owned());}
return 1;
}
return 0;
}
// ************ UOBJ_SOLID/UREV_BODY ************
bool uobj_solid::collision(upos_point_type const &p, float rad, vector3d const &v, upos_point_type &cpos, float &coll_r, bool simple) const { // maybe should be in universe.cpp
coll_r = radius;
if (!surface_test(rad, p, coll_r, simple)) return 0;
upos_point_type const norm(p, pos);
double const rsum((double)coll_r + (double)rad), nmag(norm.mag());
if (nmag > rsum) return 0;
double const vmag(v.mag());
if (nmag > TOLERANCE && (simple || vmag < TOLERANCE || dot_product(v, norm) > -0.1*(nmag*vmag))) { // use for shallow coll angles
cpos = pos + norm*(rsum/nmag); // normalize, multiply, and add
}
else {
get_sphere_mov_sphere_int_pt(pos, p, v, rsum, cpos);
if (nmag > TOLERANCE) {cpos += norm*(0.05*rad/nmag);} // slight adjustment to improve stability
}
return 1;
}
void urev_body::get_owner_info(ostringstream &oss, bool show_uninhabited) const {
if (is_owned()) {
assert(unsigned(owner) < NUM_ALIGNMENT);
oss << endl << "Owned by " << get_owner_name(owner);
}
else if (show_uninhabited) {oss << endl << "Uninhabited";}
}
void urev_body::set_owner(s_object const &sobj, int owner_) {
set_owner_int(owner_);
sobj.set_owner(owner_);
}
void urev_body::set_owner_int(int owner_) {
if (owner == owner_) return; // already set
unset_owner();
if (owner_ != NO_OWNER) {
assert(resources > 0.0);
assert(owner_ < NUM_ALIGNMENT);
++owner_counts[owner_];
resource_counts[owner_] += resources;
}
owner = owner_; // may overwrite an old value
}
void urev_body::unset_owner() {
if (is_owned()) {
assert(unsigned(owner) < NUM_ALIGNMENT);
assert(owner_counts[owner] > 0); // testing
--owner_counts[owner]; // shouldn't go negative even if read from a modmap
assert(resources > 0.0);
assert(resource_counts[owner] >= resources-1.0); // testing (account for fp error)
resource_counts[owner] -= resources;
resources = max(0.0f, resources); // in case it's slightly negative due to fp error
owner = NO_OWNER;
}
}