-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathviewer.cpp
1244 lines (1141 loc) · 36.6 KB
/
viewer.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
/************************************************************************
* Copyright (c) 2005-2007 [email protected] *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <iostream>
#include <iomanip>
#include <SDL_opengl.h>
#include <sstream>
#include <unistd.h>
#include "blockanim.h"
#include "dataholder.h"
#include "entity_controller.h"
#include "file_helper.h"
#include "gl_camera.h"
#include "gl_cityview.h"
#include "gl_font.h"
#include "gl_screen.h"
#include "gl_spritecache.h"
#include "gl_texturecache.h"
#include "id_sys.h"
#include "localplayer.h"
#include "log.h"
#include "m_exceptions.h"
#include "navdata.h"
#include "opengta.h"
#include "spritemanager.h"
#include "gl_spritecache.h"
#include "timer.h"
#ifdef WITH_LUA
#include "lua_addon/lua_vm.h"
#include "lua_addon/lua_stackguard.h"
#endif
#include "gui.h"
#include "font_cache.h"
#include "ai.h"
#define getPedById getPed
#define removePedById removePed
#define addPed add
extern SDL_Surface* screen;
extern int global_EC;
extern int global_Done;
extern int global_Restart;
GLfloat mapPos[3] = {12.0f, 12.0f, 20.0f};
OpenGTA::CityView *city = NULL;
//OpenGL::DrawableFont* m_font = NULL;
GUI::Label * fps_label = NULL;
int city_num = 0;
const char* styles_8[3] = { "STYLE001.GRY", "STYLE002.GRY", "STYLE003.GRY" };
const char* styles_24[3] = { "STYLE001.G24", "STYLE002.G24", "STYLE003.G24" };
const char* cities[3] = { "NYC.CMP", "SANB.CMP", "MIAMI.CMP" };
std::string specific_map;
std::string specific_style;
Uint32 num_frames_drawn = 0;
Uint32 fps = 0;
Uint32 last_tick;
Uint32 fps_last_tick;
Uint32 script_last_tick;
Uint32 arg_screen_w = 0;
Uint32 arg_screen_h = 0;
bool rotate = false;
bool cam_grav = false;
int tex_flip = 0;
int draw_arrows = 0;
int ped_anim = 0;
int bbox_toggle = 0;
int texsprite_toggle = 0;
int follow_toggle = 0;
float anisotropic_filter_degree = 2.0f;
int mipmap_textures = -1;
int vsync_config = -1;
int city_blocks_area = -1;
int config_scale2x = 1;
OpenGTA::SpriteObject::Animation pedAnim(0, 0);
#ifdef OGTA_DEFAULT_GRAPHICS_G24
bool highcolor_data = true;
#else
bool highcolor_data = false;
#endif
bool full_screen = false;
bool player_toggle_run = false;
const char* script_file = NULL;
int paused = 0;
int next_station_zoom = 0;
bool gamma_slide = false;
float screen_gamma = 1.0f;
Vector3D test_dot(-1, -1, -1);
/*
void ERROR(const char* s) {
std::cerr << "Error" << s << std::endl;
std::cerr << "* last SDL error was: " << SDL_GetError() << std::endl;
global_EC = 1;
exit(1);
}*/
void on_exit() {
SDL_Quit();
if (city)
delete city;
//if (m_font)
// delete m_font;
PHYSFS_deinit();
if (global_EC)
std::cerr << "Exiting after fatal problem - please see output above" << std::endl;
else
std::cout << "Goodbye" << std::endl;
}
void print_usage(const char* argv0) {
std::cout << "USAGE: " << argv0 << " [options] [city-num]" << std::endl <<
std::endl <<
"Options: " << std::endl <<
" -l k : log-level (default: 0; 1, 2)" << std::endl <<
" -c k : 0 = 8bit GRY, 1 = 24bit G24" << std::endl <<
" -f : fullscreen on program-start" << std::endl <<
" -V : show version & compile-time switches" << std::endl <<
" -M k : texture mipmaps: 0 = disable, 1 = enable" << std::endl <<
" -x k : scale2x sprites: 0 = disable, 1 = enable" << std::endl <<
" -v k : vertical sync: 0 = disable, 1 = try with SDL" <<
#ifdef LINUX
", 2 = try with GLX" <<
#elif WIN32
", 2 = try with GLW" <<
#endif
std::endl <<
" -a f : anisotropic texture filtering degree: 1.0 = disabled"
<< std::endl <<
std::endl <<
" -m map_file -g style_file : load specified files" << std::endl <<
" -w width -h height : screen dimension" << std::endl <<
std::endl <<
"City-num: 0 (default), 1, 2" << std::endl <<
std::endl <<
"The following environment variables are used when defined:" << std::endl <<
" OGTA_DATA : PhysicsFS source for main data file lookup" << std::endl <<
" OGTA_HOME : unused - will be config/save dir" << std::endl <<
" OGTA_MOD : PhysicsFS source to override main data files" << std::endl <<
" OGTA_LANG : defines the fxt language file to load" << std::endl;
}
void print_version_info() {
#define PRINT_FORMATED(spaces) std::setw(spaces) << std::left <<
#define PRINT_OFFSET PRINT_FORMATED(19)
std::cout << PRINT_OFFSET "OpenGTA version:" << OGTA_VERSION_INFO << std::endl <<
PRINT_OFFSET "platform:" << OGTA_PLATFORM_INFO << std::endl <<
PRINT_OFFSET "Lua support:" <<
#ifdef WITH_LUA
"yes [" << LUA_RELEASE << "]" <<
#else
"no" <<
#endif
std::endl <<
PRINT_OFFSET "sound support:" <<
#ifdef WITH_SOUND
"yes" <<
#else
"no" <<
#endif
std::endl <<
PRINT_OFFSET "SDL_image support:" <<
#ifdef WITH_SDL_IMAGE
"yes" <<
#else
"no" <<
#endif
std::endl <<
PRINT_OFFSET "vsync support:" <<
#ifdef HAVE_SDL_VSYNC
"yes" <<
#else
"no" <<
#endif
std::endl <<
PRINT_OFFSET "scale2x support:" <<
#ifdef DO_SCALE2X
"yes" <<
#else
"no" <<
#endif
std::endl <<
#ifdef OGTA_DEFAULT_DATA_PATH
PRINT_OFFSET "data-path:" << "[" << OGTA_DEFAULT_DATA_PATH << "]" << std::endl <<
#endif
#ifdef OGTA_DEFAULT_MOD_PATH
PRINT_OFFSET "mod-path:" << "[" << OGTA_DEFAULT_MOD_PATH << "]" << std::endl <<
#endif
PRINT_OFFSET "default graphics:" <<
#ifdef OGTA_DEFAULT_GRAPHICS_G24
"G24 - 24 bit" <<
#else
"GRY - 8 bit" <<
#endif
std::endl <<
PRINT_OFFSET "compiler: " << USED_GCC_VERSION
<< std::endl;
}
void parse_args(int argc, char* argv[]) {
int index;
int c;
opterr = 0;
#ifdef WITH_LUA
#define VIEWER_FLAGS "a:s:w:h:c:m:M:g:l:v:x:fV"
#else
#define VIEWER_FLAGS "a:w:h:c:m:M:g:l:v:x:fV"
#endif
while ((c = getopt (argc, argv, VIEWER_FLAGS)) != -1)
switch (c)
{
#ifdef WITH_LUA
case 's':
script_file = optarg;
break;
#endif
case 'a':
anisotropic_filter_degree = atof(optarg);
break;
case 'c':
highcolor_data = atoi(optarg);
break;
case 'm':
specific_map = std::string(optarg);
break;
case 'M':
mipmap_textures = atoi(optarg);
break;
case 'g':
specific_style = std::string(optarg);
break;
case 'w':
arg_screen_w = atoi(optarg);
break;
case 'h':
arg_screen_h = atoi(optarg);
break;
case 'l':
Util::Log::setOutputLevel(atoi(optarg));
break;
case 'f':
full_screen = true;
break;
case 'v':
vsync_config = atoi(optarg);
break;
case 'V':
print_version_info();
exit(0);
break;
case 'x':
config_scale2x = atoi(optarg);
break;
default:
if (optopt == '?') {
print_usage(argv[0]);
exit(0);
}
else if (isprint (optopt))
ERROR << "Unknown option `-" << char(optopt) << "'" << std::endl;
else
ERROR << "Unknown option character `" << optopt << "'" << std::endl;
print_usage(argv[0]);
exit(1);
}
for (index = optind; index < argc; index++)
city_num = atoi(argv[index]);
if (city_num > 2) {
ERROR << "Invalid city number: " << city_num << std::endl;
exit(1);
}
}
void run_init(const char* prg_name) {
// physfs
PHYSFS_init(prg_name);
// physfs-ogta
Util::FileHelper & fh = GET_FILE_HELPER;
if (fh.existsInSystemFS(fh.getBaseDataPath())) {
PHYSFS_addToSearchPath(fh.getBaseDataPath().c_str(), 1);
}
else {
WARN << "Could not load data-source: " << fh.getBaseDataPath() << std::endl;
}
PHYSFS_addToSearchPath(PHYSFS_getBaseDir(), 1);
if (fh.existsInSystemFS(fh.getModDataPath()))
PHYSFS_addToSearchPath(fh.getModDataPath().c_str(), 0);
// screen, no window yet
OpenGL::Screen & screen = OpenGL::ScreenHolder::Instance();
// check for a configfile
#ifdef WITH_LUA
if (fh.existsInVFS("config")) {
char* config_as_string = (char*)fh.bufferFromVFS(
fh.openReadVFS("config"));
OpenGTA::Script::LuaVM & vm = OpenGTA::Script::LuaVMHolder::Instance();
try {
//vm.runString(config_as_string);
lua_State *L = vm.getInternalState();
Util::LGUARD(L);
if (luaL_loadbuffer(L, config_as_string, strlen(config_as_string), "config"))
throw E_SCRIPTERROR("Error running string: " + std::string(lua_tostring(L, -1)));
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "config");
lua_setfenv(L, -2);
if (lua_pcall(L, 0, 0, 0))
throw E_SCRIPTERROR("Error running string: " + std::string(lua_tostring(L, -1)));
}
catch (const Util::ScriptError & e) {
std::cerr << "Error in config-file: " << e.what() << std::endl;
global_EC = 1;
exit(1);
}
lua_State *L = vm.getInternalState();
lua_getglobal(L, "config");
if (lua_type(L, 1) == LUA_TTABLE) {
try {
bool sh = vm.getBool("use_g24_graphics");
highcolor_data = sh;
}
catch (const Util::ScriptError & e) {}
try {
Uint32 sw = vm.getInt("screen_width");
if (!arg_screen_w)
arg_screen_w = sw;
}
catch (const Util::ScriptError & e) {}
try {
Uint32 sh = vm.getInt("screen_height");
if (!arg_screen_h)
arg_screen_h = sh;
}
catch (const Util::ScriptError & e) {}
try {
int sh = vm.getInt("screen_vsync");
screen.setupVsync(sh);
}
catch (const Util::ScriptError & e) {}
try {
bool sh = vm.getBool("full_screen");
if (!full_screen)
full_screen = sh;
}
catch (const Util::ScriptError & e) {}
float fov = screen.getFieldOfView();
float np = screen.getNearPlane();
float fp = screen.getFarPlane();
try {
fov = vm.getFloat("gl_field_of_view");
}
catch (const Util::ScriptError & e) {}
try {
np = vm.getFloat("gl_near_plane");
}
catch (const Util::ScriptError & e) {}
try {
fp = vm.getFloat("gl_far_plane");
}
catch (const Util::ScriptError & e) {}
screen.setupGlVars(fov, np, fp);
try {
bool sh = vm.getBool("gl_mipmap_textures");
ImageUtil::mipmapTextures = sh;
}
catch (const Util::ScriptError & e) {}
try {
bool sh = vm.getBool("scale2x_sprites");
OpenGL::SpriteCacheHolder::Instance().setScale2x(sh);
}
catch (const Util::ScriptError & e) {}
try {
int sh = vm.getInt("active_area_size");
city_blocks_area = sh;
}
catch (const Util::ScriptError & e) {}
}
// can't check for gl-extensions now
}
#endif
//INFO << "AREA:: " << city_blocks_area << std::endl;
// check both width & height defined
if ((arg_screen_h && !arg_screen_w) || (!arg_screen_h && arg_screen_w)) {
WARN << "Invalid screen specified: " << arg_screen_w << "x" <<
arg_screen_h << " - using default" << std::endl;
arg_screen_h = 0; arg_screen_w = 0;
}
// fullscreen before first video init; only chance to set it on win32
screen.setFullScreenFlag(full_screen);
if (vsync_config != -1)
screen.setupVsync(vsync_config);
// create screen
screen.activate(arg_screen_w, arg_screen_h);
SDL_EnableKeyRepeat( 0, 0 );
//SDL_EnableKeyRepeat( 100, SDL_DEFAULT_REPEAT_INTERVAL );
// more setup; that requires an active screen
#ifdef WITH_LUA
OpenGTA::Script::LuaVM & vm = OpenGTA::Script::LuaVMHolder::Instance();
lua_State *L = vm.getInternalState();
if (lua_type(L, 1) == LUA_TTABLE) {
try {
float v = vm.getFloat("gl_anisotropic_textures");
if (ImageUtil::supportedMaxAnisoDegree >= v)
ImageUtil::supportedMaxAnisoDegree = v;
}
catch (const Util::ScriptError & e) {}
try {
float v = (highcolor_data ? vm.getFloat("screen_gamma_g24") :
vm.getFloat("screen_gamma_gry"));
screen_gamma = v;
SDL_SetGamma(v, v, v);
}
catch (const Util::ScriptError & e) {}
}
lua_settop(L, 0);
#endif
if (ImageUtil::supportedMaxAnisoDegree >= anisotropic_filter_degree)
ImageUtil::supportedMaxAnisoDegree = anisotropic_filter_degree;
else
WARN << "Using filter degree " << ImageUtil::supportedMaxAnisoDegree <<
", requested " << anisotropic_filter_degree << " not supported" << std::endl;
switch(mipmap_textures) {
case -1:
break;
case 0:
ImageUtil::mipmapTextures = false;
break;
default:
ImageUtil::mipmapTextures = true;
break;
}
// before any graphics are loaded
OpenGL::SpriteCacheHolder::Instance().setScale2x(config_scale2x);
// FIXME: basic gui setup; should not be here
GUI::Manager & gm = GUI::ManagerHolder::Instance();
SDL_Rect rect;
rect.x = 5;
rect.y = 50;
fps_label = new GUI::Label(rect, "", "F_MTEXT.FON", 1);
//fps_label->borderColor.r = fps_label->borderColor.unused = 200;
gm.add(fps_label, 5);
}
void print_position() {
Vector3D & v = OpenGL::CameraHolder::Instance().getCenter();
Vector3D & e = OpenGL::CameraHolder::Instance().getEye();
Vector3D & u = OpenGL::CameraHolder::Instance().getUp();
if (!city->getViewMode()) {
std::cout << cities[city_num] << ": " << city->getCurrentSector()->getFullName() << std::endl <<
"camera.setCenter(" << v.x << ", " << v.y << ", " << v.z << ")" << std::endl <<
"camera.setEye(" << e.x << ", " << e.y << ", " << e.z << ")" << std::endl <<
"camera.setUp(" << u.x << ", " << u.y << ", " << u.z << ")" << std::endl <<
"city_view:setVisibleRange(" << city->getVisibleRange() << ")" << std::endl <<
"city_view:setTopDownView( false )" << std::endl;
}
else {
GLfloat* cp = city->getCamPos();
std::cout << cities[city_num] << ": " << city->getCurrentSector()->getFullName() << std::endl <<
"city_view:setCamPosition(" << cp[0] << ", " << cp[1] << ", " << cp[2] << ")" << std::endl <<
"city_view:setVisibleRange(" << city->getVisibleRange() << ")" << std::endl <<
"city_view:setTopDownView( true )" << std::endl;
}
}
void handleKeyUp( SDL_keysym *keysym) {
switch ( keysym->sym ) {
case 'j':
OpenGTA::LocalPlayer::Instance().getCtrl().releaseTurnLeft();
//OpenGTA::LocalPlayer::Instance().turn = 0;
//OpenGTA::LocalPlayer::Instance().setTurn(0);
break;
case 'l':
OpenGTA::LocalPlayer::Instance().getCtrl().releaseTurnRight();
//OpenGTA::LocalPlayer::Instance().turn = 0;
//OpenGTA::LocalPlayer::Instance().setTurn(0);
break;
case 'i':
OpenGTA::LocalPlayer::Instance().getCtrl().releaseMoveForward();
//OpenGTA::LocalPlayer::Instance().move = 0;
//OpenGTA::LocalPlayer::Instance().setMove(0);
break;
case 'k':
OpenGTA::LocalPlayer::Instance().getCtrl().releaseMoveBack();
//OpenGTA::LocalPlayer::Instance().move = 0;
//OpenGTA::LocalPlayer::Instance().setMove(0);
break;
case SDLK_LCTRL:
OpenGTA::LocalPlayer::Instance().getCtrl().setFireWeapon(false);
break;
default:
break;
}
}
void draw_mapmode();
void create_ped_at(const Vector3D v) {
OpenGTA::Pedestrian p(Vector3D(0.2f, 0.5f, 0.2f), v, 0xffffffff);
p.remap = OpenGTA::StyleHolder::Instance().get().getRandomPedRemapNumber();
INFO << "using remap: " << p.remap << std::endl;
OpenGTA::Pedestrian & pr = OpenGTA::SpriteManagerHolder::Instance().addPed(p);
pr.switchToAnim(1);
OpenGTA::LocalPlayer::Instance().setCtrl(pr.m_control);
GUI::create_ingame_gui(1);
//pr.m_control = &OpenGTA::LocalPlayer::Instance();
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).giveItem(1, 255);
}
void explode_ped() {
try {
OpenGTA::Pedestrian & ped = OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff);
Vector3D p(ped.pos);
p.y += 0.2f;
OpenGTA::SpriteManagerHolder::Instance().createExplosion(p);
}
catch (Util::UnknownKey & e) {
WARN << "Cannot place explosion - press F4 to switch to player-mode first!" << std::endl;
}
}
void zoomToTrain(int k) {
/*
OpenGTA::TrainSegment & ts = OpenGTA::SpriteManagerHolder::Instance().getTrainById(k);
Vector3D p(ts.pos);
p.y += 9;
OpenGL::CameraHolder::Instance().interpolate(p, 1, 30000);
*/
}
#include "cell_iterator.h"
namespace OpenGTA {
void ai_step_fake(OpenGTA::Pedestrian *p) {
try {
OpenGTA::Pedestrian & pr = OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff);
float t_angle = Util::xz_angle(p->pos, pr.pos);
//INFO << "dist " << Util::distance(p->pos, pr.pos) << std::endl;
//INFO << "angle " << t_angle << std::endl;
//INFO << "myrot: " << p->rot << std::endl;
if (Util::distance(p->pos, pr.pos) > 3) {
p->m_control.setTurnLeft(false);
p->m_control.setTurnRight(false);
if (t_angle > p->rot)
p->m_control.setTurnLeft(true);
else
p->m_control.setTurnRight(true);
}
else {
p->m_control.setMoveForward(true);
int k = rand() % 5;
if (k == 0) {
p->m_control.setTurnLeft(false);
p->m_control.setTurnRight(false);
}
else if (k == 1) {
p->m_control.setTurnLeft(true);
p->m_control.setTurnRight(false);
}
else if (k == 2) {
p->m_control.setTurnLeft(false);
p->m_control.setTurnRight(true);
}
}
}
catch (Util::UnknownKey & e) {
}
}
}
#include "id_sys.h"
void add_auto_ped() {
try {
OpenGTA::Pedestrian & pr = OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff);
int id = OpenGTA::TypeIdBlackBox::requestId();
Vector3D v(pr.pos);
v.y += 0.9f;
//INFO << v.x << " " << v.y << " " << v.z << std::endl;
Sint16 remap = OpenGTA::StyleHolder::Instance().get().getRandomPedRemapNumber();
OpenGTA::Pedestrian p(Vector3D(0.2f, 0.5f, 0.2f), v, id, remap);
OpenGTA::SpriteManagerHolder::Instance().addPed(p);
OpenGTA::Pedestrian & pr2 = OpenGTA::SpriteManagerHolder::Instance().getPedById(id);
pr2.switchToAnim(1);
INFO << "now " << OpenGTA::SpriteManagerHolder::Instance().getNum<OpenGTA::Pedestrian>() << " peds " << std::endl;
//pr2.m_control = &OpenGTA::nullAI;
}
catch (Util::UnknownKey & e) {
WARN << "Cannot place peds now - press F4 to switch to player-mode first!" << std::endl;
}
}
void toggle_player_run() {
OpenGTA::PedController * pc = &OpenGTA::LocalPlayer::Instance().getCtrl();
INFO << std::endl;
if (!pc) {
WARN << "no player yet!" << std::endl;
return;
}
if (!pc->getRunning())
pc->setRunning(true);
else
pc->setRunning(false);
}
void show_gamma_config() {
OpenGL::Screen & screen = OpenGL::ScreenHolder::Instance();
GUI::Manager & gm = GUI::ManagerHolder::Instance();
if (gamma_slide) {
SDL_Rect r;
r.x = screen.getWidth() / 2;
r.y = screen.getHeight() / 2;
r.w = 200;
r.h = 30;
GUI::ScrollBar * sb = new GUI::ScrollBar(GUI::GAMMA_SCROLLBAR_ID, r);
sb->color.r = sb->color.g = sb->color.b = 180;
sb->color.unused = 255;
sb->innerColor.r = 250;
sb->value = screen_gamma/2;
sb->changeCB = GUI::ScrollBar::SC_Functor(GUI::screen_gamma_callback);
gm.add(sb, 90);
r.y += 40;
std::ostringstream os;
os << "Gamma: " << screen_gamma;
GUI::Label * l = new GUI::Label(GUI::GAMMA_LABEL_ID, r, os.str(), "F_MTEXT.FON", 1);
gm.add(l, 80);
screen.setSystemMouseCursor(true);
}
else {
gm.removeById(GUI::GAMMA_SCROLLBAR_ID);
gm.removeById(GUI::GAMMA_LABEL_ID);
screen.setSystemMouseCursor(false);
}
}
void car_toggle() {
OpenGTA::Pedestrian & pped = OpenGTA::LocalPlayer::Instance().getPed();
Vector3D pos = pped.pos;
std::list<OpenGTA::Car> & list = OpenGTA::SpriteManagerHolder::Instance().getList<OpenGTA::Car>();
float min_dist = 360;
float _d;
std::list<OpenGTA::Car>::iterator j = list.end();
for (std::list<OpenGTA::Car>::iterator i = list.begin(); i != list.end(); i++) {
if ((_d = Util::distance(pos, i->pos)) < min_dist) {
j = i;
min_dist = _d;
}
}
assert(j != list.end());
std::cout << j->id() << " " << j->pos.x << ", " << j->pos.y << ", " << j->pos.z << std::endl;
Vector3D p_door(j->carInfo.door[0].rpx / 64.0f, 0,
j->carInfo.door[0].rpy / 64.0f);
Vector3D p_door_global = Transform(p_door, j->m_M);
p_door_global.y += 0.2f;
std::cout << p_door_global.x << ", " << p_door_global.y << ", " << p_door_global.z << std::endl;
test_dot = p_door_global;
//pped.aiMode = 1;
//pped.aiData.pos1 = p_door_global;
OpenGTA::AI::Pedestrian::walk_pavement(&pped);
}
void handleKeyPress( SDL_keysym *keysym ) {
GLfloat* cp = city->getCamPos();
mapPos[0] = cp[0]; mapPos[1] = cp[1]; mapPos[2] = cp[2];
OpenGL::Camera & cam = OpenGL::CameraHolder::Instance();
switch ( keysym->sym ) {
case SDLK_ESCAPE:
global_Done = 1;
break;
case SDLK_LEFT:
mapPos[0] -= 1.0f;
cam.translateBy(Vector3D(-1, 0, 0));
break;
case SDLK_RIGHT:
mapPos[0] += 1.0f;
cam.translateBy(Vector3D(1, 0, 0));
break;
case SDLK_UP:
mapPos[2] -= 1.0f;
cam.translateBy(Vector3D(0, 0, -1));
break;
case SDLK_DOWN:
mapPos[2] += 1.0f;
cam.translateBy(Vector3D(0, 0, 1));
break;
case SDLK_SPACE:
cam.setSpeed(0.0f);
break;
case SDLK_F1:
//cam.interpolate(Vector3D(254, 9, 254), 1, 20000);
//zoomToTrain(next_station_zoom++);
//if (next_station_zoom >= OpenGTA::SpriteManagerHolder::Instance().trainSystem.getNumTrains())
// next_station_zoom = 0;
global_Restart = 1;
global_Done = 1;
return;
{
Vector3D p(cam.getEye());
OpenGTA::MapHolder::Instance().get().getNearestLocationByType(0, p.x, p.z);
}
break;
case SDLK_F2:
bbox_toggle = (bbox_toggle ? 0 : 1);
OpenGTA::SpriteManagerHolder::Instance().setDrawBBox(bbox_toggle);
break;
case SDLK_F3:
texsprite_toggle = (texsprite_toggle ? 0 : 1);
OpenGTA::SpriteManagerHolder::Instance().setDrawTexBorder(texsprite_toggle);
break;
case SDLK_F4:
follow_toggle = (follow_toggle ? 0 : 1);
if (follow_toggle) {
SDL_EnableKeyRepeat( 0, SDL_DEFAULT_REPEAT_INTERVAL );
city->setViewMode(false);
Vector3D p(cam.getEye());
create_ped_at(p);
cam.setVectors( Vector3D(p.x, 10, p.z), Vector3D(p.x, 9.0f, p.z), Vector3D(0, 0, -1) );
cam.setFollowMode(OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).pos);
cam.setCamGravity(true);
}
else {
SDL_EnableKeyRepeat( 100, SDL_DEFAULT_REPEAT_INTERVAL );
cam.setVectors(cam.getEye(),
Vector3D(cam.getEye() + Vector3D(1, -1, 1)), Vector3D(0, 1, 0));
cam.setCamGravity(false);
cam.releaseFollowMode();
OpenGTA::SpriteManagerHolder::Instance().removePedById(0xffffffff);
OpenGTA::SpriteManagerHolder::Instance().removeDeadStuff();
GUI::remove_ingame_gui();
}
break;
case SDLK_RETURN:
car_toggle();
break;
case SDLK_F5:
draw_arrows = (draw_arrows ? 0 : 1);
city->setDrawHeadingArrows(draw_arrows);
break;
case SDLK_F6:
draw_mapmode();
break;
case SDLK_F7:
explode_ped();
break;
case SDLK_F8:
add_auto_ped();
break;
case SDLK_F9:
city->setDrawTextured(city->getDrawTextured() ? 0 : 1);
break;
case SDLK_F10:
city->setDrawLines(city->getDrawLines() ? 0 : 1);
break;
case SDLK_F12:
gamma_slide = (gamma_slide ? 0 : 1);
show_gamma_config();
break;
case SDLK_LSHIFT:
toggle_player_run();
break;
/*
case SDLK_F6:
tex_flip = (tex_flip ? 0 : 1);
INFO << "flipping: " << tex_flip << std::endl;
city->setTexFlipTest(tex_flip);
break;
*/
case SDLK_LCTRL:
OpenGTA::LocalPlayer::Instance().getCtrl().setFireWeapon();
break;
case '1':
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(1);
OpenGTA::LocalPlayer::Instance().getCtrl().setActiveWeapon(1);
break;
case '2':
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(2);
OpenGTA::LocalPlayer::Instance().getCtrl().setActiveWeapon(2);
break;
case '3':
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(3);
OpenGTA::LocalPlayer::Instance().getCtrl().setActiveWeapon(3);
break;
case '4':
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(4);
OpenGTA::LocalPlayer::Instance().getCtrl().setActiveWeapon(4);
break;
case '5':
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(5);
break;
case '6':
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(6);
break;
case '7':
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(7);
break;
case '8':
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(8);
break;
case '9':
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(9);
/*
ped_anim -= 1; if (ped_anim < 0) ped_anim = 0;
pedAnim.firstFrameOffset = ped_anim;
INFO << "switching to sprite: " << ped_anim << std::endl;
OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).setAnimation(pedAnim);
*/
break;
case '0':
OpenGTA::LocalPlayer::Instance().getCtrl().setActiveWeapon(0);
/*
ped_anim += 1; if (ped_anim > 200) ped_anim = 200;
pedAnim.firstFrameOffset = ped_anim;
INFO << "switching to sprite: " << ped_anim << std::endl;
OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).setAnimation(pedAnim);
*/
//OpenGTA::SpriteManagerHolder::Instance().getPedById(0xffffffff).equip(0);
break;
case 'w':
cam.setSpeed(0.2f);
break;
case 's':
cam.setSpeed(-0.2f);
break;
case 'j':
OpenGTA::LocalPlayer::Instance().getCtrl().setTurnLeft();
break;
case 'l':
OpenGTA::LocalPlayer::Instance().getCtrl().setTurnRight();
break;
case 'i':
OpenGTA::LocalPlayer::Instance().getCtrl().setMoveForward();
break;
case 'k':
OpenGTA::LocalPlayer::Instance().getCtrl().setMoveBack();
break;
case 'f':
//FIXME: simply ignored on windows for now
#ifndef WIN32
OpenGL::ScreenHolder::Instance().toggleFullscreen();
#endif
#if 0
#ifdef WIN32
city->resetTextures();
//m_font->resetTextures();
OpenGL::SpriteCacheHolder::Instance().clearAll();
#endif
#endif
break;
case 'r':
rotate = (rotate) ? false : true;
cam.setRotating(rotate);
break;
case 'g':
cam_grav = (cam_grav) ? false : true;
cam.setCamGravity(cam_grav);
break;
case 't':
mapPos[0] = mapPos[2] = 128;
mapPos[1] = 230;
city->setVisibleRange(128);
break;
case 'p':
print_position();
break;
case '+':
mapPos[1] += 1.0f;
cam.translateBy(Vector3D(0, 1, 0));
break;
case '-':
mapPos[1] -= 1.0f;
cam.translateBy(Vector3D(0, -1, 0));
break;
case 'x':
city->setViewMode(false);
city->setVisibleRange(city->getVisibleRange() * 2);
break;
case 'y':
break;
case 'z':
city->setViewMode(true);
city->setVisibleRange(city->getVisibleRange() / 2);
break;
case '.':
city->setVisibleRange(city->getVisibleRange()-1);
INFO << " new visible range " << city->getVisibleRange() << std::endl;
break;
case ',':
city->setVisibleRange(city->getVisibleRange()+1);
INFO << " new visible range " << city->getVisibleRange() << std::endl;
break;
case SDLK_PRINT:
OpenGL::ScreenHolder::Instance().makeScreenshot("screenshot.bmp");
break;
default:
return;
}
city->setPosition(mapPos[0], mapPos[1], mapPos[2]);
}
void drawScene(Uint32 ticks) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
OpenGL::ScreenHolder::Instance().set3DProjection();
city->draw(ticks);
glColor3f(1, 0, 0);
glDisable(GL_TEXTURE_2D);
glBegin(GL_POINTS);
glVertex3f(test_dot.x, test_dot.y, test_dot.z);
glEnd();
glEnable(GL_TEXTURE_2D);
glColor3f(1, 1, 1);
OpenGL::ScreenHolder::Instance().setFlatProjection();
glDisable(GL_DEPTH_TEST);
glPushMatrix();
glTranslatef(10, 10, 0);
OpenGL::DrawableFont & m_font = OpenGTA::FontCacheHolder::Instance().getFont("F_MTEXT.FON", 1);
m_font.drawString(city->getCurrentSector()->getFullName());
glPopMatrix();
/*glPushMatrix();
glTranslatef(5, 50, 0);
std::ostringstream strstr;
strstr << fps << " fps";
m_font->drawString(strstr.str());
glPopMatrix();*/
GUI::ManagerHolder::Instance().draw();
num_frames_drawn += 1;
glEnable(GL_DEPTH_TEST);