-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.cpp
More file actions
4352 lines (3716 loc) · 90.4 KB
/
console.cpp
File metadata and controls
4352 lines (3716 loc) · 90.4 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
/*
console.cpp - Use chat interface as a command console for single player.
Jason Hood, 20 October to 22 December, 2009.
Credits go to the FLHook team for making some of this easier. Particular
thanks to M0tah for help in making the ship jump around.
Requires and assumes v1.1.
v1.10, 23 February, 2010:
- did not undo after "system object", causing CTD during a jump;
- setting reputation to non-existing faction would still use friendly color;
+ added "ghost" and "godmode" commands - toggle collision and invincibility;
+ "rep = ..." will save the current reputation, "rep undo" will load it;
* use two decimals for rotation values and remove trailing zeros;
* allow reputation to be specified as a percentage.
v1.20, 14 December, 2010 to 9 January, 2011:
* handle non-bay animation separately;
- fixed rep command when setting reputation via short name;
- fixed crash when a target was set, but no object exists;
* allow "base ." to revisit the current (or last) base;
- restore original cruise constants when switching to multiplayer;
* use Console chat channel;
* update launch icon and ship cushion on Drive/Park;
+ enhanced line editing (for MP, too);
+ pos can set/recall multiple positions, use clipboard;
* improved "s ." from a base (uses proper exit point).
v1.21, 13 & 14 January, 2011:
- fixed history search with no matching command;
- fixed ship cushion under certain conditions.
v1.22, 3 May, 2011:
- preserve disabled collisions on launch/jump when ghost is off;
- set EntryPoint_Org when needed (potential Moors conflict).
v1.23, 6 October, 2013:
+ added "play sound" command to play a sound effect (DATA\AUDIO\sounds.ini);
* only use IME (the flashing 'A') if using DBCS.
v1.24, 7 May, 2016:
- don't crash if an animation isn't run twice;
* allow animations to run only forwards or backwards (ignored for bay doors).
*/
#include "internal.h"
#include "DALib.h"
#include "Server.h"
#include "BinaryRDL.h"
#include "resources.h"
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define PI 3.14159265f
#define NAKED __declspec(naked)
#define STDCALL __stdcall
#define FASTCALL __fastcall
// Get chat working in single-player.
#define ADDR_ENTER ((PBYTE) 0x46a11f+1)
#define ADDR_ENTER1 ((PBYTE) 0x437374+1)
#define ADDR_ENTER2 ((PBYTE) 0x54ae67+1)
#define ADDR_ENTER3 ((PBYTE) 0x574b91+1)
#define ADDR_ENTER4 ((PBYTE) 0x574c43+1)
#define ADDR_ENTER5 ((PBYTE) 0x574f74+1)
#define ADDR_MSGWIN ((PBYTE) 0x46b650)
#define ADDR_CHAT ((PBYTE) 0x5a625f+1)
#define ADDR_RTURN ((PDWORD) 0x45f828)
#define ADDR_UP ((PDWORD) 0x45f830)
#define ADDR_DOWN ((PDWORD) 0x45f834)
#define ADDR_BKSP ((PDWORD) 0x57ce00)
#define ADDR_END ((PDWORD) 0x57ce10)
#define ADDR_HOME ((PDWORD) 0x57ce14)
#define ADDR_LEFT ((PDWORD) 0x57ce18)
#define ADDR_RIGHT ((PDWORD) 0x57ce20)
#define ADDR_DEL ((PDWORD) 0x57ce24)
// Add this DLL to [Resources].
#define ADDR_RSRC ((PDWORD)(0x5b1caa+1))
// Multiple ships.
#define ADDR_STARTUP ((PDWORD) 0x5c6cb8)
#define ADDR_LOAD ((PDWORD)(0x5ab230+1))
#define ADDR_SHIPID ((PBYTE) 0x4c3e15)
#define ADDR_EQUIPCOMM ((PBYTE) 0x437fb4+1)
#define ADDR_NAVBAR ((PBYTE) 0x43f664)
#define ADDR_CUSH0 ((PDWORD)(0x44eb73+1))
#define ADDR_CUSH1 ((PDWORD)(0x44f01f+2))
#define ADDR_CUSH1a ((PBYTE) 0x44f072)
#define ADDR_CUSH2 ((PBYTE) 0x44f985)
#define ADDR_COMMICON ((PBYTE) 0x4781fc-1)
#define ADDR_PLAYERCASH ((PBYTE) 0x47842c+1)
#define ADDR_MAXQUANT ((PBYTE) 0x47c83f)
#define ADDR_DEALERCNT1 ((PBYTE) 0x47cb0a)
#define ADDR_DEALERCNT2 ((PBYTE) 0x47cb1d)
#define ADDR_MAXQUANT1 ((PBYTE) 0x47e15c)
#define ADDR_PRICE0 ((PBYTE) 0x47e4c0+1)
#define ADDR_XFERBUY ((PBYTE) 0x47eb35)
#define ADDR_FREEAMMO ((PUSHORT)0x47ed04)
#define ADDR_XFERSELL ((PBYTE) 0x47ef26)
#define ADDR_TIP ((PBYTE) 0x48028b-1)
#define ADDR_MAXQUANT2 ((PBYTE) 0x480337)
#define ADDR_MAXQUANT3 ((PDWORD)(0x48092b+2))
#define ADDR_DEALER ((PINT)( 0x480d40+1))
#define ADDR_LINE ((PBYTE) 0x48136b+1)
#define ADDR_TITLE ((PINT)( 0x48141f+1))
#define ADDR_HELP ((PINT)( 0x481439+1))
#define ADDR_TITLE1 ((PBYTE) 0x48151c+1)
#define ADDR_BUY ((PINT)( 0x4815bf+1))
#define ADDR_SELL ((PINT)( 0x48167e+1))
#define ADDR_QUANT1 ((float*)(0x481bd9+1))
#define ADDR_QUANT2 ((float*)(0x481c43+1))
#define ADDR_QUANT3 ((float*)(0x481c71+1))
#define ADDR_QUANT4 ((float*)(0x481cb5+1))
#define ADDR_QUANT5 ((float*)(0x481d8c+1))
#define ADDR_ITEMMAX ((PUSHORT)0x48381a)
#define ADDR_CARGOMSG ((PBYTE) 0x483ace+2)
#define ADDR_JOBS ((PBYTE) 0x48b8fe)
// NTB
#define ADDR_NTBLOOP ((PUSHORT)0x4ec087)
#define ADDR_NTBDIST ((float*) 0x5d954c)
// Show
#define ADDR_VISIT ((PBYTE) 0x4c4df1)
#define ADDR_HOUSES ((PUSHORT)0x49e799)
#define ADDR_PATHS ((PUSHORT)0x49cd34)
#define ADDR_PP1 ((PDWORD)(0x48e4d2+1))
#define ADDR_PP2 ((PDWORD)(0x48e4e2+1))
#define ADDR_PP3 ((PDWORD)(0x49d98e+2))
#define ADDR_RP8_PP (0x9288-4)
// Zoom
#define ADDR_ZOOMOUT ((float*)(0x4944b0-4))
#define ADDR_ZOOMIN ((float*)(0x49f2ab-4))
// Changing system when docked.
#define ADDR_SHIPLAUNCH ((PDWORD)(0x62b2a80+1))
#define ADDR_HARDPOINT ((PDWORD)(0x62aa9de+1))
// Animation
#define ADDR_SHIELD ((PBYTE) 0x62b2ba1)
// Ghost
#define ADDR_GHOST ((PBYTE) 0x679cc4)
#define ADDR_GHOSTJ ((PBYTE) 0x5450ca)
// Godmode
#define ADDR_GODMODE ((PBYTE) 0x67ecc0)
// Detect switch to multiplayer.
#define ADDR_NEWGAME ((PDWORD*)(0x5a8119+2))
// Intercept IME usage.
#define ADDR_IME ((PBYTE) 0x57baf3)
DWORD dummy;
#define ProtectX( addr, size ) \
VirtualProtect( addr, size, PAGE_EXECUTE_READWRITE, &dummy );
#define ProtectW( addr, size ) \
VirtualProtect( addr, size, PAGE_READWRITE, &dummy );
#define RELOFS( from, to ) \
*(PDWORD)(from) = (DWORD)(to) - (DWORD)(from) - 4;
#define CALL( from, to ) \
*(PBYTE)(from) = 0xe8; \
RELOFS( (DWORD)from+1, to )
#define JMP( from, to ) \
*(PBYTE)(from) = 0xe9; \
RELOFS( (DWORD)from+1, to )
#define FUNC( func, addr ) T##func func = (T##func)addr
FUNC( IsPlayerInCutscene, 0x41a3e0 );
FUNC( CreateSound, 0x42ae40 );
FUNC( PlaySound, 0x4285f0 );
FUNC( GetString, 0x4347e0 );
FUNC( UpdateNavBar, 0x442060 );
FUNC( CloseDialog, 0x45a460 );
FUNC( GetRepCol, 0x45a650 );
FUNC( DisplayBase, 0x45b2d0 );
FUNC( DisplaySystem, 0x45b490 );
FUNC( FmtCredits, 0x4779a0 );
FUNC( DealerDialog, 0x477ab0 );
FUNC( LoadAutosave, 0x4bc830 );
FUNC( EnterBase, 0x4c4910 );
/*
FUNC( ShipValue, 0x4c6e90 );
*/
FUNC( SetWeaponGroup, 0x53c310 );
FUNC( LoadGame, 0x5b2c70 );
Tstartup old_startup;
PUINT const pbats_id = (PUINT)0x674a88; // CreateID( "ge_s_battery_01" )
PUINT const pbots_id = (PUINT)0x674a8c; // CreateID( "ge_s_repair_01" )
// Make use of Freelancer's string buffer.
LPWSTR const wstrbuf = (LPWSTR)0x66dc60;
PUINT const wstrlen = (PUINT)0x6119f8;
// Preserve original cruise speed & acceleration for multiplayer.
float mp_cspd, mp_cacc, sp_cspd, sp_cacc;
PUINT chat_channel = (PUINT)0x66d3fc;
class Freelancer1
{
public:
LPVOID GetMarket( UINT base );
void BaseEnter( UINT base );
void BaseExit();
};
const DWORD fl1jmptable[] = { 0x43a510, 0x43b290, 0x43b3e0 };
#define FL1JMP( fn, idx ) NAKED fn { __asm jmp fl1jmptable[idx*4] }
FL1JMP( LPVOID Freelancer1::GetMarket( UINT ), 0 )
FL1JMP( void Freelancer1::BaseEnter( UINT ), 1 )
FL1JMP( void Freelancer1::BaseExit(), 2 )
Freelancer1* const fl1 = (Freelancer1*)0x668708;
const DWORD PlayerData_save = (DWORD)pub::Save - 0x6d5efa0 + 0x6d4c800;
NAKED
bool PlayerData::save( const CHARACTER_ID&, LPCWSTR, UINT )
{
__asm jmp PlayerData_save
}
// A dummy ship entry, needed after parking a ship.
UINT shipless_id;
const char shipless[] =
"[Ship]\n"
"nickname = shipless_ship\n"
// Required, so make it something that doesn't have anything useful.
"DA_archetype = ships\\utility\\csv\\csv_shield.3db\n";
bool on_launch_pad = true; // cushion is tested before this is set
bool at_equipment_dealer;
HMODULE g_hinst;
UINT rsrcid;
const UINT player = 1;
UINT ship, base, sys;
UINT docking_fixture;
BinaryRDL msg;
struct Bookmark
{
UINT system;
Vector pos;
Matrix ornt;
};
Bookmark bookmark[10];
UINT CreateIDW( LPCWSTR wName )
{
char aName[128];
wcstombs( aName, wName, sizeof(aName) );
return CreateID( aName );
}
// Retrieve a number from a string, which may be suffixed with 'k' or 'K' to
// multiply by 1000. Returns true if a number was read, updating str to the
// next number; false otherwise.
bool get_value( float& val, LPCWSTR& str )
{
double num;
LPWSTR end;
if (*str == '\0')
return true;
if (*str != ',')
{
num = wcstod( str, &end );
if (*end == 'k' || *end == 'K')
{
num *= 1000;
++end;
}
if (*end != ' ' && *end != ',' && *end != '\0')
return false;
val = (float)num;
str = end;
if (*end == '\0')
return true;
}
while (*++str == ' ') ;
return true;
}
// Retrieve a vector from a string. The vector is of the form "x [y [z]]" or
// "[x], [y], [z]". Unused components remain unchanged in VEC.
void get_vector( Vector& vec, LPCWSTR& str )
{
get_value( vec.x, str );
get_value( vec.y, str );
get_value( vec.z, str );
}
// Convert radians to degrees.
float degrees( float rad )
{
rad *= 180 / PI;
// Prevent displaying -0 and prefer 180 to -180.
if (rad < 0)
{
if (rad > -0.005f)
rad = 0;
else if (rad <= -179.995f)
rad = 180;
}
// Round to two decimal places here, so %g can display it without decimals.
float frac = modff( rad * 100, &rad );
if (frac >= 0.5f)
++rad;
else if (frac <= -0.5f)
--rad;
return rad / 100;
}
// Convert an orientation matrix to a pitch/yaw/roll vector. Based on what
// Freelancer does for the save game.
void Matrix_to_Vector( const Matrix& mat, Vector& vec )
{
Vector x = { mat.m[0][0], mat.m[1][0], mat.m[2][0] };
Vector y = { mat.m[0][1], mat.m[1][1], mat.m[2][1] };
Vector z = { mat.m[0][2], mat.m[1][2], mat.m[2][2] };
float h = (float)hypot( x.x, x.y );
if (h > 1/524288.0f)
{
vec.x = degrees( atan2f( y.z, z.z ) );
vec.y = degrees( atan2f( -x.z, h ) );
vec.z = degrees( atan2f( x.y, x.x ) );
}
else
{
vec.x = degrees( atan2f( -z.y, y.y ) );
vec.y = degrees( atan2f( -x.z, h ) );
vec.z = 0;
}
}
bool cmdAbout( LPCWSTR )
{
msg.clear();
msg.style( STYLE_INFO );
msg.string( L"Console" );
msg.style( STYLE_NOTICE );
msg.string( L" by " );
msg.style( StyleBoldYellow );
msg.string( L"Jason Hood" );
msg.style( STYLE_NOTICE );
msg.string( L" (adoxa)." );
msg.para();
msg.strid( IDS(VERSION) );
return true;
}
bool cmdHelp( LPCWSTR topic )
{
int i = -1;
if (*topic)
{
static const LPCWSTR topics[] =
{
L"game", L"base", L"move", L"ship", L"hud", L"player"
};
int len = wcslen( topic );
for (i = sizeof(topics)/sizeof(*topics); --i >= 0;)
{
if (wcsnicmp( topics[i], topic, len ) == 0)
break;
}
}
++i;
FmtStr caption, message;
caption.begin_mad_lib( rsrcid + i );
caption.end_mad_lib();
message.begin_mad_lib( rsrcid + i + 1000 );
message.end_mad_lib();
pub::Player::PopUpDialog( 0, caption, message, 8 /* OK */ );
return false;
}
bool cmdAutoSave( LPCWSTR )
{
pub::Save( player, 1 ); // 1 is Auto Save, anything else is strid
msg.strid( IDS(GAME_SAVED) + true ); // assume success
return true;
}
bool cmdAutoLoad( LPCWSTR )
{
LoadAutosave(); // same as pressing Load Autosave after you die
return false;
}
// Generate a file name and description for the save game.
void fl_name( char* file, LPCWSTR desc )
{
if (*desc == '\0' || desc[1] == '\0')
{
WCHAR slot[3], buf[64];
slot[0] = ' ';
// Explicitly disallow colon to prevent an alternate data stream on NTFS.
slot[1] = (*desc == ':') ? '?' : towupper( *desc );
slot[2] = '\0';
sprintf( file, "qsave%S.fl", slot+1 );
GetString( RSRC, IDS(QUICK_SAVE), buf, 64 );
swprintf( wstrbuf, buf, (slot[1]) ? slot : slot+1 );
}
else
{
sprintf( file, "%x.fl", CreateIDW( desc ) );
wcscpy( wstrbuf, desc - 1 ); // use the space as an indicator
}
}
bool cmdSave( LPCWSTR desc )
{
CHARACTER_ID cid;
fl_name( cid.file, desc );
msg.strid( IDS(GAME_SAVED) + Players.playerdata->save( cid, wstrbuf, 0 ) );
return true;
}
bool cmdLoad( LPCWSTR desc )
{
char path[512];
CHARACTER_ID cid;
fl_name( cid.file, desc );
GetUserDataPath( path );
strcat( path, "\\Accts\\SinglePlayer\\" );
strcat( path, cid.file );
if (GetFileAttributes( path ) == -1)
{
msg.strid( IDS(GAME_NOT_FOUND) );
return true;
}
LoadGame( cid, true );
return false;
}
// Get the ship, returning true if successful.
bool InSpace()
{
ship = 0;
pub::Player::GetShip( player, ship );
return (ship != 0);
}
// Retrieve the ship's ID, position and orientation. Returns true if
// successful, false if not in space.
bool GetLocation( Vector& pos, Matrix& ornt )
{
if (!InSpace())
return false;
pub::SpaceObj::GetLocation( ship, pos, ornt );
return true;
}
// Store the current system, position and orientation in the provided bookmark.
void SetBookmark( int idx )
{
const Universe::ISystem* pSys;
UINT sys;
Vector pos;
Matrix ornt;
if (!GetLocation( pos, ornt ))
return;
pub::Player::GetSystem( player, sys );
pSys = Universe::get_system( sys );
// Don't change previous position if only orientation has changed.
if (idx != 0 ||
bookmark[0].system != pSys->id ||
bookmark[0].pos.x != pos.x ||
bookmark[0].pos.y != pos.y ||
bookmark[0].pos.z != pos.z)
{
bookmark[idx].system = pSys->id;
bookmark[idx].pos = pos;
bookmark[idx].ornt = ornt;
}
}
// Set the ship's position and orientation (ship must already be defined) and
// update them to what was actually set.
void SetLocation( Vector& pos, Matrix& ornt )
{
SetBookmark( 0 ); // remember previous location
pub::Player::GetSystem( player, sys );
pub::SpaceObj::Relocate( ship, sys, pos, ornt );
pub::SpaceObj::GetLocation( ship, pos, ornt );
}
bool cmdSystem( LPCWSTR );
bool cmdPos( LPCWSTR opt )
{
Vector pos, rot;
Matrix ornt;
bool in_space = GetLocation( pos, ornt );
bool loc = false;
if (*opt)
{
const Universe::ISystem* pSys;
if (wcscmp( opt, L"copy" ) == 0)
{
if (!in_space)
return false;
pub::Player::GetSystem( player, sys );
pSys = Universe::get_system( sys );
Matrix_to_Vector( ornt, rot );
// Don't use Unicode, just in case there's still some 9X users.
int len = sprintf( (char*)wstrbuf, "%s %g, %g, %g, %g, %g, %g",
pSys->nickname,
pos.x, pos.y, pos.z,
rot.x, rot.y, rot.z ) + 1;
if (OpenClipboard( NULL ))
{
EmptyClipboard();
HANDLE clip = GlobalAlloc( GMEM_MOVEABLE, len );
if (clip)
{
LPVOID data = GlobalLock( clip );
if (data)
{
memcpy( data, wstrbuf, len );
GlobalUnlock( clip );
SetClipboardData( CF_TEXT, clip );
loc = true;
}
}
CloseClipboard();
}
msg.strid( (loc) ? IDS(COPIED) : IDS(NO_CLIP) );
return true;
}
if ((iswdigit( *opt ) && opt[1] == '\0') ||
wcscmp( opt, L"clip" ) == 0)
return cmdSystem( opt );
if (*opt == '=' && opt[1] == '\0')
{
bool none = true;
msg.clear();
msg.style( STYLE_INFO );
msg.TRA( TRA_Underline, TRA_Underline );
msg.strid( IDS(BOOKMARKS) );
msg.TRA( 0, TRA_Underline );
msg.style( STYLE_NOTICE );
for (int i = 0; i < 10; ++i)
{
if (bookmark[i].system != 0)
{
none = false;
msg.para();
msg.printf( L"%d = ", i );
pSys = Universe::get_system( bookmark[i].system );
msg.strid( pSys->strid_name );
msg.printf( L", %g, %g, %g", bookmark[i].pos.x,
bookmark[i].pos.y,
bookmark[i].pos.z );
}
}
if (none)
{
msg.para();
msg.strid( 3022 ); // None
}
return true;
}
if (!in_space)
return false;
if (*opt == '=' && iswdigit( opt[1] ) && opt[2] == '\0')
{
SetBookmark( opt[1] - '0' );
msg.strid( IDS(BOOKMARK_SET) );
return true;
}
get_vector( pos, opt );
if (*opt)
{
Matrix_to_Vector( ornt, rot );
get_vector( rot, opt );
ornt = EulerMatrix( rot );
loc = true;
}
SetLocation( pos, ornt );
}
else if (!in_space)
return false;
msg.printf( L"pos = %g, %g, %g", pos.x, pos.y, pos.z );
if (loc)
{
Matrix_to_Vector( ornt, rot );
msg.para();
msg.printf( L"rotate = %g, %g, %g", rot.x, rot.y, rot.z );
}
return true;
}
bool cmdPosR( LPCWSTR opt )
{
Vector pos, rot;
Matrix ornt;
if (!GetLocation( pos, ornt ))
return false;
bool loc = false;
if (*opt)
{
Vector rel;
rel.zero();
get_vector( rel, opt );
pos.x += rel.x;
pos.y += rel.y;
pos.z += rel.z;
if (*opt)
{
Matrix_to_Vector( ornt, rot );
rel.zero();
get_vector( rel, opt );
rot.x += rel.x;
rot.y += rel.y;
rot.z += rel.z;
ornt = EulerMatrix( rot );
loc = true;
}
SetLocation( pos, ornt );
}
msg.printf( L"pos = %g, %g, %g", pos.x, pos.y, pos.z );
if (loc)
{
Matrix_to_Vector( ornt, rot );
msg.para();
msg.printf( L"rotate = %g, %g, %g", rot.x, rot.y, rot.z );
}
return true;
}
bool cmdPrint( LPCWSTR opt )
{
msg.clear();
msg.style( STYLE_UNIVERSE );
msg.string( opt );
return true;
}
bool cmdRot( LPCWSTR opt )
{
Vector pos, rot;
Matrix ornt;
if (!GetLocation( pos, ornt ))
return false;
if (*opt)
{
Matrix_to_Vector( ornt, rot );
get_vector( rot, opt );
ornt = EulerMatrix( rot );
SetLocation( pos, ornt );
}
Matrix_to_Vector( ornt, rot );
msg.printf( L"rotate = %g, %g, %g", rot.x, rot.y, rot.z );
return true;
}
bool cmdRotR( LPCWSTR opt )
{
Vector pos, rot;
Matrix ornt;
if (!GetLocation( pos, ornt ))
return false;
if (*opt)
{
Vector rel;
rel.zero();
get_vector( rel, opt );
Matrix_to_Vector( ornt, rot );
rot.x += rel.x;
rot.y += rel.y;
rot.z += rel.z;
ornt = EulerMatrix( rot );
SetLocation( pos, ornt );
}
Matrix_to_Vector( ornt, rot );
msg.printf( L"rotate = %g, %g, %g", rot.x, rot.y, rot.z );
return true;
}
// Determine how many times the name should be found. If OPT ends in '*', find
// the second, "**" the third and "*N" the Nth. The '*' is replaced with NUL.
int GetCount( LPWSTR opt )
{
LPWSTR star = wcschr( opt, '*' );
if (!star)
return 1;
int count;
if (!star[1])
count = 2;
else if (star[1] == '*' && !star[2])
count = 3;
else
{
LPWSTR end;
count = wcstol( star + 1, &end, 10 );
if (*end)
return 1;
}
*star = '\0';
return count;
}
// Find PAT within TGT, ignoring case.
LPWSTR wcsstri( LPCWSTR tgt, LPCWSTR pat )
{
int delta = wcslen( tgt ) - wcslen( pat );
if (delta < 0)
return NULL;
LPCWSTR end = tgt + delta;
for (;;)
{
WCHAR ch = towlower( *pat );
while (ch != towlower( *tgt ))
{
if (++tgt > end)
return NULL;
}
for (int i = 1;; ++i)
{
if (pat[i] == '\0')
return (LPWSTR)tgt - 1;
if (towlower( pat[i] ) != towlower( tgt[i] ))
break;
}
++tgt;
}
}
// Search a system for an object.
UINT find_object( UINT system, LPWSTR object )
{
struct ObjNameEnumerator : public pub::System::SysObjEnumerator
{
UINT object;
LPCWSTR partial;
UINT id;
int count;
bool nick;
bool operator()( const pub::System::SysObj& obj )
{
WCHAR buf[128];
UINT objid = CreateID( obj.nickname );
LPWSTR match;
if (nick)
{
mbstowcs( buf, obj.nickname, 128 );
match = wcsstri( buf, partial );
}
else
{
if (id == objid)
{
object = objid;
return false;
}
GetString( RSRC, obj.ids_name, buf, 128 );
match = wcsstr( buf, partial );
}
if (match)
{
object = objid;
if (--count <= 0)
return false;
}
return true;
}
} objname;
if (*object == '~')
{
objname.nick = true;
objname.partial = object + 1;
}
else
{
objname.nick = false;
objname.partial = object;
}
objname.id = CreateIDW( object );
objname.count = GetCount( object );
objname.object = 0;
pub::System::EnumerateObjects( system, objname );
if (!objname.object)
msg.strid( IDS(OBJECT_NOT_FOUND) );
return objname.object;
}
NAKED
CShip* GetCShip()
{
__asm mov eax, 0x54baf0
__asm call eax
__asm test eax, eax
__asm jz noship
__asm mov eax, [eax+16]
noship:
__asm ret
}
bool cmdJump( LPCWSTR opt )
{
Vector pos;
Matrix ornt;
if (!GetLocation( pos, ornt ))
return false;
if (iswdigit( *opt ) || *opt == '-' || *opt == ',')
{
Vector dist;
dist.zero();
get_vector( dist, opt );
pos.add_z( dist.x, ornt );
pos.add_x( dist.y, ornt );
pos.add_y( dist.z, ornt );
}
else
{
UINT tgt;
if (!*opt)
{
pub::SpaceObj::GetTarget( ship, tgt );
if (!tgt)
return false;
}
else
{
UINT objsys;
pub::Player::GetSystem( player, sys );
tgt = CreateIDW( opt );
// Looks like it only works if it's in the current system, anyway.
if (pub::SpaceObj::GetSystem( tgt, objsys ) < 0)
{
tgt = find_object( sys, (LPWSTR)opt );
if (!tgt)
return true;
}
}
UINT type;
float rad, brad;
Vector vec, bvec;
Vector tpos;
Matrix tornt;
if (pub::SpaceObj::GetType( tgt, type ) < 0)
{
// Let's assume a waypoint.
CShip* cship = GetCShip();
IObjRW* target = cship->get_target();
if (target == NULL)
return false;
tpos = target->get_position();
rad = 0.3121905f; // target->get_physical_radius( rad, vec );
}
else
{
pub::SpaceObj::GetLocation( tgt, tpos, tornt );
pub::SpaceObj::GetRadius( tgt, rad, vec );
pub::SpaceObj::GetBurnRadius( tgt, brad, bvec );
if (rad < brad)
rad = brad;
}
bool moor = false;
if (type == 0x100) // station
{
// Test for a mooring fixture.
UINT arch;
pub::SpaceObj::GetSolarArchetypeID( tgt, arch );
Archetype::Solar* solar = Archetype::GetSolar( arch );
if (solar->archid == docking_fixture)
{
moor = true;
type = 0x20;
}
}
if (type == 0x20) // docking ring
{
pos = tpos;
ornt = tornt;
pos.add_z( -rad, ornt );
if (!moor)
pos.add_x( 98, ornt ); // move across to the opening
}
else if (type == 0x40) // jump gate
{
pos = tpos;
ornt = tornt;
pos.add_z( 1.5f * rad, ornt );
ornt.turn_around();
}
else if (type == 0x80) // trade lane
{
ornt = tornt;
Vector p = tpos;
p.add_z( -rad, ornt );
// Find which side we're on by simply seeing which distance is greater.
if (pos.distsqr( p ) > pos.distsqr( tpos ))
{
ornt.turn_around();
pos = tpos;
pos.add_z( -rad, ornt );
}
else
{
pos = p;
}
}
else
{
float dist = pos.dist( tpos );
if (type & 7) // moon/planet/sun
{
// Toggle between getting right up close and seeing the whole thing.