/
LuaUnsyncedCtrl.cpp
2959 lines (2428 loc) · 75.4 KB
/
LuaUnsyncedCtrl.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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "LuaUnsyncedCtrl.h"
#include "LuaInclude.h"
#include "LuaHandle.h"
#include "LuaHashString.h"
#include "LuaUtils.h"
#include "LuaTextures.h"
#include "ExternalAI/EngineOutHandler.h"
#include "ExternalAI/SkirmishAIHandler.h"
#include "Game/Camera.h"
#include "Game/CameraHandler.h"
#include "Game/Camera/CameraController.h"
#include "Game/Game.h"
#include "Game/GlobalUnsynced.h"
#include "Game/SelectedUnitsHandler.h"
#include "Game/Players/Player.h"
#include "Game/Players/PlayerHandler.h"
#include "Game/InMapDraw.h"
#include "Game/InMapDrawModel.h"
#include "Game/UI/CommandColors.h"
#include "Game/UI/CursorIcons.h"
#include "Game/UI/GuiHandler.h"
#include "Game/UI/InfoConsole.h"
#include "Game/UI/KeyCodes.h"
#include "Game/UI/KeySet.h"
#include "Game/UI/KeyBindings.h"
#include "Game/UI/MiniMap.h"
#include "Game/UI/MouseHandler.h"
#include "Map/MapInfo.h"
#include "Map/ReadMap.h"
#include "Map/BaseGroundDrawer.h"
#include "Map/BaseGroundTextures.h"
#include "Rendering/Env/ISky.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/CommandDrawer.h"
#include "Rendering/IconHandler.h"
#include "Rendering/LineDrawer.h"
#include "Rendering/UnitDrawer.h"
#include "Rendering/Textures/Bitmap.h"
#include "Rendering/Textures/NamedTextures.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Projectiles/Projectile.h"
#include "Sim/Projectiles/ProjectileHandler.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitDefHandler.h"
#include "Sim/Units/UnitHandler.h"
#include "Sim/Units/CommandAI/CommandAI.h"
#include "Sim/Units/Groups/Group.h"
#include "Sim/Units/Groups/GroupHandler.h"
#include "System/Config/ConfigHandler.h"
#include "System/EventHandler.h"
#include "System/GlobalConfig.h"
#include "System/Log/ILog.h"
#include "Net/Protocol/NetProtocol.h"
#include "System/Net/PackPacket.h"
#include "System/Util.h"
#include "System/Sound/ISound.h"
#include "System/Sound/SoundChannels.h"
#include "System/FileSystem/FileHandler.h"
#include "System/FileSystem/DataDirLocater.h"
#include "System/FileSystem/FileSystem.h"
#include "System/Platform/Watchdog.h"
#include "System/Platform/WindowManagerHelper.h"
#include <boost/cstdint.hpp>
#include "System/Platform/Misc.h"
#include "LuaHelper.h"
#if !defined(HEADLESS) && !defined(NO_SOUND)
#include "System/Sound/EFX.h"
#include "System/Sound/EFXPresets.h"
#endif
#include <map>
#include <set>
#include <cctype>
#include <cfloat>
#include <fstream>
#include <SDL_keysym.h>
#include <SDL_mouse.h>
using std::min;
using std::max;
// MinGW defines this for a WINAPI function
#undef SendMessage
#undef CreateDirectory
const int CMD_INDEX_OFFSET = 1; // starting index for command descriptions
/******************************************************************************/
/******************************************************************************/
std::set<int> drawCmdQueueUnits;
/******************************************************************************/
/******************************************************************************/
bool LuaUnsyncedCtrl::PushEntries(lua_State* L)
{
#define REGISTER_LUA_CFUNC(x) \
lua_pushstring(L, #x); \
lua_pushcfunction(L, x); \
lua_rawset(L, -3)
REGISTER_LUA_CFUNC(Echo);
REGISTER_LUA_CFUNC(Log);
REGISTER_LUA_CFUNC(SendMessage);
REGISTER_LUA_CFUNC(SendMessageToPlayer);
REGISTER_LUA_CFUNC(SendMessageToTeam);
REGISTER_LUA_CFUNC(SendMessageToAllyTeam);
REGISTER_LUA_CFUNC(SendMessageToSpectators);
REGISTER_LUA_CFUNC(LoadSoundDef);
REGISTER_LUA_CFUNC(PlaySoundFile);
REGISTER_LUA_CFUNC(PlaySoundStream);
REGISTER_LUA_CFUNC(StopSoundStream);
REGISTER_LUA_CFUNC(PauseSoundStream);
REGISTER_LUA_CFUNC(SetSoundStreamVolume);
REGISTER_LUA_CFUNC(SetSoundEffectParams);
REGISTER_LUA_CFUNC(SetCameraState);
REGISTER_LUA_CFUNC(SetCameraTarget);
REGISTER_LUA_CFUNC(SelectUnitMap);
REGISTER_LUA_CFUNC(SelectUnitArray);
REGISTER_LUA_CFUNC(AddWorldIcon);
REGISTER_LUA_CFUNC(AddWorldText);
REGISTER_LUA_CFUNC(AddWorldUnit);
REGISTER_LUA_CFUNC(DrawUnitCommands);
REGISTER_LUA_CFUNC(SetTeamColor);
REGISTER_LUA_CFUNC(AssignMouseCursor);
REGISTER_LUA_CFUNC(ReplaceMouseCursor);
REGISTER_LUA_CFUNC(SetCustomCommandDrawData);
REGISTER_LUA_CFUNC(SetDrawSky);
REGISTER_LUA_CFUNC(SetDrawWater);
REGISTER_LUA_CFUNC(SetDrawGround);
REGISTER_LUA_CFUNC(SetWaterParams);
REGISTER_LUA_CFUNC(AddMapLight);
REGISTER_LUA_CFUNC(AddModelLight);
REGISTER_LUA_CFUNC(UpdateMapLight);
REGISTER_LUA_CFUNC(UpdateModelLight);
REGISTER_LUA_CFUNC(SetMapLightTrackingState);
REGISTER_LUA_CFUNC(SetModelLightTrackingState);
REGISTER_LUA_CFUNC(SetMapSquareTexture);
REGISTER_LUA_CFUNC(SetUnitNoDraw);
REGISTER_LUA_CFUNC(SetUnitNoMinimap);
REGISTER_LUA_CFUNC(SetUnitNoSelect);
REGISTER_LUA_CFUNC(SetUnitLeaveTracks);
REGISTER_LUA_CFUNC(AddUnitIcon);
REGISTER_LUA_CFUNC(FreeUnitIcon);
REGISTER_LUA_CFUNC(ExtractModArchiveFile);
// moved from LuaUI
//FIXME REGISTER_LUA_CFUNC(SetShockFrontFactors);
REGISTER_LUA_CFUNC(GetConfigInt);
REGISTER_LUA_CFUNC(SetConfigInt);
REGISTER_LUA_CFUNC(GetConfigString);
REGISTER_LUA_CFUNC(SetConfigString);
REGISTER_LUA_CFUNC(CreateDir);
REGISTER_LUA_CFUNC(SendCommands);
REGISTER_LUA_CFUNC(GiveOrder);
REGISTER_LUA_CFUNC(GiveOrderToUnit);
REGISTER_LUA_CFUNC(GiveOrderToUnitMap);
REGISTER_LUA_CFUNC(GiveOrderToUnitArray);
REGISTER_LUA_CFUNC(GiveOrderArrayToUnitMap);
REGISTER_LUA_CFUNC(GiveOrderArrayToUnitArray);
REGISTER_LUA_CFUNC(SendLuaUIMsg);
REGISTER_LUA_CFUNC(SendLuaGaiaMsg);
REGISTER_LUA_CFUNC(SendLuaRulesMsg);
REGISTER_LUA_CFUNC(LoadCmdColorsConfig);
REGISTER_LUA_CFUNC(LoadCtrlPanelConfig);
REGISTER_LUA_CFUNC(SetActiveCommand);
REGISTER_LUA_CFUNC(ForceLayoutUpdate);
REGISTER_LUA_CFUNC(SetMouseCursor);
REGISTER_LUA_CFUNC(WarpMouse);
REGISTER_LUA_CFUNC(SetCameraOffset);
REGISTER_LUA_CFUNC(SetLosViewColors);
REGISTER_LUA_CFUNC(Restart);
REGISTER_LUA_CFUNC(SetWMIcon);
REGISTER_LUA_CFUNC(SetWMCaption);
REGISTER_LUA_CFUNC(SetUnitDefIcon);
REGISTER_LUA_CFUNC(SetUnitDefImage);
REGISTER_LUA_CFUNC(SetUnitGroup);
REGISTER_LUA_CFUNC(SetShareLevel);
REGISTER_LUA_CFUNC(ShareResources);
REGISTER_LUA_CFUNC(SetLastMessagePosition);
REGISTER_LUA_CFUNC(MarkerAddPoint);
REGISTER_LUA_CFUNC(MarkerAddLine);
REGISTER_LUA_CFUNC(MarkerErasePosition);
REGISTER_LUA_CFUNC(SetDrawSelectionInfo);
REGISTER_LUA_CFUNC(SetBuildSpacing);
REGISTER_LUA_CFUNC(SetBuildFacing);
REGISTER_LUA_CFUNC(SetSunParameters);
REGISTER_LUA_CFUNC(SetSunManualControl);
REGISTER_LUA_CFUNC(SetSunDirection);
REGISTER_LUA_CFUNC(SendSkirmishAIMessage);
REGISTER_LUA_CFUNC(ClearWatchDogTimer);
return true;
}
/******************************************************************************/
/******************************************************************************/
//
// Parsing helpers
//
static inline CProjectile* ParseRawProjectile(lua_State* L, const char* caller, int index, bool synced)
{
if (!lua_isnumber(L, index)) {
if (caller != NULL) {
luaL_error(L, "[%s] projectile ID parameter in %s() not a number\n", __FUNCTION__, caller);
}
return NULL;
}
const int projID = lua_toint(L, index);
const ProjectileMapValPair* pp = NULL;
if (synced) {
pp = projectileHandler->GetMapPairBySyncedID(projID);
} else {
pp = projectileHandler->GetMapPairByUnsyncedID(projID);
}
return (pp) ? pp->first : NULL;
}
static inline CUnit* ParseRawUnit(lua_State* L, const char* caller, int index)
{
if (!lua_isnumber(L, index)) {
if (caller != NULL) {
luaL_error(L, "[%s] unit ID parameter in %s() not a number\n", __FUNCTION__, caller);
}
return NULL;
}
const int unitID = lua_toint(L, index);
if ((unitID < 0) || (static_cast<size_t>(unitID) >= unitHandler->MaxUnits())) {
luaL_error(L, "%s(): Bad unitID: %i\n", caller, unitID);
}
return unitHandler->units[unitID];
}
static inline CUnit* ParseAllyUnit(lua_State* L, const char* caller, int index)
{
CUnit* unit = ParseRawUnit(L, caller, index);
if (unit == NULL) {
return NULL;
}
if (CLuaHandle::GetHandleReadAllyTeam(L) < 0) {
return CLuaHandle::GetHandleFullRead(L) ? unit : NULL;
}
return (unit->allyteam == CLuaHandle::GetHandleReadAllyTeam(L)) ? unit : NULL;
}
static inline CUnit* ParseCtrlUnit(lua_State* L,
const char* caller, int index)
{
CUnit* unit = ParseRawUnit(L, caller, index);
if (unit == NULL) {
return NULL;
}
return (CanControlTeam(L, unit->team) ? unit : NULL);
}
static inline CUnit* ParseSelectUnit(lua_State* L,
const char* caller, int index)
{
CUnit* unit = ParseRawUnit(L, caller, index);
if (unit == NULL || unit->noSelect) {
return NULL;
}
const int selectTeam = CLuaHandle::GetHandleSelectTeam(L);
if (selectTeam < 0) {
return (selectTeam == CEventClient::AllAccessTeam) ? unit : NULL;
}
if (selectTeam == unit->team) {
return unit;
}
return NULL;
}
/******************************************************************************/
/******************************************************************************/
void LuaUnsyncedCtrl::DrawUnitCommandQueues()
{
if (drawCmdQueueUnits.empty()) {
return;
}
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
lineDrawer.Configure(cmdColors.UseColorRestarts(),
cmdColors.UseRestartColor(),
cmdColors.restart,
cmdColors.RestartAlpha());
lineDrawer.SetupLineStipple();
glEnable(GL_BLEND);
glBlendFunc((GLenum)cmdColors.QueuedBlendSrc(),
(GLenum)cmdColors.QueuedBlendDst());
glLineWidth(cmdColors.QueuedLineWidth());
GML_RECMUTEX_LOCK(unit); // DrawUnitCommandQueues
GML_RECMUTEX_LOCK(feat); // DrawUnitCommandQueues
GML_STDMUTEX_LOCK(cai); // DrawUnitCommandQueues
GML_STDMUTEX_LOCK(dque); // DrawUnitCommandQueues
std::set<int>::const_iterator ui;
for (ui = drawCmdQueueUnits.begin(); ui != drawCmdQueueUnits.end(); ++ui) {
const CUnit* unit = unitHandler->GetUnit(*ui);
if (unit == NULL || unit->commandAI == NULL) {
continue;
}
commandDrawer->Draw(unit->commandAI);
}
glLineWidth(1.0f);
glEnable(GL_DEPTH_TEST);
}
void LuaUnsyncedCtrl::ClearUnitCommandQueues()
{
GML_STDMUTEX_LOCK(dque); // ClearUnitCommandQueues
drawCmdQueueUnits.clear();
}
/******************************************************************************/
/******************************************************************************/
//
// The call-outs
//
int LuaUnsyncedCtrl::Echo(lua_State* L)
{
return LuaUtils::Echo(L);
}
int LuaUnsyncedCtrl::Log(lua_State* L)
{
return LuaUtils::Log(L);
}
static string ParseMessage(lua_State* L, const string& msg)
{
string::size_type start = msg.find("<PLAYER");
if (start == string::npos) {
return msg;
}
const char* number = msg.c_str() + start + strlen("<PLAYER");
char* endPtr;
const int playerID = (int)strtol(number, &endPtr, 10);
if ((endPtr == number) || (*endPtr != '>')) {
luaL_error(L, "Bad message format: %s", msg.c_str());
}
if (!playerHandler->IsValidPlayer(playerID)) {
luaL_error(L, "Invalid message playerID: %i", playerID);
}
const CPlayer* player = playerHandler->Player(playerID);
if ((player == NULL) || !player->active || player->name.empty()) {
luaL_error(L, "Invalid message playerID: %i", playerID);
}
const string head = msg.substr(0, start);
const string tail = msg.substr(endPtr - msg.c_str() + 1);
return head + player->name + ParseMessage(L, tail);
}
static void PrintMessage(lua_State* L, const string& msg)
{
LOG("%s", ParseMessage(L, msg).c_str());
}
int LuaUnsyncedCtrl::SendMessage(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 1) || !lua_isstring(L, 1)) {
luaL_error(L, "Incorrect arguments to SendMessage()");
}
PrintMessage(L, lua_tostring(L, 1));
return 0;
}
int LuaUnsyncedCtrl::SendMessageToSpectators(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 1) || !lua_isstring(L, 1)) {
luaL_error(L, "Incorrect arguments to SendMessageToSpectators()");
}
if (gu->spectating) {
PrintMessage(L, lua_tostring(L, 1));
}
return 0;
}
int LuaUnsyncedCtrl::SendMessageToPlayer(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 2) || !lua_isnumber(L, 1) || !lua_isstring(L, 2)) {
luaL_error(L, "Incorrect arguments to SendMessageToPlayer()");
}
const int playerID = lua_toint(L, 1);
if (playerID == gu->myPlayerNum) {
PrintMessage(L, lua_tostring(L, 2));
}
return 0;
}
int LuaUnsyncedCtrl::SendMessageToTeam(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 2) || !lua_isnumber(L, 1) || !lua_isstring(L, 2)) {
luaL_error(L, "Incorrect arguments to SendMessageToTeam()");
}
const int teamID = lua_toint(L, 1);
if (teamID == gu->myTeam) {
PrintMessage(L, lua_tostring(L, 2));
}
return 0;
}
int LuaUnsyncedCtrl::SendMessageToAllyTeam(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 2) || !lua_isnumber(L, 1) || !lua_isstring(L, 2)) {
luaL_error(L, "Incorrect arguments to SendMessageToAllyTeam()");
}
const int allyTeamID = lua_toint(L, 1);
if (allyTeamID == gu->myAllyTeam) {
PrintMessage(L, lua_tostring(L, 2));
}
return 0;
}
/******************************************************************************/
int LuaUnsyncedCtrl::LoadSoundDef(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 1) || !lua_isstring(L, 1)) {
luaL_error(L, "Incorrect arguments to LoadSoundDef()");
}
const string soundFile = lua_tostring(L, 1);
bool success = sound->LoadSoundDefs(soundFile);
if (!CLuaHandle::GetHandleSynced(L)) {
lua_pushboolean(L, success);
return 1;
} else {
return 0;
}
}
int LuaUnsyncedCtrl::PlaySoundFile(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 1) || !lua_isstring(L, 1)) {
luaL_error(L, "Incorrect arguments to PlaySoundFile()");
}
bool success = false;
const string soundFile = lua_tostring(L, 1);
const unsigned int soundID = sound->GetSoundId(soundFile);
if (soundID > 0) {
float volume = luaL_optfloat(L, 2, 1.0f);
float3 pos;
float3 speed;
bool pos_given = false;
bool speed_given = false;
int index = 3;
if (args >= 5 && lua_isnumber(L, 3) && lua_isnumber(L, 4) && lua_isnumber(L, 5)) {
pos = float3(lua_tofloat(L, 3), lua_tofloat(L, 4), lua_tofloat(L, 5));
pos_given = true;
index += 3;
if (args >= 8 && lua_isnumber(L, 6) && lua_isnumber(L, 7) && lua_isnumber(L, 8))
{
speed = float3(lua_tofloat(L, 6), lua_tofloat(L, 7), lua_tofloat(L, 8));
speed_given = true;
index += 3;
}
}
//! last argument (with and without pos/speed arguments) is the optional `sfx channel`
AudioChannelImpl* channel = &Channels::General;
if (args >= index) {
if (lua_isstring(L, index)) {
string channelStr = lua_tostring(L, index);
StringToLowerInPlace(channelStr);
if (channelStr == "battle" || channelStr == "sfx") {
channel = &Channels::Battle;
}
else if (channelStr == "unitreply" || channelStr == "voice") {
channel = &Channels::UnitReply;
}
else if (channelStr == "userinterface" || channelStr == "ui") {
channel = &Channels::UserInterface;
}
} else if (lua_isnumber(L, index)) {
const int channelNum = lua_toint(L, index);
if (channelNum == 1) {
channel = &Channels::Battle;
}
else if (channelNum == 2) {
channel = &Channels::UnitReply;
}
else if (channelNum == 3) {
channel = &Channels::UserInterface;
}
}
}
if (pos_given) {
if (speed_given) {
channel->PlaySample(soundID, pos, speed, volume);
} else {
channel->PlaySample(soundID, pos, volume);
}
} else
channel->PlaySample(soundID, volume);
success = true;
}
if (!CLuaHandle::GetHandleSynced(L)) {
lua_pushboolean(L, success);
return 1;
} else {
return 0;
}
}
int LuaUnsyncedCtrl::PlaySoundStream(lua_State* L)
{
const int args = lua_gettop(L);
const string soundFile = luaL_checkstring(L, 1);
const float volume = luaL_optnumber(L, 2, 1.0f);
bool enqueue = false;
if (args >= 3)
enqueue = lua_toboolean(L, 3);
Channels::BGMusic.StreamPlay(soundFile, volume, enqueue);
// .ogg files don't have sound ID's generated
// for them (yet), so we always succeed here
if (!CLuaHandle::GetHandleSynced(L)) {
lua_pushboolean(L, true);
return 1;
} else {
return 0;
}
}
int LuaUnsyncedCtrl::StopSoundStream(lua_State*)
{
Channels::BGMusic.StreamStop();
return 0;
}
int LuaUnsyncedCtrl::PauseSoundStream(lua_State*)
{
Channels::BGMusic.StreamPause();
return 0;
}
int LuaUnsyncedCtrl::SetSoundStreamVolume(lua_State* L)
{
Channels::BGMusic.SetVolume(luaL_checkfloat(L, 1));
return 0;
}
int LuaUnsyncedCtrl::SetSoundEffectParams(lua_State* L)
{
#if !defined(HEADLESS) && !defined(NO_SOUND)
if (!efx)
return 0;
//! only a preset name given?
if (lua_israwstring(L, 1)) {
const std::string presetname = lua_tostring(L, 1);
efx->SetPreset(presetname, false);
return 0;
}
if (!lua_istable(L, 1)) {
luaL_error(L, "Incorrect arguments to SetSoundEffectParams()");
}
//! first parse the 'preset' key (so all following params use it as base and override it)
lua_pushliteral(L, "preset");
lua_gettable(L, -2);
if (lua_israwstring(L, -1)) {
std::string presetname = lua_tostring(L, -1);
efx->SetPreset(presetname, false, false);
}
lua_pop(L, 1);
if (!efx->sfxProperties)
return 0;
EAXSfxProps* efxprops = efx->sfxProperties;
//! parse pass filter
lua_pushliteral(L, "passfilter");
lua_gettable(L, -2);
if (lua_istable(L, -1)) {
for (lua_pushnil(L); lua_next(L, -2) != 0; lua_pop(L, 1)) {
if (lua_israwstring(L, -2)) {
const string key = StringToLower(lua_tostring(L, -2));
std::map<std::string, ALuint>::iterator it = nameToALFilterParam.find(key);
if (it != nameToALFilterParam.end()) {
ALuint param = it->second;
if (lua_isnumber(L, -1)) {
if (alParamType[param] == EFXParamTypes::FLOAT) {
const float value = lua_tofloat(L, -1);
efxprops->filter_properties_f[param] = value;
}
}
}
}
}
}
lua_pop(L, 1);
//! parse EAX Reverb
lua_pushliteral(L, "reverb");
lua_gettable(L, -2);
if (lua_istable(L, -1)) {
for (lua_pushnil(L); lua_next(L, -2) != 0; lua_pop(L, 1)) {
if (lua_israwstring(L, -2)) {
const string key = StringToLower(lua_tostring(L, -2));
std::map<std::string, ALuint>::iterator it = nameToALParam.find(key);
if (it != nameToALParam.end()) {
ALuint param = it->second;
if (lua_istable(L, -1)) {
if (alParamType[param] == EFXParamTypes::VECTOR) {
float3 v;
const int size = LuaUtils::ParseFloatArray(L, -1, &v[0], 3);
if (size >= 3) {
efxprops->properties_v[param] = v;
}
}
}
else if (lua_isnumber(L, -1)) {
if (alParamType[param] == EFXParamTypes::FLOAT) {
const float value = lua_tofloat(L, -1);
efxprops->properties_f[param] = value;
}
}
else if (lua_isboolean(L, -1)) {
if (alParamType[param] == EFXParamTypes::BOOL) {
const bool value = lua_toboolean(L, -1);
efxprops->properties_i[param] = value;
}
}
}
}
}
}
lua_pop(L, 1);
//! commit effects
efx->CommitEffects();
#endif /// !defined(HEADLESS) && !defined(NO_SOUND)
return 0;
}
/******************************************************************************/
/******************************************************************************/
int LuaUnsyncedCtrl::AddWorldIcon(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args != 4) ||
!lua_isnumber(L, 1) || !lua_isnumber(L, 2) ||
!lua_isnumber(L, 3) || !lua_isnumber(L, 4)) {
luaL_error(L, "Incorrect arguments to AddWorldIcon(id, x, y, z");
}
const int cmdID = lua_toint(L, 1);
const float3 pos(lua_tofloat(L, 2),
lua_tofloat(L, 3),
lua_tofloat(L, 4));
cursorIcons.AddIcon(cmdID, pos);
return 0;
}
int LuaUnsyncedCtrl::AddWorldText(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args != 4) ||
!lua_isstring(L, 1) || !lua_isnumber(L, 2) ||
!lua_isnumber(L, 3) || !lua_isnumber(L, 4)) {
luaL_error(L, "Incorrect arguments to AddWorldIcon(text, x, y, z");
}
const string text = lua_tostring(L, 1);
const float3 pos(lua_tofloat(L, 2),
lua_tofloat(L, 3),
lua_tofloat(L, 4));
cursorIcons.AddIconText(text, pos);
return 0;
}
int LuaUnsyncedCtrl::AddWorldUnit(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args != 6) ||
!lua_isstring(L, 1) || !lua_isnumber(L, 2) ||
!lua_isnumber(L, 3) || !lua_isnumber(L, 4) ||
!lua_isnumber(L, 5) || !lua_isnumber(L, 6)) {
luaL_error(L,
"Incorrect arguments to AddWorldUnit(unitDefID, x, y, z, team, facing)");
}
const int unitDefID = lua_toint(L, 1);
if (!unitDefHandler->IsValidUnitDefID(unitDefID)) {
return 0;
}
const float3 pos(lua_tofloat(L, 2),
lua_tofloat(L, 3),
lua_tofloat(L, 4));
const int teamId = lua_toint(L, 5);
if (!teamHandler->IsValidTeam(teamId)) {
return 0;
}
const int facing = lua_toint(L, 6);
cursorIcons.AddBuildIcon(-unitDefID, pos, teamId, facing);
return 0;
}
int LuaUnsyncedCtrl::DrawUnitCommands(lua_State* L)
{
GML_STDMUTEX_LOCK(dque); // DrawUnitCommands
if (lua_istable(L, 1)) {
const bool isMap = lua_isboolean(L, 2) && lua_toboolean(L, 2);
const int unitArg = isMap ? -2 : -1;
const int table = 1;
for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
if (lua_israwnumber(L, -2)) {
CUnit* unit = ParseAllyUnit(L, __FUNCTION__, unitArg);
if (unit != NULL) {
drawCmdQueueUnits.insert(unit->id);
}
}
}
return 0;
}
CUnit* unit = ParseAllyUnit(L, __FUNCTION__, 1);
if (unit != NULL) {
drawCmdQueueUnits.insert(unit->id);
}
return 0;
}
/******************************************************************************/
/******************************************************************************/
int LuaUnsyncedCtrl::SetCameraTarget(lua_State* L)
{
if (mouse == NULL) {
return 0;
}
const int args = lua_gettop(L); // number of arguments
if ((args < 3) ||
!lua_isnumber(L, 1) || !lua_isnumber(L, 2) || !lua_isnumber(L, 3)) {
luaL_error(L, "Incorrect arguments to SetCameraTarget(x, y, z)");
}
const float3 pos(lua_tofloat(L, 1),
lua_tofloat(L, 2),
lua_tofloat(L, 3));
float transTime = 0.5f;
if ((args >= 4) && lua_isnumber(L, 4)) {
transTime = lua_tofloat(L, 4);
}
camHandler->CameraTransition(transTime);
camHandler->GetCurrentController().SetPos(pos);
return 0;
}
int LuaUnsyncedCtrl::SetCameraState(lua_State* L)
{
if (mouse == NULL) {
return 0;
}
if (!lua_istable(L, 1)) {
luaL_error(L, "Incorrect arguments to SetCameraState(table, camTime)");
}
const float camTime = luaL_checkfloat(L, 2);
CCameraController::StateMap camState;
const int table = 1;
for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
if (lua_israwstring(L, -2)) {
const string key = lua_tostring(L, -2);
if (lua_isnumber(L, -1)) {
camState[key] = lua_tofloat(L, -1);
}
else if (lua_isboolean(L, -1)) {
camState[key] = lua_toboolean(L, -1) ? +1.0f : -1.0f;
}
}
}
camHandler->CameraTransition(camTime);
lua_pushboolean(L, camHandler->SetState(camState));
if (!CLuaHandle::GetHandleSynced(L)) {
return 1;
} else {
return 0;
}
}
/******************************************************************************/
/******************************************************************************/
int LuaUnsyncedCtrl::SelectUnitArray(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 1) || !lua_istable(L, 1) ||
((args >= 2) && !lua_isboolean(L, 2))) {
luaL_error(L, "Incorrect arguments to SelectUnitArray()");
}
// clear the current units, unless the append flag is present
if ((args < 2) || !lua_toboolean(L, 2)) {
selectedUnitsHandler.ClearSelected();
}
const int table = 1;
for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
if (lua_israwnumber(L, -2) && lua_isnumber(L, -1)) { // avoid 'n'
CUnit* unit = ParseSelectUnit(L, __FUNCTION__, -1); // the value
if (unit != NULL) {
selectedUnitsHandler.AddUnit(unit);
}
}
}
return 0;
}
int LuaUnsyncedCtrl::SelectUnitMap(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 1) || !lua_istable(L, 1) ||
((args >= 2) && !lua_isboolean(L, 2))) {
luaL_error(L, "Incorrect arguments to SelectUnitMap()");
}
// clear the current units, unless the append flag is present
if ((args < 2) || !lua_toboolean(L, 2)) {
selectedUnitsHandler.ClearSelected();
}
const int table = 1;
for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
if (lua_israwnumber(L, -2)) {
CUnit* unit = ParseSelectUnit(L, __FUNCTION__, -2); // the key
if (unit != NULL) {
selectedUnitsHandler.AddUnit(unit);
}
}
}
return 0;
}
/******************************************************************************/
int LuaUnsyncedCtrl::SetTeamColor(lua_State* L)
{
const int teamID = luaL_checkint(L, 1);
if (!teamHandler->IsValidTeam(teamID)) {
return 0;
}
CTeam* team = teamHandler->Team(teamID);
if (team == NULL) {
return 0;
}
const float r = max(0.0f, min(1.0f, luaL_checkfloat(L, 2)));
const float g = max(0.0f, min(1.0f, luaL_checkfloat(L, 3)));
const float b = max(0.0f, min(1.0f, luaL_checkfloat(L, 4)));
team->color[0] = (unsigned char)(r * 255.0f);
team->color[1] = (unsigned char)(g * 255.0f);
team->color[2] = (unsigned char)(b * 255.0f);
return 0;
}
/******************************************************************************/
int LuaUnsyncedCtrl::AssignMouseCursor(lua_State* L)
{
const int args = lua_gettop(L); // number of arguments
if ((args < 2) || !lua_isstring(L, 1) || !lua_isstring(L, 2)) {
luaL_error(L, "Incorrect arguments to AssignMouseCursor()");
}
const string cmdName = lua_tostring(L, 1);
const string fileName = lua_tostring(L, 2);
bool overwrite = true;
if ((args >= 3) && lua_isboolean(L, 3)) {
overwrite = lua_toboolean(L, 3);
}
CMouseCursor::HotSpot hotSpot = CMouseCursor::Center;
if ((args >= 4) && lua_isboolean(L, 4)) {
if (lua_toboolean(L, 4)) {
hotSpot = CMouseCursor::TopLeft;
}