-
Notifications
You must be signed in to change notification settings - Fork 60
/
boat.cpp
2032 lines (1789 loc) · 59.6 KB
/
boat.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
/** @file
*
* @par History
* - 2005/04/02 Shinigami: move_offline_mobiles - added realm param
* - 2005/08/22 Shinigami: do_tellmoves - bugfix - sometimes we've destroyed objects because of
* control scripts
* - 2009/07/23 MuadDib: updates for new Enum::Packet Out ID
* - 2009/08/25 Shinigami: STLport-5.2.1 fix: cassert removed
* STLport-5.2.1 fix: boat_components changed little bit
* - 2009/09/03 MuadDib: Changes for account related source file relocation
* Changes for multi related source file relocation
* - 2009/12/02 Turley: added 0xf3 packet - Tomi
* - 2011/11/12 Tomi: added extobj.tillerman, extobj.port_plank, extobj.starboard_plank and
* extobj.hold
* - 2011/12/13 Tomi: added support for new boats
*/
#include "boat.h"
#include <exception>
#include <string>
#include "../../bscript/berror.h"
#include "../../bscript/executor.h"
#include "../../clib/cfgelem.h"
#include "../../clib/cfgfile.h"
#include "../../clib/clib.h"
#include "../../clib/clib_endian.h"
#include "../../clib/logfacility.h"
#include "../../clib/passert.h"
#include "../../clib/stlutil.h"
#include "../../clib/streamsaver.h"
#include "../../plib/systemstate.h"
#include "../../plib/tiles.h"
#include "../../plib/uconst.h"
#include "../containr.h"
#include "../extobj.h"
#include "../fnsearch.h"
#include "../globals/object_storage.h"
#include "../globals/settings.h"
#include "../globals/uvars.h"
#include "../item/item.h"
#include "../item/itemdesc.h"
#include "../mkscrobj.h"
#include "../mobile/charactr.h"
#include "../module/uomod.h"
#include "../network/client.h"
#include "../network/packethelper.h"
#include "../network/packets.h"
#include "../network/pktdef.h"
#include "../polvar.h"
#include "../realms/realm.h"
#include "../scrsched.h"
#include "../scrstore.h"
#include "../syshookscript.h"
#include "../ufunc.h"
#include "../uobject.h"
#include "../uworld.h"
#include "boatcomp.h"
#include "multi.h"
#include "multidef.h"
namespace Pol
{
namespace Multi
{
// #define DEBUG_BOATS
std::vector<Network::Client*> boat_sent_to;
BoatShape::ComponentShape::ComponentShape( const std::string& str, unsigned char type )
{
altgraphic = 0;
objtype = get_component_objtype( type );
ISTRINGSTREAM is( str );
std::string tmp;
if ( is >> tmp )
{
graphic = static_cast<unsigned short>( strtoul( tmp.c_str(), nullptr, 0 ) );
if ( graphic )
{
unsigned short xd, yd;
if ( is >> xd >> yd )
{
xdelta = xd;
ydelta = yd;
zdelta = 0;
signed short zd;
if ( is >> zd )
zdelta = zd;
return;
}
}
}
ERROR_PRINT << "Boat component definition '" << str << "' is poorly formed.\n";
throw std::runtime_error( "Poorly formed boat.cfg component definition" );
}
BoatShape::ComponentShape::ComponentShape( const std::string& str, const std::string& altstr,
unsigned char type )
{
altgraphic = 0;
bool ok = false;
objtype = get_component_objtype( type );
ISTRINGSTREAM is( str );
std::string tmp;
if ( is >> tmp )
{
graphic = static_cast<unsigned short>( strtoul( tmp.c_str(), nullptr, 0 ) );
if ( graphic )
{
unsigned short xd, yd;
if ( is >> xd >> yd )
{
xdelta = xd;
ydelta = yd;
zdelta = 0;
signed short zd;
if ( is >> zd )
zdelta = zd;
ok = true;
}
}
}
ISTRINGSTREAM altis( altstr );
std::string alttmp;
if ( ok && altis >> alttmp )
{
altgraphic = static_cast<unsigned short>( strtoul( alttmp.c_str(), nullptr, 0 ) );
return;
}
else
ok = false;
if ( !ok )
{
ERROR_PRINT << "Boat component definition '" << str << "' is poorly formed.\n";
throw std::runtime_error( "Poorly formed boat.cfg component definition" );
}
}
BoatShape::BoatShape() {}
BoatShape::BoatShape( Clib::ConfigElem& elem )
{
std::string tmp_str;
while ( elem.remove_prop( "Tillerman", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_TILLERMAN ) );
while ( elem.remove_prop( "PortGangplankRetracted", &tmp_str ) )
Componentshapes.push_back( ComponentShape(
tmp_str, elem.remove_string( "PortGangplankExtended" ), COMPONENT_PORT_PLANK ) );
while ( elem.remove_prop( "StarboardGangplankRetracted", &tmp_str ) )
Componentshapes.push_back( ComponentShape(
tmp_str, elem.remove_string( "StarboardGangplankExtended" ), COMPONENT_STARBOARD_PLANK ) );
while ( elem.remove_prop( "Hold", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_HOLD ) );
while ( elem.remove_prop( "Rope", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_ROPE ) );
while ( elem.remove_prop( "Wheel", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_WHEEL ) );
while ( elem.remove_prop( "Hull", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_HULL ) );
while ( elem.remove_prop( "Tiller", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_TILLER ) );
while ( elem.remove_prop( "Rudder", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_RUDDER ) );
while ( elem.remove_prop( "Sails", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_SAILS ) );
while ( elem.remove_prop( "Storage", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_STORAGE ) );
while ( elem.remove_prop( "Weaponslot", &tmp_str ) )
Componentshapes.push_back( ComponentShape( tmp_str, COMPONENT_WEAPONSLOT ) );
}
bool BoatShape::objtype_is_component( unsigned int objtype )
{
if ( objtype == Core::settingsManager.extobj.tillerman )
return true;
else if ( objtype == Core::settingsManager.extobj.port_plank )
return true;
else if ( objtype == Core::settingsManager.extobj.starboard_plank )
return true;
else if ( objtype == Core::settingsManager.extobj.hold )
return true;
else if ( objtype == Core::settingsManager.extobj.rope )
return true;
else if ( objtype == Core::settingsManager.extobj.wheel )
return true;
else if ( objtype == Core::settingsManager.extobj.hull )
return true;
else if ( objtype == Core::settingsManager.extobj.tiller )
return true;
else if ( objtype == Core::settingsManager.extobj.rudder )
return true;
else if ( objtype == Core::settingsManager.extobj.sails )
return true;
else if ( objtype == Core::settingsManager.extobj.storage )
return true;
else if ( objtype == Core::settingsManager.extobj.weaponslot )
return true;
else
return false;
}
size_t BoatShape::estimateSize() const
{
return 3 * sizeof( ComponentShape* ) + Componentshapes.capacity() * sizeof( ComponentShape );
}
unsigned int get_component_objtype( unsigned char type )
{
switch ( type )
{
case COMPONENT_TILLERMAN:
return Core::settingsManager.extobj.tillerman;
case COMPONENT_PORT_PLANK:
return Core::settingsManager.extobj.port_plank;
case COMPONENT_STARBOARD_PLANK:
return Core::settingsManager.extobj.starboard_plank;
case COMPONENT_HOLD:
return Core::settingsManager.extobj.hold;
case COMPONENT_ROPE:
return Core::settingsManager.extobj.rope;
case COMPONENT_WHEEL:
return Core::settingsManager.extobj.wheel;
case COMPONENT_HULL:
return Core::settingsManager.extobj.hull;
case COMPONENT_TILLER:
return Core::settingsManager.extobj.tiller;
case COMPONENT_RUDDER:
return Core::settingsManager.extobj.rudder;
case COMPONENT_SAILS:
return Core::settingsManager.extobj.sails;
case COMPONENT_STORAGE:
return Core::settingsManager.extobj.storage;
case COMPONENT_WEAPONSLOT:
return Core::settingsManager.extobj.weaponslot;
default:
return 0;
}
}
void read_boat_cfg( void )
{
Clib::ConfigFile cf( "config/boats.cfg", "Boat" );
Clib::ConfigElem elem;
while ( cf.read( elem ) )
{
unsigned short multiid = elem.remove_ushort( "MultiID" );
try
{
Core::gamestate.boatshapes[multiid] = new BoatShape( elem );
}
catch ( std::exception& )
{
ERROR_PRINT << "Error occurred reading definition for boat 0x" << fmt::hexu( multiid )
<< "\n";
throw;
}
}
}
void clean_boatshapes()
{
Core::BoatShapes::iterator iter = Core::gamestate.boatshapes.begin();
for ( ; iter != Core::gamestate.boatshapes.end(); ++iter )
{
if ( iter->second != nullptr )
delete iter->second;
iter->second = nullptr;
}
Core::gamestate.boatshapes.clear();
}
bool BoatShapeExists( u16 multiid )
{
return Core::gamestate.boatshapes.count( multiid ) != 0;
}
void UBoat::send_smooth_move( Network::Client* client, Core::UFACING move_dir, u8 speed, u16 newx,
u16 newy, bool relative )
{
Network::PktHelper::PacketOut<Network::PktOut_F6> msg;
u16 xmod = newx - x();
u16 ymod = newy - y();
Core::UFACING b_facing = boat_facing();
if ( relative == false )
move_dir = static_cast<Core::UFACING>( ( b_facing + move_dir ) * 7 );
msg->offset += 2; // Length
msg->Write<u32>( serial_ext );
msg->Write<u8>( speed );
msg->Write<u8>( move_dir );
msg->Write<u8>( b_facing );
msg->WriteFlipped<u16>( newx );
msg->WriteFlipped<u16>( newy );
msg->WriteFlipped<u16>( ( z() < 0 ) ? static_cast<u16>( 0x10000 + z() )
: static_cast<u16>( z() ) );
// 0xf000 encoding room, huffman can take more space then the maximum of 0xffff
const u16 max_count = ( 0xf000 - 18 ) / 10;
u16 object_count = 0;
u16 len_offset = msg->offset;
msg->offset += 2; // Length
for ( auto& component : Components )
{
if ( object_count >= max_count )
{
POLLOG_INFO.Format(
"Boat 0x{:X} at ({},{},{}) with {} items is too full - truncating movement packet\n",
serial, x(), y(), z(), travellers_.size() );
break;
}
if ( component == nullptr || component->orphan() )
continue;
msg->Write<u32>( component->serial_ext );
msg->WriteFlipped<u16>( static_cast<u16>( component->x() + xmod ) );
msg->WriteFlipped<u16>( static_cast<u16>( component->y() + ymod ) );
msg->WriteFlipped<u16>( static_cast<u16>( ( component->z() < 0 ) ? ( 0x10000 + component->z() )
: ( component->z() ) ) );
++object_count;
}
for ( auto& travellerRef : travellers_ )
{
if ( object_count >= max_count )
{
POLLOG_INFO.Format(
"Boat 0x{:X} at ({},{},{}) with {} items is too full - truncating movement packet\n",
serial, x(), y(), z(), travellers_.size() );
break;
}
UObject* obj = travellerRef.get();
if ( obj->orphan() )
continue;
if ( obj->ismobile() )
{
auto* chr = static_cast<Mobile::Character*>( obj );
if ( !client->chr->is_visible_to_me( chr, /*check_range*/ false ) )
continue;
}
else
{
auto* item = static_cast<Items::Item*>( obj );
if ( item->invisible() && !client->chr->can_seeinvisitems() )
continue;
}
msg->Write<u32>( obj->serial_ext );
msg->WriteFlipped<u16>( static_cast<u16>( obj->x() + xmod ) );
msg->WriteFlipped<u16>( static_cast<u16>( obj->y() + ymod ) );
msg->WriteFlipped<u16>(
static_cast<u16>( ( obj->z() < 0 ) ? ( 0x10000 + obj->z() ) : ( obj->z() ) ) );
++object_count;
}
u16 len = msg->offset;
msg->offset = len_offset;
msg->WriteFlipped<u16>( object_count );
msg->offset = 1;
msg->WriteFlipped<u16>( len );
msg.Send( client, len );
}
void UBoat::send_smooth_move_to_inrange( Core::UFACING move_dir, u8 speed, u16 newx, u16 newy,
bool relative )
{
const Core::Pos2d newpos = Core::Pos2d( newx, newy );
Core::WorldIterator<Core::OnlinePlayerFilter>::InMaxVisualRange(
newpos, realm(),
[&]( Mobile::Character* zonechr )
{
Network::Client* client = zonechr->client;
if ( zonechr->in_visual_range( this, newpos ) && zonechr->in_visual_range( this ) &&
client->ClientType & Network::CLIENTTYPE_7090 ) // send this only to those who see the
// old location aswell
send_smooth_move( client, move_dir, speed, newx, newy, relative );
} );
}
void UBoat::send_display_boat( Network::Client* client )
{
Network::PktHelper::PacketOut<Network::PktOut_F7> msg;
msg->offset += 2; // Length
// Send_display_boat is only called for CLIENTTYPE_7090, so each 0xF3 is 26 bytes here
// 0xf000 encoding room, huffman can take more space then the maximum of 0xffff
const u16 max_count = ( 0xf000 - 5 ) / 26;
u16 object_count = 1; // Add 1 for the boat aswell
u16 len_offset = msg->offset;
msg->offset += 2; // Length
// Build boat part
msg->Write<u8>( 0xF3u );
msg->WriteFlipped<u16>( 0x1u );
msg->Write<u8>( 0x2u ); // MultiData flag
msg->Write<u32>( this->serial_ext );
msg->WriteFlipped<u16>( this->multidef().multiid );
msg->offset++; // ID offset, TODO CHECK IF NEED THESE
msg->WriteFlipped<u16>( 0x1u ); // Amount
msg->WriteFlipped<u16>( 0x1u ); // Amount
msg->WriteFlipped<u16>( this->x() );
msg->WriteFlipped<u16>( this->y() );
msg->Write<s8>( this->z() );
msg->offset++; // facing 0 for multis
msg->WriteFlipped<u16>( this->color );
msg->offset++; // flags 0 for multis
msg->offset += 2; // HSA access flags, TODO find out what these are for and implement it
for ( auto& component : Components )
{
if ( object_count >= max_count )
{
POLLOG_INFO.Format(
"Boat 0x{:X} at ({},{},{}) with {} items is too full - truncating display boat packet\n",
serial, x(), y(), z(), travellers_.size() );
break;
}
if ( component == nullptr || component->orphan() )
continue;
msg->Write<u8>( 0xF3u );
msg->WriteFlipped<u16>( 0x1u );
msg->Write<u8>( 0x0u ); // ItemData flag
msg->Write<u32>( component->serial_ext );
msg->WriteFlipped<u16>( component->graphic );
msg->offset++; // ID offset, TODO CHECK IF NEED THESE
msg->WriteFlipped<u16>( component->get_senditem_amount() ); // Amount
msg->WriteFlipped<u16>( component->get_senditem_amount() ); // Amount
msg->WriteFlipped<u16>( component->x() );
msg->WriteFlipped<u16>( component->y() );
msg->Write<s8>( component->z() );
msg->Write<u8>( component->facing );
msg->WriteFlipped<u16>( component->color );
msg->offset++; // FLAGS, no flags for components
msg->offset += 2; // HSA access flags, TODO find out what these are for and implement it
++object_count;
}
for ( auto& travellerRef : travellers_ )
{
UObject* obj = travellerRef.get();
if ( obj->orphan() )
continue;
if ( object_count >= max_count )
{
POLLOG_INFO.Format(
"Boat 0x{:X} at ({},{},{}) with {} items is too full - truncating display boat packet\n",
serial, x(), y(), z(), travellers_.size() );
break;
}
u8 flags = 0;
if ( obj->ismobile() )
{
auto* chr = static_cast<Mobile::Character*>( obj );
if ( !client->chr->is_visible_to_me( chr, /*check_range*/ false ) )
continue;
}
else
{
auto* item = static_cast<Items::Item*>( obj );
if ( item->invisible() && !client->chr->can_seeinvisitems() )
{
send_remove_object( client, item );
continue;
}
if ( client->chr->can_move( item ) )
flags |= ITEM_FLAG_FORCE_MOVABLE;
}
msg->Write<u8>( 0xF3u );
msg->WriteFlipped<u16>( 0x1u );
if ( obj->ismobile() )
msg->Write<u8>( 0x1u ); // CharData flag
else
msg->Write<u8>( 0x0u ); // ItemData flag
msg->Write<u32>( obj->serial_ext );
msg->WriteFlipped<u16>( obj->graphic );
msg->offset++; // ID offset, TODO CHECK IF NEED THESE
if ( obj->ismobile() )
{
msg->WriteFlipped<u16>( 0x1u ); // Amount
msg->WriteFlipped<u16>( 0x1u ); // Amount
}
else
{
Items::Item* item = static_cast<Items::Item*>( obj );
msg->WriteFlipped<u16>( item->get_senditem_amount() ); // Amount
msg->WriteFlipped<u16>( item->get_senditem_amount() ); // Amount
}
msg->WriteFlipped<u16>( obj->x() );
msg->WriteFlipped<u16>( obj->y() );
msg->Write<s8>( obj->z() );
msg->Write<u8>( obj->facing );
msg->WriteFlipped<u16>( obj->color );
msg->Write<u8>( flags ); // FLAGS
msg->offset += 2; // HSA access flags, TODO find out what these are for and implement it
++object_count;
}
u16 len = msg->offset;
msg->offset = len_offset;
msg->WriteFlipped<u16>( object_count );
msg->offset = 1;
msg->WriteFlipped<u16>( len );
msg.Send( client, len );
}
void UBoat::send_boat_newly_inrange( Network::Client* client )
{
Network::PktHelper::PacketOut<Network::PktOut_F3> msg;
msg->WriteFlipped<u16>( 0x1u );
msg->Write<u8>( 0x02u );
msg->Write<u32>( serial_ext );
msg->WriteFlipped<u16>( multidef().multiid );
msg->offset++; // 0;
msg->WriteFlipped<u16>( 0x1u ); // amount
msg->WriteFlipped<u16>( 0x1u ); // amount2
msg->WriteFlipped<u16>( x() );
msg->WriteFlipped<u16>( y() );
msg->Write<s8>( z() );
msg->offset++; // u8 facing
msg->WriteFlipped<u16>( color ); // u16 color
msg->offset += 3; // u8 flags + u16 HSA access flags
msg.Send( client );
for ( auto& travellerRef : travellers_ )
{
UObject* obj = travellerRef.get();
if ( !obj->orphan() )
{
if ( obj->ismobile() )
{
Mobile::Character* chr = static_cast<Mobile::Character*>( obj );
send_owncreate( client, chr );
}
else
{
Items::Item* item = static_cast<Items::Item*>( obj );
send_item( client, item );
}
}
}
for ( auto& component : Components )
{
if ( component != nullptr && !component->orphan() )
send_item( client, component.get() );
}
}
void UBoat::send_display_boat_to_inrange( u16 oldx, u16 oldy )
{
Core::WorldIterator<Core::OnlinePlayerFilter>::InMaxVisualRange(
this,
[&]( Mobile::Character* zonechr )
{
Network::Client* client = zonechr->client;
if ( !zonechr->in_visual_range( this ) )
return;
if ( client->ClientType & Network::CLIENTTYPE_7090 )
send_display_boat( client );
else if ( client->ClientType & Network::CLIENTTYPE_7000 )
send_boat( client );
else
send_boat_old( client );
} );
const Core::Pos2d oldpos = Core::Pos2d( oldx, oldy );
Core::WorldIterator<Core::OnlinePlayerFilter>::InMaxVisualRange(
oldpos, realm(),
[&]( Mobile::Character* zonechr )
{
if ( !zonechr->in_visual_range( this, oldpos ) )
return;
if ( !zonechr->in_visual_range( this ) ) // send remove to chrs only seeing the old loc
send_remove_boat( zonechr->client );
} );
}
void UBoat::send_boat( Network::Client* client )
{
// Client >= 7.0.0.0 ( SA )
Network::PktHelper::PacketOut<Network::PktOut_F3> msg2;
msg2->WriteFlipped<u16>( 0x1u );
msg2->Write<u8>( 0x02u );
msg2->Write<u32>( this->serial_ext );
msg2->WriteFlipped<u16>( this->multidef().multiid );
msg2->offset++; // 0;
msg2->WriteFlipped<u16>( 0x1u ); // amount
msg2->WriteFlipped<u16>( 0x1u ); // amount2
msg2->WriteFlipped<u16>( this->x() );
msg2->WriteFlipped<u16>( this->y() );
msg2->Write<s8>( this->z() );
msg2->offset++; // u8 facing
msg2->WriteFlipped<u16>( this->color ); // u16 color
msg2->offset++; // u8 flags
msg2.Send( client );
}
void UBoat::send_boat_old( Network::Client* client )
{
Network::PktHelper::PacketOut<Network::PktOut_1A> msg;
msg->offset += 2;
u16 b_graphic = this->multidef().multiid | 0x4000;
msg->Write<u32>( this->serial_ext );
msg->WriteFlipped<u16>( b_graphic );
msg->WriteFlipped<u16>( this->x() );
msg->WriteFlipped<u16>( this->y() );
msg->Write<s8>( this->z() );
u16 len1A = msg->offset;
msg->offset = 1;
msg->WriteFlipped<u16>( len1A );
client->pause();
msg.Send( client, len1A );
boat_sent_to.push_back( client );
}
void UBoat::send_remove_boat( Network::Client* client )
{
send_remove_object( client, this );
}
void unpause_paused()
{
for ( Network::Client* client : boat_sent_to )
{
client->restart();
}
boat_sent_to.clear();
}
UBoat::UBoat( const Items::ItemDesc& descriptor )
: UMulti( descriptor ),
tillerman( nullptr ),
portplank( nullptr ),
starboardplank( nullptr ),
hold( nullptr ),
mountpiece( nullptr )
{
passert( Core::gamestate.boatshapes.count( multiid ) != 0 );
}
UBoat* UBoat::as_boat()
{
return this;
}
/* boats are a bit strange - they can move from side to side, forwards and
backwards, without turning.
*/
void UBoat::regself()
{
const MultiDef& md = multidef();
for ( const auto& ele : md.hull )
{
Core::Pos2d hullpos = pos2d() + ele->relpos.xy();
unsigned int gh = realm()->encode_global_hull( hullpos );
realm()->global_hulls.insert( gh );
}
}
void UBoat::unregself()
{
const MultiDef& md = multidef();
for ( const auto& ele : md.hull )
{
Core::Pos2d hullpos = pos2d() + ele->relpos.xy();
unsigned int gh = realm()->encode_global_hull( hullpos );
realm()->global_hulls.erase( gh );
}
}
bool UBoat::navigable( const MultiDef& md, unsigned short x, unsigned short y, short z,
Realms::Realm* realm )
{
auto desired_pos = Core::Pos4d( x, y, Clib::clamp_convert<s8>( z ), realm );
return navigable( md, desired_pos );
}
// navigable: Can the ship sit here? ie is every point on the hull on water,and not blocked?
bool UBoat::navigable( const MultiDef& md, const Core::Pos4d& desired_pos )
{
if ( !desired_pos.can_move_to( md.minrxyz.xy() ) || !desired_pos.can_move_to( md.maxrxyz.xy() ) )
{
#ifdef DEBUG_BOATS
INFO_PRINT << "Location " << desired_pos << " impassable, location is off the map\n";
#endif
return false;
}
/* Test the external hull to make sure it's on water */
for ( const auto& ele : md.hull )
{
Core::Pos3d hullpos = desired_pos.xyz() + ele->relpos;
#ifdef DEBUG_BOATS
INFO_PRINT << "[" << hullpos << "]";
#endif
/*
* See if any other ship hulls occupy this space
*/
unsigned int gh = desired_pos.realm()->encode_global_hull( hullpos.xy() );
if ( desired_pos.realm()->global_hulls.count( gh ) ) // already a boat there
{
#ifdef DEBUG_BOATS
INFO_PRINT << "Location " << desired_pos.realm()->name() << " " << hullpos
<< " already has a ship hull present\n";
#endif
return false;
}
if ( !desired_pos.realm()->navigable( hullpos, Plib::systemstate.tile[ele->objtype].height ) )
return false;
}
return true;
}
// ugh, we've moved the ship already - but we're comparing using the character's coords
// which used to be on the ship.
// let's hope it's always illegal to stand on the edges. !!
bool UBoat::on_ship( const BoatContext& bc, const UObject* obj )
{
if ( Core::IsItem( obj->serial ) )
{
const Item* item = static_cast<const Item*>( obj );
if ( item->container != nullptr )
return false;
}
Core::Vec2d rxy = obj->pos2d() - Core::Pos2d( bc.x, bc.y );
return bc.mdef.body_contains( rxy );
}
void UBoat::move_travellers( Core::UFACING move_dir, const BoatContext& oldlocation,
unsigned short newx, unsigned short newy, Realms::Realm* oldrealm )
{
bool any_orphans = false;
for ( auto& travellerRef : travellers_ )
{
UObject* obj = travellerRef.get();
// consider: occasional sweeps of all boats to reap orphans
if ( obj->orphan() || !on_ship( oldlocation, obj ) )
{
any_orphans = true;
travellerRef.clear();
continue;
}
obj->set_dirty();
if ( obj->ismobile() )
{
Mobile::Character* chr = static_cast<Mobile::Character*>( obj );
if ( chr->logged_in() )
{
Core::Pos4d oldpos = chr->pos();
chr->lastpos = oldpos;
if ( newx != USHRT_MAX &&
newy != USHRT_MAX ) // dave added 3/27/3, if move_xy was used, dont use facing
{
s16 dx, dy;
dx = chr->x() - oldlocation.x; // keeps relative distance from boat mast
dy = chr->y() - oldlocation.y;
chr->setposition( Core::Pos4d( chr->pos() ).x( newx + dx ).y( newy + dy ) );
}
else
{
chr->setposition( chr->pos().move( move_dir ) );
}
MoveCharacterWorldPosition( oldpos, chr );
chr->position_changed();
if ( chr->client != nullptr )
{
if ( oldrealm != chr->realm() )
{
Core::send_new_subserver( chr->client );
Core::send_owncreate( chr->client, chr );
}
if ( chr->client->ClientType & Network::CLIENTTYPE_7090 )
{
Core::send_objects_newly_inrange_on_boat( chr->client, this->serial );
if ( chr->poisoned() ) // if poisoned send 0x17 for newer clients
send_poisonhealthbar( chr->client, chr );
if ( chr->invul() ) // if invul send 0x17 for newer clients
send_invulhealthbar( chr->client, chr );
}
else
{
Core::send_goxyz( chr->client, chr );
// lastpos are set above so these two calls will work right.
// FIXME these are also called, in this order, in MOVEMENT.CPP.
// should be consolidated.
Core::send_objects_newly_inrange_on_boat( chr->client, this->serial );
}
}
chr->move_reason = Mobile::Character::MULTIMOVE;
}
else
{
// characters that are logged out move with the boat
// they aren't in the worldzones so this is real easy.
chr->lastpos = chr->pos(); // I think in this case setting last? isn't
// necessary, but I'll do it anyway.
if ( newx != USHRT_MAX &&
newy != USHRT_MAX ) // dave added 3/27/3, if move_xy was used, dont use facing
{
s16 dx, dy;
dx = chr->x() - oldlocation.x; // keeps relative distance from boat mast
dy = chr->y() - oldlocation.y;
chr->setposition( Core::Pos4d( chr->pos() ).x( newx + dx ).y( newy + dy ) );
}
else
{
chr->setposition( chr->pos().move( move_dir ) );
}
}
}
else
{
Items::Item* item = static_cast<Items::Item*>( obj );
Core::Pos4d oldpos = item->pos();
if ( newx != USHRT_MAX &&
newy != USHRT_MAX ) // dave added 4/9/3, if move_xy was used, dont use facing
{
s16 dx, dy;
dx = item->x() - oldlocation.x; // keeps relative distance from boat mast
dy = item->y() - oldlocation.y;
item->set_dirty();
item->setposition( Core::Pos4d( item->pos() ).x( newx + dx ).y( newy + dy ) );
if ( Core::settingsManager.ssopt.refresh_decay_after_boat_moves )
item->restart_decay_timer();
MoveItemWorldPosition( oldpos, item );
}
else
{
item->set_dirty();
item->setposition( item->pos().move( move_dir ) );
if ( Core::settingsManager.ssopt.refresh_decay_after_boat_moves )
item->restart_decay_timer();
MoveItemWorldPosition( oldpos, item );
}
Core::WorldIterator<Core::OnlinePlayerFilter>::InMaxVisualRange(
item,
[&]( Mobile::Character* zonechr )
{
if ( !zonechr->in_visual_range( item ) )
return;
Network::Client* client = zonechr->client;
if ( !( client->ClientType & Network::CLIENTTYPE_7090 ) )
send_item( client, item );
} );
Core::WorldIterator<Core::OnlinePlayerFilter>::InMaxVisualRange(
oldpos,
[&]( Mobile::Character* zonechr )
{
if ( !zonechr->in_visual_range( item, oldpos ) )
return;
if ( !zonechr->in_visual_range(
item ) ) // not in range. If old loc was in range, send a delete.
send_remove_object( zonechr->client, item );
} );
}
}
if ( any_orphans )
remove_orphans();
}
void UBoat::turn_traveller_coords( Mobile::Character* chr, RELATIVE_DIR dir )
{
chr->lastpos = chr->pos();
s16 xd = chr->x() - x();
s16 yd = chr->y() - y();
switch ( dir )
{
case LEFT:
chr->setposition( Core::Pos4d( x() + yd, y() - xd, chr->z(), chr->realm() ) );
chr->facing = static_cast<Core::UFACING>( ( chr->facing + 6 ) & 7 );
break;
case AROUND:
chr->setposition( Core::Pos4d( x() - xd, y() - yd, chr->z(), chr->realm() ) );
chr->facing = static_cast<Core::UFACING>( ( chr->facing + 4 ) & 7 );
break;
case RIGHT:
chr->setposition( Core::Pos4d( x() - yd, y() + xd, chr->z(), chr->realm() ) );
chr->facing = static_cast<Core::UFACING>( ( chr->facing + 2 ) & 7 );
break;
case NO_TURN:
break;
}
}
void UBoat::turn_travellers( RELATIVE_DIR dir, const BoatContext& oldlocation )
{
bool any_orphans = false;
for ( auto& travellerRef : travellers_ )
{
UObject* obj = travellerRef.get();
// consider: occasional sweeps of all boats to reap orphans
if ( obj->orphan() || !on_ship( oldlocation, obj ) )
{
any_orphans = true;
travellerRef.clear();
continue;
}
obj->set_dirty();
if ( obj->ismobile() )
{
Mobile::Character* chr = static_cast<Mobile::Character*>( obj );
if ( chr->logged_in() )
{
// send_remove_character_to_nearby( chr );
Core::Pos4d oldpos = chr->pos();
turn_traveller_coords( chr, dir );
Core::MoveCharacterWorldPosition( oldpos, chr );
chr->position_changed();
if ( chr->client != nullptr )
{
if ( chr->client->ClientType & Network::CLIENTTYPE_7090 )
{
if ( chr->poisoned() ) // if poisoned send 0x17 for newer clients
send_poisonhealthbar( chr->client, chr );
if ( chr->invul() ) // if invul send 0x17 for newer clients
send_invulhealthbar( chr->client, chr );
Core::send_objects_newly_inrange_on_boat( chr->client, this->serial );
}
else
{
Core::send_goxyz( chr->client, chr );
// lastx and lasty are set above so these two calls will work right.
// FIXME these are also called, in this order, in MOVEMENT.CPP.
// should be consolidated.
Core::send_objects_newly_inrange_on_boat( chr->client, this->serial );
}
}
chr->move_reason = Mobile::Character::MULTIMOVE;
// chr->lastx = ~ (unsigned short) 0; // force tellmove() to send "owncreate" and not send
// deletes.
// chr->lasty = ~ (unsigned short) 0;
}
else
{
turn_traveller_coords( chr, dir );
}
}
else
{
Items::Item* item = static_cast<Items::Item*>( obj );
s16 xd = item->x() - x();
s16 yd = item->y() - y();