-
Notifications
You must be signed in to change notification settings - Fork 119
/
custom_object_functions.cpp
2400 lines (2023 loc) · 82.3 KB
/
custom_object_functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <boost/bind.hpp>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <time.h>
#include "achievements.hpp"
#include "asserts.hpp"
#include "blur.hpp"
#include "clipboard.hpp"
#include "collision_utils.hpp"
#include "controls.hpp"
#include "current_generator.hpp"
#include "custom_object_functions.hpp"
#include "custom_object.hpp"
#include "debug_console.hpp"
#include "draw_scene.hpp"
#include "entity.hpp"
#include "filesystem.hpp"
#include "formatter.hpp"
#include "formula_callable_definition.hpp"
#include "formula_function_registry.hpp"
#include "formula_profiler.hpp"
#include "i18n.hpp"
#include "json_parser.hpp"
#include "level.hpp"
#include "level_runner.hpp"
#include "load_level.hpp"
#include "object_events.hpp"
#include "pause_game_dialog.hpp"
#include "player_info.hpp"
#include "raster.hpp"
#if !defined(__native_client__)
#include "tbs_client.hpp"
#endif
#include "texture.hpp"
#include "message_dialog.hpp"
#include "options_dialog.hpp"
#include "playable_custom_object.hpp"
#include "preferences.hpp"
#include "random.hpp"
#include "raster_distortion.hpp"
#include "rectangle_rotator.hpp"
#include "sound.hpp"
#include "speech_dialog.hpp"
#include "stats.hpp"
#include "string_utils.hpp"
#include "thread.hpp"
#include "unit_test.hpp"
#include "preferences.hpp"
#include "settings_dialog.hpp"
#include "module.hpp"
#include "variant_utils.hpp"
#include "widget_factory.hpp"
#include "graphical_font.hpp"
using namespace game_logic;
namespace {
const std::string FunctionModule = "custom_object";
FUNCTION_DEF(set_language, 1, 1, "set_language(str): set the language using a new locale code")
std::string locale = args()[0]->evaluate(variables).as_string();
preferences::set_locale(locale);
i18n::init();
graphical_font::init_for_locale(i18n::get_locale());
return variant(0);
END_FUNCTION_DEF(set_language)
FUNCTION_DEF(time, 0, 0, "time() -> timestamp: returns the current real time")
formula::fail_if_static_context();
time_t t1;
time(&t1);
return variant(static_cast<int>(t1));
END_FUNCTION_DEF(time)
FUNCTION_DEF(translate, 1, 1, "translate(str): returns the translated version of the given string")
return variant(i18n::tr(args()[0]->evaluate(variables).as_string()));
END_FUNCTION_DEF(translate)
FUNCTION_DEF(performance, 0, 0, "performance(): returns an object with current performance stats")
formula::fail_if_static_context();
return variant(performance_data::current());
END_FUNCTION_DEF(performance)
FUNCTION_DEF(get_clipboard_text, 0, 0, "get_clipboard_text(): returns the text currentl in the windowing clipboard")
formula::fail_if_static_context();
return variant(copy_from_clipboard(false));
END_FUNCTION_DEF(get_clipboard_text)
class set_clipboard_text_command : public game_logic::command_callable
{
std::string str_;
public:
explicit set_clipboard_text_command(const std::string& str) : str_(str)
{}
virtual void execute(game_logic::formula_callable& ob) const {
copy_to_clipboard(str_, false);
}
};
FUNCTION_DEF(set_clipboard_text, 1, 1, "set_clipboard_text(str): sets the clipboard text to the given string")
return variant(new set_clipboard_text_command(args()[0]->evaluate(variables).as_string()));
END_FUNCTION_DEF(set_clipboard_text)
#if !defined(__native_client__)
FUNCTION_DEF(tbs_client, 2, 3, "tbs_client(host, port, session=-1): creates a client object to the tbs server")
const std::string host = args()[0]->evaluate(variables).as_string();
const int port = args()[1]->evaluate(variables).as_int();
const int session = args().size() >= 3 ? args()[2]->evaluate(variables).as_int() : -1;
return variant(new tbs::client(host, formatter() << port, session));
END_FUNCTION_DEF(tbs_client)
void tbs_send_event(entity_ptr e, game_logic::map_formula_callable_ptr callable, const std::string& ev)
{
e->handle_event(ev, callable.get());
}
class tbs_send_command : public entity_command_callable
{
variant client_, msg_;
public:
tbs_send_command(variant client, variant msg) : client_(client), msg_(msg)
{}
virtual void execute(level& lvl, entity& ob) const {
tbs::client* tbs_client = client_.try_convert<tbs::client>();
game_logic::map_formula_callable_ptr callable(new game_logic::map_formula_callable);
tbs_client->send_request(msg_.write_json(), callable, boost::bind(tbs_send_event, entity_ptr(&ob), callable, _1));
}
};
FUNCTION_DEF(tbs_send, 2, 2, "tbs_send(tbs_client, msg): sends a message through the given tbs_client connection")
variant client = args()[0]->evaluate(variables);
variant msg = args()[1]->evaluate(variables);
return variant(new tbs_send_command(client, msg));
END_FUNCTION_DEF(tbs_send)
class tbs_process_command : public entity_command_callable
{
variant client_;
public:
explicit tbs_process_command(variant client) : client_(client)
{}
virtual void execute(level& lvl, entity& ob) const {
tbs::client* tbs_client = client_.try_convert<tbs::client>();
tbs_client->process();
}
};
FUNCTION_DEF(tbs_process, 1, 1, "tbs_process(tbs_client): processes events for the tbs client")
variant client = args()[0]->evaluate(variables);
return variant(new tbs_process_command(client));
END_FUNCTION_DEF(tbs_process)
#endif // __native_client__
class report_command : public entity_command_callable
{
variant v_;
public:
explicit report_command(variant v) : v_(v)
{}
virtual void execute(level& lvl, entity& ob) const {
stats::record(v_);
}
};
FUNCTION_DEF(report, 1, 1, "report(): Write a key and a value into [custom] in the stats.")
return variant(new report_command(args()[0]->evaluate(variables)));
END_FUNCTION_DEF(report)
namespace {
int show_simple_option_dialog(level& lvl, const std::string& text, const std::vector<std::string>& options)
{
std::vector<std::string> txt;
txt.push_back(text);
boost::shared_ptr<speech_dialog> d(new speech_dialog);
d->set_text(txt);
d->set_options(options);
lvl.add_speech_dialog(d);
bool done = false;
while(!done) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
if(d->key_press(event)) {
done = true;
}
}
if(d->process() || d->detect_joystick_press()) {
done = true;
}
draw_scene(lvl, last_draw_position(), &lvl.player()->get_entity());
SDL_GL_SwapBuffers();
#if defined(__ANDROID__)
graphics::reset_opengl_state();
#endif
SDL_Delay(20);
}
lvl.remove_speech_dialog();
return d->option_selected();
}
}
class set_save_slot_command : public entity_command_callable
{
public:
explicit set_save_slot_command(int slot) : slot_(slot)
{}
virtual void execute(level& lvl, entity& ob) const {
bool has_options = false;
std::vector<std::string> options;
std::string SaveFiles[] = {"save.cfg", "save2.cfg", "save3.cfg", "save4.cfg"};
int nslot = 0;
foreach(const std::string& fname, SaveFiles) {
options.push_back(formatter() << "Slot " << (nslot+1) << ": (empty)");
const std::string path = std::string(preferences::user_data_path()) + "/" + fname;
if(sys::file_exists(path)) {
has_options = true;
try {
const variant doc = json::parse_from_file(path);
if(doc.is_null() == false) {
options.back() = formatter() << "Slot " << (nslot+1) << ": " << doc["title"].as_string();
}
} catch(json::parse_error&) {
}
}
++nslot;
}
if(slot_ >= 0) {
std::cerr << "setting save slot: " << slot_ << " -> " << SaveFiles[slot_] << "\n";
preferences::set_save_slot(SaveFiles[slot_]);
} else if(has_options) {
const int noption = show_simple_option_dialog(lvl, _("Select save slot to use."), options);
if(noption != -1) {
std::cerr << "setting save slot: " << noption << " -> " << SaveFiles[noption] << "\n";
preferences::set_save_slot(SaveFiles[noption]);
}
}
}
int slot_;
};
FUNCTION_DEF(set_save_slot, 0, 1, "set_save_slot((optional) int slot): Allows the user to select the save slot, if no slot is specified a dialog is displayed.")
const int slot = (args().size() > 0) ? args()[0]->evaluate(variables).as_int() : -1;
ASSERT_LOG(slot == -1 || (slot >= 1 && slot <= 4), "Invalid slot specified: " << slot);
return variant(new set_save_slot_command(slot-1));
END_FUNCTION_DEF(set_save_slot)
class save_game_command : public entity_command_callable
{
bool persistent_;
public:
explicit save_game_command(bool persistent) : persistent_(persistent)
{}
virtual void execute(level& lvl, entity& ob) const {
lvl.player()->get_entity().save_game();
if(persistent_) {
variant node = lvl.write();
if(sound::current_music().empty() == false) {
node = node.add_attr(variant("music"), variant(sound::current_music()));
}
sys::write_file(preferences::save_file_path(), node.write_json());
}
}
};
FUNCTION_DEF(checkpoint_game, 0, 0, "checkpoint_game(): saves a checkpoint of the game")
return variant(new save_game_command(false));
END_FUNCTION_DEF(checkpoint_game)
FUNCTION_DEF(get_save_document, 1, 1, "get_save_document(int slot): gets the FFL document for the save in the given slot")
const int slot = args()[0]->evaluate(variables).as_int();
std::string fname = "save.cfg";
if(slot != 0) {
fname = formatter() << "save" << (slot+1) << ".cfg";
}
const std::string path = std::string(preferences::user_data_path()) + "/" + fname;
try {
const variant v = json::parse_from_file(path, json::JSON_NO_PREPROCESSOR);
return v;
} catch(json::parse_error&) {
return variant();
}
END_FUNCTION_DEF(get_save_document)
FUNCTION_DEF(save_game, 0, 0, "save_game(): saves the current game state")
return variant(new save_game_command(true));
END_FUNCTION_DEF(save_game)
class load_game_command : public entity_command_callable
{
std::string transition_;
int slot_;
public:
load_game_command(const std::string& transition, int slot) : transition_(transition), slot_(slot) {}
virtual void execute(level& lvl, entity& ob) const {
std::string dest_file;
if(slot_ >= 0) {
std::string fname = "save.cfg";
if(slot_ != 0) {
fname = formatter() << "save" << (slot_+1) << ".cfg";
}
if(sys::file_exists(std::string(preferences::user_data_path()) + "/" + fname)) {
preferences::set_save_slot(fname);
dest_file = fname;
}
}
if(dest_file.empty()) {
std::vector<std::string> save_options;
save_options.push_back("save.cfg");
for(int n = 2; n <= 4; ++n) {
std::string fname = formatter() << "save" << n << ".cfg";
if(sys::file_exists(std::string(preferences::user_data_path()) + "/" + fname)) {
save_options.push_back(fname);
}
}
int noption = 0;
if(save_options.size() > 1) {
int nslot = 1;
std::vector<std::string> option_descriptions;
foreach(const std::string& option, save_options) {
const std::string fname = std::string(preferences::user_data_path()) + "/" + option;
try {
const variant doc = json::parse_from_file(fname);
option_descriptions.push_back(formatter() << "Slot " << nslot << ": " << doc["title"].as_string());
} catch(json::parse_error&) {
option_descriptions.push_back(formatter() << "Slot " << nslot << ": Frogatto");
}
++nslot;
}
noption = show_simple_option_dialog(lvl, _("Select save slot to load."), option_descriptions);
if(noption == -1) {
return;
}
preferences::set_save_slot(save_options[noption]);
}
dest_file = save_options[noption];
}
level::portal p;
p.level_dest = dest_file;
p.dest_starting_pos = true;
p.saved_game = true;
p.transition = transition_;
lvl.force_enter_portal(p);
}
};
FUNCTION_DEF(load_game, 0, 2, "load_game(transition, slot): loads the saved game. If transition (a string) is given, it will use that type of transition.")
std::string transition;
int slot = -1;
if(args().size() >= 1) {
transition = args()[0]->evaluate(variables).as_string();
}
if(args().size() >= 2) {
slot = args()[1]->evaluate(variables).as_int();
}
return variant(new load_game_command(transition, slot));
END_FUNCTION_DEF(load_game)
FUNCTION_DEF(can_load_game, 0, 0, "can_load_game(): returns true if there is a saved game available to load")
return variant(sys::file_exists(preferences::save_file_path()));
END_FUNCTION_DEF(can_load_game)
FUNCTION_DEF(available_save_slots, 0, 0, "available_save_slots(): returns a list of numeric indexes of available save slots")
std::vector<variant> result;
for(int slot = 0; slot != 4; ++slot) {
std::string fname = "save.cfg";
if(slot != 0) {
fname = formatter() << "save" << (slot+1) << ".cfg";
}
if(json::file_exists_and_is_valid(std::string(preferences::user_data_path()) + "/" + fname)) {
result.push_back(variant(slot));
}
}
return variant(&result);
END_FUNCTION_DEF(available_save_slots)
class move_to_standing_command : public entity_command_callable
{
public:
virtual void execute(level& lvl, entity& ob) const {
ob.move_to_standing(lvl);
}
};
FUNCTION_DEF(move_to_standing, 0, 0, "move_to_standing(): tries to move the object downwards if it's in the air, or upwards if it's in solid space, until it's standing on solid ground.");
return variant(new move_to_standing_command());
END_FUNCTION_DEF(move_to_standing)
class music_command : public entity_command_callable
{
public:
explicit music_command(const std::string& name, const bool loops)
: name_(name), loops_(loops)
{}
virtual void execute(level& lvl, entity& ob) const {
if(loops_){
sound::play_music(name_);
}else{
sound::play_music_interrupt(name_);
}
}
private:
std::string name_;
bool loops_;
};
FUNCTION_DEF(music, 1, 1, "music(string id): plays the music file given by 'id' in a loop")
return variant(new music_command(
args()[0]->evaluate(variables).as_string(),
true));
END_FUNCTION_DEF(music)
FUNCTION_DEF(music_onetime, 1, 1, "music_onetime(string id): plays the music file given by 'id' once")
return variant(new music_command(
args()[0]->evaluate(variables).as_string(),
false));
END_FUNCTION_DEF(music_onetime)
class sound_command : public entity_command_callable
{
public:
explicit sound_command(const std::string& name, const bool loops, float volume)
: names_(util::split(name)), loops_(loops), volume_(volume)
{}
virtual void execute(level& lvl, entity& ob) const {
if(loops_){
if (names_.empty() == false){
int randomNum = rand()%names_.size(); //like a 1d-size die
if(names_[randomNum].empty() == false) {
sound::play_looped(names_[randomNum], &ob, volume_);
}
}
}else{
//std::cerr << "PLAY SOUND COMMAND: " << name_ << "\n";
if (names_.empty() == false){
int randomNum = rand()%names_.size(); //like a 1d-size die
if(names_[randomNum].empty() == false) {
sound::play(names_[randomNum], &ob, volume_);
}
}
}
}
private:
std::vector<std::string> names_;
bool loops_;
float volume_;
};
FUNCTION_DEF(sound, 1, 2, "sound(string id, decimal volume): plays the sound file given by 'id'")
return variant(new sound_command(
args()[0]->evaluate(variables).as_string(),
false,
args().size() > 1 ? args()[1]->evaluate(variables).as_decimal().as_float() : 1.0));
END_FUNCTION_DEF(sound)
FUNCTION_DEF(sound_loop, 1, 2, "sound_loop(string id, decimal volume): plays the sound file given by 'id' in a loop")
return variant(new sound_command(
args()[0]->evaluate(variables).as_string(),
true,
args().size() > 1 ? args()[1]->evaluate(variables).as_decimal().as_float() : 1.0));
END_FUNCTION_DEF(sound_loop)
class sound_volume_command : public entity_command_callable {
public:
explicit sound_volume_command(const int volume)
: volume_(volume)
{}
virtual void execute(level& lvl,entity& ob) const {
//sound::change_volume(&ob, volume_);
ob.set_sound_volume(volume_);
}
private:
std::string name_;
int volume_;
};
FUNCTION_DEF(sound_volume, 1, 1, "sound_volume(int volume): sets the volume of sound effects")
return variant(new sound_volume_command(
args()[0]->evaluate(variables).as_int()));
END_FUNCTION_DEF(sound_volume)
class stop_sound_command : public entity_command_callable
{
public:
explicit stop_sound_command(const std::string& name)
: name_(name)
{}
virtual void execute(level& lvl, entity& ob) const {
sound::stop_sound(name_, &ob);
}
private:
std::string name_;
};
FUNCTION_DEF(stop_sound, 1, 1, "stop_sound(string id): stops the sound that the current object is playing with the given id")
return variant(new stop_sound_command(
args()[0]->evaluate(variables).as_string()));
END_FUNCTION_DEF(stop_sound)
class preload_sound_command : public entity_command_callable
{
public:
explicit preload_sound_command(const std::string& name)
: name_(name)
{}
virtual void execute(level& lvl, entity& ob) const {
sound::preload(name_);
}
private:
std::string name_;
};
FUNCTION_DEF(preload_sound, 1, 1, "preload_sound(string id): preload the given sound so it'll be in the sound effects cache")
return variant(new preload_sound_command(
args()[0]->evaluate(variables).as_string()));
END_FUNCTION_DEF(preload_sound)
class screen_flash_command : public entity_command_callable
{
public:
screen_flash_command(const graphics::color_transform& color,
const graphics::color_transform& delta, int duration)
: color_(color), delta_(delta), duration_(duration)
{}
virtual void execute(level& lvl, entity& ob) const {
screen_color_flash(color_, delta_, duration_);
}
private:
graphics::color_transform color_, delta_;
int duration_;
};
FUNCTION_DEF(screen_flash, 2, 3, "screen_flash(list int[4] color, (optional) list int[4] delta, int duration): flashes the screen the given color, and keeps the flash going for duration cycles. If delta is given, the color of the flash will be changed every cycle until the duration expires.")
const variant color = args()[0]->evaluate(variables);
const variant delta = args().size() > 2 ? args()[1]->evaluate(variables) : variant();
const variant duration = args()[args().size() - 1]->evaluate(variables);
ASSERT_LOG(color.is_list() && color.num_elements() == 4 &&
(delta.is_null() || delta.is_list() && delta.num_elements() == 4) &&
duration.is_int(),
"BAD ARGUMENT TO screen_flash() FUNCTION: ARGUMENT FORMAT "
"IS screen_flash([r,g,b,a], (optional)[dr,dg,db,da], duration)");
graphics::color_transform delta_color = graphics::color_transform(0,0,0,0);
if(delta.is_null() == false) {
delta_color = graphics::color_transform(
delta[0].as_int(), delta[1].as_int(),
delta[2].as_int(), delta[3].as_int());
}
return variant(new screen_flash_command(
graphics::color_transform(color[0].as_int(), color[1].as_int(),
color[2].as_int(), color[3].as_int()),
delta_color, duration.as_int()));
END_FUNCTION_DEF(screen_flash)
class title_command : public entity_command_callable
{
public:
title_command(const std::string& title, int duration)
: title_(title), duration_(duration)
{}
virtual void execute(level& lvl, entity& ob) const {
set_scene_title(title_, duration_);
}
private:
std::string title_;
int duration_;
};
FUNCTION_DEF(title, 1, 2, "title(string text, int duration=50): shows level title text on the screen for duration cycles")
return variant(new title_command(
args()[0]->evaluate(variables).as_string(),
args().size() >= 2 ? args()[1]->evaluate(variables).as_int() : 50));
END_FUNCTION_DEF(title)
class shake_screen_command : public entity_command_callable
{
public:
explicit shake_screen_command(int x_offset, int y_offset, int x_velocity, int y_velocity)
: x_offset_(x_offset), y_offset_(y_offset), x_velocity_(x_velocity), y_velocity_(y_velocity)
{}
virtual void execute(level& lvl, entity& ob) const {
screen_position& screen_pos = last_draw_position();
screen_pos.shake_x_offset = x_offset_;
screen_pos.shake_y_offset = y_offset_;
screen_pos.shake_x_vel = x_velocity_;
screen_pos.shake_y_vel = y_velocity_;
}
private:
int x_offset_,y_offset_,x_velocity_,y_velocity_;
};
FUNCTION_DEF(shake_screen, 4, 4, "shake_screen(int x_offset, int y_offset, int x_velocity, int y_velocity): makes the screen camera shake")
return variant(new shake_screen_command(
args()[0]->evaluate(variables).as_int(),
args()[1]->evaluate(variables).as_int(),
args()[2]->evaluate(variables).as_int(),
args()[3]->evaluate(variables).as_int() ));
END_FUNCTION_DEF(shake_screen)
FUNCTION_DEF(radial_current, 2, 2, "radial_current(int intensity, int radius) -> current object: creates a current generator with the given intensity and radius")
return variant(new radial_current_generator(args()[0]->evaluate(variables).as_int(), args()[1]->evaluate(variables).as_int()));
END_FUNCTION_DEF(radial_current)
FUNCTION_DEF(distortion, 3, 3, "distortion(int, int, int): (currently unsupported")
return variant(new graphics::radial_distortion(
args()[0]->evaluate(variables).as_int(),
args()[1]->evaluate(variables).as_int(),
args()[2]->evaluate(variables).as_int()));
END_FUNCTION_DEF(distortion)
class execute_on_command : public entity_command_callable
{
entity_ptr e_;
variant cmd_;
public:
execute_on_command(entity_ptr e, variant cmd) : e_(e), cmd_(cmd)
{}
virtual void execute(level& lvl, entity& ob) const {
e_->execute_command(cmd_);
}
};
FUNCTION_DEF(execute, 2, 2, "execute(object context, command cmd): this function will execute the command or list of commands given by cmd on the object given by context. For instance, animation('foo') will set the current object to animation 'foo'. execute(obj, animation('foo')) can be used to set the object given by obj to the animation 'foo'.")
entity_ptr e(args()[0]->evaluate(variables).convert_to<entity>());
variant cmd = args()[1]->evaluate(variables);
return variant(new execute_on_command(e, cmd));
END_FUNCTION_DEF(execute)
class spawn_command : public entity_command_callable
{
public:
spawn_command(boost::intrusive_ptr<custom_object> obj, variant instantiation_commands)
: obj_(obj), instantiation_commands_(instantiation_commands)
{}
virtual void execute(level& lvl, entity& ob) const {
obj_->set_level(lvl);
obj_->set_spawned_by(ob.label());
if(!place_entity_in_level_with_large_displacement(lvl, *obj_)) {
return;
}
lvl.add_character(obj_);
obj_->execute_command(instantiation_commands_);
//send an event to the parent to let them know they've spawned a child,
//and let them record the child's details.
game_logic::map_formula_callable* spawn_callable(new game_logic::map_formula_callable);
variant holder(spawn_callable);
spawn_callable->add("spawner", variant(&ob));
spawn_callable->add("child", variant(obj_.get()));
ob.handle_event("child_spawned", spawn_callable);
obj_->handle_event("spawned", spawn_callable);
obj_->create_object();
if(entity_collides(lvl, *obj_, MOVE_NONE)) {
lvl.remove_character(obj_);
}
}
private:
boost::intrusive_ptr<custom_object> obj_;
variant instantiation_commands_;
};
FUNCTION_DEF(spawn, 4, 5, "spawn(string type_id, int midpoint_x, int midpoint_y, int facing, (optional) list of commands cmd): will create a new object of type given by type_id with the given midpoint and facing. Immediately after creation the object will have any commands given by cmd executed on it. The child object will have the spawned event sent to it, and the parent object will have the child_spawned event sent to it.")
formula::fail_if_static_context();
const std::string type = EVAL_ARG(0).as_string();
const int x = EVAL_ARG(1).as_int();
const int y = EVAL_ARG(2).as_int();
const bool facing = EVAL_ARG(3).as_int() > 0;
boost::intrusive_ptr<custom_object> obj(new custom_object(type, x, y, facing));
obj->set_pos(obj->x() - obj->current_frame().width() / 2 , obj->y() - obj->current_frame().height() / 2);
variant commands;
if(args().size() > 4) {
map_formula_callable_ptr callable = new map_formula_callable(&variables);
callable->add("child", variant(obj.get()));
commands = args()[4]->evaluate(*callable);
}
return variant(new spawn_command(obj, commands));
END_FUNCTION_DEF(spawn)
FUNCTION_DEF(spawn_player, 4, 5, "spawn_player(string type_id, int midpoint_x, int midpoint_y, int facing, (optional) list of commands cmd): identical to spawn except that the new object is playable.")
formula::fail_if_static_context();
const std::string type = EVAL_ARG(0).as_string();
const int x = EVAL_ARG(1).as_int();
const int y = EVAL_ARG(2).as_int();
const bool facing = EVAL_ARG(3).as_int() > 0;
boost::intrusive_ptr<custom_object> obj(new custom_object(type, x, y, facing));
obj.reset(new playable_custom_object(*obj));
obj->set_pos(obj->x() - obj->current_frame().width() / 2 , obj->y() - obj->current_frame().height() / 2);
variant commands;
if(args().size() > 4) {
map_formula_callable_ptr callable = new map_formula_callable(&variables);
callable->add("child", variant(obj.get()));
commands = args()[4]->evaluate(*callable);
}
return variant(new spawn_command(obj, commands));
END_FUNCTION_DEF(spawn_player)
FUNCTION_DEF(object, 1, 5, "object(string type_id, int midpoint_x, int midpoint_y, int facing, (optional) map properties) -> object: constructs and returns a new object. Note that the difference between this and spawn is that spawn returns a command to actually place the object in the level. object only creates the object and returns it. It may be stored for later use.")
formula::fail_if_static_context();
const std::string type = args()[0]->evaluate(variables).as_string();
custom_object* obj;
if(args().size() > 1) {
const int x = args()[1]->evaluate(variables).as_int();
const int y = args()[2]->evaluate(variables).as_int();
const bool face_right = args()[3]->evaluate(variables).as_int() > 0;
obj = new custom_object(type, x, y, face_right);
} else {
const int x = 0;
const int y = 0;
const bool face_right = true;
obj = new custom_object(type, x, y, face_right);
}
//adjust so the object's x/y is its midpoint.
obj->set_pos(obj->x() - obj->current_frame().width() / 2 , obj->y() - obj->current_frame().height() / 2);
if(args().size() > 4) {
variant properties = args()[4]->evaluate(variables);
variant keys = properties.get_keys();
for(int n = 0; n != keys.num_elements(); ++n) {
variant value = properties[keys[n]];
obj->mutate_value(keys[n].as_string(), value);
}
}
return variant(obj);
END_FUNCTION_DEF(object)
FUNCTION_DEF(object_playable, 1, 5, "object_playable(string type_id, int midpoint_x, int midpoint_y, int facing, (optional) map properties) -> object: constructs and returns a new object. Note that the difference between this and spawn is that spawn returns a command to actually place the object in the level. object_playable only creates the playble object and returns it. It may be stored for later use.")
formula::fail_if_static_context();
const std::string type = args()[0]->evaluate(variables).as_string();
playable_custom_object* obj;
if(args().size() > 1) {
const int x = args()[1]->evaluate(variables).as_int();
const int y = args()[2]->evaluate(variables).as_int();
const bool face_right = args()[3]->evaluate(variables).as_int() > 0;
obj = new playable_custom_object(custom_object(type, x, y, face_right));
} else {
const int x = 0;
const int y = 0;
const bool face_right = true;
obj = new playable_custom_object(custom_object(type, x, y, face_right));
}
//adjust so the object's x/y is its midpoint.
obj->set_pos(obj->x() - obj->current_frame().width() / 2 , obj->y() - obj->current_frame().height() / 2);
if(args().size() > 4) {
variant properties = args()[4]->evaluate(variables);
variant keys = properties.get_keys();
for(int n = 0; n != keys.num_elements(); ++n) {
variant value = properties[keys[n]];
obj->mutate_value(keys[n].as_string(), value);
}
}
return variant(obj);
END_FUNCTION_DEF(object_playable)
class animation_command : public custom_object_command_callable
{
public:
animation_command(const std::string& anim)
: anim_(anim)
{}
virtual void execute(level& lvl, custom_object& ob) const {
ob.set_frame(anim_);
}
private:
std::string anim_;
};
FUNCTION_DEF(animation, 1, 1, "animation(string id): changes the current object's animation to the given animation. time_in_animation is reset to 0.")
return variant(new animation_command(args()[0]->evaluate(variables).as_string()));
END_FUNCTION_DEF(animation)
class die_command : public custom_object_command_callable
{
public:
virtual void execute(level& lvl, custom_object& ob) const {
ob.die();
}
};
FUNCTION_DEF(die, 0, 0, "die(): causes the current object to die. The object will receive the on_die signal and may even use it to resurrect itself. Use remove_object() to remove an object from play without it receiving on_die.")
return variant(new die_command());
END_FUNCTION_DEF(die)
class facing_command : public custom_object_command_callable
{
public:
explicit facing_command(int facing) : facing_(facing)
{}
virtual void execute(level& lvl, custom_object& ob) const {
ob.set_face_right(facing_ > 0);
}
private:
int facing_;
};
FUNCTION_DEF(facing, 1, 1, "facing(int new_facing): changes the current object's facing according to the value of new_facing (1 for right, otherwise left).")
return variant(new facing_command(args()[0]->evaluate(variables).as_int()));
END_FUNCTION_DEF(facing)
class set_var_command : public custom_object_command_callable
{
public:
explicit set_var_command(const std::string& attr, variant val)
: attr_(attr), val_(val)
{}
virtual void execute(level& lvl, custom_object& ob) const {
ob.vars()->mutate_value(attr_, val_);
}
private:
std::string attr_;
variant val_;
};
FUNCTION_DEF(set_var, 2, 2, "set_var(string varname, variant value): sets the variable named varname within the current object. Note that you should generally use set(vars.blah, x) rather than set_var('blah', x). The only exception is if you want to create the command and save it for later execution on an object you don't yet have access to -- most useful with the spawn() function.")
return variant(new set_var_command(
args()[0]->evaluate(variables).as_string(),
args()[1]->evaluate(variables)));
END_FUNCTION_DEF(set_var)
class add_debug_chart_command : public game_logic::command_callable {
std::string id_;
decimal value_;
public:
add_debug_chart_command(const std::string& id, decimal value)
: id_(id), value_(value)
{}
virtual void execute(game_logic::formula_callable& ob) const {
debug_console::add_graph_sample(id_, value_);
}
};
FUNCTION_DEF(debug_chart, 2, 2, "debug_chart(string id, decimal value): plots a sample in a graph")
return variant(new add_debug_chart_command(args()[0]->evaluate(variables).as_string(), args()[1]->evaluate(variables).as_decimal()));
END_FUNCTION_DEF(debug_chart)
class add_debug_rect_command : public game_logic::command_callable {
rect r_;
public:
explicit add_debug_rect_command(const rect& r) : r_(r)
{}
virtual void execute(game_logic::formula_callable& ob) const {
add_debug_rect(r_);
}
};
FUNCTION_DEF(solid, 3, 6, "solid(level, int x, int y, (optional)int w=1, (optional) int h=1, (optional) int debug=0) -> boolean: returns true iff the level contains solid space within the given (x,y,w,h) rectangle. If 'debug' is set, then the tested area will be displayed on-screen.")
level* lvl = args()[0]->evaluate(variables).convert_to<level>();
const int x = args()[1]->evaluate(variables).as_int();
const int y = args()[2]->evaluate(variables).as_int();
int w = args().size() >= 4 ? args()[3]->evaluate(variables).as_int() : 1;
int h = args().size() >= 5 ? args()[4]->evaluate(variables).as_int() : 1;
rect r(x, y, w, h);
if(args().size() >= 6) {
//debugging so set the debug rect
add_debug_rect(r);
}
return variant(lvl->solid(r));
END_FUNCTION_DEF(solid)
FUNCTION_DEF(debug_rect, 2, 4, "debug_rect(int x, int y, (optional)int w=1, (optional) int h=1) -> Draws, for one frame, a rectangle on the level")
const int x = args()[0]->evaluate(variables).as_int();
const int y = args()[1]->evaluate(variables).as_int();
int w = args().size() >= 3 ? args()[2]->evaluate(variables).as_int() : 100;
int h = args().size() >= 4 ? args()[3]->evaluate(variables).as_int() : 100;
rect r(x, y, w, h);
return variant(new add_debug_rect_command(r));
END_FUNCTION_DEF(debug_rect)
FUNCTION_DEF(plot_x, 1, 1, "plot_x(int x): plots a vertical debug line at the given position")
const int x = args()[0]->evaluate(variables).as_int();
return variant(new add_debug_rect_command(rect(x, -32000, 2, 64000)));
END_FUNCTION_DEF(plot_x)
FUNCTION_DEF(plot_y, 1, 1, "plot_y(int x): plots a horizontal debug line at the given position")
const int y = args()[0]->evaluate(variables).as_int();
return variant(new add_debug_rect_command(rect(-32000, y, 64000, 2)));
END_FUNCTION_DEF(plot_y)
FUNCTION_DEF(standable, 3, 5, "standable(level, int x, int y, (optional)int w=1, (optional) int h=1) -> boolean: returns true iff the level contains standable space within the given (x,y,w,h) rectangle")
level* lvl = args()[0]->evaluate(variables).convert_to<level>();
const int x = args()[1]->evaluate(variables).as_int();
const int y = args()[2]->evaluate(variables).as_int();
int w = args().size() >= 4 ? args()[3]->evaluate(variables).as_int() : 1;
int h = args().size() >= 5 ? args()[4]->evaluate(variables).as_int() : 1;
rect r(x, y, w, h);
return variant(lvl->standable(r));
END_FUNCTION_DEF(standable)
class set_solid_command : public entity_command_callable {
rect r_;
bool is_solid_;
public:
set_solid_command(const rect& r, bool is_solid) : r_(r), is_solid_(is_solid)
{}
virtual void execute(level& lvl, entity& ob) const {
lvl.set_solid_area(r_, is_solid_);
}
};
FUNCTION_DEF(set_solid, 4, 5, "set_solid(x1, y1, x2, y2, boolean is_solid=false): modifies the solidity of the level such that the rectangle given by (x1, y1, x2, y2) will have its solidity set to the value of is_solid")
return variant(new set_solid_command(
rect::from_coordinates(
args()[0]->evaluate(variables).as_int(),
args()[1]->evaluate(variables).as_int(),
args()[2]->evaluate(variables).as_int(),
args()[3]->evaluate(variables).as_int()),
args().size() > 4 ? args()[4]->evaluate(variables).as_bool() : false));
END_FUNCTION_DEF(set_solid)
FUNCTION_DEF(group_size, 2, 2, "group_size(level, int group_id) -> int: gives the number of objects in the object group given by group_id")
level* lvl = args()[0]->evaluate(variables).convert_to<level>();
return variant(lvl->group_size(args()[1]->evaluate(variables).as_int()));
END_FUNCTION_DEF(group_size)
class set_group_command : public entity_command_callable {
public:
explicit set_group_command(int group=-1) : group_(group)
{}
virtual void execute(level& lvl, entity& ob) const {
int group = group_;
if(group < 0) {