-
Notifications
You must be signed in to change notification settings - Fork 178
/
GUIScript.cpp
13906 lines (12435 loc) · 398 KB
/
GUIScript.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
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003 The GemRB Project
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
*/
#include "GUIScript.h"
#include "PythonCallbacks.h"
#include "PythonConversions.h"
#include "PythonErrors.h"
#include "Audio.h"
#include "CharAnimations.h"
#include "DataFileMgr.h"
#include "DialogHandler.h"
#include "DisplayMessage.h"
#include "EffectQueue.h"
#include "Game.h"
#include "GameData.h"
#include "ImageFactory.h"
#include "Interface.h"
#include "Item.h"
#include "KeyMap.h"
#include "Map.h"
#include "MusicMgr.h"
#include "Palette.h"
#include "PalettedImageMgr.h"
#include "ResourceDesc.h"
#include "RNG.h"
#include "SaveGameIterator.h"
#include "Spell.h"
#include "TileMap.h"
#include "Video/Video.h"
#include "WorldMap.h"
#include "GameScript/GSUtils.h" //checkvariable
#include "GUI/Button.h"
#include "GUI/Console.h"
#include "GUI/EventMgr.h"
#include "GUI/GameControl.h"
#include "GUI/GUIScriptInterface.h"
#include "GUI/Label.h"
#include "GUI/MapControl.h"
#include "GUI/ScrollBar.h"
#include "GUI/TextArea.h"
#include "GUI/TextEdit.h"
#include "GUI/WorldMapControl.h"
#include "Scriptable/Container.h"
#include "Scriptable/Door.h"
#include "Scriptable/InfoPoint.h"
#include "System/FileStream.h"
#include "System/FileFilters.h"
#include <algorithm>
#include <cstdio>
// MIPSPro fix for IRIX
GEM_EXPORT size_t strlcpy(char *, const char *, size_t);
using namespace GemRB;
GUIScript *GemRB::gs = NULL;
// a shorthand for declaring methods in method table
#define METHOD(name, args) {#name, GemRB_ ## name, args, GemRB_ ## name ## __doc}
//Check removal/equip/swap of item based on item name and actor's scriptname
#define CRI_REMOVE 0
#define CRI_EQUIP 1
#define CRI_SWAP 2
#define CRI_REMOVEFORSWAP 3
//bit used in SetCreatureStat to access some fields
#define EXTRASETTINGS 0x1000
//maximum distance for passing items between two characters
#define MAX_DISTANCE 500
struct UsedItemType {
ResRef itemname;
ieVariable username; //death variable
ieStrRef value;
int flags;
};
using EventNameType = char[17];
#define IS_DROP 0
#define IS_GET 1
#define IS_SWINGOFFSET 2 // offset to the swing sound columns
#define UNINIT_IEDWORD 0xcccccccc
static std::vector<SpellDescType> SpecialItems;
static std::vector<SpellDescType> StoreSpells;
static std::vector<ItemExtHeader> ItemArray(GUIBT_COUNT);
static SpellExtHeader *SpellArray = NULL;
static std::vector<UsedItemType> UsedItems;
static int ReputationIncrease[20]={(int) UNINIT_IEDWORD};
static int ReputationDonation[20]={(int) UNINIT_IEDWORD};
//4 action button indices are packed on a single ieDword, there are 32 actions max.
//there are additional fake action buttons
static ieDword GUIAction[MAX_ACT_COUNT]={UNINIT_IEDWORD};
static ieDword GUITooltip[MAX_ACT_COUNT]={UNINIT_IEDWORD};
static ResRef GUIResRef[MAX_ACT_COUNT];
static EventNameType GUIEvent[MAX_ACT_COUNT];
static Store *rhstore = NULL;
static EffectRef fx_learn_spell_ref = { "Spell:Learn", -1 };
#define GET_GAME() \
Game *game = core->GetGame(); \
if (!game) { \
return RuntimeError( "No game loaded!\n" ); \
}
#define GET_MAP() \
Map *map = game->GetCurrentArea(); \
if (!map) { \
return RuntimeError( "No current area!" ); \
}
#define GET_GAMECONTROL() \
GameControl *gc = core->GetGameControl(); \
if (!gc) { \
return RuntimeError("Can't find GameControl!"); \
}
#define GET_ACTOR_GLOBAL() \
Actor* actor; \
if (globalID > 1000) { \
actor = game->GetActorByGlobalID( globalID ); \
} else { \
actor = game->FindPC( globalID ); \
} \
if (!actor) { \
return RuntimeError( "Actor not found!\n" ); \
}
#define PARSE_ARGS(args, fmt, ...) \
if (!PyArg_ParseTuple( args, fmt, __VA_ARGS__ )) { \
return NULL; \
}
#define ABORT_IF_NULL(thing) \
if (thing == nullptr) return RuntimeError(#thing " cannot be null.")
#define RETURN_BOOL(boolean) \
if (boolean) { \
Py_RETURN_TRUE; \
} else { \
Py_RETURN_FALSE; \
}
static const ScriptingRefBase* GetScriptingRef(PyObject* obj) {
if (!obj || obj == Py_None) {
return NULL;
}
PyObject* attr = PyObject_GetAttrString(obj, "ID");
if (!attr) {
RuntimeError("Invalid Scripting reference, must have ID attribute.");
return NULL;
}
ScriptingId id = (ScriptingId)PyLong_AsUnsignedLongLong( attr );
Py_DecRef(attr);
attr = PyObject_GetAttrString(obj, "SCRIPT_GROUP");
if (!attr) {
RuntimeError("Invalid Scripting reference, must have SCRIPT_GROUP attribute.");
return NULL;
}
ScriptingGroup_t group = static_cast<const char*>(PyString_AsString(attr));
Py_DecRef(attr);
return gs->GetScripingRef(group, id);
}
template <class RETURN>
static RETURN* GetView(PyObject* obj) {
return dynamic_cast<RETURN*>(GetView(GetScriptingRef(obj)));
}
static PyObject* ConstructObjectForScriptableView(const ViewScriptingRef* ref);
static int GetCreatureStrRef(const Actor *actor, unsigned int Str)
{
return actor->StrRefs[Str];
}
static inline bool CheckStat(const Actor * actor, ieDword stat, ieDword value, int op)
{
return DiffCore(actor->GetBase(stat), value, op);
}
static bool StatIsASkill(unsigned int StatID) {
// traps, lore, stealth, lockpicking, pickpocket
if (StatID >= IE_LORE && StatID <= IE_PICKPOCKET) return true;
// alchemy, animals, bluff, concentration, diplomacy, intimidate, search, spellcraft, magicdevice
// NOTE: change if you want to use IE_PROFICIENCYCLUB or IE_EXTRAPROFICIENCY2 etc., as they use the same values
if (StatID >= IE_ALCHEMY && StatID <= IE_MAGICDEVICE) return true;
// Hide, Wilderness_Lore
if (StatID == IE_HIDEINSHADOWS || StatID == IE_TRACKING) return true;
return false;
}
static int GetCreatureStat(const Actor *actor, unsigned int StatID, int Mod)
{
//this is a hack, if more PCStats fields are needed, improve it
if (StatID&EXTRASETTINGS) {
const PCStatsStruct *ps = actor->PCStats;
if (!ps) {
//the official invalid value in GetStat
return 0xdadadada;
}
StatID&=15;
return ps->ExtraSettings[StatID];
}
if (Mod) {
if (core->HasFeature(GF_3ED_RULES) && StatIsASkill(StatID)) {
return actor->GetSkill(StatID);
} else {
if (StatID != IE_HITPOINTS || actor->HasVisibleHP()) {
return actor->GetStat(StatID);
} else {
return 0xdadadada;
}
}
}
return actor->GetBase( StatID );
}
static int SetCreatureStat(Actor *actor, unsigned int StatID, int StatValue, bool pcf)
{
// special AC handling
if (StatID == IE_ARMORCLASS) {
actor->AC.SetNatural(StatValue);
return 1;
} else if (StatID == IE_TOHIT) {
actor->ToHit.SetBase(StatValue);
return 1;
}
//this is a hack, if more PCStats fields are needed, improve it
if (StatID&EXTRASETTINGS) {
PCStatsStruct *ps = actor->PCStats;
if (!ps) {
return 0;
}
StatID&=15;
ps->ExtraSettings[StatID] = StatValue;
actor->ApplyExtraSettings();
return 1;
}
if (pcf) {
actor->SetBase( StatID, StatValue );
} else {
actor->SetBaseNoPCF( StatID, StatValue );
}
actor->CreateDerivedStats();
return 1;
}
PyDoc_STRVAR( GemRB_SetInfoTextColor__doc,
"===== SetInfoTextColor =====\n\
\n\
**Prototype:** GemRB.SetInfoTextColor (red, green, blue[, alpha])\n\
\n\
**Description:**\n\
Sets the color of floating messages in GameControl. Floating messages are\n\
in-game messages issued by actors, or information text coming from game objects.\n\
\n\
**Parameters:** red, green, blue, alpha - color code, alpha defaults to 255 (completely opaque)\n\
\n\
**Return value:** N/A\n\
\n\
**See also:** [[guiscript:Control_SetColor]]"
);
static PyObject* GemRB_SetInfoTextColor(PyObject*, PyObject* args)
{
PyObject* pyColor = NULL;
if (PyArg_ParseTuple( args, "O", &pyColor)) {
core->SetInfoTextColor( ColorFromPy(pyColor) );
}
Py_RETURN_NONE;
}
PyDoc_STRVAR( GemRB_GetGameString__doc,
"===== GetGameString =====\n\
\n\
**Prototype:** GemRB.GetGameString (Index)\n\
\n\
**Description:** Returns a system variable of string type referenced by Index.\n\
\n\
**Parameters:** Index\n\
* 0 - returns the loading picture's name (MOS resref)\n\
* 1 - returns the current area's name (ARE resref)\n\
* 2 - returns the table name for the text screen (2DA resref)\n\
\n\
**Return value:** string - the referenced system variable\n\
\n\
**See also:** [[guiscript:GetSystemVariable]], [[guiscript:GetToken]]"
);
static PyObject* GemRB_GetGameString(PyObject*, PyObject* args)
{
int Index = -1;
PARSE_ARGS(args, "i", &Index);
switch(Index&0xf0) {
case 0: //game strings
const Game *game = core->GetGame();
if (!game) {
return PyString_FromString("");
}
switch(Index&15) {
case 0: // STR_LOADMOS
return PyString_FromResRef(game->LoadMos);
case 1: // STR_AREANAME
return PyString_FromResRef(game->CurrentArea);
case 2: // STR_TEXTSCREEN
return PyString_FromResRef(game->TextScreen);
}
}
return NULL;
}
PyDoc_STRVAR( GemRB_LoadGame__doc,
"===== LoadGame =====\n\
\n\
**Prototype:** GemRB.LoadGame (index[, version])\n\
\n\
**Description:**\n\
Loads a saved game. This must be done before party creation. \n\
You must set the variable called PlayMode before loading a game (see SetVar). \n\
The game won't be loaded before the current GUIScript function returns!\n\
\n\
**Parameters:**\n\
* index - the saved game's index, -1 means new game.\n\
* version - optional version to override some buggy default savegame versions\n\
* PlayMode (variable) - 0 (single player), 1 (tutorial), 2 (multi player)\n\
\n\
**Return value:** N/A\n\
\n\
**Example:**\n\
GemRB.SetVar ('PlayMode', 0)\n\
GemRB.LoadGame (-1, 22)\n\
\n\
**See also:** [[guiscript:EnterGame]], [[guiscript:CreatePlayer]], [[guiscript:SetVar]], [[guiscript:SaveGame]]\n\
"
);
static PyObject* GemRB_LoadGame(PyObject*, PyObject* args)
{
PyObject *obj = NULL;
int VersionOverride = 0;
if (PyArg_ParseTuple( args, "O|i", &obj, &VersionOverride )) {
CObject<SaveGame> save(obj);
core->SetupLoadGame(save, VersionOverride);
Py_RETURN_NONE;
}
return NULL;
}
PyDoc_STRVAR( GemRB_EnterGame__doc,
"===== EnterGame =====\n\
\n\
**Prototype:** GemRB.EnterGame ()\n\
\n\
**Description:** Starts new game and enters it. \n\
It destroys all existing windows, and creates a GameControl window as the 0th \n\
window (the GameControl object will be the games 0th control). \n\
You should already have loaded a game using LoadGame(), otherwise the engine \n\
may terminate. The game won't be entered until the execution of the current \n\
script ends, but a LoadGame() may precede EnterGame() in the same function\n\
(SetNextScript too).\n\
\n\
**Parameters:** N/A\n\
\n\
**Return value:** N/A\n\
\n\
**See also:** [[guiscript:QuitGame]], [[guiscript:LoadGame]], [[guiscript:SetNextScript]]"
);
static PyObject* GemRB_EnterGame(PyObject*, PyObject* /*args*/)
{
core->QuitFlag|=QF_ENTERGAME;
Py_RETURN_NONE;
}
PyDoc_STRVAR( GemRB_QuitGame__doc,
"===== QuitGame =====\n\
\n\
**Prototype:** GemRB.QuitGame ()\n\
\n\
**Description:** Ends the current game session. \n\
To go back to the main screen, you must call SetNextScript. \n\
Automatically unloads all existing windows and resets the window variables\n\
used by HideGUI().\n\
\n\
**Parameters:** N/A\n\
\n\
**Return value:** N/A\n\
\n\
**See also:** [[guiscript:EnterGame]], [[guiscript:Quit]], [[guiscript:SetNextScript]], [[guiscript:HideGUI]]\n\
"
);
static PyObject* GemRB_QuitGame(PyObject*, PyObject* /*args*/)
{
core->QuitFlag=QF_QUITGAME;
Py_RETURN_NONE;
}
PyDoc_STRVAR( GemRB_TextArea_SetChapterText__doc,
"===== TextArea_SetChapterText =====\n\
\n\
**Prototype:** GemRB.SetChapterText (Win, Ctrl, Text)\n\
\n\
**Metaclass Prototype:** SetChapterText (Text)\n\
\n\
**Description:**\n\
Sets up a TextArea for scrolling the chapter text from below the TextArea \n\
to beyond the top.\n\
\n\
**Parameters:** \n\
* Win - window id\n\
* Ctrl - textarea id\n\
* Text - The text to set in the TA\n\
\n\
**Example:**\n\
TextArea = ChapterWindow.GetControl (5)\n\
TextArea.SetChapterText (text)\n\
\n\
**Return value:** N/A\n\
\n\
**See also:**"
);
static PyObject* GemRB_TextArea_SetChapterText(PyObject* self, PyObject* args)
{
char* text = NULL;
PARSE_ARGS(args, "Os", &self, &text);
TextArea* ta = GetView<TextArea>(self);
ABORT_IF_NULL(ta);
ta->ClearText();
String* chapText = StringFromCString(text);
if (chapText) {
// insert enough newlines to push the text offscreen
auto margins = ta->GetMargins();
int rowHeight = ta->LineHeight();
int h = ta->Frame().h - (margins.top + margins.bottom);
int w = ta->Frame().w - (margins.left + margins.right);
int newlines = CeilDiv(h, rowHeight);
ta->AppendText(String(newlines - 1, L'\n'));
ta->AppendText(*chapText);
// append again (+1 since there may not be a trailing newline) after the chtext so it will scroll out of view
ta->AppendText(String(newlines + 1, L'\n'));
delete chapText;
ta->SetFlags(View::IgnoreEvents, BitOp::OR);
int lines = ta->ContentHeight() / rowHeight;
float heightScale = 12.0f / rowHeight; // scale based on text size so smaller text scrolls more slowly
float widthScale = 640.0f / w; // scale based on width to become more slow as we get wider
int textSpeed = gamedata->GetTextSpeed();
int ticksPerLine = 11.0f * heightScale * widthScale * textSpeed;
ta->ScrollToY(-ta->ContentHeight(), lines * ticksPerLine);
}
Py_RETURN_NONE;
}
PyDoc_STRVAR( GemRB_StatComment__doc,
"===== StatComment =====\n\
\n\
**Prototype:** GemRB.StatComment (Strref, X, Y)\n\
\n\
**Description:**\n\
Replaces %%d's with the values of X and Y in a string referenced by strref.\n\
\n\
PST uses %%d values in place of tokens, thus it requires a special command. \n\
In other engines use GetString after setting the needed tokens with \n\
SetToken (if you need to set them at all).\n\
\n\
**Parameters:**\n\
* Strref - a string reference from the dialog.tlk table.\n\
* X, Y - two numerical values to be replaced in place of %%d's.\n\
\n\
**Return value:** A string with resolved %%d's.\n\
\n\
**Example:**\n\
def IntPress():\n\
global Int, StatTable, TextArea\n\
TextArea.SetText (18488)\n\
intComment = StatTable.GetValue (Int, 1)\n\
TextArea.Append (GemRB.StatComment (intComment, 0, 0))\n\
\n\
The above example comes directly from our PST script, it will display the \n\
description of the intelligence stat (strref==18488), adding a comment \n\
based on the current Int variable. StatTable (a 2da table) contains the \n\
comment strref values associated with an intelligence value.\n\
\n\
**See also:** [[guiscript:GetString]], [[guiscript:SetToken]],\n\
[[guiscript:Table_GetValue]], [[guiscript:Control_SetText]],[[guiscript:LoadTable]],\n\
[[guiscript:TextArea_Append]]\n\
"
);
static PyObject* GemRB_StatComment(PyObject * /*self*/, PyObject* args)
{
ieStrRef Strref;
int X, Y;
PyObject* ret;
PARSE_ARGS( args, "iii", &Strref, &X, &Y );
char* text = core->GetCString( Strref );
size_t bufflen = strlen( text ) + 12;
if (bufflen<12) {
return NULL;
}
char* newtext = ( char* ) malloc( bufflen );
//this could be DANGEROUS, not anymore (snprintf is your friend)
snprintf( newtext, bufflen, text, X, Y );
free(text);
ret = PyString_FromString( newtext );
free( newtext );
return ret;
}
PyDoc_STRVAR( GemRB_GetString__doc,
"===== GetString =====\n\
\n\
**Prototype:** GemRB.GetString (Strref[, Flags])\n\
\n\
**Description:** Returns string for given strref. Usually, you don't need to \n\
resolve a string before use, as you can use SetText with a strref parameter. \n\
This command lets you alter the string. For example, if you want to add a \n\
level value without a token, you'll need this.\n\
\n\
**Parameters:** \n\
* Strref - a string reference from the dialog.tlk table\n\
* Flags - a bitfield:\n\
* 1 - display strrefs on\n\
* 2 - play attached sound\n\
* 4 - speech (stop previous sound)\n\
* 256 - strref off (overrides cfg)\n\
\n\
**Return value:** A string with resolved tokens. To resolve %d's, you must\n\
either use StatComment or do it manually.\n\
\n\
**Example:**\n\
Level = GemRB.GetPlayerStat (pc, IE_LEVEL) # 1 at character generation\n\
Label.SetText (GemRB.GetString(12137) + str(Level)) \n\
The above example will display 'Level: 1' in the addressed label.\n\
\n\
**See also:** [[guiscript:StatComment]], [[guiscript:Control_SetText]]\n\
"
);
static PyObject* GemRB_GetString(PyObject * /*self*/, PyObject* args)
{
ieStrRef strref = -1;
int flags = 0;
PARSE_ARGS(args, "i|i", &strref, &flags);
if (strref >= INT_MAX) return PyString_FromString("");
char *text = core->GetCString( strref, flags );
PyObject* ret = text ? PyString_FromString(text) : PyString_FromString("");
free( text );
return ret;
}
PyDoc_STRVAR( GemRB_EndCutSceneMode__doc,
"===== EndCutSceneMode =====\n\
\n\
**Prototype:** EndCutSceneMode ()\n\
\n\
**Description:** Exits the CutScene Mode. It is similar to the gamescript \n\
command of the same name. It gives back the cursor and shows the game GUI \n\
windows hidden by the CutSceneMode() gamescript action. \n\
This is mainly a debugging command.\n\
\n\
**Parameters:** N/A\n\
\n\
**Return value:** N/A\n\
"
);
static PyObject* GemRB_EndCutSceneMode(PyObject * /*self*/, PyObject* /*args*/)
{
core->SetCutSceneMode( false );
Py_RETURN_NONE;
}
PyDoc_STRVAR( GemRB_LoadWindow__doc,
"===== LoadWindow =====\n\
\n\
**Prototype:** GemRB.LoadWindow (WindowID)\n\
\n\
**Description:** Returns a Window. You must call LoadWindowPack before using \n\
this command. The window won't be displayed. If LoadWindowPack() set nonzero \n\
natural screen size with Width and Height parameters, the loaded window is \n\
then moved by (screen size - winpack size) / 2\n\
\n\
**Parameters:** a window ID, see the .chu file specification\n\
\n\
**Return value:** GWindow (index)\n\
\n\
**See also:** [[guiscript:LoadWindowPack]], [[guiscript:Window_GetControl]], [[guiscript:Window_ShowModal]], [[guiscript:accessing_gui_controls]]"
);
static PyObject* GemRB_LoadWindow(PyObject * /*self*/, PyObject* args)
{
int WindowID = -1;
Window::WindowPosition pos = Window::PosCentered;
char* ref = NULL;
PARSE_ARGS(args, "i|si", &WindowID, &ref, &pos);
Window* win = core->LoadWindow( WindowID, ref, pos );
ABORT_IF_NULL(win);
win->SetFlags(Window::AlphaChannel, BitOp::OR);
PyObject* pyWin = ConstructObjectForScriptableView( win->GetScriptingRef() );
return pyWin;
}
PyDoc_STRVAR( GemRB_EnableCheatKeys__doc,
"===== EnableCheatKeys =====\n\
\n\
**Prototype:** GemRB.EnableCheatKeys (flag)\n\
\n\
**Description:** Turns the debug keys on or off. \n\
They are currently turned on by default.\n\
\n\
**Parameters:** flag - boolean\n\
\n\
**Return value:** N/A\n\
\n\
**See also:** GameControl.cpp for actual cheat key functions"
);
static PyObject* GemRB_EnableCheatKeys(PyObject * /*self*/, PyObject* args)
{
int Flag = core->CheatEnabled();
PARSE_ARGS(args, "i", &Flag);
core->EnableCheatKeys( Flag );
Py_RETURN_NONE;
}
PyDoc_STRVAR( GemRB_LoadTable__doc,
"===== LoadTable =====\n\
\n\
**Prototype:** GemRB.LoadTable (2DAResRef[, ignore_error=0])\n\
\n\
**Description:** Loads a 2DA Table. In case it was already loaded, it \n\
will return the table's existing reference (won't load it again).\n\
\n\
**Parameters:** \n\
* 2DAResRef - the table's name (.2da resref)\n\
* ignore_error - boolean, if set, handle missing files gracefully\n\
\n\
**Return value:** GTable\n\
\n\
**See also:** [[guiscript:UnloadTable]], [[guiscript:LoadSymbol]]"
);
static PyObject* GemRB_LoadTable(PyObject * /*self*/, PyObject* args)
{
char* tablename;
int noerror = 0;
PARSE_ARGS( args, "s|i", &tablename, &noerror );
auto tab = gamedata->LoadTable(tablename);
if (tab == nullptr) {
if (noerror) {
Py_RETURN_NONE;
} else {
return RuntimeError("Can't find resource");
}
}
return CObject<TableMgr, std::shared_ptr>(tab);
}
PyDoc_STRVAR( GemRB_Table_GetValue__doc,
"===== Table_GetValue =====\n\
\n\
**Prototype:** GemRB.GetTableValue (TableIndex, RowIndex/RowString, ColIndex/ColString, Type)\n\
\n\
**Metaclass Prototype:** GetValue (RowIndex/RowString, ColIndex/ColString[, Type])\n\
\n\
**Description:** Returns a field of a 2DA Table. The row and column indices \n\
must be of same type (either string or numeric), the return value will be \n\
of the same type, unless Type is specified and different.\n\
\n\
**Parameters:**\n\
* TableIndex - returned by a previous LoadTable command.\n\
* RowIndex, ColIndex - numeric row/column indices\n\
* RowString, ColString - the row/column names as written in the 2da file\n\
* Type - forces a specific return type (GUIDefines.py)\n\
* -1 - default\n\
* GTV_STR 0 - string\n\
* GTV_INT 1 - int\n\
* GTV_STAT 2 - stat symbol (translated to numeric - value of stat)\n\
* GTV_REF 3 - string reference (expanded to string)\n\
\n\
**Return value:** numeric or string, based on the indices or type\n\
\n\
**See also:** [[guiscript:GetSymbolValue]], [[guiscript:Table_FindValue]], [[guiscript:LoadTable]]"
);
static PyObject* GemRB_Table_GetValue(PyObject* self, PyObject* args)
{
PyObject* row = nullptr;
PyObject* col = nullptr;
int type = -1;
PARSE_ARGS(args, "OOO|i", &self, &row, &col, &type);
if (!PyObject_TypeCheck( row, Py_TYPE(col))) {
return AttributeError("RowIndex/RowString and ColIndex/ColString must be the same type.");
}
AutoTable tm = CObject<TableMgr, std::shared_ptr>(self);
ABORT_IF_NULL(tm);
const char* ret;
if (PyObject_TypeCheck( row, &PyString_Type )) {
const auto rows = PyString_AsString(row);
const auto cols = PyString_AsString(col);
ret = tm->QueryField( rows, cols );
} else {
size_t rowi = PyInt_AsLong( row );
size_t coli = PyInt_AsLong( col );
ret = tm->QueryField( rowi, coli );
}
ABORT_IF_NULL(ret);
switch (type) {
case 0: // string
return PyString_FromString( ret );
case 2:
return PyInt_FromLong( core->TranslateStat(ret) );
default:
long val;
bool valid = valid_signednumber(ret, val);
if (type == 3) {
char* cstr = core->GetCString(ieStrRef(val));
PyObject* pystr = PyString_FromString(cstr);
free(cstr);
return pystr;
}
if (valid || type == 1) {
return PyInt_FromLong( val );
}
// else return string
return PyString_FromString( ret );
}
}
PyDoc_STRVAR( GemRB_Table_FindValue__doc,
"===== Table_FindValue =====\n\
\n\
**Prototype:** GemRB.FindTableValue (TableIndex, ColumnIndex, Value[, StartRow])\n\
\n\
**Metaclass Prototype:** FindValue (ColumnIndex, Value[, StartRow])\n\
\n\
**Description:** Returns the first row index of a field value in a 2DA \n\
Table. If StartRowis omitted, the search starts from the beginning.\n\
\n\
**Parameters:**\n\
* TableIndex - integer, returned by a previous LoadTable command.\n\
* Column - index or name of the column in which to look for value.\n\
* Value - value to find in the table\n\
* StartRow - integer, starting row (offset)\n\
\n\
**Return value:** numeric, -1 if the value isn't to be found\n\
\n\
**See also:** [[guiscript:LoadTable]], [[guiscript:Table_GetValue]]"
);
static PyObject* GemRB_Table_FindValue(PyObject* self, PyObject* args)
{
int col;
int start = 0;
long Value;
char* colname = NULL;
char* strvalue = NULL;
if (!PyArg_ParseTuple( args, "Oil|i", &self, &col, &Value, &start )) {
col = -1;
if (!PyArg_ParseTuple( args, "Osl|i", &self, &colname, &Value, &start )) {
col = -2;
PARSE_ARGS( args, "Oss|i", &self, &colname, &strvalue, &start );
}
PyErr_Clear(); //clearing the exception
}
AutoTable tm = CObject<TableMgr, std::shared_ptr>(self);
ABORT_IF_NULL(tm);
if (col == -1) {
return PyInt_FromLong(tm->FindTableValue(colname, Value, start));
} else if (col == -2) {
return PyInt_FromLong(tm->FindTableValue(colname, strvalue, start));
} else {
return PyInt_FromLong(tm->FindTableValue(col, Value, start));
}
}
PyDoc_STRVAR( GemRB_Table_GetRowIndex__doc,
"===== Table_GetRowIndex =====\n\
\n\
**Prototype:** GemRB.GetTableRowIndex (TableIndex, RowName)\n\
\n\
**Metaclass Prototype:** GetRowIndex (RowName)\n\
\n\
**Description:** Returns the index of a row in a 2DA Table.\n\
\n\
**Parameters:**\n\
* TableIndex - returned by a previous LoadTable command.\n\
* RowName - a row label\n\
\n\
**Return value:** numeric, -1 if row doesn't exist\n\
\n\
**See also:** [[guiscript:LoadTable]]"
);
static PyObject* GemRB_Table_GetRowIndex(PyObject* self, PyObject* args)
{
char* rowname;
PARSE_ARGS( args, "Os", &self, &rowname );
AutoTable tm = CObject<TableMgr, std::shared_ptr>(self);
ABORT_IF_NULL(tm);
int row = tm->GetRowIndex( rowname );
//no error if the row doesn't exist
return PyInt_FromLong( row );
}
PyDoc_STRVAR( GemRB_Table_GetRowName__doc,
"===== Table_GetRowName =====\n\
\n\
**Prototype:** GemRB.GetTableRowName (TableIndex, RowIndex)\n\
\n\
**Metaclass Prototype:** GetRowName (RowIndex)\n\
\n\
**Description:** Returns the name of a Row in a 2DA Table.\n\
\n\
**Parameters:**\n\
* TableIndex - returned by a previous LoadTable command.\n\
* RowIndex - the numeric index of the row.\n\
\n\
**Return value:** string\n\
\n\
**See also:** [[guiscript:LoadTable]], [[guiscript:Table_GetColumnName]]"
);
static PyObject* GemRB_Table_GetRowName(PyObject* self, PyObject* args)
{
int row;
PARSE_ARGS( args, "Oi", &self, &row );
AutoTable tm = CObject<TableMgr, std::shared_ptr>(self);
ABORT_IF_NULL(tm);
const char* str = tm->GetRowName( row );
ABORT_IF_NULL(str);
return PyString_FromString( str );
}
PyDoc_STRVAR( GemRB_Table_GetColumnIndex__doc,
"===== Table_GetColumnIndex =====\n\
\n\
**Prototype:** GemRB.GetTableColumnIndex (TableIndex, ColumnName)\n\
\n\
**Metaclass Prototype:** GetColumnIndex (ColumnName)\n\
\n\
**Description:** Returns the index of a column in a 2DA Table.\n\
\n\
**Parameters:**\n\
* TableIndex - returned by a previous LoadTable command.\n\
* ColumnName - a column label\n\
\n\
**Return value:** numeric, -1 if column doesn't exist\n\
\n\
**See also:** [[guiscript:LoadTable]], [[guiscript:Table_GetRowIndex]]"
);
static PyObject* GemRB_Table_GetColumnIndex(PyObject* self, PyObject* args)
{
char* colname;
PARSE_ARGS( args, "Os", &self, &colname );
AutoTable tm = CObject<TableMgr, std::shared_ptr>(self);
ABORT_IF_NULL(tm);
int col = tm->GetColumnIndex( colname );
//no error if the column doesn't exist
return PyInt_FromLong( col );
}
PyDoc_STRVAR( GemRB_Table_GetColumnName__doc,
"===== Table_GetColumnName =====\n\
\n\
**Prototype:** GemRB.GetTableColumnName (TableIndex, ColumnIndex)\n\
\n\
**Metaclass Prototype:** GetColumnName (ColumnIndex)\n\
\n\
**Description:** Returns the name of a Column in a 2DA Table.\n\
\n\
**Parameters:**\n\
* TableIndex - returned by a previous LoadTable command.\n\
* ColumnIndex - the numeric index of the column.\n\
\n\
**Return value:** string\n\
\n\
**See also:** [[guiscript:LoadTable]], [[guiscript:Table_GetRowName]]"
);
static PyObject* GemRB_Table_GetColumnName(PyObject* self, PyObject* args)
{
int col;
PARSE_ARGS( args, "Oi", &self, &col );
AutoTable tm = CObject<TableMgr, std::shared_ptr>(self);
ABORT_IF_NULL(tm);
const char* str = tm->GetColumnName( col );
ABORT_IF_NULL(str);
return PyString_FromString( str );
}
PyDoc_STRVAR( GemRB_Table_GetRowCount__doc,
"===== Table_GetRowCount =====\n\
\n\
**Prototype:** GemRB.GetTableRowCount (TableIndex)\n\
\n\
**Metaclass Prototype:** GetRowCount ()\n\
\n\
**Description:** Returns the number of rows in a 2DA Table.\n\
\n\
**Parameters:** TableIndex - returned by a previous LoadTable command.\n\
\n\
**Return value:** numeric\n\
\n\
**See also:** [[guiscript:LoadTable]], [[guiscript:Table_GetColumnCount]]"
);
static PyObject* GemRB_Table_GetRowCount(PyObject* self, PyObject* args)
{
PARSE_ARGS( args, "O", &self );
AutoTable tm = CObject<TableMgr, std::shared_ptr>(self);
ABORT_IF_NULL(tm);
return PyInt_FromLong( tm->GetRowCount() );
}
PyDoc_STRVAR( GemRB_Table_GetColumnCount__doc,
"===== Table_GetColumnCount =====\n\
\n\
**Prototype:** GemRB.GetTableColumnCount (TableIndex[, Row])\n\
\n\
**Metaclass Prototype:** GetColumnCount ([Row])\n\
\n\
**Description:** Returns the column count of the specified row in a 2DA Table.\n\
\n\
**Parameters:**\n\
* TableIndex - returned by a previous LoadTable command.\n\
* Row - the row of the table, if omitted, defaults to 0\n\
\n\
**Return value:** numeric\n\
\n\
**See also:** [[guiscript:LoadTable]], [[guiscript:Table_GetRowCount]]"
);