forked from nuvie/nuvie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvent.cpp
3793 lines (3460 loc) · 125 KB
/
Event.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
/*
* Event.cpp
* Nuvie
*
* Created by Eric Fry on Wed Mar 26 2003.
* Copyright (c) 2003. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <cassert>
#include <cstring>
#include "SDL.h"
#include "nuvieDefs.h"
#include "U6misc.h"
#include "Configuration.h"
#include "Game.h"
#include "GameClock.h"
#include "MapWindow.h"
#include "MsgScroll.h"
#include "ActorManager.h"
#include "Actor.h"
#include "Converse.h"
#include "Player.h"
#include "Party.h"
#include "Book.h"
#include "ViewManager.h"
#include "PortraitView.h"
#include "TimedEvent.h"
#include "InventoryView.h"
#include "PartyView.h"
#include "ActorView.h"
#include "CommandBar.h"
#include "Event.h"
#include "U6objects.h"
#include "Effect.h"
#include "EffectManager.h"
#include "NuvieIOFile.h"
#include "SaveManager.h"
#include "Magic.h"
#include "GUI_YesNoDialog.h"
#include "GameMenuDialog.h"
#include "views/InventoryWidget.h"
#include "Keys.h"
#include "SpellView.h"
#include "FpsCounter.h"
using std::string;
EventInput_s::~EventInput_s() {
if (target_init) delete target_init;
if (str) delete str;
if (loc) delete loc;
}
void EventInput_s::set_loc(MapCoord c) {
if ((type == EVENTINPUT_MAPCOORD || type == EVENTINPUT_MAPCOORD_DIR) && loc != 0) delete loc;
loc = new MapCoord(c);
}
Event::Event(Configuration *cfg) {
config = cfg;
clear_alt_code();
active_alt_code = 0;
alt_code_input_num = 0;
game = Game::get_game();
gui = NULL;
obj_manager = NULL;
map_window = NULL;
scroll = NULL;
clock = NULL;
player = NULL;
view_manager = NULL;
usecode = NULL;
magic = NULL;
drop_obj = NULL;
ts = 0;
drop_qty = 0;
drop_x = drop_y = -1;
rest_time = 0;
rest_guard = 0;
push_obj = NULL;
push_actor = NULL;
drop_from_key = false;
move_in_inventory = false;
time_queue = game_time_queue = NULL;
showingDialog = false;
gamemenu_dialog = NULL;
ignore_timeleft = false;
in_control_cheat = false;
looking_at_spellbook = false;
using_pickpocket_cheat = false;
do_not_show_target_cursor = false;
config->value("config/input/direction_selects_target", direction_selects_target, true);
mode = MOVE_MODE;
last_mode = MOVE_MODE;
fps_timestamp = 0;
fps_counter = 0;
scriptThread = NULL;
}
Event::~Event() {
delete time_queue;
delete game_time_queue;
}
bool Event::init(ObjManager *om, MapWindow *mw, MsgScroll *ms, Player *p, Magic *mg,
GameClock *gc, ViewManager *vm, UseCode *uc, GUI *g, KeyBinder *kb) {
gui = g;
obj_manager = om;
map_window = mw;
scroll = ms;
clock = gc;
player = p;
view_manager = vm;
usecode = uc;
mode = MOVE_MODE;
last_mode = MOVE_MODE;
input.get_direction = false;
input.get_text = false;
cursor_mode = false;
input.target_init = NULL;
time_queue = new TimeQueue;
game_time_queue = new TimeQueue;
magic = mg;
keybinder = kb;
fps_timestamp = clock->get_ticks();
fps_counter_widget = new FpsCounter(game);
gui->AddWidget(fps_counter_widget);
fps_counter_widget->Hide();
scriptThread = NULL;
return true;
}
void Event::update_timers() {
time_queue->call_timers(clock->get_ticks());
game_time_queue->call_timers(clock->get_game_ticks());
}
bool Event::update() {
bool idle = true;
// timed
time_queue->call_timers(clock->get_ticks());
game_time_queue->call_timers(clock->get_game_ticks());
// polled
SDL_Event event;
while (SDL_PollEvent(&event)) {
idle = false;
switch (gui->HandleEvent(&event)) {
case GUI_PASS :
if (handleEvent(&event) == false) {
game->quit();
return false;
}
break;
case GUI_QUIT : game->quit();
return false;
default : break;
}
}
if (idle)
gui->Idle(); // run Idle() for all widgets
if (showingDialog) // temp. fix to show normal cursor over quit dialog
game->set_mouse_pointer(0);
return true;
}
bool Event::handleSDL_KEYDOWN(const SDL_Event *event) {
// when casting the magic class will handle keyboard events
if (mode == KEYINPUT_MODE) {
SDL_Keycode sym = event->key.keysym.sym;
ActionKeyType action_key_type = OTHER_KEY;
if (!((magic->is_selecting_spell() && ((sym >= SDLK_a && sym <= SDLK_z) || sym == SDLK_BACKSPACE)) ||
((magic->is_waiting_for_location() || last_mode == USE_MODE) && sym >= SDLK_1 && sym <= SDLK_9))) {
ActionType a = keybinder->get_ActionType(event->key.keysym);
action_key_type = keybinder->GetActionKeyType(a);
switch (action_key_type) {
default: if (keybinder->handle_always_available_keys(a)) return true;
break;
}
}
input.type = EVENTINPUT_KEY;
input.key = sym;
input.action_key_type = action_key_type;
// callback should return a true value if it handled the event
if (action_key_type != CANCEL_ACTION_KEY && message(CB_DATA_READY, (char *) &input))
return true;
callback_target = 0;
endAction(); // no more keys for you! (end KEYINPUT_MODE)
keybinder->HandleEvent(event);
return true;
}
SDL_Keymod mods = SDL_GetModState();
// alt-code input
if (mods & KMOD_ALT) {
if (mode == MOVE_MODE)
switch (event->key.keysym.sym) {
case SDLK_KP_0:
case SDLK_0: alt_code_str[alt_code_len++] = '0';
break;
case SDLK_KP_1:
case SDLK_1: alt_code_str[alt_code_len++] = '1';
break;
case SDLK_KP_2:
case SDLK_2: alt_code_str[alt_code_len++] = '2';
break;
case SDLK_KP_3:
case SDLK_3: alt_code_str[alt_code_len++] = '3';
break;
case SDLK_KP_4:
case SDLK_4: alt_code_str[alt_code_len++] = '4';
break;
case SDLK_KP_5:
case SDLK_5: alt_code_str[alt_code_len++] = '5';
break;
case SDLK_KP_6:
case SDLK_6: alt_code_str[alt_code_len++] = '6';
break;
case SDLK_KP_7:
case SDLK_7: alt_code_str[alt_code_len++] = '7';
break;
case SDLK_KP_8:
case SDLK_8: alt_code_str[alt_code_len++] = '8';
break;
case SDLK_KP_9:
case SDLK_9: alt_code_str[alt_code_len++] = '9';
break;
default: keybinder->HandleEvent(event);
return true;
}
if (alt_code_len != 0) {
alt_code_str[alt_code_len] = '\0';
if (alt_code_len == 3) {
alt_code(alt_code_str);
clear_alt_code();
}
}
return true;
}
keybinder->HandleEvent(event);
return true;
}
void Event::target_spell() {
input.type = EVENTINPUT_KEY;
input.key = SDLK_RETURN; // only needed to overwrite old value so it isn't a number or backspace
input.action_key_type = DO_ACTION_KEY;
message(CB_DATA_READY, (char *) &input);
callback_target = 0;
endAction();
doAction();
}
void Event::close_spellbook() {
if (callback_target) {
callback_target = 0;
endAction();
}
cancelAction();
}
bool Event::handleEvent(const SDL_Event *event) {
if (game->user_paused())
return true;
switch (event->type) {
case SDL_MOUSEMOTION: break;
case SDL_MOUSEBUTTONDOWN: break;
case SDL_KEYUP:
if (event->key.keysym.sym == SDLK_RALT
|| event->key.keysym.sym == SDLK_LALT) {
clear_alt_code();
}
break;
case SDL_KEYDOWN: handleSDL_KEYDOWN(event);
break;
case SDL_QUIT : quitDialog();
break;
default: break;
}
if (input.get_text && scroll->has_input()) {
if (active_alt_code) {
endAction(); // exit INPUT_MODE
alt_code_input(scroll->get_input().c_str());
} else {
doAction();
}
}
return true;
}
void Event::get_direction(const char *prompt) {
// use_obj = src;
assert(mode != INPUT_MODE);
set_mode(INPUT_MODE); // saves previous mode
if (prompt)
scroll->display_string(prompt);
input.get_direction = true;
moveCursorToMapWindow();
map_window->centerCursor();
map_window->set_show_cursor(false);
// map_window->set_show_use_cursor(true); // set in moveCursorToMapWindow()
if (do_not_show_target_cursor && direction_selects_target)
map_window->set_show_use_cursor(false);
input.target_init = new MapCoord(map_window->get_cursorCoord()); // depends on MapWindow size
}
/* This version of get_direction() doesn't show the cursor. */
void Event::get_direction(const MapCoord &from, const char *prompt) {
get_direction(prompt);
map_window->moveCursor(from.x - map_window->get_cur_x(), from.y - map_window->get_cur_y());
input.target_init->x = from.x;
input.target_init->y = from.y;
if (input_really_needs_directon()) // actually getting a direction
{
if (!direction_selects_target)
map_window->set_show_cursor(true);
map_window->set_show_use_cursor(false);
map_window->set_mousecenter(from.x - map_window->get_cur_x(), from.y - map_window->get_cur_y());
}
}
void Event::get_target(const char *prompt) {
// use_obj = src;
assert(mode != INPUT_MODE);
set_mode(INPUT_MODE); // saves previous mode
if (prompt)
scroll->display_string(prompt);
input.get_direction = false;
map_window->centerCursor();
moveCursorToMapWindow();
}
void Event::get_target(const MapCoord &init, const char *prompt) {
get_target(prompt);
map_window->moveCursor(init.x, init.y);
}
/* Switch focus to MsgScroll and start getting user input. */
void Event::get_scroll_input(const char *allowed,
bool can_escape,
bool using_target_cursor,
bool set_numbers_only_to_true) {
assert(scroll);
if (!using_target_cursor) {
assert(mode != INPUT_MODE);
set_mode(INPUT_MODE); // saves previous mode
}
input.get_text = true;
scroll->set_input_mode(true, allowed, can_escape, using_target_cursor, set_numbers_only_to_true);
//no need to grab focus because any input will eventually reach MsgScroll,
// scroll->grab_focus();
}
void Event::get_inventory_obj(Actor *actor, bool getting_target) {
if (getting_target) {
get_target("");
moveCursorToInventory();
} else if (!game->is_new_style())
view_manager->set_inventory_mode();
if (game->is_new_style()) {
//view_manager->set_inventory_mode();
view_manager->open_container_view(actor); //FIXME need to open container gump in pickpocket mode.
view_manager->open_doll_view(actor);
} else {
view_manager->get_inventory_view()->set_actor(actor, true);
}
}
void Event::get_spell_num(Actor *caster, Obj *spell_container) {
//get_target("");
view_manager->set_spell_mode(caster, spell_container, true);
view_manager->get_current_view()->grab_focus();
}
/* Send all keyboard input to caller, with user_data.
ESC always cancels sending any further input. */
void Event::key_redirect(CallBack *caller, void *user_data) {
assert(mode != INPUT_MODE && mode != KEYINPUT_MODE);
request_input(caller, user_data);
set_mode(KEYINPUT_MODE); // saves previous mode
}
void Event::cancel_key_redirect() {
assert(mode == KEYINPUT_MODE);
endAction();
}
/* Switch focus to PortraitView, display a portrait, and wait for user input. */
void Event::display_portrait(Actor *actor, const char *name) {
view_manager->set_portrait_mode(actor, (char *) name);
view_manager->get_portrait_view()->set_waiting(true);
}
/* Set callback & callback_user_data so that a message will be sent to the
* caller when input has been gathered. */
void Event::request_input(CallBack *caller, void *user_data) {
callback_target = caller;
callback_user_data = (char *) user_data;
}
// typically this will be coming from inventory
bool Event::select_obj(Obj *obj, Actor *actor) {
if (looking_at_spellbook && view_manager->get_spell_view() != NULL) {
view_manager->get_spell_view()->close_look();
return false;
}
assert(mode == INPUT_MODE);
//assert(input.select_from_inventory == true);
input.type = EVENTINPUT_OBJECT;
input.obj = obj;
input.actor = actor;
endAction(); // mode = prev_mode
doAction();
return true;
}
bool Event::select_actor(Actor *actor) {
assert(mode == INPUT_MODE);
input.type = EVENTINPUT_MAPCOORD;
input.actor = actor;
input.set_loc(actor->get_location());
endAction(); // mode = prev_mode
doAction();
return true;
}
bool Event::select_direction(sint16 rel_x, sint16 rel_y) {
assert(mode == INPUT_MODE);
assert(input.get_direction == true);
input.type = EVENTINPUT_MAPCOORD_DIR;
input.set_loc(MapCoord(rel_x, rel_y));
// assumes mapwindow cursor is at the location
input.actor = map_window->get_actorAtCursor();
input.obj = map_window->get_objAtCursor();
endAction(); // mode = prev_mode
doAction();
return true;
}
// automatically converted to direction if requested
bool Event::select_target(uint16 x, uint16 y, uint8 z) {
// FIXME: is this even correct behavior?! if an arrow key is used, a direction
// should be returned, but you can still select any target with the mouse
// (which works, but then what's the point of using directions?)
if (input.get_direction)
return select_direction(x - input.target_init->x,
y - input.target_init->y);
if (mode != ATTACK_MODE) // FIXME: make ATTACK_MODE use INPUT_MODE
{ // need to handle weapon range
assert(mode == INPUT_MODE);
input.type = EVENTINPUT_MAPCOORD;
input.set_loc(MapCoord(x, y, z));
// assumes mapwindow cursor is at the location
input.actor = map_window->get_actorAtCursor();
input.obj = map_window->get_objAtCursor();
endAction(); // mode = prev_mode
}
doAction();
return true;
}
// called when selecting an actor by number
bool Event::select_party_member(uint8 num) {
Party *party = player->get_party();
if (num < party->get_party_size()) {
select_actor(party->get_actor(num));
return true;
}
return false;
}
bool Event::select_spell_num(sint16 spell_num) {
//assert(mode == INPUT_MODE);
//assert(input.select_from_inventory == true);
input.type = EVENTINPUT_SPELL_NUM;
input.spell_num = spell_num;
//endAction(); // mode = prev_mode
game->get_view_manager()->close_spell_mode();
doAction();
return true;
}
// move the cursor or walk around; do action for direction-targeted modes
bool Event::move(sint16 rel_x, sint16 rel_y) {
MapCoord cursor_coord;
if (game->user_paused())
return false;
EventMode current_mode;
if (last_mode == MULTIUSE_MODE && game->get_party()->is_in_combat_mode())
current_mode = ATTACK_MODE;
else
current_mode = mode;
switch (current_mode) {
case ATTACK_MODE : cursor_coord = map_window->get_cursorCoord();
cursor_coord.x = WRAPPED_COORD(cursor_coord.x + rel_x, cursor_coord.z);
cursor_coord.y = WRAPPED_COORD(cursor_coord.y + rel_y, cursor_coord.z);
if (!player->weapon_can_hit(cursor_coord.x, cursor_coord.y))
break;
DEBUG(0, LEVEL_DEBUGGING, "attack select(%d,%d)\n", cursor_coord.x, cursor_coord.y);
map_window->moveCursorRelative(rel_x, rel_y);
break;
case EQUIP_MODE : map_window->moveCursorRelative(rel_x, rel_y);
break;
case INPUT_MODE : {
bool needs_dir = input_really_needs_directon();
if (!direction_selects_target && needs_dir) {
cursor_coord = map_window->get_cursorCoord();
cursor_coord.x = WRAPPED_COORD(cursor_coord.x + rel_x, cursor_coord.z);
cursor_coord.y = WRAPPED_COORD(cursor_coord.y + rel_y, cursor_coord.z);
if (input.target_init->distance(cursor_coord) > 1)
break;
} else if (last_mode == CAST_MODE) {
cursor_coord = map_window->get_cursorCoord();
cursor_coord.x = WRAPPED_COORD(cursor_coord.x + rel_x, cursor_coord.z);
cursor_coord.y = WRAPPED_COORD(cursor_coord.y + rel_y, cursor_coord.z);
if (player->get_actor()->get_range(cursor_coord.x, cursor_coord.y) > 7)
break;
}
map_window->moveCursorRelative(rel_x, rel_y);
if (direction_selects_target && needs_dir)
select_direction(rel_x, rel_y);
break;
}
default :
if (player->check_walk_delay() && !view_manager->gumps_are_active()) {
player->moveRelative(rel_x, rel_y);
game->time_changed();
}
break;
}
return true;
}
/* Begin a conversation with an actor if him/her/it is willing to talk.
* Returns true if conversation starts.
*/
bool Event::perform_talk(Actor *actor) {
ActorManager *actor_manager = game->get_actor_manager();
Actor *pc = player->get_actor();
uint8 id = actor->get_actor_num();
if (game->get_game_type() != NUVIE_GAME_U6) {
return game->get_script()->call_talk_to_actor(actor);
}
if (actor->is_in_vehicle()) {
scroll->display_string("Not in vehicle.\n");
return false;
}
if (id == pc->get_actor_num()) // actor is controlled by player
{
// Note: being the player, this should ALWAYS use the real name
scroll->display_string(actor->get_name());
scroll->display_string("\n");
scroll->display_string("Talking to yourself?\n");
return false;
}
if (actor->is_in_party() && !actor->is_onscreen()) {
scroll->display_string(actor->get_name());
scroll->display_string("\n");
scroll->display_string("Not on screen.\n");
return false;
}
// FIXME: this check and the "no response" messages should be in Converse
if (!player->in_party_mode() && !pc->is_avatar()) //only the avatar can talk in solo mode
{
// always display look-string on failure
scroll->display_string(actor->get_name());
scroll->display_string("\n");
scroll->display_string("Not in solo mode.\n");
} else if (actor->is_sleeping() || actor->is_paralyzed() || actor->get_corpser_flag()
|| actor->get_alignment() == ACTOR_ALIGNMENT_EVIL
|| actor->get_alignment() == ACTOR_ALIGNMENT_CHAOTIC
|| (actor->get_alignment() == ACTOR_ALIGNMENT_NEUTRAL && actor->will_not_talk())) {
// always display name or look-string on failure
scroll->display_string(actor->get_name());
scroll->display_string("\n\nNo response\n");
} else if (game->get_converse()->start(actor)) // load and begin npc script
{
// try to use real name
scroll->display_string(actor->get_name());
scroll->display_string("\n");
// turn towards eachother
pc->face_actor(actor);
if (!actor->is_immobile())
actor->face_actor(pc);
return (true);
} else // some actor that has no script
{
// always display look-string on failure
scroll->display_string(actor_manager->look_actor(actor));
scroll->display_string("\n");
scroll->display_string("Funny, no response.\n");
}
return (false);
}
/* Talk to `actor'. Return to the prompt if no conversation starts.
* Returns the result of the talk function.
*/
bool Event::talk(Actor *actor) {
bool talking = true;
if (game->user_paused())
return (false);
endAction();
if (!actor) {
scroll->display_string("nothing!\n");
talking = false;
} else if (!perform_talk(actor))
talking = false;
if (!talking) {
// scroll->display_string("\n");
// scroll->display_prompt();
endAction(true);
}
return (talking);
}
bool Event::talk_cursor() {
Actor *actor = map_window->get_actorAtCursor();
if (actor && input.actor->is_visible())
return talk(actor);
return talk(map_window->get_objAtCursor());
}
bool Event::talk_start() {
if (game->user_paused())
return (false);
close_gumps();
get_target("Talk-");
return true;
}
/* You can talk to some objects using their quality as actor number. */
bool Event::talk(Obj *obj) {
ActorManager *actor_manager = game->get_actor_manager();
if (obj) {
if (game->get_game_type() == NUVIE_GAME_U6) {
if (obj->obj_n == OBJ_U6_SHRINE
|| obj->obj_n == OBJ_U6_STATUE_OF_MONDAIN
|| obj->obj_n == OBJ_U6_STATUE_OF_MINAX
|| obj->obj_n == OBJ_U6_STATUE_OF_EXODUS)
return (talk(actor_manager->get_actor(obj->quality)));
} else {
endAction();
bool status = game->get_script()->call_talk_to_obj(obj);
if (status == false) {
scroll->display_string("\n");
scroll->display_prompt();
}
return status;
}
}
scroll->display_string("nothing!\n");
endAction();
scroll->display_string("\n");
scroll->display_prompt();
return (false);
}
void Event::try_next_attack() {
if (Game::get_game()->get_actor_manager()->get_avatar()->get_hp() == 0) // need to end turn if Avatar died
{
endAction();
Game::get_game()->get_actor_manager()->startActors();
return;
} else if (player->attack_select_next_weapon(true) == false) {
player->subtract_movement_points(10);
game->get_actor_manager()->startActors(); // end player turn
endAction();
} else {
map_window->set_show_cursor(true);
mode = ATTACK_MODE; // FIXME: need to return after WAIT_MODE
//endAction(false);
//newAction(ATTACK_MODE);
}
}
bool Event::attack() {
MapCoord target = map_window->get_cursorCoord();
Actor *actor = map_window->get_actorAtCursor();
Actor *p = player->get_actor();
bool tile_is_black = map_window->tile_is_black(target.x, target.y);
if (game->get_script()->call_out_of_ammo(p, p->get_weapon_obj(player->get_current_weapon()), true)) {
// the function prints out the message
try_next_attack(); // SE and MD have weapons that need ammo and only take up 1 slot
return true;
} else if (tile_is_black)
scroll->display_string("nothing!\n");
else if (actor) {
if (actor->get_actor_num() == player->get_actor()->get_actor_num() //don't attack yourself.
|| (actor->is_in_party() && actor->get_alignment() == ACTOR_ALIGNMENT_GOOD)) {
ActorManager *actor_manager = game->get_actor_manager();
Actor *a = actor_manager->get_actor(actor->get_x(), actor->get_y(), actor->get_z(), true, actor);
if (a) // exclude previous target if we find another actor
actor = a;
else if (actor->get_actor_num() == player->get_actor()->get_actor_num()) {
scroll->display_string("pass.\n");
player->subtract_movement_points(10);
endAction(true);
return true;
}
}
if (actor->is_visible()) {
scroll->display_string(actor->get_name());
scroll->display_string(".\n");
}
}
if ((!actor || !actor->is_visible()) && !tile_is_black) {
Obj *obj = map_window->get_objAtCursor();
if (obj && (!obj->is_on_map() || !map_window->tile_is_black(obj->x, obj->y, obj))) {
scroll->display_string(obj_manager->get_obj_name(obj->obj_n, obj->frame_n));
scroll->display_string(".\n");
} else {
scroll->display_string(game->get_game_map()->look(target.x, target.y, target.z));
scroll->display_string(".\n");
}
}
map_window->set_show_cursor(false);
player->attack(target, actor);
try_next_attack();
return true;
}
bool Event::get_start() {
if (game->user_paused())
return false;
if (game->get_script()->call_is_ranged_select(GET))
get_target("Get-");
else
get_direction("Get-");
return true;
}
bool Event::push_start() {
if (game->user_paused())
return false;
push_obj = NULL;
push_actor = NULL;
if (game->get_script()->call_is_ranged_select(MOVE))
get_target("Move-");
else
get_direction("Move-");
return true;
}
/* Get object into an actor. (no mode change) */
bool Event::perform_get(Obj *obj, Obj *container_obj, Actor *actor) {
bool got_object = false;
bool can_perform_get = false;
//float weight;
if (game->user_paused())
return (false);
if (obj) {
if (!actor)
actor = player->get_actor();
if (obj->is_on_map() && map_window->tile_is_black(obj->x, obj->y, obj)) {
scroll->display_string("nothing");
} else {
scroll->display_string(obj_manager->look_obj(obj));
if (game->using_hackmove())
can_perform_get = true;
else if (!map_window->can_get_obj(actor, obj)) {
scroll->display_string("\n\nCan't reach it.");
} else if (obj->is_on_map()) {
MapCoord target(obj->x, obj->y, obj->z);
if (!game->get_script()->call_is_ranged_select(GET)
&& player->get_actor()->get_location().distance(target) > 1
&& map_window->get_interface() == INTERFACE_NORMAL) {
scroll->display_string("\n\nOut of range!");
} else if (obj_manager->obj_is_damaging(obj, actor)) {
return false;
} else {
can_perform_get = true;
}
} else {
can_perform_get = true;
}
}
} else
scroll->display_string("nothing");
if (can_perform_get) {
// perform GET usecode (can't add to container)
if (usecode->has_getcode(obj) && (usecode->get_obj(obj, actor) == false)) {
game->get_script()->call_actor_subtract_movement_points(actor, 3);
scroll->display_string("\n");
scroll->display_prompt();
map_window->updateBlacking();
return (false); // ???
}
got_object = game->get_script()->call_actor_get_obj(actor, obj, container_obj);
}
scroll->display_string("\n\n");
scroll->display_prompt();
map_window->updateBlacking();
return (got_object);
}
/* Get object at selected position, and end action. */
bool Event::get(sint16 rel_x, sint16 rel_y) {
uint16 x, y;
uint8 level;
player->get_location(&x, &y, &level);
return get(MapCoord((uint16) (x + rel_x), (uint16) (y + rel_y), level));
}
bool Event::get(MapCoord coord) {
Obj *obj = obj_manager->get_obj(coord.x, coord.y, coord.z, OBJ_SEARCH_TOP, OBJ_EXCLUDE_IGNORED);
bool got_object;
if (!game->is_new_style())
got_object = perform_get(obj, view_manager->get_inventory_view()->get_inventory_widget()->get_container(),
player->get_actor());
else
got_object = perform_get(obj, NULL, player->get_actor());
view_manager->update(); //redraw views to show new item.
endAction();
return got_object;
}
bool Event::use_start() {
if (game->user_paused())
return false;
if (game->get_script()->call_is_ranged_select(USE))
get_target("Use-");
else
get_direction("Use-");
return true;
}
bool Event::use(Obj *obj) {
if (game->user_paused())
return false;
if (obj && obj->is_on_map() && map_window->tile_is_black(obj->x, obj->y, obj)) {
Obj *bottom_obj = obj_manager->get_obj(obj->x, obj->y, obj->z, false);
if (game->get_game_type() == NUVIE_GAME_U6 && bottom_obj->obj_n == OBJ_U6_SECRET_DOOR // hack for frame 2
&& !map_window->tile_is_black(obj->x, obj->y, bottom_obj))
obj = bottom_obj;
else
obj = NULL;
}
if (!obj) {
scroll->display_string("nothing\n");
endAction(true);
return true;
}
MapCoord target(obj->x, obj->y, obj->z);
MapCoord player_loc = player->get_actor()->get_location();
bool display_prompt = true;
scroll->display_string(obj_manager->look_obj(obj));
scroll->display_string("\n");
if (!usecode->has_usecode(obj)) {
scroll->display_string("\nNot usable\n");
DEBUG(0, LEVEL_DEBUGGING, "Object %d:%d\n", obj->obj_n, obj->frame_n);
} else if (!obj->is_in_inventory() && map_window->get_interface() == INTERFACE_NORMAL
&& !game->get_script()->call_is_ranged_select(USE) && player->get_actor()->get_location().distance(target) > 1) {
scroll->display_string("\nOut of range!\n");
DEBUG(0, LEVEL_DEBUGGING, "distance to object: %d\n", player->get_actor()->get_location().distance(target));
} else if (!player->in_party_mode() && obj->is_in_inventory() && !obj->get_actor_holding_obj()->is_onscreen()) {
scroll->display_string("\nNot on screen.\n");
} else if (!obj->is_in_inventory() && !game->get_script()->call_is_ranged_select(USE)
&& !map_window->can_get_obj(player->get_actor(), obj) && player_loc != target) {
scroll->display_string("\nCan't reach it\n");
} else // Usable
{
display_prompt = usecode->use_obj(obj, player->get_actor());
player->subtract_movement_points(MOVE_COST_USE);
}
if (mode == USE_MODE && usecode->get_running_script() == NULL) // check mode because UseCode may have changed it
endAction(display_prompt);
return true;
}
bool Event::use(Actor *actor, uint16 x, uint16 y) {
if (game->user_paused())
return false;
bool display_prompt = true;
Obj *obj = actor->make_obj();
if (!map_window->tile_is_black(x, y) && usecode->has_usecode(actor)) {
if (game->get_game_type() == NUVIE_GAME_U6 && obj->obj_n == OBJ_U6_HORSE_WITH_RIDER)
scroll->display_string("horse");
else
scroll->display_string(obj_manager->look_obj(obj));
scroll->display_string("\n");
MapCoord player_loc = player->get_actor()->get_location();
MapCoord target = MapCoord(x, y, player_loc.z);
if (player_loc.distance(target) > 1
&& map_window->get_interface() == INTERFACE_NORMAL) {
scroll->display_string("\nOut of range!\n");
DEBUG(0, LEVEL_DEBUGGING, "distance to object: %d\n", player_loc.distance(target));
} else if (!can_get_to_actor(actor, x, y))
scroll->display_string("\nBlocked.\n");
else {
display_prompt = usecode->use_obj(obj, player->get_actor());
player->subtract_movement_points(5);
}
} else {
scroll->display_string("nothing\n");
DEBUG(0, LEVEL_DEBUGGING, "Object %d:%d\n", obj->obj_n, obj->frame_n);
}
// FIXME: usecode might request input, causing the obj to be accessed again,
// so we can't delete it in that case
assert(mode == USE_MODE || game->user_paused());
delete_obj(obj); // we were using an actor so free the temp Obj
if (mode == USE_MODE) // check mode because UseCode may have changed it
endAction(display_prompt);
return (true);
}
bool Event::use(sint16 rel_x, sint16 rel_y) {
map_window->centerCursor();
map_window->moveCursorRelative(rel_x, rel_y);
return use(map_window->get_cursorCoord());
}