forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cpp
3404 lines (3128 loc) · 93.1 KB
/
map.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
# include "map.h"
#include "lightmap.h"
#include "output.h"
#include "rng.h"
#include "game.h"
#include "line.h"
#include "options.h"
#include "mapbuffer.h"
#include <cmath>
#include <stdlib.h>
#include <fstream>
#include "debug.h"
#define SGN(a) (((a)<0) ? -1 : 1)
#define INBOUNDS(x, y) \
(x >= 0 && x < SEEX * my_MAPSIZE && y >= 0 && y < SEEY * my_MAPSIZE)
#define dbg(x) dout((DebugLevel)(x),D_MAP) << __FILE__ << ":" << __LINE__ << ": "
enum astar_list {
ASL_NONE,
ASL_OPEN,
ASL_CLOSED
};
map::map()
{
nulter = t_null;
nultrap = tr_null;
if (is_tiny())
my_MAPSIZE = 2;
else
my_MAPSIZE = MAPSIZE;
dbg(D_INFO) << "map::map(): my_MAPSIZE: " << my_MAPSIZE;
veh_in_active_range = true;
}
map::map(std::vector<itype*> *itptr, std::vector<itype_id> (*miptr)[num_itloc],
std::vector<trap*> *trptr)
{
nulter = t_null;
nultrap = tr_null;
itypes = itptr;
mapitems = miptr;
traps = trptr;
if (is_tiny())
my_MAPSIZE = 2;
else
my_MAPSIZE = MAPSIZE;
for (int n = 0; n < my_MAPSIZE * my_MAPSIZE; n++)
grid[n] = NULL;
dbg(D_INFO) << "map::map( itptr["<<itptr<<"], miptr["<<miptr<<"], trptr["<<trptr<<"] ): my_MAPSIZE: " << my_MAPSIZE;
veh_in_active_range = true;
memset(veh_exists_at, 0, sizeof(veh_exists_at));
}
map::~map()
{
}
VehicleList map::get_vehicles(){
return get_vehicles(0,0,SEEX*my_MAPSIZE, SEEY*my_MAPSIZE);
}
VehicleList map::get_vehicles(const int sx, const int sy, const int ex, const int ey)
{
const int chunk_sx = (sx / SEEX) - 1;
const int chunk_ex = (ex / SEEX) + 1;
const int chunk_sy = (sy / SEEY) - 1;
const int chunk_ey = (ey / SEEY) + 1;
VehicleList vehs;
for(int cx = chunk_sx; cx <= chunk_ex; ++cx) {
for(int cy = chunk_sy; cy <= chunk_ey; ++cy) {
const int nonant = cx + cy * my_MAPSIZE;
if (nonant < 0 || nonant >= my_MAPSIZE * my_MAPSIZE)
continue; // out of grid
for(int i = 0; i < grid[nonant]->vehicles.size(); ++i) {
wrapped_vehicle w;
w.v = grid[nonant]->vehicles[i];
w.x = w.v->posx + cx * SEEX;
w.y = w.v->posy + cy * SEEY;
w.i = cx;
w.j = cy;
vehs.push_back(w);
}
}
}
return vehs;
}
vehicle* map::veh_at(const int x, const int y, int &part_num)
{
// This function is called A LOT. Move as much out of here as possible.
if (!veh_in_active_range || !inbounds(x, y))
return NULL; // Out-of-bounds - null vehicle
if(!veh_exists_at[x][y])
return NULL; // cache cache indicates no vehicle. This should optimize a great deal.
std::pair<int,int> point(x,y);
std::map< std::pair<int,int>, std::pair<vehicle*,int> >::iterator it;
if ((it = veh_cached_parts.find(point)) != veh_cached_parts.end())
{
part_num = it->second.second;
return it->second.first;
}
debugmsg ("vehicle part cache cache indacated vehicle not found :/");
return NULL;
}
vehicle* map::veh_at(const int x, const int y)
{
int part = 0;
vehicle *veh = veh_at(x, y, part);
return veh;
}
void map::reset_vehicle_cache()
{
clear_vehicle_cache();
// Cache all vehicles
veh_in_active_range = false;
for( std::set<vehicle*>::iterator veh = vehicle_list.begin(),
it_end = vehicle_list.end(); veh != it_end; ++veh ) {
update_vehicle_cache(*veh, true);
}
}
void map::update_vehicle_cache(vehicle * veh, const bool brand_new)
{
veh_in_active_range = true;
if(!brand_new){
// Existing must be cleared
std::map< std::pair<int,int>, std::pair<vehicle*,int> >::iterator it =
veh_cached_parts.begin(), end = veh_cached_parts.end(), tmp;
while( it != end ) {
if( it->second.first == veh ) {
int x = it->first.first;
int y = it->first.second;
if ((x > 0) && (y > 0) &&
x < SEEX*MAPSIZE &&
y < SEEY*MAPSIZE){
veh_exists_at[x][y] = false;
}
tmp = it;
++it;
veh_cached_parts.erase( tmp );
}else
++it;
}
}
// Get parts
std::vector<vehicle_part> & parts = veh->parts;
const int gx = veh->global_x();
const int gy = veh->global_y();
int partid = 0;
for( std::vector<vehicle_part>::iterator it = parts.begin(),
end = parts.end(); it != end; ++it, ++partid ) {
const int px = gx + it->precalc_dx[0];
const int py = gy + it->precalc_dy[0];
veh_cached_parts.insert( std::make_pair( std::make_pair(px,py),
std::make_pair(veh,partid) ));
if ((px > 0) && (py > 0) &&
px < SEEX*MAPSIZE &&
py < SEEY*MAPSIZE){
veh_exists_at[px][py] = true;
}
}
}
void map::clear_vehicle_cache()
{
std::map< std::pair<int,int>, std::pair<vehicle*,int> >::iterator part;
while( veh_cached_parts.size() ) {
part = veh_cached_parts.begin();
int x = part->first.first;
int y = part->first.second;
if ((x > 0) && (y > 0) &&
x < SEEX*MAPSIZE &&
y < SEEY*MAPSIZE){
veh_exists_at[x][y] = false;
}
veh_cached_parts.erase(part);
}
}
void map::update_vehicle_list(const int to) {
// Update vehicle data
for( std::vector<vehicle*>::iterator it = grid[to]->vehicles.begin(),
end = grid[to]->vehicles.end(); it != end; ++it ) {
vehicle_list.insert(*it);
}
}
void map::board_vehicle(game *g, int x, int y, player *p)
{
if (!p) {
debugmsg ("map::board_vehicle: null player");
return;
}
int part = 0;
vehicle *veh = veh_at(x, y, part);
if (!veh) {
debugmsg ("map::board_vehicle: vehicle not found");
return;
}
const int seat_part = veh->part_with_feature (part, vpf_seat);
if (part < 0) {
debugmsg ("map::board_vehicle: boarding %s (not seat)",
veh->part_info(part).name);
return;
}
if (veh->parts[seat_part].has_flag(vehicle_part::passenger_flag)) {
player *psg = veh->get_passenger (seat_part);
debugmsg ("map::board_vehicle: passenger (%s) is already there",
psg ? psg->name.c_str() : "<null>");
return;
}
veh->parts[seat_part].set_flag(vehicle_part::passenger_flag);
veh->parts[seat_part].passenger_id = p->id; // Player is 0
p->posx = x;
p->posy = y;
p->in_vehicle = true;
if (p == &g->u &&
(x < SEEX * int(my_MAPSIZE / 2) || y < SEEY * int(my_MAPSIZE / 2) ||
x >= SEEX * (1 + int(my_MAPSIZE / 2)) ||
y >= SEEY * (1 + int(my_MAPSIZE / 2)) ))
g->update_map(x, y);
}
void map::unboard_vehicle(game *g, const int x, const int y)
{
int part = 0;
vehicle *veh = veh_at(x, y, part);
if (!veh) {
debugmsg ("map::unboard_vehicle: vehicle not found");
return;
}
const int seat_part = veh->part_with_feature (part, vpf_seat, false);
if (part < 0) {
debugmsg ("map::unboard_vehicle: unboarding %s (not seat)",
veh->part_info(part).name);
return;
}
player *psg = veh->get_passenger(seat_part);
if (!psg) {
debugmsg ("map::unboard_vehicle: passenger not found");
return;
}
psg->in_vehicle = false;
psg->driving_recoil = 0;
veh->parts[seat_part].remove_flag(vehicle_part::passenger_flag);
veh->skidding = true;
}
void map::destroy_vehicle (vehicle *veh)
{
if (!veh) {
debugmsg("map::destroy_vehicle was passed NULL");
return;
}
const int sm = veh->smx + veh->smy * my_MAPSIZE;
for (int i = 0; i < grid[sm]->vehicles.size(); i++) {
if (grid[sm]->vehicles[i] == veh) {
vehicle_list.erase(veh);
reset_vehicle_cache();
grid[sm]->vehicles.erase (grid[sm]->vehicles.begin() + i);
return;
}
}
debugmsg ("destroy_vehicle can't find it! sm=%d", sm);
}
bool map::displace_vehicle (game *g, int &x, int &y, const int dx, const int dy, bool test=false)
{
const int x2 = x + dx;
const int y2 = y + dy;
int srcx = x;
int srcy = y;
int dstx = x2;
int dsty = y2;
if (!inbounds(srcx, srcy)){
debugmsg ("map::displace_vehicle: coords out of bounds %d,%d->%d,%d",
srcx, srcy, dstx, dsty);
return false;
}
const int src_na = int(srcx / SEEX) + int(srcy / SEEY) * my_MAPSIZE;
srcx %= SEEX;
srcy %= SEEY;
const int dst_na = int(dstx / SEEX) + int(dsty / SEEY) * my_MAPSIZE;
dstx %= SEEX;
dsty %= SEEY;
if (test)
return src_na != dst_na;
// first, let's find our position in current vehicles vector
int our_i = -1;
for (int i = 0; i < grid[src_na]->vehicles.size(); i++) {
if (grid[src_na]->vehicles[i]->posx == srcx &&
grid[src_na]->vehicles[i]->posy == srcy) {
our_i = i;
break;
}
}
if (our_i < 0) {
debugmsg ("displace_vehicle our_i=%d", our_i);
return false;
}
// move the vehicle
vehicle *veh = grid[src_na]->vehicles[our_i];
// don't let it go off grid
if (!inbounds(x2, y2)){
veh->stop();
debugmsg ("stopping vehicle, displaced dx=%d, dy=%d", dx,dy);
return false;
}
// record every passenger inside
std::vector<int> psg_parts = veh->boarded_parts();
std::vector<player *> psgs;
for (int p = 0; p < psg_parts.size(); p++)
psgs.push_back (veh->get_passenger (psg_parts[p]));
const int rec = abs(veh->velocity) / 5 / 100;
bool need_update = false;
int upd_x, upd_y;
// move passengers
for (int i = 0; i < psg_parts.size(); i++) {
player *psg = psgs[i];
const int p = psg_parts[i];
if (!psg) {
debugmsg ("empty passenger part %d pcoord=%d,%d u=%d,%d?", p,
veh->global_x() + veh->parts[p].precalc_dx[0],
veh->global_y() + veh->parts[p].precalc_dy[0],
g->u.posx, g->u.posy);
continue;
}
int trec = rec -psgs[i]->skillLevel("driving");
if (trec < 0) trec = 0;
// add recoil
psg->driving_recoil = rec;
// displace passenger taking in account vehicle movement (dx, dy)
// and turning: precalc_dx/dy [0] contains previous frame direction,
// and precalc_dx/dy[1] should contain next direction
psg->posx += dx + veh->parts[p].precalc_dx[1] - veh->parts[p].precalc_dx[0];
psg->posy += dy + veh->parts[p].precalc_dy[1] - veh->parts[p].precalc_dy[0];
if (psg == &g->u) { // if passenger is you, we need to update the map
need_update = true;
upd_x = psg->posx;
upd_y = psg->posy;
}
}
for (int p = 0; p < veh->parts.size(); p++) {
veh->parts[p].precalc_dx[0] = veh->parts[p].precalc_dx[1];
veh->parts[p].precalc_dy[0] = veh->parts[p].precalc_dy[1];
}
veh->posx = dstx;
veh->posy = dsty;
if (src_na != dst_na) {
vehicle * veh1 = veh;
veh1->smx = int(x2 / SEEX);
veh1->smy = int(y2 / SEEY);
grid[dst_na]->vehicles.push_back (veh1);
grid[src_na]->vehicles.erase (grid[src_na]->vehicles.begin() + our_i);
}
x += dx;
y += dy;
update_vehicle_cache(veh);
bool was_update = false;
if (need_update &&
(upd_x < SEEX * int(my_MAPSIZE / 2) || upd_y < SEEY *int(my_MAPSIZE / 2) ||
upd_x >= SEEX * (1+int(my_MAPSIZE / 2)) ||
upd_y >= SEEY * (1+int(my_MAPSIZE / 2)) )) {
// map will shift, so adjust vehicle coords we've been passed
if (upd_x < SEEX * int(my_MAPSIZE / 2))
x += SEEX;
else if (upd_x >= SEEX * (1+int(my_MAPSIZE / 2)))
x -= SEEX;
if (upd_y < SEEY * int(my_MAPSIZE / 2))
y += SEEY;
else if (upd_y >= SEEY * (1+int(my_MAPSIZE / 2)))
y -= SEEY;
g->update_map(upd_x, upd_y);
was_update = true;
}
return (src_na != dst_na) || was_update;
}
void map::vehmove(game *g)
{
// give vehicles movement points
{
VehicleList vehs = g->m.get_vehicles();
for(int v = 0; v < vehs.size(); ++v) {
vehicle* veh = vehs[v].v;
veh->gain_moves (abs (veh->velocity));
}
}
int count = 0;
while(vehproceed(g)){
count++;// lots of movement stuff. maybe 10 is low for collisions.
if (count > 10)
break;
}
}
// find veh with the most amt of turn remaining, and move it a bit.
// proposal:
// move it at most, a tenth of a turn, and at least one square.
bool map::vehproceed(game* g){
VehicleList vehs = g->m.get_vehicles();
vehicle* veh = NULL;
float max_of_turn = 0;
int x; int y;
for(int v = 0; v < vehs.size(); ++v) {
if(vehs[v].v->of_turn > max_of_turn){
veh = vehs[v].v;
x = vehs[v].x;
y = vehs[v].y;
max_of_turn = veh->of_turn;
}
}
if(!veh)
return false;
if (!inbounds(x, y)){
debugmsg ("stopping out-of-map vehicle. (x,y)=(%d,%d)",x,y);
veh->stop();
veh->of_turn = 0;
return true;
}
bool pl_ctrl = veh->player_in_control(&g->u);
// k slowdown first.
int slowdown = veh->skidding? 200 : 20; // mph lost per tile when coasting
float kslw = (0.1 + veh->k_dynamics()) / ((0.1) + veh->k_mass());
slowdown = (int) ceil(kslw * slowdown);
if (abs(slowdown) > abs(veh->velocity))
veh->stop();
else if (veh->velocity < 0)
veh->velocity += slowdown;
else
veh->velocity -= slowdown;
if (veh->velocity && abs(veh->velocity) < 20) //low enough for bicycles to go in reverse.
veh->stop();
if(veh->velocity == 0) {
veh->of_turn -= .321;
return true;
}
{ // sink in water?
int num_wheels = 0, submerged_wheels = 0;
for (int ep = 0; ep < veh->external_parts.size(); ep++) {
const int p = veh->external_parts[ep];
if (veh->part_flag(p, vpf_wheel)){
num_wheels++;
const int px = x + veh->parts[p].precalc_dx[0];
const int py = y + veh->parts[p].precalc_dy[0];
if(move_cost_ter_only(px, py) == 0) // deep water
submerged_wheels++;
}
}
// submerged wheels threshold is 2/3.
if (num_wheels && (float)submerged_wheels / num_wheels > .666){
g->add_msg ("Your %s sank.", veh->name.c_str());
if (pl_ctrl)
veh->unboard_all ();
// destroy vehicle (sank to nowhere)
destroy_vehicle(veh);
return true;
}
}
// One-tile step take some of movement
// terrain cost is 1000 on roads.
// This is stupid btw, it makes veh magically seem
// to accelerate when exiting rubble areas.
float ter_turn_cost = 500.0 * move_cost_ter_only (x,y) / abs(veh->velocity);
//can't afford it this turn?
if(ter_turn_cost >= veh->of_turn){
veh->of_turn_carry = veh->of_turn;
veh->of_turn = 0;
return true;
}
veh->of_turn -= ter_turn_cost;
// if not enough wheels, mess up the ground a bit.
if (!veh->valid_wheel_config()) {
veh->velocity += veh->velocity < 0 ? 2000 : -2000;
for (int ep = 0; ep < veh->external_parts.size(); ep++) {
const int p = veh->external_parts[ep];
const int px = x + veh->parts[p].precalc_dx[0];
const int py = y + veh->parts[p].precalc_dy[0];
ter_id &pter = ter(px, py);
if (pter == t_dirt || pter == t_grass)
pter = t_dirtmound;
}
}
if (veh->skidding){
if (one_in(4)){ // might turn uncontrollably while skidding
veh->move.init (veh->move.dir() +
(one_in(2) ? -15 * rng(1, 3) : 15 * rng(1, 3)));
}
}
else if (pl_ctrl && rng(0, 4) > g->u.skillLevel("driving") && one_in(20)) {
g->add_msg("You fumble with the %s's controls.", veh->name.c_str());
veh->turn (one_in(2) ? -15 : 15);
}
// eventually send it skidding if no control
if (!veh->boarded_parts().size() && one_in (10))
veh->skidding = true;
tileray mdir; // the direction we're moving
if (veh->skidding) // if skidding, it's the move vector
mdir = veh->move;
else if (veh->turn_dir != veh->face.dir())
mdir.init (veh->turn_dir); // driver turned vehicle, get turn_dir
else
mdir = veh->face; // not turning, keep face.dir
mdir.advance (veh->velocity < 0? -1 : 1);
const int dx = mdir.dx(); // where do we go
const int dy = mdir.dy(); // where do we go
bool can_move = true;
// calculate parts' mount points @ next turn (put them into precalc[1])
veh->precalc_mounts(1, veh->skidding ? veh->turn_dir : mdir.dir());
int imp = 0;
std::vector<veh_collision> veh_veh_colls;
if (veh->velocity == 0)
can_move = false;
// find collisions
for (int ep = 0; ep < veh->external_parts.size() && can_move; ep++) {
const int p = veh->external_parts[ep];
// coords of where part will go due to movement (dx/dy)
// and turning (precalc_dx/dy [1])
const int dsx = x + dx + veh->parts[p].precalc_dx[1];
const int dsy = y + dy + veh->parts[p].precalc_dy[1];
veh_collision coll = veh->part_collision (x, y, p, dsx, dsy);
if(coll.type == veh_coll_veh)
veh_veh_colls.push_back(coll);
else if (coll.type != veh_coll_nothing){ //run over someone?
if (can_move)
imp += coll.imp;
if (veh->velocity == 0)
can_move = false;
}
}
if(veh_veh_colls.size()){ // we have dynamic crap!
// effects of colliding with another vehicle:
// transfers of momentum, skidding,
// parts are damaged/broken on both sides,
// remaining times are normalized,
veh_collision c = veh_veh_colls[0];
vehicle* veh2 = (vehicle*) c.target;
g->add_msg("The %s's %s collides with the %s's %s",
veh->name.c_str(), veh->part_info(c.part).name,
veh2->name.c_str(), veh2->part_info(c.target_part).name);
// for reference, a cargo truck weighs ~25300, a bicycle 690,
// and 38mph is 3800 'velocity'
rl_vec2d velo_veh1 = veh->velo_vec();
rl_vec2d velo_veh2 = veh2->velo_vec();
float m1 = veh->total_mass();
float m2 = veh2->total_mass();
rl_vec2d collision_axis = (velo_veh1 - velo_veh2).normalized();
// impulse vectors
rl_vec2d imp1 = collision_axis * collision_axis.dot_product (velo_veh1) * m1;
rl_vec2d imp2 = (collision_axis) * (-collision_axis).dot_product (velo_veh2) * m2;
// finally, changes in veh velocity
// 30% is absorbed as bashing damage??
rl_vec2d delta1 = imp2 * .7 / m1;
rl_vec2d delta2 = imp1 * .7 / m2;
rl_vec2d final1 = velo_veh1 + delta1;
veh->move.init (final1.x, final1.y);
veh->velocity = final1.norm();
// shrug it off if the change is less than 8mph.
if(delta1.norm() > 800) {
veh->skidding = 1;
}
rl_vec2d final2 = velo_veh2 + delta2;
veh2->move.init(final2.x, final2.y);
veh2->velocity = final2.norm();
if(delta2.norm() > 800) {
veh2->skidding = 1;
}
//give veh2 the initiative to proceed next before veh1
float avg_of_turn = (veh2->of_turn + veh->of_turn) / 2;
if(avg_of_turn < .1)
avg_of_turn = .1;
veh->of_turn = avg_of_turn * .9;
veh2->of_turn = avg_of_turn * 1.1;
return true;
}
int coll_turn = 0;
if (imp > 0) { // imp == impulse from collisions
// debugmsg ("collision imp=%d dam=%d-%d", imp, imp/10, imp/6);
if (imp > 100)
veh->damage_all(imp / 20, imp / 10, 1);// shake veh because of collision
std::vector<int> ppl = veh->boarded_parts();
const int vel2 = imp * k_mvel * 100 / (veh->total_mass() / 8);
for (int ps = 0; ps < ppl.size(); ps++) {
player *psg = veh->get_passenger (ppl[ps]);
if (!psg) {
debugmsg ("throw passenger: empty passenger at part %d", ppl[ps]);
continue;
}
const int throw_roll = rng (vel2/100, vel2/100 * 2);
const int psblt = veh->part_with_feature (ppl[ps], vpf_seatbelt);
const int sb_bonus = psblt >= 0? veh->part_info(psblt).bonus : 0;
bool throw_from_seat = throw_roll > (psg->str_cur + sb_bonus) * 3;
std::string psgname, psgverb;
if (psg == &g->u) {
psgname = "You";
psgverb = "were";
} else {
psgname = psg->name;
psgverb = "was";
}
if (throw_from_seat) {
if (psgname.length())
g->add_msg("%s %s hurled from the %s's seat by the power of impact!",
psgname.c_str(), psgverb.c_str(), veh->name.c_str());
g->m.unboard_vehicle(g, x + veh->parts[ppl[ps]].precalc_dx[0],
y + veh->parts[ppl[ps]].precalc_dy[0]);
g->fling_player_or_monster(psg, 0, mdir.dir() + rng(0, 60) - 30,
(vel2/100 - sb_bonus < 10 ? 10 :
vel2/100 - sb_bonus));
} else if (veh->part_with_feature (ppl[ps], vpf_controls) >= 0) {
const int lose_ctrl_roll = rng (0, imp);
if (lose_ctrl_roll > psg->dex_cur * 2 + psg->skillLevel("driving") * 3) {
if (psgname.length())
g->add_msg ("%s lose%s control of the %s.", psgname.c_str(),
(psg == &g->u ? "" : "s"), veh->name.c_str());
int turn_amount = (rng (1, 3) * sqrt (vel2) / 2) / 15;
if (turn_amount < 1)
turn_amount = 1;
turn_amount *= 15;
if (turn_amount > 120)
turn_amount = 120;
//veh->skidding = true;
//veh->turn (one_in (2)? turn_amount : -turn_amount);
coll_turn = one_in (2)? turn_amount : -turn_amount;
}
}
}
}
// now we're gonna handle traps we're standing on (if we're still moving).
// this is done here before displacement because
// after displacement veh reference would be invdalid.
// damn references!
if (can_move) {
for (int ep = 0; ep < veh->external_parts.size(); ep++) {
const int p = veh->external_parts[ep];
if (veh->part_flag(p, vpf_wheel) && one_in(2))
if (displace_water (x + veh->parts[p].precalc_dx[0], y + veh->parts[p].precalc_dy[0]) && pl_ctrl)
g->add_msg ("You hear a splash!");
veh->handle_trap(x + veh->parts[p].precalc_dx[0],
y + veh->parts[p].precalc_dy[0], p);
}
}
int last_turn_dec = 1;
if (veh->last_turn < 0) {
veh->last_turn += last_turn_dec;
if (veh->last_turn > -last_turn_dec)
veh->last_turn = 0;
} else if (veh->last_turn > 0) {
veh->last_turn -= last_turn_dec;
if (veh->last_turn < last_turn_dec)
veh->last_turn = 0;
}
if (pl_ctrl && veh->velocity) {
// a bit of delay for animation
// total delay is roughly one third of a second.
int ns_per_frame = abs ( (BILLION/3) / ( (float)veh->velocity / 1000) );
if (ns_per_frame > BILLION/15)
ns_per_frame = BILLION/15;
timespec ts; // Timespec for the animation
ts.tv_sec = 0;
ts.tv_nsec = ns_per_frame;
nanosleep (&ts, 0);
}
if (can_move) {
// accept new direction
if (veh->skidding){
veh->face.init (veh->turn_dir);
veh->possibly_recover_from_skid();
}
else
veh->face = mdir;
veh->move = mdir;
if (coll_turn) {
veh->skidding = true;
veh->turn (coll_turn);
}
// accept new position
// if submap changed, we need to process grid from the beginning.
displace_vehicle (g, x, y, dx, dy);
} else { // can_move
veh->stop();
}
// redraw scene
g->draw();
return true;
}
bool map::displace_water (const int x, const int y)
{
if (move_cost_ter_only(x, y) > 0 && has_flag(swimmable, x, y)) // shallow water
{ // displace it
int dis_places = 0, sel_place = 0;
for (int pass = 0; pass < 2; pass++)
{ // we do 2 passes.
// first, count how many non-water places around
// then choose one within count and fill it with water on second pass
if (pass)
{
sel_place = rng (0, dis_places - 1);
dis_places = 0;
}
for (int tx = -1; tx <= 1; tx++)
for (int ty = -1; ty <= 1; ty++)
{
if ((!tx && !ty) || move_cost_ter_only(x + tx, y + ty) == 0)
continue;
ter_id ter0 = ter (x + tx, y + ty);
if (ter0 == t_water_sh ||
ter0 == t_water_dp)
continue;
if (pass && dis_places == sel_place)
{
ter (x + tx, y + ty) = t_water_sh;
ter (x, y) = t_dirt;
return true;
}
dis_places++;
}
}
}
return false;
}
ter_id& map::ter(const int x, const int y)
{
if (!INBOUNDS(x, y)) {
nulter = t_null;
return nulter; // Out-of-bounds - null terrain
}
/*
int nonant;
cast_to_nonant(x, y, nonant);
*/
const int nonant = int(x / SEEX) + int(y / SEEY) * my_MAPSIZE;
const int lx = x % SEEX;
const int ly = y % SEEY;
return grid[nonant]->ter[lx][ly];
}
bool map::is_indoor(const int x, const int y)
{
if (!INBOUNDS(x, y))
return false;
int iNumFloor = 0;
for (int iRow = -1; iRow <= 1; iRow++) {
for (int iCol = -1; iCol <= 1; iCol++) {
if (terlist[ter(iRow+x, iCol+y)].name == "floor" &&
terlist[ter(iRow+x, iCol+y)].flags & mfb(supports_roof)) {
iNumFloor++;
}
}
}
if (iNumFloor > 0)
return true;
return false;
}
std::string map::tername(const int x, const int y)
{
return terlist[ter(x, y)].name;
}
std::string map::features(const int x, const int y)
{
// This is used in an info window that is 46 characters wide, and is expected
// to take up one line. So, make sure it does that.
std::string ret;
if (has_flag(bashable, x, y))
ret += "Smashable. "; // 11 chars (running total)
if (has_flag(diggable, x, y))
ret += "Diggable. "; // 21 chars
if (has_flag(rough, x, y))
ret += "Rough. "; // 28 chars
if (has_flag(sharp, x, y))
ret += "Sharp. "; // 35 chars
return ret;
}
int map::move_cost(const int x, const int y)
{
int vpart = -1;
vehicle *veh = veh_at(x, y, vpart);
if (veh) { // moving past vehicle cost
const int dpart = veh->part_with_feature(vpart, vpf_obstacle);
if (dpart >= 0 &&
(!veh->part_flag(dpart, vpf_openable) || !veh->parts[dpart].open))
return 0;
else
return 8;
}
return terlist[ter(x, y)].movecost;
}
int map::move_cost_ter_only(const int x, const int y)
{
return terlist[ter(x, y)].movecost;
}
bool map::trans(const int x, const int y, char * trans_buf)
{
if(trans_buf && trans_buf[x + (y * my_MAPSIZE * SEEX)] != -1)
return trans_buf[x + (y + my_MAPSIZE * SEEX)];
// Control statement is a problem. Normally returning false on an out-of-bounds
// is how we stop rays from going on forever. Instead we'll have to include
// this check in the ray loop.
int vpart = -1;
vehicle *veh = veh_at(x, y, vpart);
bool tertr;
if (veh) {
tertr = !veh->part_flag(vpart, vpf_opaque) || veh->parts[vpart].hp <= 0;
if (!tertr) {
const int dpart = veh->part_with_feature(vpart, vpf_openable);
if (dpart >= 0 && veh->parts[dpart].open)
tertr = true; // open opaque door
}
} else
tertr = terlist[ter(x, y)].flags & mfb(transparent);
if( tertr ){
// Fields may obscure the view, too
field & f(field_at(x, y));
if(f.type == 0 || fieldlist[f.type].transparent[f.density - 1]){
if(trans_buf) trans_buf[x + (y * my_MAPSIZE * SEEX)] = 1;
return true;
}
}
if(trans_buf) trans_buf[x + (y * my_MAPSIZE * SEEX)] = 0;
return false;
}
bool map::has_flag(const t_flag flag, const int x, const int y)
{
if (flag == bashable) {
int vpart;
vehicle *veh = veh_at(x, y, vpart);
if (veh && veh->parts[vpart].hp > 0 && // if there's a vehicle part here...
veh->part_with_feature (vpart, vpf_obstacle) >= 0) {// & it is obstacle...
const int p = veh->part_with_feature (vpart, vpf_openable);
if (p < 0 || !veh->parts[p].open) // and not open door
return true;
}
}
return terlist[ter(x, y)].flags & mfb(flag);
}
bool map::has_flag_ter_only(const t_flag flag, const int x, const int y)
{
return terlist[ter(x, y)].flags & mfb(flag);
}
bool map::is_destructable(const int x, const int y)
{
return (has_flag(bashable, x, y) ||
(move_cost(x, y) == 0 && !has_flag(liquid, x, y)));
}
bool map::is_destructable_ter_only(const int x, const int y)
{
return (has_flag_ter_only(bashable, x, y) ||
(move_cost_ter_only(x, y) == 0 && !has_flag(liquid, x, y)));
}
bool map::is_outside(const int x, const int y)
{
bool out = (ter(x, y) != t_bed && ter(x, y) != t_groundsheet && ter(x, y) != t_makeshift_bed);
for(int i = -1; out && i <= 1; i++)
for(int j = -1; out && j <= 1; j++) {
const ter_id terrain = ter( x + i, y + j );
out = (terrain != t_floor && terrain != t_rock_floor && terrain != t_floor_wax &&
terrain != t_fema_groundsheet && terrain != t_dirtfloor && terrain != t_skin_groundsheet);
}
if (out) {
int vpart;
vehicle *veh = veh_at (x, y, vpart);
if (veh && veh->is_inside(vpart))
out = false;
}
return out;
}
bool map::flammable_items_at(const int x, const int y)
{
for (int i = 0; i < i_at(x, y).size(); i++) {
item *it = &(i_at(x, y)[i]);
if (it->made_of(PAPER) || it->made_of(WOOD) || it->made_of(COTTON) ||
it->made_of(POWDER) || it->made_of(VEGGY) || it->is_ammo() ||
it->type->id == itm_whiskey || it->type->id == itm_vodka ||
it->type->id == itm_rum || it->type->id == itm_tequila)
return true;
}
return false;
}
bool map::moppable_items_at(const int x, const int y)
{
for (int i = 0; i < i_at(x, y).size(); i++) {
item *it = &(i_at(x, y)[i]);
if (it->made_of(LIQUID))
return true;
}
return false;
}
point map::random_outdoor_tile()
{
std::vector<point> options;
for (int x = 0; x < SEEX * my_MAPSIZE; x++) {
for (int y = 0; y < SEEY * my_MAPSIZE; y++) {
if (is_outside(x, y))
options.push_back(point(x, y));
}
}
if (options.empty()) // Nowhere is outdoors!
return point(-1, -1);
return options[rng(0, options.size() - 1)];
}
bool map::has_adjacent_furniture(const int x, const int y)
{
for (int i = -1; i <= 1; i += 2)
for (int j = -1; j <= 1; j += 2)
switch( ter(i, j) ) {
case t_fridge:
case t_glass_fridge:
case t_dresser:
case t_rack:
case t_bookcase:
case t_locker:
return true;
}
return false;
}
void map::mop_spills(const int x, const int y) {
for (int i = 0; i < i_at(x, y).size(); i++) {
item *it = &(i_at(x, y)[i]);
if (it->made_of(LIQUID)) {
i_rem(x, y, i);
i--;
}
}
}
bool map::bash(const int x, const int y, const int str, std::string &sound, int *res)
{