forked from MinoMino/minqlx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_embed.c
1916 lines (1637 loc) · 64.8 KB
/
python_embed.c
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 <Python.h>
#include <structmember.h>
#include <structseq.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "pyminqlx.h"
#include "quake_common.h"
#include "patterns.h"
#include "common.h"
PyObject* client_command_handler = NULL;
PyObject* server_command_handler = NULL;
PyObject* client_connect_handler = NULL;
PyObject* client_loaded_handler = NULL;
PyObject* client_disconnect_handler = NULL;
PyObject* frame_handler = NULL;
PyObject* custom_command_handler = NULL;
PyObject* new_game_handler = NULL;
PyObject* set_configstring_handler = NULL;
PyObject* rcon_handler = NULL;
PyObject* console_print_handler = NULL;
PyObject* client_spawn_handler = NULL;
PyObject* kamikaze_use_handler = NULL;
PyObject* kamikaze_explode_handler = NULL;
static PyThreadState* mainstate;
static int initialized = 0;
/*
* If we don't do this, we'll simply get NULL from both PyRun_File*()
* and PyRun_String*() if the module has an error in it. It's ugly as
* fuck, but other than doing this, I have no idea how to extract the
* traceback. The documentation or Google doesn't help much either.
*/
static const char loader[] = "import traceback\n" \
"try:\n" \
" import sys\n" \
" sys.path.append('" CORE_MODULE "')\n" \
" sys.path.append('.')\n" \
" import minqlx\n" \
" minqlx.initialize()\n" \
" ret = True\n" \
"except Exception as e:\n" \
" e = traceback.format_exc().rstrip('\\n')\n" \
" for line in e.split('\\n'): print(line)\n" \
" ret = False\n";
/*
* The number of handlers was getting large, so instead of a bunch of
* else ifs in register_handler, I'm using a struct to hold name-handler
* pairs and iterate over them instead.
*/
static handler_t handlers[] = {
{"client_command", &client_command_handler},
{"server_command", &server_command_handler},
{"frame", &frame_handler},
{"player_connect", &client_connect_handler},
{"player_loaded", &client_loaded_handler},
{"player_disconnect", &client_disconnect_handler},
{"custom_command", &custom_command_handler},
{"new_game", &new_game_handler},
{"set_configstring", &set_configstring_handler},
{"rcon", &rcon_handler},
{"console_print", &console_print_handler},
{"player_spawn", &client_spawn_handler},
{"kamikaze_use", &kamikaze_use_handler},
{"kamikaze_explode", &kamikaze_explode_handler},
{NULL, NULL}
};
/*
* ================================================================
* Struct Sequences
* ================================================================
*/
// Players
static PyTypeObject player_info_type = {0};
static PyStructSequence_Field player_info_fields[] = {
{"client_id", "The player's client ID."},
{"name", "The player's name."},
{"connection_state", "The player's connection state."},
{"userinfo", "The player's userinfo."},
{"steam_id", "The player's 64-bit representation of the Steam ID."},
{"team", "The player's team."},
{"privileges", "The player's privileges."},
{NULL}
};
static PyStructSequence_Desc player_info_desc = {
"PlayerInfo",
"Information about a player, such as Steam ID, name, client ID, and whatnot.",
player_info_fields,
(sizeof(player_info_fields)/sizeof(PyStructSequence_Field)) - 1
};
// Player state
static PyTypeObject player_state_type = {0};
static PyStructSequence_Field player_state_fields[] = {
{"is_alive", "Whether the player's alive or not."},
{"position", "The player's position."},
{"velocity", "The player's velocity."},
{"health", "The player's health."},
{"armor", "The player's armor."},
{"noclip", "Whether the player has noclip or not."},
{"weapon", "The weapon the player is currently using."},
{"weapons", "The player's weapons."},
{"ammo", "The player's weapon ammo."},
{"powerups", "The player's powerups."},
{"holdable", "The player's holdable item."},
{"flight", "A struct sequence with flight parameters."},
{"is_frozen", "Whether the player is frozen(freezetag)."},
{NULL}
};
static PyStructSequence_Desc player_state_desc = {
"PlayerState",
"Information about a player's state in the game.",
player_state_fields,
(sizeof(player_state_fields)/sizeof(PyStructSequence_Field)) - 1
};
// Stats
static PyTypeObject player_stats_type = {0};
static PyStructSequence_Field player_stats_fields[] = {
{"score", "The player's primary score."},
{"kills", "The player's number of kills."},
{"deaths", "The player's number of deaths."},
{"damage_dealt", "The player's total damage dealt."},
{"damage_taken", "The player's total damage taken."},
{"time", "The time in milliseconds the player has on a team since the game started."},
{"ping", "The player's ping."},
{NULL}
};
static PyStructSequence_Desc player_stats_desc = {
"PlayerStats",
"A player's score and some basic stats.",
player_stats_fields,
(sizeof(player_stats_fields)/sizeof(PyStructSequence_Field)) - 1
};
// Vectors
static PyTypeObject vector3_type = {0};
static PyStructSequence_Field vector3_fields[] = {
{"x", NULL},
{"y", NULL},
{"z", NULL},
{NULL}
};
static PyStructSequence_Desc vector3_desc = {
"Vector3",
"A three-dimensional vector.",
vector3_fields,
(sizeof(vector3_fields)/sizeof(PyStructSequence_Field)) - 1
};
// Weapons
static PyTypeObject weapons_type = {0};
static PyStructSequence_Field weapons_fields[] = {
{"g", NULL}, {"mg", NULL}, {"sg", NULL},
{"gl", NULL}, {"rl", NULL}, {"lg", NULL},
{"rg", NULL}, {"pg", NULL}, {"bfg", NULL},
{"gh", NULL}, {"ng", NULL}, {"pl", NULL},
{"cg", NULL}, {"hmg", NULL}, {"hands", NULL},
{NULL}
};
static PyStructSequence_Desc weapons_desc = {
"Weapons",
"A struct sequence containing all the weapons in the game.",
weapons_fields,
(sizeof(weapons_fields)/sizeof(PyStructSequence_Field)) - 1
};
// Powerups
static PyTypeObject powerups_type = {0};
static PyStructSequence_Field powerups_fields[] = {
{"quad", NULL}, {"battlesuit", NULL},
{"haste", NULL}, {"invisibility", NULL},
{"regeneration", NULL}, {"invulnerability", NULL},
{NULL}
};
static PyStructSequence_Desc powerups_desc = {
"Powerups",
"A struct sequence containing all the powerups in the game.",
powerups_fields,
(sizeof(powerups_fields)/sizeof(PyStructSequence_Field)) - 1
};
// Flight
static PyTypeObject flight_type = {0};
static PyStructSequence_Field flight_fields[] = {
{"fuel", NULL},
{"max_fuel", NULL},
{"thrust", NULL},
{"refuel", NULL},
{NULL}
};
static PyStructSequence_Desc flight_desc = {
"Flight",
"A struct sequence containing parameters for the flight holdable item.",
flight_fields,
(sizeof(flight_fields)/sizeof(PyStructSequence_Field)) - 1
};
/*
* ================================================================
* player_info/players_info
* ================================================================
*/
static PyObject* makePlayerTuple(int client_id) {
PyObject *name, *team, *priv;
PyObject* cid = PyLong_FromLongLong(client_id);
if (g_entities[client_id].client != NULL) {
if (g_entities[client_id].client->pers.connected == CON_DISCONNECTED)
name = PyUnicode_FromString("");
else
name = PyUnicode_DecodeUTF8(g_entities[client_id].client->pers.netname,
strlen(g_entities[client_id].client->pers.netname), "ignore");
if (g_entities[client_id].client->pers.connected == CON_DISCONNECTED)
team = PyLong_FromLongLong(TEAM_SPECTATOR); // Set team to spectator if not yet connected.
else
team = PyLong_FromLongLong(g_entities[client_id].client->sess.sessionTeam);
priv = PyLong_FromLongLong(g_entities[client_id].client->sess.privileges);
}
else {
name = PyUnicode_FromString("");
team = PyLong_FromLongLong(TEAM_SPECTATOR);
priv = PyLong_FromLongLong(-1);
}
PyObject* state = PyLong_FromLongLong(svs->clients[client_id].state);
PyObject* userinfo = PyUnicode_DecodeUTF8(svs->clients[client_id].userinfo, strlen(svs->clients[client_id].userinfo), "ignore");
PyObject* steam_id = PyLong_FromLongLong(svs->clients[client_id].steam_id);
PyObject* info = PyStructSequence_New(&player_info_type);
PyStructSequence_SetItem(info, 0, cid);
PyStructSequence_SetItem(info, 1, name);
PyStructSequence_SetItem(info, 2, state);
PyStructSequence_SetItem(info, 3, userinfo);
PyStructSequence_SetItem(info, 4, steam_id);
PyStructSequence_SetItem(info, 5, team);
PyStructSequence_SetItem(info, 6, priv);
return info;
}
static PyObject* PyMinqlx_PlayerInfo(PyObject* self, PyObject* args) {
int i;
if (!PyArg_ParseTuple(args, "i:player", &i))
return NULL;
if (i < 0 || i >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (allow_free_client != i && svs->clients[i].state == CS_FREE) {
#ifndef NDEBUG
DebugPrint("WARNING: PyMinqlx_PlayerInfo called for CS_FREE client %d.\n", i);
#endif
Py_RETURN_NONE;
}
return makePlayerTuple(i);
}
static PyObject* PyMinqlx_PlayersInfo(PyObject* self, PyObject* args) {
PyObject* ret = PyList_New(sv_maxclients->integer);
for (int i = 0; i < sv_maxclients->integer; i++) {
if (svs->clients[i].state == CS_FREE) {
if (PyList_SetItem(ret, i, Py_None) == -1)
return NULL;
Py_INCREF(Py_None);
continue;
}
if (PyList_SetItem(ret, i, makePlayerTuple(i)) == -1)
return NULL;
}
return ret;
}
/*
* ================================================================
* get_userinfo
* ================================================================
*/
static PyObject* PyMinqlx_GetUserinfo(PyObject* self, PyObject* args) {
int i;
if (!PyArg_ParseTuple(args, "i:get_userinfo", &i))
return NULL;
if (i < 0 || i >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (allow_free_client != i && svs->clients[i].state == CS_FREE)
Py_RETURN_NONE;
return PyUnicode_DecodeUTF8(svs->clients[i].userinfo, strlen(svs->clients[i].userinfo), "ignore");
}
/*
* ================================================================
* send_server_command
* ================================================================
*/
static PyObject* PyMinqlx_SendServerCommand(PyObject* self, PyObject* args) {
PyObject* client_id;
int i;
char* cmd;
if (!PyArg_ParseTuple(args, "Os:send_server_command", &client_id, &cmd))
return NULL;
if (client_id == Py_None) {
My_SV_SendServerCommand(NULL, "%s\n", cmd); // Send to all.
Py_RETURN_TRUE;
}
else if (PyLong_Check(client_id)) {
i = PyLong_AsLong(client_id);
if (i >= 0 && i < sv_maxclients->integer) {
if (svs->clients[i].state != CS_ACTIVE)
Py_RETURN_FALSE;
else {
My_SV_SendServerCommand(&svs->clients[i], "%s\n", cmd);
Py_RETURN_TRUE;
}
}
}
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d, or None.",
sv_maxclients->integer);
return NULL;
}
/*
* ================================================================
* client_command
* ================================================================
*/
static PyObject* PyMinqlx_ClientCommand(PyObject* self, PyObject* args) {
int i;
char* cmd;
if (!PyArg_ParseTuple(args, "is:client_command", &i, &cmd))
return NULL;
if (i >= 0 && i < sv_maxclients->integer) {
if (svs->clients[i].state == CS_FREE || svs->clients[i].state == CS_ZOMBIE)
Py_RETURN_FALSE;
else {
My_SV_ExecuteClientCommand(&svs->clients[i], cmd, qtrue);
Py_RETURN_TRUE;
}
}
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d, or None.",
sv_maxclients->integer);
return NULL;
}
/*
* ================================================================
* console_command
* ================================================================
*/
static PyObject* PyMinqlx_ConsoleCommand(PyObject* self, PyObject* args) {
char* cmd;
if (!PyArg_ParseTuple(args, "s:console_command", &cmd))
return NULL;
Cmd_ExecuteString(cmd);
Py_RETURN_NONE;
}
/*
* ================================================================
* get_cvar
* ================================================================
*/
static PyObject* PyMinqlx_GetCvar(PyObject* self, PyObject* args) {
char* name;
if (!PyArg_ParseTuple(args, "s:get_cvar", &name))
return NULL;
cvar_t* cvar = Cvar_FindVar(name);
if (cvar) {
return PyUnicode_FromString(cvar->string);
}
Py_RETURN_NONE;
}
/*
* ================================================================
* set_cvar
* ================================================================
*/
static PyObject* PyMinqlx_SetCvar(PyObject* self, PyObject* args) {
char *name, *value;
int flags = 0;
if (!PyArg_ParseTuple(args, "ss|i:set_cvar", &name, &value, &flags))
return NULL;
cvar_t* var = Cvar_FindVar(name);
if (!var) {
Cvar_Get(name, value, flags);
Py_RETURN_TRUE;
}
if (flags == -1)
Cvar_Set2(name, value, qtrue);
else
Cvar_Set2(name, value, qfalse);
Py_RETURN_FALSE;
}
/*
* ================================================================
* set_cvar_limit
* ================================================================
*/
static PyObject* PyMinqlx_SetCvarLimit(PyObject* self, PyObject* args) {
char *name, *value, *min, *max;
int flags = 0;
if (!PyArg_ParseTuple(args, "ssss|i:set_cvar_limit", &name, &value, &min, &max, &flags))
return NULL;
Cvar_GetLimit(name, value, min, max, flags);
Py_RETURN_NONE;
}
/*
* ================================================================
* kick
* ================================================================
*/
static PyObject* PyMinqlx_Kick(PyObject* self, PyObject* args) {
int i;
PyObject* reason;
if (!PyArg_ParseTuple(args, "iO:kick", &i, &reason))
return NULL;
if (i >= 0 && i < sv_maxclients->integer) {
if (svs->clients[i].state != CS_ACTIVE) {
PyErr_Format(PyExc_ValueError,
"client_id must be None or the ID of an active player.");
return NULL;
}
else if (reason == Py_None || (PyUnicode_Check(reason) && PyUnicode_AsUTF8(reason)[0] == 0)) {
// Default kick message for None or empty strings.
My_SV_DropClient(&svs->clients[i], "was kicked.");
}
else if (PyUnicode_Check(reason)) {
My_SV_DropClient(&svs->clients[i], PyUnicode_AsUTF8(reason));
}
}
else {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d, or None.",
sv_maxclients->integer);
return NULL;
}
Py_RETURN_NONE;
}
/*
* ================================================================
* console_print
* ================================================================
*/
static PyObject* PyMinqlx_ConsolePrint(PyObject* self, PyObject* args) {
char* text;
if (!PyArg_ParseTuple(args, "s:console_print", &text))
return NULL;
My_Com_Printf("%s\n", text);
Py_RETURN_NONE;
}
/*
* ================================================================
* get_configstring
* ================================================================
*/
static PyObject* PyMinqlx_GetConfigstring(PyObject* self, PyObject* args) {
int i;
char csbuffer[4096];
if (!PyArg_ParseTuple(args, "i:get_configstring", &i))
return NULL;
else if (i < 0 || i > MAX_CONFIGSTRINGS) {
PyErr_Format(PyExc_ValueError,
"index needs to be a number from 0 to %d.",
MAX_CONFIGSTRINGS);
return NULL;
}
SV_GetConfigstring(i, csbuffer, sizeof(csbuffer));
return PyUnicode_DecodeUTF8(csbuffer, strlen(csbuffer), "ignore");
}
/*
* ================================================================
* set_configstring
* ================================================================
*/
static PyObject* PyMinqlx_SetConfigstring(PyObject* self, PyObject* args) {
int i;
char* cs;
if (!PyArg_ParseTuple(args, "is:set_configstring", &i, &cs))
return NULL;
else if (i < 0 || i > MAX_CONFIGSTRINGS) {
PyErr_Format(PyExc_ValueError,
"index needs to be a number from 0 to %d.",
MAX_CONFIGSTRINGS);
return NULL;
}
My_SV_SetConfigstring(i, cs);
Py_RETURN_NONE;
}
/*
* ================================================================
* force_vote
* ================================================================
*/
static PyObject* PyMinqlx_ForceVote(PyObject* self, PyObject* args) {
int pass;
if (!PyArg_ParseTuple(args, "p:force_vote", &pass))
return NULL;
if (!level->voteTime) {
// No active vote.
Py_RETURN_FALSE;
}
else if (pass && level->voteTime) {
// We tell the server every single client voted yes, making it pass in the next G_RunFrame.
for (int i = 0; i < sv_maxclients->integer; i++) {
if (svs->clients[i].state == CS_ACTIVE)
g_entities[i].client->pers.voteState = VOTE_YES;
}
}
else if (!pass && level->voteTime) {
// If we tell the server the vote is over, it'll fail it right away.
level->voteTime -= 30000;
}
Py_RETURN_TRUE;
}
/*
* ================================================================
* add_console_command
* ================================================================
*/
static PyObject* PyMinqlx_AddConsoleCommand(PyObject* self, PyObject* args) {
char* cmd;
if (!PyArg_ParseTuple(args, "s:add_console_command", &cmd))
return NULL;
Cmd_AddCommand(cmd, PyCommand);
Py_RETURN_NONE;
}
/*
* ================================================================
* register_handler
* ================================================================
*/
static PyObject* PyMinqlx_RegisterHandler(PyObject* self, PyObject* args) {
char* event;
PyObject* new_handler;
if (!PyArg_ParseTuple(args, "sO:register_handler", &event, &new_handler)) {
return NULL;
}
else if (new_handler != Py_None && !PyCallable_Check(new_handler)) {
PyErr_SetString(PyExc_TypeError, "The handler must be callable.");
return NULL;
}
for (handler_t* h = handlers; h->name; h++) {
if (!strcmp(h->name, event)) {
Py_XDECREF(*h->handler);
if (new_handler == Py_None)
*h->handler = NULL;
else {
*h->handler = new_handler;
Py_INCREF(new_handler);
}
Py_RETURN_NONE;
}
}
PyErr_SetString(PyExc_ValueError, "Invalid event.");
return NULL;
}
/*
* ================================================================
* player_state
* ================================================================
*/
static PyObject* PyMinqlx_PlayerState(PyObject* self, PyObject* args) {
int client_id;
if (!PyArg_ParseTuple(args, "i:player_state", &client_id))
return NULL;
else if (client_id < 0 || client_id >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (!g_entities[client_id].client)
Py_RETURN_NONE;
PyObject* state = PyStructSequence_New(&player_state_type);
PyStructSequence_SetItem(state, 0, PyBool_FromLong(g_entities[client_id].client->ps.pm_type == 0));
PyObject* pos = PyStructSequence_New(&vector3_type);
PyStructSequence_SetItem(pos, 0,
PyFloat_FromDouble(g_entities[client_id].client->ps.origin[0]));
PyStructSequence_SetItem(pos, 1,
PyFloat_FromDouble(g_entities[client_id].client->ps.origin[1]));
PyStructSequence_SetItem(pos, 2,
PyFloat_FromDouble(g_entities[client_id].client->ps.origin[2]));
PyStructSequence_SetItem(state, 1, pos);
PyObject* vel = PyStructSequence_New(&vector3_type);
PyStructSequence_SetItem(vel, 0,
PyFloat_FromDouble(g_entities[client_id].client->ps.velocity[0]));
PyStructSequence_SetItem(vel, 1,
PyFloat_FromDouble(g_entities[client_id].client->ps.velocity[1]));
PyStructSequence_SetItem(vel, 2,
PyFloat_FromDouble(g_entities[client_id].client->ps.velocity[2]));
PyStructSequence_SetItem(state, 2, vel);
PyStructSequence_SetItem(state, 3, PyLong_FromLongLong(g_entities[client_id].health));
PyStructSequence_SetItem(state, 4, PyLong_FromLongLong(g_entities[client_id].client->ps.stats[STAT_ARMOR]));
PyStructSequence_SetItem(state, 5, PyBool_FromLong(g_entities[client_id].client->noclip));
PyStructSequence_SetItem(state, 6, PyLong_FromLongLong(g_entities[client_id].client->ps.weapon));
// Get weapons and ammo count.
PyObject* weapons = PyStructSequence_New(&weapons_type);
PyObject* ammo = PyStructSequence_New(&weapons_type);
for (int i = 0; i < weapons_desc.n_in_sequence; i++) {
PyStructSequence_SetItem(weapons, i, PyBool_FromLong(g_entities[client_id].client->ps.stats[STAT_WEAPONS] & (1 << (i+1))));
PyStructSequence_SetItem(ammo, i, PyLong_FromLongLong(g_entities[client_id].client->ps.ammo[i+1]));
}
PyStructSequence_SetItem(state, 7, weapons);
PyStructSequence_SetItem(state, 8, ammo);
PyObject* powerups = PyStructSequence_New(&powerups_type);
int index;
for (int i = 0; i < powerups_desc.n_in_sequence; i++) {
index = i+PW_QUAD;
if (index == PW_FLIGHT) // Skip flight.
index = PW_INVULNERABILITY;
int remaining = g_entities[client_id].client->ps.powerups[index];
if (remaining) // We don't want the time, but the remaining time.
remaining -= level->time;
PyStructSequence_SetItem(powerups, i, PyLong_FromLongLong(remaining));
}
PyStructSequence_SetItem(state, 9, powerups);
PyObject* holdable;
switch (g_entities[client_id].client->ps.stats[STAT_HOLDABLE_ITEM]) {
case 0:
holdable = Py_None;
Py_INCREF(Py_None);
break;
case 27:
holdable = PyUnicode_FromString("teleporter");
break;
case 28:
holdable = PyUnicode_FromString("medkit");
break;
case 34:
holdable = PyUnicode_FromString("flight");
break;
case 37:
holdable = PyUnicode_FromString("kamikaze");
break;
case 38:
holdable = PyUnicode_FromString("portal");
break;
case 39:
holdable = PyUnicode_FromString("invulnerability");
break;
default:
holdable = PyUnicode_FromString("unknown");
}
PyStructSequence_SetItem(state, 10, holdable);
PyObject* flight = PyStructSequence_New(&flight_type);
PyStructSequence_SetItem(flight, 0,
PyLong_FromLongLong(g_entities[client_id].client->ps.stats[STAT_CUR_FLIGHT_FUEL]));
PyStructSequence_SetItem(flight, 1,
PyLong_FromLongLong(g_entities[client_id].client->ps.stats[STAT_MAX_FLIGHT_FUEL]));
PyStructSequence_SetItem(flight, 2,
PyLong_FromLongLong(g_entities[client_id].client->ps.stats[STAT_FLIGHT_THRUST]));
PyStructSequence_SetItem(flight, 3,
PyLong_FromLongLong(g_entities[client_id].client->ps.stats[STAT_FLIGHT_REFUEL]));
PyStructSequence_SetItem(state, 11, flight);
PyStructSequence_SetItem(state, 12, PyBool_FromLong(g_entities[client_id].client->ps.pm_type == 4));
return state;
}
/*
* ================================================================
* player_stats
* ================================================================
*/
static PyObject* PyMinqlx_PlayerStats(PyObject* self, PyObject* args) {
int client_id;
if (!PyArg_ParseTuple(args, "i:player_stats", &client_id))
return NULL;
else if (client_id < 0 || client_id >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (!g_entities[client_id].client)
Py_RETURN_NONE;
PyObject* stats = PyStructSequence_New(&player_stats_type);
int score = g_entities[client_id].client->sess.sessionTeam == TEAM_SPECTATOR ?
0 : g_entities[client_id].client->ps.persistant[PERS_ROUND_SCORE];
PyStructSequence_SetItem(stats, 0, PyLong_FromLongLong(score));
PyStructSequence_SetItem(stats, 1, PyLong_FromLongLong(g_entities[client_id].client->expandedStats.numKills));
PyStructSequence_SetItem(stats, 2, PyLong_FromLongLong(g_entities[client_id].client->expandedStats.numDeaths));
PyStructSequence_SetItem(stats, 3, PyLong_FromLongLong(g_entities[client_id].client->expandedStats.totalDamageDealt));
PyStructSequence_SetItem(stats, 4, PyLong_FromLongLong(g_entities[client_id].client->expandedStats.totalDamageTaken));
PyStructSequence_SetItem(stats, 5, PyLong_FromLongLong(level->time - g_entities[client_id].client->pers.enterTime));
PyStructSequence_SetItem(stats, 6, PyLong_FromLongLong(g_entities[client_id].client->ps.ping));
return stats;
}
/*
* ================================================================
* set_position
* ================================================================
*/
static PyObject* PyMinqlx_SetPosition(PyObject* self, PyObject* args) {
int client_id;
PyObject* new_position;
if (!PyArg_ParseTuple(args, "iO:set_position", &client_id, &new_position))
return NULL;
else if (client_id < 0 || client_id >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (!g_entities[client_id].client)
Py_RETURN_FALSE;
else if (!PyObject_TypeCheck(new_position, &vector3_type)) {
PyErr_Format(PyExc_ValueError, "Argument must be of type minqlx.Vector3.");
return NULL;
}
g_entities[client_id].client->ps.origin[0] =
(float)PyFloat_AsDouble(PyStructSequence_GetItem(new_position, 0));
g_entities[client_id].client->ps.origin[1] =
(float)PyFloat_AsDouble(PyStructSequence_GetItem(new_position, 1));
g_entities[client_id].client->ps.origin[2] =
(float)PyFloat_AsDouble(PyStructSequence_GetItem(new_position, 2));
Py_RETURN_TRUE;
}
/*
* ================================================================
* set_velocity
* ================================================================
*/
static PyObject* PyMinqlx_SetVelocity(PyObject* self, PyObject* args) {
int client_id;
PyObject* new_velocity;
if (!PyArg_ParseTuple(args, "iO:set_velocity", &client_id, &new_velocity))
return NULL;
else if (client_id < 0 || client_id >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (!g_entities[client_id].client)
Py_RETURN_FALSE;
else if (!PyObject_TypeCheck(new_velocity, &vector3_type)) {
PyErr_Format(PyExc_ValueError, "Argument must be of type minqlx.Vector3.");
return NULL;
}
g_entities[client_id].client->ps.velocity[0] =
(float)PyFloat_AsDouble(PyStructSequence_GetItem(new_velocity, 0));
g_entities[client_id].client->ps.velocity[1] =
(float)PyFloat_AsDouble(PyStructSequence_GetItem(new_velocity, 1));
g_entities[client_id].client->ps.velocity[2] =
(float)PyFloat_AsDouble(PyStructSequence_GetItem(new_velocity, 2));
Py_RETURN_TRUE;
}
/*
* ================================================================
* noclip
* ================================================================
*/
static PyObject* PyMinqlx_NoClip(PyObject* self, PyObject* args) {
int client_id, activate;
if (!PyArg_ParseTuple(args, "ip:noclip", &client_id, &activate))
return NULL;
else if (client_id < 0 || client_id >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (!g_entities[client_id].client)
Py_RETURN_FALSE;
if ((activate && g_entities[client_id].client->noclip) || (!activate && !g_entities[client_id].client->noclip)) {
// Change was made.
Py_RETURN_FALSE;
}
g_entities[client_id].client->noclip = activate ? qtrue : qfalse;
Py_RETURN_TRUE;
}
/*
* ================================================================
* set_health
* ================================================================
*/
static PyObject* PyMinqlx_SetHealth(PyObject* self, PyObject* args) {
int client_id, health;
if (!PyArg_ParseTuple(args, "ii:set_health", &client_id, &health))
return NULL;
else if (client_id < 0 || client_id >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (!g_entities[client_id].client)
Py_RETURN_FALSE;
g_entities[client_id].health = health;
Py_RETURN_TRUE;
}
/*
* ================================================================
* set_armor
* ================================================================
*/
static PyObject* PyMinqlx_SetArmor(PyObject* self, PyObject* args) {
int client_id, armor;
if (!PyArg_ParseTuple(args, "ii:set_armor", &client_id, &armor))
return NULL;
else if (client_id < 0 || client_id >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (!g_entities[client_id].client)
Py_RETURN_FALSE;
g_entities[client_id].client->ps.stats[STAT_ARMOR] = armor;
Py_RETURN_TRUE;
}
/*
* ================================================================
* set_weapons
* ================================================================
*/
static PyObject* PyMinqlx_SetWeapons(PyObject* self, PyObject* args) {
int client_id, weapon_flags = 0;
PyObject* weapons;
if (!PyArg_ParseTuple(args, "iO:set_weapons", &client_id, &weapons))
return NULL;
else if (client_id < 0 || client_id >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (!g_entities[client_id].client)
Py_RETURN_FALSE;
else if (!PyObject_TypeCheck(weapons, &weapons_type)) {
PyErr_Format(PyExc_ValueError, "Argument must be of type minqlx.Weapons.");
return NULL;
}
PyObject* w;
for (int i = 0; i < weapons_desc.n_in_sequence; i++) {
w = PyStructSequence_GetItem(weapons, i);
if (!PyBool_Check(w)) {
PyErr_Format(PyExc_ValueError, "Tuple argument %d is not a boolean.", i);
return NULL;
}
weapon_flags |= w == Py_True ? (1 << (i+1)) : 0;
}
g_entities[client_id].client->ps.stats[STAT_WEAPONS] = weapon_flags;
Py_RETURN_TRUE;
}
/*
* ================================================================
* set_weapon
* ================================================================
*/
static PyObject* PyMinqlx_SetWeapon(PyObject* self, PyObject* args) {
int client_id, weapon;
if (!PyArg_ParseTuple(args, "ii:set_weapon", &client_id, &weapon))
return NULL;
else if (client_id < 0 || client_id >= sv_maxclients->integer) {
PyErr_Format(PyExc_ValueError,
"client_id needs to be a number from 0 to %d.",
sv_maxclients->integer);
return NULL;
}
else if (!g_entities[client_id].client)
Py_RETURN_FALSE;
else if (weapon < 0 || weapon > 16) {