-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.h
More file actions
1969 lines (1641 loc) · 75.1 KB
/
Functions.h
File metadata and controls
1969 lines (1641 loc) · 75.1 KB
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
#pragma once
#include <functional>
#include "ECS.h"
//move order
string order_data::move::get_string() const
{
return "Destination: " + format("{:.1f}", destination.x) + " | " + format("{:.1f}", destination.y);
}
void order_data::move::clear(Entity* parent_entity_arg)
{
//TO DO
}
// find route between origin and destination systems using Dijkstra's algorithm
// returned route DOES NOT INCLUDE the origin system, but INCLUDES the destination system
vector<Entity*> find_route(Entity* origin_system, Entity* destination_system)
{
unordered_set<Entity*> Unvisited_system_ptrs;
unordered_map<Entity*, int> System_ptr_distance_map;
unordered_map<Entity*, Entity*> Previous_system_ptr; // previous system in route from origin
auto system_count = Systems.size(); // reserve memory in data structures
Unvisited_system_ptrs.reserve(system_count);
System_ptr_distance_map.reserve(system_count);
Previous_system_ptr.reserve(system_count);
for (Entity* system_entity : Systems) // place initial data in structures
{
Unvisited_system_ptrs.emplace(system_entity); // initially all systems are unvisited
System_ptr_distance_map.emplace(system_entity, INT_MAX); // initially jump distance from origin to all systems is INT_MAX
Previous_system_ptr.emplace(system_entity, nullptr); // initially all systems do not have previous system in route from origin
}
System_ptr_distance_map[origin_system] = 0; // initialize origin system distance to 0
bool stop = false; //stays false until destination system is found
while (Unvisited_system_ptrs.empty() == false and stop == false) //if destination is not yet found and there are still unvisited systems
{
// find unvisited system with the lowest distance from origin, set as current system
Entity* current_system = *std::min_element(Unvisited_system_ptrs.begin(), Unvisited_system_ptrs.end(), [&](Entity* system1, Entity* system2)
{
return System_ptr_distance_map[system1] < System_ptr_distance_map[system2];
});
for (Entity* jn_ptr : current_system->c_star_system_ptr->jump_node_entity_ptrs) // for all jump nodes in the current system
{
Entity* neighbour_system = jn_ptr->c_jump_node_ptr->destination_node_ptr->c_drawable_ptr->in_system_ptr; // find node's destination system - neighbour system
if (Unvisited_system_ptrs.find(neighbour_system) != Unvisited_system_ptrs.end()) // if neighbour system is unvisited
{
int alt_dist = System_ptr_distance_map[current_system] + 1; //determine distance if jumping from current system
if (alt_dist < System_ptr_distance_map[neighbour_system]) //if this distance lower than current value in the distance map
{
//set this distance as new value and add current system as neighbour's previous system in route from origin
System_ptr_distance_map[neighbour_system] = alt_dist;
Previous_system_ptr[neighbour_system] = current_system;
}
}
}
Unvisited_system_ptrs.erase(current_system); // erase current system from unvisited systems
if (current_system == destination_system) // if current system is the destination system, stop the loop
{
stop = true;
}
}
// loop finished, computing route..
vector<Entity*> route;
route.emplace_back(destination_system); // first place destination system in route
Entity* current_system_in_route = destination_system;
while (true) // then follow Previous_system_ptr map from destination system to origin system, placing systems in route
{
Entity* previous_system = Previous_system_ptr[current_system_in_route];
if (previous_system == origin_system)
{
break;
}
else
{
route.emplace_back(previous_system);
current_system_in_route = previous_system;
}
}
std::reverse(route.begin(), route.end()); // reverse route (we filled it backwards - from destination system to origin system)
return route;
}
void order_data::move::execute(Entity* parent_entity_arg)
{
Entity* destination_closest_system = *std::min_element(Systems.begin(), Systems.end(), [&](const Entity* system1, const Entity* system2)
{
double dist_to_system1 = point_squared_distance(destination, system1->c_drawable_ptr->sprite.getPosition());
double dist_to_system2 = point_squared_distance(destination, system2->c_drawable_ptr->sprite.getPosition());
return dist_to_system1 < dist_to_system2;
});
double distance_from_closest_system = point_distance(destination_closest_system->c_drawable_ptr->sprite.getPosition(), destination);
if (distance_from_closest_system <= destination_closest_system->c_star_system_ptr->radius) //destination not in interstellar space
{
if (destination_closest_system->get_reference().id == parent_entity_arg->c_drawable_ptr->in_system_ptr->get_reference().id) // destination in the same system
{
if (parent_entity_arg->c_jump_drive_ptr == nullptr) // if entity does not have a jump drive
{
// move sublight
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move_sublight,
order_data::move_sublight{destination});
}
else // jump drive detected
{
double distance = point_distance(destination, parent_entity_arg->c_drawable_ptr->sprite.getPosition());
if (distance <= parent_entity_arg->c_jump_drive_ptr->min_range) // too close for jumping
{
// move sublight
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move_sublight,
order_data::move_sublight{destination});
}
else if (distance > parent_entity_arg->c_jump_drive_ptr->min_range and distance < parent_entity_arg->c_jump_drive_ptr->max_range) // destination in jump range
{
// first check if we are not inside jump inhibition field
auto& jump_inhib_fields = parent_entity_arg->c_drawable_ptr->in_system_ptr->c_star_system_ptr->jump_inhibition_entity_ptrs;
const auto ji_field_source = std::find_if(jump_inhib_fields.begin(), jump_inhib_fields.end(), [&](Entity* ji_field_entity_ptr)
{
double dist = point_distance(parent_entity_arg->c_drawable_ptr->sprite.getPosition(), ji_field_entity_ptr->c_drawable_ptr->sprite.getPosition());
return (dist <= ji_field_entity_ptr->c_jump_inhibition_field_ptr->radius);
});
if (ji_field_source != jump_inhib_fields.end()) // we are inside JI field
{
Entity* ji_field_source_ptr = *ji_field_source;
if (point_in_circle(destination,
ji_field_source_ptr->c_drawable_ptr->sprite.getPosition(),
ji_field_source_ptr->c_jump_inhibition_field_ptr->radius) == false) // if destination is not inside the same JI field
{
// get JI field escape destination
Vector2d vector_from_field_center = parent_entity_arg->c_drawable_ptr->sprite.getPosition() - ji_field_source_ptr->c_drawable_ptr->sprite.getPosition();
if (vector_from_field_center.x == 0.0 and vector_from_field_center.y == 0.0)
{
// if we are in the exact center of the field, get rotation direction of parent
vector_from_field_center = angle_rad_to_vector(parent_entity_arg->c_drawable_ptr->sprite.getRotation());
}
vector_from_field_center.resize(ji_field_source_ptr->c_jump_inhibition_field_ptr->radius + 100.0);
Vector2d escape_destination = ji_field_source_ptr->c_drawable_ptr->sprite.getPosition() + vector_from_field_center;
if (point_distance(escape_destination, destination) < parent_entity_arg->c_jump_drive_ptr->min_range) // if final destination would be too close for jump after moving out of the field
{
// move directly to final destination by sublight
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move_sublight,
order_data::move_sublight(destination));
}
else // we can jump after moving out of the field
{
// move out of the field by sublight
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move_sublight,
order_data::move_sublight(escape_destination));
// then move to final destination
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move,
order_data::move(destination));
}
}
else // destination is in the same JI field
{
// move sublight
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move_sublight,
order_data::move_sublight{destination});
}
}
else // we are NOT inside JI field
{
// jump directly to final destination
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::jump_intrasystem,
order_data::jump_intrasystem{destination});
}
}
else // too far for one jump
{
// plan moving to destination in multiple submoves
Vector2d current_pos = parent_entity_arg->c_drawable_ptr->sprite.getPosition();
Vector2d jump_vector = destination - current_pos;
jump_vector.resize(parent_entity_arg->c_jump_drive_ptr->max_range * 0.98);
while (true) // while we are not yet at final destination
{
double current_distance_to_destination = point_distance(current_pos, destination);
if (current_distance_to_destination >=
parent_entity_arg->c_jump_drive_ptr->max_range) // if final destination is farther than jump drive max range, plan potential max distance jump (later resolution), continue loop
{
current_pos += jump_vector;
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move,
order_data::move{current_pos});
}
else // final destination is closer than jump drive max range, plan to move to it by single potential jump or sublight travel (later resolution), break loop
{
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move,
order_data::move{destination});
break;
}
}
}
}
}
else // destination in another system
{
// plan intersystem route
Entity* jump_node = nullptr;
for (Entity* jn : parent_entity_arg->c_drawable_ptr->in_system_ptr->c_star_system_ptr->jump_node_entity_ptrs)
{
if (jn->c_jump_node_ptr->destination_node_ptr->c_drawable_ptr->in_system_ptr->get_reference().id == destination_closest_system->get_reference().id)
{
jump_node = jn;
break;
}
}
if (jump_node != nullptr) // jump node to destination found in the current system
{
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move, // move to node
order_data::move{jump_node->c_drawable_ptr->sprite.getPosition()});
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::jump_intersystem, // jump through node
order_data::jump_intersystem{jump_node->c_jump_node_ptr->destination_node_ptr->c_drawable_ptr->in_system_ptr});
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move, // move to final destination
order_data::move{destination});
}
else // jump node to destination NOT found in the current system
{
vector<Entity*> route = find_route(parent_entity_arg->c_drawable_ptr->in_system_ptr, destination_closest_system); // find route using Dijkstra's algorithm
// starting from current system, follow the route and place orders to move to the node to the next system and jump through
Entity* current_system = parent_entity_arg->c_drawable_ptr->in_system_ptr;
for (Entity* sys : route)
{
auto& nodes = current_system->c_star_system_ptr->jump_node_entity_ptrs;
Entity* node = *std::find_if(nodes.begin(), nodes.end(), [&](Entity* nd)
{
return nd->c_jump_node_ptr->destination_node_ptr->c_drawable_ptr->in_system_ptr == sys;
});
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move, // move to node
order_data::move{node->c_drawable_ptr->sprite.getPosition()});
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::jump_intersystem, // jump through node
order_data::jump_intersystem{sys});
current_system = sys;
}
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move, // move to final destination
order_data::move{destination});
}
}
}
}
bool order_data::move::is_finished(Entity* parent_entity_arg) const
{
return (holds_alternative<std::nullptr_t>(parent_entity_arg->c_jump_drive_ptr->destination) and
parent_entity_arg->c_engines_ptr->destination.has_value() == false and
point_distance(destination, parent_entity_arg->c_drawable_ptr->sprite.getPosition()) < 10.0);
}
// jump_intrasystem order
string order_data::jump_intrasystem::get_string() const
{
return "Destination: " + format("{:.10f}", destination.x) + " | " + format("{:.10f}", destination.y);
}
void order_data::jump_intrasystem::clear(Entity* parent_entity_arg)
{
parent_entity_arg->c_jump_drive_ptr->destination = std::nullptr_t();
}
void order_data::jump_intrasystem::execute(Entity* parent_entity_arg)
{
parent_entity_arg->c_jump_drive_ptr->destination = destination;
}
bool order_data::jump_intrasystem::is_finished(Entity* parent_entity_arg) const
{
if (holds_alternative<std::nullptr_t>(parent_entity_arg->c_jump_drive_ptr->destination))
{
if (point_distance(destination, parent_entity_arg->c_drawable_ptr->sprite.getPosition()) < 10.0)
{
return true; // jump successful
}
else // jump into inhibition field
{
auto& jump_inhib_fields = parent_entity_arg->c_drawable_ptr->in_system_ptr->c_star_system_ptr->jump_inhibition_entity_ptrs;
const auto ji_field_source = std::find_if(jump_inhib_fields.begin(), jump_inhib_fields.end(), [&](Entity* ji_field_entity_ptr)
{
auto dist = point_distance(destination, ji_field_entity_ptr->c_drawable_ptr->sprite.getPosition());
return (dist <= ji_field_entity_ptr->c_jump_inhibition_field_ptr->radius);
});
if (ji_field_source != jump_inhib_fields.end())
{
Entity* ji_field_source_ptr = *ji_field_source;
auto intersection_pts_vector = line_segment_circle_overlap_points(parent_entity_arg->c_drawable_ptr->sprite.getPosition(), destination,
ji_field_source_ptr->c_drawable_ptr->sprite.getPosition(), ji_field_source_ptr->c_jump_inhibition_field_ptr->radius + 100.0);
Vector2d& partial_jump_dest = intersection_pts_vector[1];
if (point_distance(partial_jump_dest, parent_entity_arg->c_drawable_ptr->sprite.getPosition()) > parent_entity_arg->c_jump_drive_ptr->min_range)
{
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::jump_intrasystem, order_data::jump_intrasystem{partial_jump_dest});
}
parent_entity_arg->c_spacecraft_ptr->current_order->Suborders.emplace_back(Order_Type::move_sublight, order_data::move_sublight(destination));
}
return false;
}
}
else
{
return false; // jump in progresss
}
}
// jump_intersystem order
string order_data::jump_intersystem::get_string() const
{
return "Destination system: " + destination_system->c_selectable_ptr->name;
}
void order_data::jump_intersystem::clear(Entity* parent_entity_arg)
{
parent_entity_arg->c_jump_drive_ptr->destination = std::nullptr_t();
}
void order_data::jump_intersystem::execute(Entity* parent_entity_arg)
{
parent_entity_arg->c_jump_drive_ptr->destination = destination_system;
}
bool order_data::jump_intersystem::is_finished(Entity* parent_entity_arg) const
{
return (holds_alternative<std::nullptr_t>(parent_entity_arg->c_jump_drive_ptr->destination) and parent_entity_arg->c_drawable_ptr->in_system_ptr == destination_system);
}
// colonize order
string order_data::colonize::get_string() const
{
return "colonize order string";
}
void order_data::colonize::clear(Entity* parent_entity_arg)
{
print(runtime("order_data::colonize::clear" + NEWL));
}
void order_data::colonize::execute(Entity* parent_entity_arg)
{
print(runtime("order_data::colonize::execute" + NEWL));
}
bool order_data::colonize::is_finished(Entity* parent_entity_arg) const
{
print(runtime("order_data::colonize::is_finished" + NEWL));
return false;
}
// construct order
string order_data::construct::get_string() const
{
string location_str_x = format("{:.1f}", location.x);
string location_str_y = format("{:.1f}", location.y);
return "Constructing: " + entity_template + " at:" + NEWL + location_str_x + " | " + location_str_y;
}
void order_data::construct::execute(Entity* parent_entity_arg)
{
if (not construction_started)
{
if (parent_entity_arg->c_constructor_ptr != nullptr)
{
parent_entity_arg->c_constructor_ptr->Build(entity_template, location);
construction_started = true;
}
else
{
print("ERROR: Constructor component does not exist.");
}
}
}
bool order_data::construct::is_finished(Entity* parent_entity_arg) const
{
// The order is considered finished when the constructed entity is no longer under construction
if (parent_entity_arg->c_constructor_ptr->entity_under_construction_ptr != nullptr)
{
// if under_construction is false, return true (finished), if it is true, return false (not finished)
return parent_entity_arg->c_constructor_ptr->entity_under_construction_ptr->c_spacecraft_ptr->under_construction == false;
}
else // if entity_under_construction_ptr == nullptr, return true (finished)
{
return true;
}
}
void order_data::construct::clear(Entity* parent_entity_arg)
{
// Nullify the ptr to the entity under construction in the Constructor
parent_entity_arg->c_constructor_ptr->entity_under_construction_ptr = nullptr;
}
// move_sublight order
string order_data::move_sublight::get_string() const
{
return "Destination: " + format("{:.1f}", destination.x) + " | " + format("{:.1f}", destination.y);
}
void order_data::move_sublight::clear(Entity* parent_entity_arg)
{
parent_entity_arg->c_engines_ptr->Stop();
}
void order_data::move_sublight::execute(Entity* parent_entity_arg)
{
parent_entity_arg->c_engines_ptr->destination = destination;
}
bool order_data::move_sublight::is_finished(Entity* parent_entity_arg) const
{
return (parent_entity_arg->c_engines_ptr->destination.has_value() == false and
point_distance(destination, parent_entity_arg->c_drawable_ptr->sprite.getPosition()) < 10.0);
}
// attack order
string order_data::attack::get_string() const
{
return "attack order string";
}
void order_data::attack::clear(Entity* parent_entity_arg)
{
parent_entity_arg->c_weapons_ptr->Stop();
}
void order_data::attack::execute(Entity* parent_entity_arg)
{
parent_entity_arg->c_weapons_ptr->Attack_Entity(target);
}
bool order_data::attack::is_finished(Entity* parent_entity_arg) const
{
return not target.is_valid();
}
Turret::Turret(Weapon_Type weapon_type_arg, Weapon_Name weapon_name_arg, float reload_time_arg, Entity* parent_spacecraft_ptr_arg,
Vector2d position_offset_local_arg, Texture_Atlas_Subrect* sprite_texture_subrect_arg, Vector2d sprite_scaling_arg) :
weapon_type(weapon_type_arg),
weapon_name(weapon_name_arg),
reload_timer(reload_time_arg),
reload_time(reload_time_arg),
parent_spacecraft_ptr(parent_spacecraft_ptr_arg),
position_offset_local(position_offset_local_arg),
sprite(Shader_Lighting_Mode::normal_lighting, Colors::White, Draw_Layer::Effects_Higher, sprite_texture_subrect_arg, Blend_Mode::Alpha,
SpriteToWorldCoords(position_offset_local_arg, parent_spacecraft_ptr_arg->c_drawable_ptr->sprite), parent_spacecraft_ptr_arg->c_drawable_ptr->sprite.getRotation(), sprite_scaling_arg)
{}
bool Projectile::Step()
{
sprite.translate(velocity);
light.position1 = sprite.getPosition();
--lifetime;
if (lifetime <= 0)
{
return true;
}
if (target_entity.is_valid())
{
const bool target_hit = target_entity.ptr->c_collidable_ptr->point_collides(sprite.getPosition());
if (target_hit)
{
owning_system->Effects.emplace_back(Effect{sprite.getPosition(), get_random_real_in_range(0.0, 3.0), {130.0, 130.0}, Colors::White, name_animation_map["exp_1"],
Blend_Mode::Additive, Draw_Layer::Effects_Higher, {0.0, 0.0}, false, 1.5, Color{1.0f, 0.8f, 0.1f}, 800.0f});
target_entity.ptr->c_spacecraft_ptr->hull_points -= damage;
return true;
}
}
return false;
}
bool Missile::Step()
{
if (target_entity.is_valid() == false)
{
owning_system->Effects.emplace_back(Effect{sprite.getPosition(), get_random_real_in_range(0.0, 3.0), {130.0, 130.0}, Colors::White, name_animation_map["exp_1"],
Blend_Mode::Additive, Draw_Layer::Effects_Higher, {0.0, 0.0}, false, 1.5, Color{1.0f, 0.8f, 0.1f}, 800.0f});
return true;
}
Vector2d current_pos = sprite.getPosition();
Vector2d direction_to_target = target_entity.ptr->c_drawable_ptr->sprite.getPosition() - current_pos;
double angle_to_target = vector_to_angle_rad(direction_to_target);
double current_rot = sprite.getRotation();
double angle_difference_signed = angle_difference_rad(angle_to_target, current_rot);
if (abs(angle_difference_signed) <= turn_rate / 2) //rotation finished
{
sprite.setRotation(angle_to_target);
}
else //rotation not finished
{
sprite.setRotation(current_rot + clamp(angle_difference_signed, -turn_rate, turn_rate));
}
Vector2d velocity = angle_rad_to_vector(sprite.getRotation()).get_resized(speed);
sprite.setPosition(current_pos + velocity);
Vector2d engine_pos = SpriteToWorldCoords({-0.5, 0.0}, sprite);;
light.position1 = engine_pos;
int modulus = int(lifetime) % 2;
if (modulus == 0)
{
owning_system->Effects.emplace_back(Effect{engine_pos, get_random_real_in_range(0.0, 5.0), {30.0, 30.0}, Colors::White, name_animation_map["missile_trail_1"],
Blend_Mode::Alpha, Draw_Layer::Effects_Higher, velocity.get_negated() / 2.0, true, 0.0, Color{0.0f, 0.0f, 0.0f}, 0.0f});
}
--lifetime;
if (lifetime <= 0)
{
return true;
}
const bool target_hit = target_entity.ptr->c_collidable_ptr->point_collides(sprite.getPosition());
if (target_hit)
{
owning_system->Effects.emplace_back(Effect{sprite.getPosition(), get_random_real_in_range(0.0, 3.0), {130.0, 130.0}, Colors::White, name_animation_map["exp_1"],
Blend_Mode::Additive, Draw_Layer::Effects_Higher, {0.0, 0.0}, false, 1.5, Color{1.0f, 0.8f, 0.1f}, 800.0f});
target_entity.ptr->c_spacecraft_ptr->hull_points -= damage;
return true;
}
return false;
}
Beam::Beam(Entity_Ref origin_entity_arg,
Turret* origin_turret_arg,
Entity_Ref target_entity_arg,
Vector2f endpoint1_offset_arg,
Vector2f endpoint2_offset_arg,
float damage_arg,
float lifetime_arg,
Color light_diffuse_arg,
float light_intensity_arg,
Color color_arg,
Component::Star_System* owning_system_arg) :
origin_entity(origin_entity_arg),
origin_turret(origin_turret_arg),
target_entity(target_entity_arg),
endpoint1_offset(endpoint1_offset_arg),
endpoint2_offset(endpoint2_offset_arg),
current_endpoint_offset(endpoint1_offset_arg),
damage(damage_arg),
lifetime(lifetime_arg),
remaining_lifetime(lifetime_arg)
{
auto origin = SpriteToWorldCoords(origin_turret_arg->position_offset_local, origin_entity_arg.ptr->c_drawable_ptr->sprite);
auto endpoint = SpriteToWorldCoords(Vector2d(current_endpoint_offset), target_entity_arg.ptr->c_drawable_ptr->sprite);
auto long_dimension = endpoint - origin;
auto position = origin + long_dimension / 2.0;
auto rotation = vector_to_angle_rad(long_dimension);
auto scaling = Vector2d(long_dimension.get_length(), 5.0);
sprite = Sprite{Shader_Lighting_Mode::no_lighting, color_arg, Draw_Layer::Effects_Higher, name_texture_atlas_subrect_map["beam"], Blend_Mode::Additive,
position, rotation, scaling};
light = Light{false, light_diffuse_arg, light_intensity_arg, 1.5, origin, origin + long_dimension};
muzzle_glow = Sprite_Translatable_Only{Shader_Lighting_Mode::no_lighting, Colors::White, Draw_Layer::Effects_Higher, *name_animation_map["redbeamglow"], Blend_Mode::Additive,
origin, 0.0, {100.0, 100.0}};
owning_system = owning_system_arg;
}
bool Beam::Step()
{
remaining_lifetime--;
if (remaining_lifetime <= 0 or target_entity.is_valid() == false)
{
return true;
}
float beam_progress = 1.0f - (remaining_lifetime / lifetime);
current_endpoint_offset = lerp_vectors(endpoint1_offset, endpoint2_offset, beam_progress);
Vector2d origin = SpriteToWorldCoords(origin_turret->position_offset_local, origin_entity.ptr->c_drawable_ptr->sprite);
muzzle_glow.setPosition(origin);
if (muzzle_glow.graphics.index() == 1)
{
Animation& animation = get<Animation>(muzzle_glow.graphics);
animation.advance();
}
Vector2d endpoint = SpriteToWorldCoords(Vector2d(current_endpoint_offset), target_entity.ptr->c_drawable_ptr->sprite);
const bool target_hit = target_entity.ptr->c_collidable_ptr->point_collides(endpoint);
Vector2d long_dimension;
Vector2d position;
double rotation;
Vector2d scaling;
if (target_hit)
{
long_dimension = endpoint - origin;
position = origin + long_dimension / 2.0;
if (get_random_int_in_range(0, 9) < 3)
{
owning_system->Effects.emplace_back(Effect{endpoint, get_random_real_in_range(0.0, 3.0), {130.0, 130.0}, Colors::White, name_animation_map["exp_1"],
Blend_Mode::Additive, Draw_Layer::Effects_Higher, {0.0, 0.0}, false, 1.5, Color{1.0f, 0.8f, 0.1f}, 350.0f});
}
target_entity.ptr->c_spacecraft_ptr->hull_points -= damage;
}
else
{
long_dimension = (endpoint - origin)*2.0;
position = endpoint;
}
rotation = vector_to_angle_rad(long_dimension);
scaling = Vector2d(long_dimension.get_length(), 12.0);
sprite.setPosition(position);
sprite.setRotation(rotation);
sprite.setScaling(scaling);
Color outer_color = sprite.color;
sprite.setScaling(Vector2d(scaling.x, scaling.y/2.2));
sprite.color = Colors::White;
sprite.color = outer_color;
light.position1 = origin;
light.position2 = origin + long_dimension;
return false;
}
void spawn_spacecraft_grid(Vector2d position, int spawn_num)
{
vector<Vector2d> collision_polygon_texture_coords;
collision_polygon_texture_coords.reserve(41);
collision_polygon_texture_coords.emplace_back(64, 206);
collision_polygon_texture_coords.emplace_back(17, 201);
collision_polygon_texture_coords.emplace_back(1, 192);
collision_polygon_texture_coords.emplace_back(3, 105);
collision_polygon_texture_coords.emplace_back(13, 94);
collision_polygon_texture_coords.emplace_back(155, 101);
collision_polygon_texture_coords.emplace_back(166, 120);
collision_polygon_texture_coords.emplace_back(166, 175);
collision_polygon_texture_coords.emplace_back(174, 174);
collision_polygon_texture_coords.emplace_back(179, 180);
collision_polygon_texture_coords.emplace_back(180, 195);
collision_polygon_texture_coords.emplace_back(199, 195);
collision_polygon_texture_coords.emplace_back(199, 0);
collision_polygon_texture_coords.emplace_back(256, 0);
collision_polygon_texture_coords.emplace_back(255, 188);
collision_polygon_texture_coords.emplace_back(266, 196);
collision_polygon_texture_coords.emplace_back(278, 196);
collision_polygon_texture_coords.emplace_back(279, 176);
collision_polygon_texture_coords.emplace_back(293, 167);
collision_polygon_texture_coords.emplace_back(336, 173);
collision_polygon_texture_coords.emplace_back(336, 144);
collision_polygon_texture_coords.emplace_back(550, 140);
collision_polygon_texture_coords.emplace_back(549, 290); //middle
collision_polygon_texture_coords.emplace_back(336, 287);
collision_polygon_texture_coords.emplace_back(335, 264);
collision_polygon_texture_coords.emplace_back(296, 267);
collision_polygon_texture_coords.emplace_back(278, 259);
collision_polygon_texture_coords.emplace_back(279, 239);
collision_polygon_texture_coords.emplace_back(257, 241);
collision_polygon_texture_coords.emplace_back(254, 421);
collision_polygon_texture_coords.emplace_back(200, 421);
collision_polygon_texture_coords.emplace_back(198, 240);
collision_polygon_texture_coords.emplace_back(179, 239);
collision_polygon_texture_coords.emplace_back(177, 258);
collision_polygon_texture_coords.emplace_back(166, 259);
collision_polygon_texture_coords.emplace_back(166, 320);
collision_polygon_texture_coords.emplace_back(153, 332);
collision_polygon_texture_coords.emplace_back(12, 336);
collision_polygon_texture_coords.emplace_back(1, 328);
collision_polygon_texture_coords.emplace_back(1, 239);
collision_polygon_texture_coords.emplace_back(11, 233);
collision_polygon_texture_coords.emplace_back(61, 231);
for (auto& polygon : collision_polygon_texture_coords)
{
polygon.y += 8.0;
}
for (int i = 0; i < spawn_num; ++i)
{
for (int j = 0; j < spawn_num; ++j)
{
Vector2d spawn_position{position.x + 1000.0*i, position.y + 1000.0*j};
auto entity_it = Entity::Colony.emplace();
entity_it->c_drawable_ptr = &*Component::Drawable::Colony.emplace(&*entity_it,
Sprite{Shader_Lighting_Mode::normal_lighting, Colors::White, Draw_Layer::Spacecrafts, name_texture_atlas_subrect_map["faustus"],
Blend_Mode::Alpha, spawn_position, 0, Vector2d(556, 442), name_texture_atlas_subrect_map["faustus_glow"]},
Icon{Colors::Green, Draw_Layer::Spacecrafts, Blend_Mode::Additive,
name_texture_atlas_subrect_map["icon_cruiser"],
Vector2d(screen_resolution.x * 0.036, screen_resolution.y * 0.032)});
entity_it->c_collidable_ptr = &*Component::Collidable::Colony.emplace(&*entity_it,
CollisionPolygonToSpriteCoords(collision_polygon_texture_coords, {556, 442}));
entity_it->c_dynamic_ptr = &*Component::Dynamic::Colony.emplace(&*entity_it,
Vector2d{0.0, 0.0}, 0);
entity_it->c_engines_ptr = &*Component::Engines::Colony.emplace(&*entity_it,
2, 0.1, deg_to_rad(2));
entity_it->c_selectable_ptr = &*Component::Selectable::Colony.emplace(&*entity_it,
"USS Marha " + to_string(i) + to_string(j));
entity_it->c_spacecraft_ptr = &*Component::Spacecraft::Colony.emplace(&*entity_it,
300);
entity_it->c_weapons_ptr = &*Component::Weapons::Colony.emplace(&*entity_it);
entity_it->c_weapons_ptr->Turrets.emplace_back(Turret{Weapon_Type::Mass_driver, Weapon_Name::Ravager_Cannon, 30.0f,
&*entity_it, Vector2d{0.3, 0.05}, name_texture_atlas_subrect_map["faustus"], Vector2d{50.0, 50.0}});
entity_it->c_weapons_ptr->Turrets.emplace_back(Turret{Weapon_Type::Mass_driver, Weapon_Name::Ravager_Cannon, 30.0f,
&*entity_it, Vector2d{0.3, -0.05}, name_texture_atlas_subrect_map["faustus"], Vector2d{50.0, 50.0}});
entity_it->c_weapons_ptr->Turrets.emplace_back(Turret{Weapon_Type::Missile_launcher, Weapon_Name::Stinger_Missile, 30.0f,
&*entity_it, Vector2d{0.2, 0.0}, name_texture_atlas_subrect_map["faustus"], Vector2d{50.0, 50.0}});
entity_it->c_weapons_ptr->Turrets.emplace_back(Turret{Weapon_Type::Beam_emitter, Weapon_Name::Winter_King_Beam, 60.0f*8,
&*entity_it, Vector2d{-0.2, 0.0}, name_texture_atlas_subrect_map["faustus"], Vector2d{50.0, 50.0}});
entity_it->c_jump_drive_ptr = &*Component::Jump_Drive::Colony.emplace(&*entity_it,
0.0f, 20.0f / 60.0f, 1000.0, system_radius / 5.0, true);
if (i == 5 and j == 5)
{
entity_it->c_jump_inhibition_field_ptr = &*Component::Jump_Inhibition_Field::Colony.emplace(&*entity_it,
1500.0, true);
}
entity_it->c_constructor_ptr = &*Component::Constructor::Colony.emplace(&*entity_it);
}
}
}
void spawn_spacecraft()
{
spawn_spacecraft_grid(Systems[0]->c_drawable_ptr->sprite.getPosition() + Vector2d(system_radius/5, system_radius/5), 10);
}
void SpawnEntities()
{
// *** Spawn Entities *** //
print(runtime(NEWL + "Spawning entities: " + NEWL));
double gal_size = system_radius * 4.5 * sqrt(max(num_of_systems, 5));
galaxy_size.x = gal_size;
galaxy_size.y = gal_size;
spawn_systems();
spawn_celestial_bodies();
spawn_spacecraft();
vector<Vector2d> collision_polygon_local;
collision_polygon_local.reserve(41);
collision_polygon_local.emplace_back(64, 206);
collision_polygon_local.emplace_back(17, 201);
collision_polygon_local.emplace_back(1, 192);
collision_polygon_local.emplace_back(3, 105);
collision_polygon_local.emplace_back(13, 94);
collision_polygon_local.emplace_back(155, 101);
collision_polygon_local.emplace_back(166, 120);
collision_polygon_local.emplace_back(166, 175);
collision_polygon_local.emplace_back(174, 174);
collision_polygon_local.emplace_back(179, 180);
collision_polygon_local.emplace_back(180, 195);
collision_polygon_local.emplace_back(199, 195);
collision_polygon_local.emplace_back(199, 0);
collision_polygon_local.emplace_back(256, 0);
collision_polygon_local.emplace_back(255, 188);
collision_polygon_local.emplace_back(266, 196);
collision_polygon_local.emplace_back(278, 196);
collision_polygon_local.emplace_back(279, 176);
collision_polygon_local.emplace_back(293, 167);
collision_polygon_local.emplace_back(336, 173);
collision_polygon_local.emplace_back(336, 144);
collision_polygon_local.emplace_back(550, 140);
collision_polygon_local.emplace_back(549, 290);//stred
collision_polygon_local.emplace_back(336, 287);
collision_polygon_local.emplace_back(335, 264);
collision_polygon_local.emplace_back(296, 267);
collision_polygon_local.emplace_back(278, 259);
collision_polygon_local.emplace_back(279, 239);
collision_polygon_local.emplace_back(257, 241);
collision_polygon_local.emplace_back(254, 421);
collision_polygon_local.emplace_back(200, 421);
collision_polygon_local.emplace_back(198, 240);
collision_polygon_local.emplace_back(179, 239);
collision_polygon_local.emplace_back(177, 258);
collision_polygon_local.emplace_back(166, 259);
collision_polygon_local.emplace_back(166, 320);
collision_polygon_local.emplace_back(153, 332);
collision_polygon_local.emplace_back(12, 336);
collision_polygon_local.emplace_back(1, 328);
collision_polygon_local.emplace_back(1, 239);
collision_polygon_local.emplace_back(11, 233);
collision_polygon_local.emplace_back(61, 231);
}
void Initialize_game()
{
print(runtime(NEWL + "Initialize_game()" + NEWL));
// Initialize imgui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplWin32_Init(hwnd);
ImGui_ImplDX11_Init(D3D11_Device, D3D11_Device_Context);
auto& io = ImGui::GetIO();
io.IniFilename = NULL;
io.MouseDrawCursor = true;
io.Fonts->AddFontFromFileTTF("arial.ttf", float(screen_resolution.y) * 0.0156f);
LoadResources();
//Restart timers
Stopwatch_FPS_limit.restart();
Second.restart();
camera.refresh();
mouse.sprite = Sprite{Shader_Lighting_Mode::no_lighting, Color(1.0f, 1.0f, 1.0f, 1.0f), Draw_Layer::GUI_Higher, name_texture_atlas_subrect_map["marker2"], Blend_Mode::Additive,
mouse.world_position, 0, mouse.scaling};
background_sprite = Sprite{Shader_Lighting_Mode::no_lighting, Color(1.0f, 1.0f, 1.0f, 1.0f), Draw_Layer::Background_and_Universe, name_texture_atlas_subrect_map["background_milkyway_4K"], Blend_Mode::Opaque,
camera.position, 0.0, camera.scaling};
/// Faustus EntityTemplate
EntityTemplate faustus;
faustus.name = "GTSC Faustus";
faustus.collision_polygon_texture_coords.reserve(41);
faustus.collision_polygon_texture_coords.emplace_back(64, 206);
faustus.collision_polygon_texture_coords.emplace_back(17, 201);
faustus.collision_polygon_texture_coords.emplace_back(1, 192);
faustus.collision_polygon_texture_coords.emplace_back(3, 105);
faustus.collision_polygon_texture_coords.emplace_back(13, 94);
faustus.collision_polygon_texture_coords.emplace_back(155, 101);
faustus.collision_polygon_texture_coords.emplace_back(166, 120);
faustus.collision_polygon_texture_coords.emplace_back(166, 175);
faustus.collision_polygon_texture_coords.emplace_back(174, 174);
faustus.collision_polygon_texture_coords.emplace_back(179, 180);
faustus.collision_polygon_texture_coords.emplace_back(180, 195);
faustus.collision_polygon_texture_coords.emplace_back(199, 195);
faustus.collision_polygon_texture_coords.emplace_back(199, 0);
faustus.collision_polygon_texture_coords.emplace_back(256, 0);
faustus.collision_polygon_texture_coords.emplace_back(255, 188);
faustus.collision_polygon_texture_coords.emplace_back(266, 196);
faustus.collision_polygon_texture_coords.emplace_back(278, 196);
faustus.collision_polygon_texture_coords.emplace_back(279, 176);
faustus.collision_polygon_texture_coords.emplace_back(293, 167);
faustus.collision_polygon_texture_coords.emplace_back(336, 173);
faustus.collision_polygon_texture_coords.emplace_back(336, 144);
faustus.collision_polygon_texture_coords.emplace_back(550, 140);
faustus.collision_polygon_texture_coords.emplace_back(549, 290); //middle
faustus.collision_polygon_texture_coords.emplace_back(336, 287);
faustus.collision_polygon_texture_coords.emplace_back(335, 264);
faustus.collision_polygon_texture_coords.emplace_back(296, 267);
faustus.collision_polygon_texture_coords.emplace_back(278, 259);
faustus.collision_polygon_texture_coords.emplace_back(279, 239);
faustus.collision_polygon_texture_coords.emplace_back(257, 241);
faustus.collision_polygon_texture_coords.emplace_back(254, 421);
faustus.collision_polygon_texture_coords.emplace_back(200, 421);
faustus.collision_polygon_texture_coords.emplace_back(198, 240);
faustus.collision_polygon_texture_coords.emplace_back(179, 239);
faustus.collision_polygon_texture_coords.emplace_back(177, 258);
faustus.collision_polygon_texture_coords.emplace_back(166, 259);
faustus.collision_polygon_texture_coords.emplace_back(166, 320);
faustus.collision_polygon_texture_coords.emplace_back(153, 332);
faustus.collision_polygon_texture_coords.emplace_back(12, 336);
faustus.collision_polygon_texture_coords.emplace_back(1, 328);
faustus.collision_polygon_texture_coords.emplace_back(1, 239);
faustus.collision_polygon_texture_coords.emplace_back(11, 233);
faustus.collision_polygon_texture_coords.emplace_back(61, 231);
faustus.scaling = Vector2d(556, 442);
faustus.graphics = name_texture_atlas_subrect_map["faustus"];
faustus.glowmap_subrect_ptr = name_texture_atlas_subrect_map["faustus_glow"];
faustus.crew = 300;
faustus.max_hull_points = 1000.0f;
Spacecraft_Component_Definition::Jump_Drive jd;
jd.charge = 0.0f;
jd.charge_rate = 20.0f / 60.0f;
jd.min_range = 1000.0;
jd.max_range = system_radius / 5.0;
jd.intersystem = true;
faustus.jump_drive = jd;
Spacecraft_Component_Definition::Engines engines;
engines.max_speed = 2.0;
engines.acceleration = 0.1;
engines.max_angular_velocity = deg_to_rad(2);
faustus.engines = engines;
Spacecraft_Component_Definition::Jump_Inhibition_Field ji_field;
ji_field.radius = 1500.0;
faustus.ji_field = ji_field;
Spacecraft_Component_Definition::Constructor constructor;
constructor.construction_speed_multiplier = 1.0f;
faustus.constructor = constructor;
TurretTemplate turret1;
turret1.name = "turret1MD";
turret1.weapon_name_arg = Weapon_Name::Ravager_Cannon;
turret1.weapon_type_arg = Weapon_Type::Mass_driver;
turret1.position_offset_local_arg = Vector2d{0.3, 0.05};
TurretTemplate turret2;
turret2.name = "turret2MD";
turret2.weapon_name_arg = Weapon_Name::Ravager_Cannon;
turret2.weapon_type_arg = Weapon_Type::Mass_driver;
turret2.position_offset_local_arg = Vector2d{0.3, -0.05};
TurretTemplate turret3;
turret3.name = "turret3ML";
turret3.weapon_name_arg = Weapon_Name::Stinger_Missile;
turret3.weapon_type_arg = Weapon_Type::Missile_launcher;
turret3.position_offset_local_arg = Vector2d{0.2, 0.0};
TurretTemplate turret4;
turret4.name = "turret4BE";
turret4.weapon_name_arg = Weapon_Name::Winter_King_Beam;
turret4.weapon_type_arg = Weapon_Type::Beam_emitter;