From 67d53f4e56b285127924e97f5d894ca613241776 Mon Sep 17 00:00:00 2001 From: Robin Redeker Date: Sat, 2 Feb 2002 09:38:19 +0000 Subject: [PATCH] Added some QC-API functions: String_Cut, String_Len, Key_CountBindings. --- libs/gamecode/builtins/bi_keys.c | 45 ++++++++++++++++++++++++++++-- libs/gamecode/builtins/bi_string.c | 43 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/libs/gamecode/builtins/bi_keys.c b/libs/gamecode/builtins/bi_keys.c index d8b36a6b2..a7bb1546e 100644 --- a/libs/gamecode/builtins/bi_keys.c +++ b/libs/gamecode/builtins/bi_keys.c @@ -61,11 +61,17 @@ bi_Key_SetBinding (progs_t *pr) Key_SetBinding (target, keynum, binding); } +/* + bi_Key_LookupBinding + + Perform a reverse-binding-lookup +*/ static void bi_Key_LookupBinding (progs_t *pr) { int target = G_INT (pr, OFS_PARM0); - const char *binding = G_STRING (pr, OFS_PARM1); + int bindnum = G_INT (pr, OFS_PARM1); + const char *binding = G_STRING (pr, OFS_PARM2); int i; knum_t keynum = -1; const char *keybind = NULL; @@ -74,13 +80,47 @@ bi_Key_LookupBinding (progs_t *pr) keybind = keybindings[target][i]; if(keybind == NULL) { continue; } if(strcmp(keybind, binding) == 0) { - keynum = i; + bindnum--; + if(bindnum == 0) { + keynum = i; + break; + } } } G_INT (pr, OFS_RETURN) = keynum; }; +/* + bi_Key_CountBinding + + Counts how often a binding is assigned to a key +*/ +static void +bi_Key_CountBinding (progs_t *pr) +{ + int target = G_INT (pr, OFS_PARM0); + const char *binding = G_STRING (pr, OFS_PARM1); + int i, res = 0; + const char *keybind = NULL; + + for (i = 0; i < QFK_LAST; i++) { + keybind = keybindings[target][i]; + if(keybind == NULL) { continue; } + if(strcmp(keybind, binding) == 0) { + res++; + } + } + + G_INT (pr, OFS_RETURN) = res; +}; + + +/* + bi_Key_LookupBinding + + Convertes a keynum to a string +*/ static void bi_Key_KeynumToString (progs_t *pr) { @@ -95,6 +135,7 @@ Key_Progs_Init (progs_t *pr) { PR_AddBuiltin (pr, "Key_SetBinding", bi_Key_SetBinding, -1); PR_AddBuiltin (pr, "Key_LookupBinding", bi_Key_LookupBinding, -1); + PR_AddBuiltin (pr, "Key_CountBinding", bi_Key_CountBinding, -1); PR_AddBuiltin (pr, "Key_KeynumToString", bi_Key_KeynumToString, -1); // NEED THIS ?// PR_AddBuiltin (pr, "Key_StringToKeynum", bi_Key_KeynumToString, -1); } diff --git a/libs/gamecode/builtins/bi_string.c b/libs/gamecode/builtins/bi_string.c index f1cdb8c9d..3ff48e93c 100644 --- a/libs/gamecode/builtins/bi_string.c +++ b/libs/gamecode/builtins/bi_string.c @@ -41,6 +41,11 @@ static const char rcsid[] = #include "QF/progs.h" #include "QF/zone.h" +/* + bi_String_ReplaceChar + + Repalces a special character in a string with another +*/ static void bi_String_ReplaceChar (progs_t *pr) { @@ -60,8 +65,46 @@ bi_String_ReplaceChar (progs_t *pr) RETURN_STRING (pr, dst); } +/* + bi_String_Cut + + Cuts a specified part from a string +*/ +static void +bi_String_Cut (progs_t *pr) +{ + char pos = G_INT (pr, OFS_PARM0); + char len = G_INT (pr, OFS_PARM1); + const char *str = G_STRING (pr, OFS_PARM2); + char *dst = Hunk_TempAlloc ((strlen (str) - len) + 1); + int cnt; + + memset (dst, 0, (strlen (str) - len) + 1); + strncpy(dst, str, pos); + str += pos; + for (cnt = 0; cnt < len; cnt++) + str++; + strcpy(dst, str); + RETURN_STRING (pr, dst); +} + +/* + bi_String_Len + + Gives back the length of the string +*/ +static void +bi_String_Len (progs_t *pr) +{ + const char *str = G_STRING (pr, OFS_PARM0); + + G_INT (pr, OFS_RETURN) = strlen(str); +} + void String_Progs_Init (progs_t *pr) { PR_AddBuiltin (pr, "String_ReplaceChar", bi_String_ReplaceChar, -1); + PR_AddBuiltin (pr, "String_Cut", bi_String_Cut, -1); + PR_AddBuiltin (pr, "String_Len", bi_String_Len, -1); }