return sc;
}
+float fexists(string f)
+{
+ float fh;
+ fh = fopen(f, FILE_READ);
+ if (fh < 0)
+ return FALSE;
+ fclose(fh);
+ return TRUE;
+}
+
// Databases (hash tables)
#define DB_BUCKETS 8192
void db_save(float db, string pFilename)
return -1;
fh = fopen(pFilename, FILE_READ);
if(fh < 0)
- return buf;
+ {
+ buf_del(buf);
+ return -1;
+ }
i = 0;
while((l = fgets(fh)))
{
float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
{
- float ICanHasKallerz;
-
- // detect color codes support in the width function
- ICanHasKallerz = (w("^7", theSize) == 0);
-
// STOP.
// The following function is SLOW.
// For your safety and for the protection of those around you...
}
while(left < right - 1);
- if(ICanHasKallerz)
+ if(w("^7", theSize) == 0) // detect color codes support in the width function
{
// NOTE: when color codes are involved, this binary search is,
// mathematically, BROKEN. However, it is obviously guaranteed to
float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t w)
{
- float ICanHasKallerz;
-
- // detect color codes support in the width function
- ICanHasKallerz = (w("^7") == 0);
-
// STOP.
// The following function is SLOW.
// For your safety and for the protection of those around you...
}
while(left < right - 1);
- if(ICanHasKallerz)
+ if(w("^7") == 0) // detect color codes support in the width function
{
// NOTE: when color codes are involved, this binary search is,
// mathematically, BROKEN. However, it is obviously guaranteed to
return left;
}
+string find_last_color_code(string s)
+{
+ float start, len, i, carets;
+ start = strstrofs(s, "^", 0);
+ if (start == -1) // no caret found
+ return "";
+ len = strlen(s)-1;
+ for(i = len; i >= start; --i)
+ {
+ if(substring(s, i, 1) != "^")
+ continue;
+
+ carets = 1;
+ while (i-carets >= start && substring(s, i-carets, 1) == "^")
+ ++carets;
+
+ // check if carets aren't all escaped
+ if (carets == 1 || mod(carets, 2) == 1) // first check is just an optimization
+ {
+ if(i+1 <= len)
+ if(strstrofs("0123456789", substring(s, i+1, 1), 0) >= 0)
+ return substring(s, i, 2);
+
+ if(i+4 <= len)
+ if(substring(s, i+1, 1) == "x")
+ if(strstrofs("0123456789abcdefABCDEF", substring(s, i+2, 1), 0) >= 0)
+ if(strstrofs("0123456789abcdefABCDEF", substring(s, i+3, 1), 0) >= 0)
+ if(strstrofs("0123456789abcdefABCDEF", substring(s, i+4, 1), 0) >= 0)
+ return substring(s, i, 5);
+ }
+ i -= carets; // this also skips one char before the carets
+ }
+
+ return "";
+}
+
string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
{
float cantake;
getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
if(getWrappedLine_remaining == "")
getWrappedLine_remaining = string_null;
+ else if (tw("^7", theFontSize) == 0)
+ getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
return substring(s, 0, cantake);
}
else
getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
if(getWrappedLine_remaining == "")
getWrappedLine_remaining = string_null;
+ else if (tw("^7", theFontSize) == 0)
+ getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
return substring(s, 0, take);
}
}
getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
if(getWrappedLine_remaining == "")
getWrappedLine_remaining = string_null;
+ else if (tw("^7") == 0)
+ getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
return substring(s, 0, cantake);
}
else
getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
if(getWrappedLine_remaining == "")
getWrappedLine_remaining = string_null;
+ else if (tw("^7") == 0)
+ getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
return substring(s, 0, take);
}
}
return strcat(substring(theText, 0, textLengthUpToLength(theText, maxWidth - tw("..."), tw)), "...");
}
-float isGametypeInFilter(float gt, float tp, string pattern)
+float isGametypeInFilter(float gt, float tp, float ts, string pattern)
{
- string subpattern, subpattern2, subpattern3;
+ string subpattern, subpattern2, subpattern3, subpattern4;
subpattern = strcat(",", GametypeNameFromType(gt), ",");
if(tp)
subpattern2 = ",teams,";
else
subpattern2 = ",noteams,";
+ if(ts)
+ subpattern3 = ",teamspawns,";
+ else
+ subpattern3 = ",noteamspawns,";
if(gt == GAME_RACE || gt == GAME_CTS)
- subpattern3 = ",race,";
+ subpattern4 = ",race,";
else
- subpattern3 = string_null;
+ subpattern4 = string_null;
if(substring(pattern, 0, 1) == "-")
{
return 0;
if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) >= 0)
return 0;
- if(subpattern3 && strstrofs(strcat(",", pattern, ","), subpattern3, 0) >= 0)
+ if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) >= 0)
+ return 0;
+ if(subpattern4 && strstrofs(strcat(",", pattern, ","), subpattern4, 0) >= 0)
return 0;
}
else
pattern = substring(pattern, 1, strlen(pattern) - 1);
if(strstrofs(strcat(",", pattern, ","), subpattern, 0) < 0)
if(strstrofs(strcat(",", pattern, ","), subpattern2, 0) < 0)
- if((!subpattern3) || strstrofs(strcat(",", pattern, ","), subpattern3, 0) < 0)
+ if(strstrofs(strcat(",", pattern, ","), subpattern3, 0) < 0)
+ if((!subpattern4) || strstrofs(strcat(",", pattern, ","), subpattern4, 0) < 0)
return 0;
}
return 1;
return v;
}
-
-float _unacceptable_compiler_bug_1_a(float b, float c) { return b == c; }
-float _unacceptable_compiler_bug_1_b() { return 1; }
-float _unacceptable_compiler_bug_1_c(float d) { return 2 * d; }
-float _unacceptable_compiler_bug_1_d() { return 1; }
-
void check_unacceptable_compiler_bugs()
{
if(cvar("_allow_unacceptable_compiler_bugs"))
fn = get_model_datafilename(m, sk, "txt");
fh = fopen(fn, FILE_READ);
if(fh < 0)
- return 0;
+ {
+ sk = 0;
+ fn = get_model_datafilename(m, sk, "txt");
+ fh = fopen(fn, FILE_READ);
+ if(fh < 0)
+ return 0;
+ }
get_model_parameters_modelname = m;
get_model_parameters_modelskin = sk;
}
return l;
}
+
+// translation helpers
+string language_filename(string s)
+{
+ string fn;
+ float fh;
+ fn = prvm_language;
+ if(fn == "" || fn == "dump")
+ return s;
+ fn = strcat(s, ".", fn);
+ if((fh = fopen(fn, FILE_READ)) >= 0)
+ {
+ fclose(fh);
+ return fn;
+ }
+ return s;
+}
+string CTX(string s)
+{
+ float p = strstrofs(s, "^", 0);
+ if(p < 0)
+ return s;
+ return substring(s, p+1, -1);
+}
+
+// x-encoding (encoding as zero length invisible string)
+const string XENCODE_2 = "xX";
+const string XENCODE_22 = "0123456789abcdefABCDEF";
+string xencode(float f)
+{
+ float a, b, c, d;
+ d = mod(f, 22); f = floor(f / 22);
+ c = mod(f, 22); f = floor(f / 22);
+ b = mod(f, 22); f = floor(f / 22);
+ a = mod(f, 2); // f = floor(f / 2);
+ return strcat(
+ "^",
+ substring(XENCODE_2, a, 1),
+ substring(XENCODE_22, b, 1),
+ substring(XENCODE_22, c, 1),
+ substring(XENCODE_22, d, 1)
+ );
+}
+float xdecode(string s)
+{
+ float a, b, c, d;
+ if(substring(s, 0, 1) != "^")
+ return -1;
+ if(strlen(s) < 5)
+ return -1;
+ a = strstrofs(XENCODE_2, substring(s, 1, 1), 0);
+ b = strstrofs(XENCODE_22, substring(s, 2, 1), 0);
+ c = strstrofs(XENCODE_22, substring(s, 3, 1), 0);
+ d = strstrofs(XENCODE_22, substring(s, 4, 1), 0);
+ if(a < 0 || b < 0 || c < 0 || d < 0)
+ return -1;
+ return ((a * 22 + b) * 22 + c) * 22 + d;
+}