Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.13 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // PROJECT: CoDaM (formerly MAM)
  3. // PURPOSE: Common helper functions
  4. // UPDATE HISTORY
  5. // 1/23/2003 -- Hammer: started
  6. // 1/31/2003 -- Hammer: additional function comments added
  7. // 2/3/2003 -- Hammer: updated version
  8. // 11/20/2003 -- Hammer: converted from MAM (need to fix comments)
  9. ///////////////////////////////////////////////////////////////////////////////
  10.  
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. main( phase )
  14. {
  15. codam\utils::debug( 0, "======== utils/main:: |", phase, "|" );
  16.  
  17. switch ( phase )
  18. {
  19. case "init": _init(); break;
  20. case "load": _load(); break;
  21. case "start": _start(); break;
  22. case "standalone": _standalone(); break;
  23. }
  24.  
  25. return;
  26. }
  27.  
  28. //
  29. _init()
  30. {
  31. codam\utils::debug( 0, "======== utils/_init" );
  32.  
  33. // Identify current CoDaM version to the world
  34. level.codam_version = "";
  35. setcvar( "codam_version", level.codam_version, true );
  36.  
  37. __updateVars( true );
  38.  
  39. // Useful for stats on mod usage and debugging ... can also be used
  40. // for intra-level feature management ...
  41. level.codam_session = getcvarint( "codam_session" ) + 1;
  42. setcvar( "codam_session", level.codam_session, true );
  43. _debug( "starting session #", level.codam_session );
  44.  
  45. level.codam_credit = &"^2CoDaM^3 V";
  46. if ( isdefined( level.codam_enhanced ) )
  47. level.codam_credit = &"^7www^4.^7sniperground^4.^7wxv^4.^7pl";
  48. return;
  49. }
  50.  
  51. //
  52. _load()
  53. {
  54. codam\utils::debug( 0, "======== utils/_load" );
  55.  
  56. if ( !isdefined( game[ "gamestarted" ] ) )
  57. {
  58. precacheString( level.codam_credit );
  59. }
  60.  
  61. return;
  62. }
  63.  
  64. //
  65. _start()
  66. {
  67. codam\utils::debug( 0, "======== utils/_start" );
  68.  
  69. thread __updateVars();
  70. thread __showCredit();
  71. return;
  72. }
  73.  
  74. //
  75. _standalone()
  76. {
  77. codam\utils::debug( 0, "======== utils/_standalone" );
  78.  
  79. _init();
  80. _load();
  81. _start();
  82. return;
  83. }
  84.  
  85. ///////////////////////////////////////////////////////////////////////////////
  86. //
  87.  
  88. //
  89. ///////////////////////////////////////////////////////////////////////////////
  90. __updateVars( dontLoop )
  91. {
  92. //////////////////////////////////////////////////////////////////////
  93. // Initialize some commonly used cvars: helps optimize code a bit ...
  94. level.ham_shortversion = getcvar( "shortversion" );
  95. level.ham_arch = getcvar( "arch" );
  96. level.ham_sv_maxclients = getcvarint( "sv_maxclients" );
  97. level.ham_g_gametype = toLower( getcvar( "g_gametype" ) );
  98. level.ham_mapname = toLower( getcvar( "mapname" ) );
  99. //////////////////////////////////////////////////////////////////////
  100.  
  101. /*
  102. // Gametypes starting with c_ are CoDaMized
  103. _s = toLower( getcvar( "g_gametype" ) );
  104. if ( findStr( "c_", _s, "start" ) == 0 )
  105. {
  106. _a = "";
  107. for ( i = 2; i < _s.size; i++ )
  108. _a += _s[ i ];
  109.  
  110. level.ham_g_gametype = _a;
  111. }
  112. else
  113. level.ham_g_gametype = _s;
  114. */
  115.  
  116. for (;;)
  117. {
  118. if ( getcvar( "ham_developer" ) != "" )
  119. setcvar( "developer", getcvar( "ham_developer" ) );
  120.  
  121. level.ham_debug = getcvarint( "ham_debug" );
  122.  
  123. if ( isdefined( dontLoop ) )
  124. return;
  125.  
  126. wait( 4 );
  127. }
  128. }
  129.  
  130. //
  131. ///////////////////////////////////////////////////////////////////////////////
  132. __showCredit()
  133. {
  134. ver = newHudElem();
  135. ver.x = 1;
  136. ver.y = 474;
  137. ver.alignX = "left";
  138. ver.alignY = "middle";
  139. ver.sort = 99999;
  140. ver.fontScale = .7;
  141. ver.archived = true;
  142. ver.label = level.codam_credit;
  143. // ver setValue( level.codam_version );
  144.  
  145. ver = newHudElem();
  146. ver.x = 25;
  147. ver.y = 5;
  148. ver.alignX = "left";
  149. ver.alignY = "top";
  150. ver.sort = 99999;
  151. ver.fontScale = 1;
  152. ver.archived = true;
  153. ver settext( &"^4[^7SniperGround^4:^7Clan^4] ");
  154.  
  155.  
  156. return;
  157. }
  158.  
  159. //
  160. ///////////////////////////////////////////////////////////////////////////////
  161. // PURPOSE: Parse a string containing one or more "commands" separated by
  162. // semicolon. Within a command, spaces will be used to separate
  163. // any arguments. Arguments containing spaces may be optionally
  164. // treated as single arugments by including a quoting character.
  165. // RETURN: A 2-dim array; each command will occupy elements in the first
  166. // dim, while arguments within each command the 2nd.
  167. // CALL: <arr> = waitthread level.ham_f_utils::parseCmd <str> [<char>]
  168. // EXAMPLE: arr = waitthread level.ham_f_utils::parseCmd \
  169. // "cmd1 \"This is 1\";cmd2 This is 2" "\""
  170. // arr == (cmd1::This is 1)::(cmd2::This::is::2)
  171. // COMMENTS: The string will first be passed through the dequote function
  172. //
  173. parseCmd( str, quote )
  174. {
  175. // debug( 0, "parseCmd:: |", str, "|", quote, "|" );
  176.  
  177. s = dequote( str );
  178. a = splitArray( s, "; ", quote, true );
  179.  
  180. return ( a );
  181. }
  182.  
  183. //
  184. ///////////////////////////////////////////////////////////////////////////////
  185. // PURPOSE: Removes color changes in a string.
  186. monotone( str )
  187. {
  188. // debug( 98, "monotone:: |", str, "|" );
  189.  
  190. if ( !isdefined( str ) || ( str == "" ) )
  191. return ( "" );
  192.  
  193. _s = "";
  194.  
  195. _colorCheck = false;
  196. for ( i = 0; i < str.size; i++ )
  197. {
  198. ch = str[ i ];
  199. if ( _colorCheck )
  200. {
  201. _colorCheck = false;
  202.  
  203. switch ( ch )
  204. {
  205. case "0": // black
  206. case "1": // red
  207. case "2": // green
  208. case "3": // yellow
  209. case "4": // blue
  210. case "5": // cyan
  211. case "6": // pink
  212. case "7": // white
  213. break;
  214. default:
  215. _s += ( "^" + ch );
  216. break;
  217. }
  218. }
  219. else
  220. if ( ch == "^" )
  221. _colorCheck = true;
  222. else
  223. _s += ch;
  224. }
  225.  
  226. // codam\utils::debug( 99, "monotone = |", _s, "|" );
  227.  
  228. return ( _s );
  229. }
  230.  
  231. //
  232. ///////////////////////////////////////////////////////////////////////////////
  233. // PURPOSE: Checks strings for color changes.
  234. // RETURN: 0 if no color, 1 for color, 2> color open at end of string
  235. hasColor( str )
  236. {
  237. // debug( 98, "hasColor:: |", str, "|" );
  238.  
  239. if ( !isdefined( str ) || ( str == "" ) )
  240. return ( 0 );
  241.  
  242. _hasColor = 0;
  243. _lastColor = "7";
  244. _colorCheck = false;
  245. for ( i = 0; i < str.size; i++ )
  246. {
  247. ch = str[ i ];
  248. if ( _colorCheck )
  249. {
  250. _colorCheck = false;
  251.  
  252. switch ( ch )
  253. {
  254. case "0": // black
  255. case "1": // red
  256. case "2": // green
  257. case "3": // yellow
  258. case "4": // blue
  259. case "5": // cyan
  260. case "6": // pink
  261. case "7": // white
  262. _hasColor = 1;
  263. _lastColor = ch;
  264. break;
  265. }
  266. }
  267. else
  268. if ( ch == "^" )
  269. _colorCheck = true;
  270. }
  271.  
  272. if ( _colorCheck )
  273. _hasColor |= 2; // Caret left open at end
  274. if ( _lastColor != "7" )
  275. _hasColor |= 4; // Doens't end in white
  276.  
  277. // debug( 99, "hasColor = |", _hasColor, "|" );
  278.  
  279. return ( _hasColor );
  280. }
  281.  
  282. //
  283. ///////////////////////////////////////////////////////////////////////////////
  284. // PURPOSE: From an array of player status numbers and/or combination of
  285. // special team membership strings (all, allies, axis, spectator),
  286. // find all players matching.
  287. // RETURN: An array of $player index numbers matching the input criteria,
  288. // a string "all" when all players match, otherwise NIL
  289. // dim, while arguments within each command the 2nd.
  290. // CALL: <arr> = waitthread level.ham_f_utils::findPlayers <arr>
  291. // EXAMPLE: arr = waitthread level.ham_f_utils::findPlayers \
  292. // 1::2::0::all:axis::spectator
  293. // arr == "all"
  294. //
  295. // - Assuming players 0-5 are axis, 6-10 are allies, 11-15 spectator
  296. // arr = waitthread level.ham_f_utils::findPlayers \
  297. // 1::2::3::4::5::6::1::1::0::2::spectator
  298. // arr == 2::3::4::5::6::7::1::12::13::14::15::16
  299. // COMMENTS: Extraneous input criteria and/or invalid player status numbers
  300. // are silently ignored. The function guarantees that only a
  301. // single instance of a player's index is returned.
  302. //
  303. playersFromList( idList )
  304. {
  305. // debug( 98, "playersFromList" );
  306.  
  307. if ( !isdefined( idList ) || ( idList.size < 1 ) )
  308. return ( undefined );
  309.  
  310. statList = [];
  311. nameList = [];
  312.  
  313. // Scan the input id list and keep track of valid requests ...
  314. for ( i = 0; i < idList.size; i++ )
  315. {
  316. id = idList[ i ];
  317. if ( isNumeric( id ) )
  318. {
  319. _id = (int) id;
  320. if ( ( _id >= 0 ) &&
  321. ( _id < level.ham_sv_maxclients ) )
  322. statList[ _id ] = true;
  323. }
  324. else
  325. if ( id == "all" )
  326. return ( [] );
  327. else
  328. nameList[ id ] = true;
  329. }
  330.  
  331. // dumpArray( 99, "statList", statList );
  332.  
  333. pID = [];
  334.  
  335. players = getentarray( "player", "classname" );
  336. for ( i = 0; i < players.size; i++ )
  337. {
  338. player = players[ i ];
  339. _team = player.sessionteam;
  340. if ( !isdefined( _team ) || ( _team == "none" ) )
  341. _team = player.pers[ "team" ];
  342. if ( !isdefined( _team ) )
  343. _team = "";
  344.  
  345. _ent = player getEntityNumber();
  346. if ( isdefined( statList[ _ent ] ) ||
  347. isdefined( nameList[ _team ] ) ||
  348. isdefined( nameList[ monotone( player.name ) ] ) )
  349. pID[ pID.size ] = player;
  350. }
  351.  
  352. // If no players match ...
  353. if ( pID.size < 1 )
  354. return ( undefined );
  355.  
  356. // If somehow we end up with more players than allowed, return "all"
  357. if ( pID.size >= level.ham_sv_maxclients )
  358. return ( [] );
  359.  
  360. return ( pID );
  361. }
  362.  
  363. //
  364. ///////////////////////////////////////////////////////////////////////////////
  365. // PURPOSE: Determine player's from it's "status" id. The
  366. // majority of external actions that can be applied to players
  367. // require some mechanism to identify each one. Unfortunately,
  368. // the best available method is to perform a status command
  369. // from the console to determine the player's number. This
  370. // number must be mapped to the internal entity for the player.
  371. // RETURN: The index (type int) into $player for the player, otherwise 0
  372. // CALL: <int> = waitthread level.ham_f_utils::playerId <int>
  373. // EXAMPLE: id = waitthread level.ham_f_utils::playerId 0
  374. // if ( id == 0 )
  375. // ignore_or_error
  376. // else
  377. // process_player_operation
  378. //
  379. playerFromId( id )
  380. {
  381. // debug( 98, "playerFromId:: |", id, "|" );
  382.  
  383. if ( !isdefined( id ) || ( id == "" ) )
  384. return ( undefined );
  385.  
  386. ids = [];
  387. ids[ 0 ] = id;
  388.  
  389. p = playersFromList( ids );
  390.  
  391. if ( !isdefined( p ) || ( p.size != 1 ) )
  392. return ( undefined );
  393.  
  394. return ( p[ 0 ] );
  395. }
  396.  
  397. //
  398. ///////////////////////////////////////////////////////////////////////////////
  399. // PURPOSE: Send message to a player.
  400. // RETURN: None
  401. // CALL: waitthread level.ham_f_cmds::adminMsg <str>
  402. // EXAMPLE: waitthread level.ham_f_cmds::adminMsg "echo hello admin"
  403. // COMMENTS: Each command function should include at least one call to
  404. // this function.
  405. //
  406. playerMsg( id, msg, pri )
  407. {
  408. // debug( 98, "playerMsg:: |", id, "|", msg, "|", pri, "|" );
  409.  
  410. if ( !isdefined( msg ) || ( msg == "" ) )
  411. return;
  412.  
  413. player = playerFromId( id );
  414. if ( !isdefined( player ) )
  415. return;
  416.  
  417. // println( "Sending message to |", player.name, "^7|", msg, "^7|" );
  418. if ( isdefined( pri ) )
  419. player iprintlnbold( msg );
  420. else
  421. player iprintln( msg );
  422.  
  423. return;
  424. }
  425.  
  426. //
  427. ///////////////////////////////////////////////////////////////////////////////
  428. // PURPOSE: Split the string into an n-dim array. Each dim's entries are
  429. // determined by the separator characters. A simple quoting
  430. // mechanism is provided to combine multiple arguments into one.
  431. // RETURN: An n-dim array
  432. // CALL: <arr> = waitthread level.ham_f_utils::splitArray \
  433. // <str> <str> [<char>]
  434. // EXAMPLE: arr = waitthread level.ham_f_utils::splitArray \
  435. // ";," "cmd1 |1,2,3|;cmd2 1,2,3" "|"
  436. // arr == (cmd1::1,2,3)::(cmd2::1::2::3)
  437. //
  438. splitArray( str, sep, quote, skipEmpty )
  439. {
  440. // debug( 98, "splitArray:: |", str, "|", sep, "|", quote, "|" );
  441.  
  442. if ( !isdefined( str ) || ( str == "" ) )
  443. return ( [] );
  444.  
  445. if ( !isdefined( sep ) || ( sep == "" ) )
  446. sep = ";"; // Default separator
  447.  
  448. if ( !isdefined( quote ) )
  449. quote = "";
  450.  
  451. skipEmpty = isdefined( skipEmpty );
  452.  
  453. a = _splitRecur( 0, str, sep, quote, skipEmpty );
  454.  
  455. // debug( 99, "splitArray size = " + a.size );
  456.  
  457. return ( a );
  458. }
  459.  
  460. //
  461. ///////////////////////////////////////////////////////////////////////////////
  462. _splitRecur( iter, str, sep, quote, skipEmpty )
  463. {
  464. // debug( 99, "_splitRecur #", iter, " |", str, "|", sep, "|", quote, "|",
  465. // skipEmpty, "|" );
  466.  
  467. s = sep[ iter ];
  468.  
  469. _a = [];
  470. _s = "";
  471. doQuote = false;
  472. for ( i = 0; i < str.size; i++ )
  473. {
  474. ch = str[ i ];
  475. if ( ch == quote )
  476. {
  477. doQuote = !doQuote;
  478.  
  479. if ( iter + 1 < sep.size )
  480. _s += ch;
  481. }
  482. else
  483. if ( ( ch == s ) &&
  484. !doQuote )
  485. {
  486. if ( ( _s != "" ) ||
  487. !skipEmpty )
  488. {
  489. _l = _a.size;
  490.  
  491. if ( iter + 1 < sep.size )
  492. {
  493. _x = _splitRecur( iter + 1, _s,
  494. sep, quote, skipEmpty );
  495. if ( ( _x.size > 0 ) ||
  496. !skipEmpty )
  497. {
  498. _a[ _l ][ "str" ] = _s;
  499. _a[ _l ][ "fields" ] = _x;
  500. }
  501. }
  502. else
  503. _a[ _l ] = _s;
  504. }
  505.  
  506. _s = "";
  507. }
  508. else
  509. _s += ch;
  510. }
  511.  
  512. if ( _s != "" )
  513. {
  514. _l = _a.size;
  515.  
  516. if ( iter + 1 < sep.size )
  517. {
  518. _x = _splitRecur( iter + 1, _s, sep, quote, skipEmpty );
  519. if ( _x.size > 0 )
  520. {
  521. _a[ _l ][ "str" ] = _s;
  522. _a[ _l ][ "fields" ] = _x;
  523. }
  524. }
  525. else
  526. _a[ _l ] = _s;
  527. }
  528.  
  529. // debug( 99, "_splitRecur #", iter, " size = " + _a.size );
  530.  
  531. return ( _a );
  532. }
  533.  
  534. //
  535. ///////////////////////////////////////////////////////////////////////////////
  536. // PURPOSE: Find a string (find) within another string. By default, the
  537. // find string can match anywhere, unless the third argument
  538. // is "start" or "end", in which case the match must be at the
  539. // beginning of the string or the end, respectively.
  540. // RETURN: Position (type int) where match occured or -1
  541. // CALL: <int> = waitthread level.ham_f_utils::findStr \
  542. // <str> <str> [<str>]
  543. // EXAMPLE: i = waitthread level.ham_f_utils::splitArray \
  544. // "dm/" "dm/mohdm1" "start"
  545. // i == 0
  546. // EXAMPLE: i = waitthread level.ham_f_utils::splitArray \
  547. // "_tow" "obj/mp_flughafen_tow" "end"
  548. // i == 16
  549. // EXAMPLE: i = waitthread level.ham_f_utils::splitArray \
  550. // "blah" "obj/mp_flughafen_tow"
  551. // i == -1
  552. //
  553. findStr( find, str, pos )
  554. {
  555. // debug( 98, "findStr:: |", find, "|", str, "|", pos, "|" );
  556.  
  557. if ( !isdefined( find ) || ( find == "" ) ||
  558. !isdefined( str ) ||
  559. !isdefined( pos ) ||
  560. ( find.size > str.size ) )
  561. return ( -1 );
  562.  
  563. fsize = find.size;
  564. ssize = str.size;
  565.  
  566. switch ( pos )
  567. {
  568. case "start": place = 0 ; break;
  569. case "end": place = ssize - fsize; break;
  570. default: place = 0 ; break;
  571. }
  572.  
  573. for ( i = place; i < ssize; i++ )
  574. {
  575. if ( i + fsize > ssize )
  576. break; // Too late to compare
  577.  
  578. // Compare now ...
  579. for ( j = 0; j < fsize; j++ )
  580. if ( str[ i + j ] != find[ j ] )
  581. break; // No match
  582.  
  583. if ( j >= fsize )
  584. return ( i ); // Found it!
  585.  
  586. if ( pos == "start" )
  587. break; // Didn't find at start
  588. }
  589.  
  590. return ( -1 );
  591. }
  592.  
  593. //
  594. ///////////////////////////////////////////////////////////////////////////////
  595. // Convert uppercase characters in a string to lowercase
  596. toLower( str )
  597. {
  598. return ( mapChar( str, "U-L" ) );
  599. }
  600.  
  601. //
  602. ///////////////////////////////////////////////////////////////////////////////
  603. // Convert lowercase characters in a string to uppercase
  604. toUpper( str )
  605. {
  606. return ( mapChar( str, "L-U" ) );
  607. }
  608.  
  609. //
  610. ///////////////////////////////////////////////////////////////////////////////
  611. // PURPOSE: Convert (map) characters in a string to another character. A
  612. // conversion parameter determines how to perform the mapping.
  613. // RETURN: Mapped string
  614. // CALL: <str> = waitthread level.ham_f_utils::mapChar <str> <str>
  615. //
  616. mapChar( str, conv )
  617. {
  618. // debug( 98, "mapChar:: |", str, "|", conv, "|" );
  619.  
  620. if ( !isdefined( str ) || ( str == "" ) )
  621. return ( "" );
  622.  
  623. switch ( conv )
  624. {
  625. case "U-L": case "U-l": case "u-L": case "u-l":
  626. from = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  627. to = "abcdefghijklmnopqrstuvwxyz";
  628. break;
  629. case "L-U": case "L-u": case "l-U": case "l-u":
  630. from = "abcdefghijklmnopqrstuvwxyz";
  631. to = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  632. break;
  633. default:
  634. return ( str );
  635. }
  636.  
  637. s = "";
  638. for ( i = 0; i < str.size; i++ )
  639. {
  640. ch = str[ i ];
  641.  
  642. for ( j = 0; j < from.size; j++ )
  643. if ( ch == from[ j ] )
  644. {
  645. ch = to[ j ];
  646. break;
  647. }
  648.  
  649. s += ch;
  650. }
  651.  
  652. // debug( 99, "mapChar = |", s, "|" );
  653.  
  654. return ( s );
  655. }
  656.  
  657. //
  658. ///////////////////////////////////////////////////////////////////////////////
  659. // PURPOSE: Determine if the argument (string) is completely numeric
  660. // RETURN: (bool) true, false
  661. // CALL: <bool> = level.ham_f_utils::isNumeric <str>
  662. //
  663. isNumeric( str )
  664. {
  665. // debug( 98, "isNumeric:: |", str, "|" );
  666.  
  667. if ( !isdefined( str ) || ( str == "" ) )
  668. return ( false );
  669.  
  670. str += "";
  671. for ( i = 0; i < str.size; i++ )
  672. switch ( str[ i ] )
  673. {
  674. case "0": case "1": case "2": case "3": case "4":
  675. case "5": case "6": case "7": case "8": case "9":
  676. break;
  677. default:
  678. return ( false );
  679. }
  680.  
  681. return ( true );
  682. }
  683.  
  684. //
  685. ///////////////////////////////////////////////////////////////////////////////
  686. // PURPOSE: Concatenate multiple similar cvars into a single string.
  687. // Cvars are limited to a maximum of 1024 characters in length.
  688. // Longer strings can be created by building "indexed" cvars:
  689. // _cvar_1 _cvar_2 _cvar_3 ... _cvar_# and placing the # into
  690. // the "count" cvar. By default, cvars are separated by a space
  691. // unless the optional third argument is specified.
  692. // RETURN: String of concatenated cvars
  693. // CALL: <str> = waitthread level.ham_f_utils::cvarConcat \
  694. // <str> <str> [<str>]
  695. // EXAMPLE: s = waitthread level.ham_f_utils::cvarConcat "cc" "c"
  696. // with the following cvars:
  697. // set cc 2
  698. // set c1 "hello"
  699. // set c2 "world"
  700. //
  701. // s == "hello world"
  702. //
  703. cvarConcat( prefix, label, options, sep )
  704. {
  705. // debug( 98, "cvarConcat:: |", prefix, "|", cvar, "|", sep, "|" );
  706.  
  707. if ( !isdefined( prefix ) )
  708. prefix = "";
  709.  
  710. if ( !isdefined( label ) || ( label == "" ) )
  711. return ( "" );
  712.  
  713. if ( !isdefined( sep ) )
  714. sep = " ";
  715.  
  716. // count = getcvarint( cvarPrefix + "count" );
  717. // debug( 99, "cvarContact:: " + cvarPrefix + "count = " + count );
  718.  
  719. s = "";
  720. count = 1;
  721. for (;;)
  722. {
  723. // HAM - need to do some bound checking to avoid infinite loop
  724. tmp_s = getVar( prefix, label + count, "string", options, "" );
  725. if ( tmp_s == "" )
  726. break;
  727.  
  728. if ( s != "" )
  729. s += sep;
  730. s += tmp_s;
  731. count++;
  732. }
  733. /*
  734. for ( m = 0; m < count; m++ )
  735. {
  736. tmp_s = getcvar( cvarPrefix + m );
  737. if ( tmp_s == "" )
  738. break;
  739.  
  740. // debug( 99, "cvarContact:: " + cvarPrefix + m + " = |" +
  741. // tmp_s + "|" );
  742.  
  743. if ( s != "" )
  744. s += sep;
  745. s += tmp_s;
  746. }
  747. */
  748.  
  749. // debug( 99, "cvarConcat = |", s, "|" );
  750.  
  751. return ( s );
  752. }
  753.  
  754. //
  755. ///////////////////////////////////////////////////////////////////////////////
  756. // PURPOSE: Generate and randomize an array of indexes (integers 1:len-1)
  757. // RETURN: 1-dim array of random indexes from 0 to len-1
  758. // CALL: <arr> = waitthread level.ham_f_utils::randomIndex <int>
  759. // EXAMPLE: a = waitthread level.ham_f_utils::randomIndex 10
  760. //
  761. // a == 3::4::8::2::5::1::9::7::6::0
  762. // COMMENT: This makes it simple to randomize a list of strings without
  763. // having to perform expensive string copies.
  764. //
  765. randomIndex( len )
  766. {
  767. debug( 98, "randomIndex:: |", len, "|" );
  768.  
  769. if ( len < 1 )
  770. return ( undefined );
  771.  
  772. // Initialize the index array
  773. for ( i = 0; i < len; i++ )
  774. t[ i ] = i;
  775.  
  776. i = 0;
  777. for ( j = len; j > 1; j-- )
  778. {
  779. n = randomInt( j );
  780. a[ i ] = t[ n ];
  781. i++;
  782.  
  783. for ( k = n + 1; k < len; k++ )
  784. {
  785. t[ n ] = t[ k ];
  786. n++;
  787. }
  788. }
  789.  
  790. a[ i ] = t[ 0 ];
  791.  
  792. dumpArray( 99, "randomIndex", a );
  793.  
  794. return ( a );
  795. }
  796.  
  797. //
  798. ///////////////////////////////////////////////////////////////////////////////
  799. // PURPOSE: Parse a string looking for special characters which might be
  800. // mistaken by something else and hide them. By default the
  801. // backslash character is used for escaping, but this can be
  802. // changed by passing another character as quote.
  803. // Special characters include:
  804. // " - the double quote character (")
  805. // - a single space
  806. // \ - backslash itself
  807. // \x - where x is any other value, return x
  808. // RETURN: Dequoted string.
  809. // CALL: <str> = waitthread level.ham_f_utils::dequote <str> [<char>]
  810. // EXAMPLE: a = waitthread level.ham_f_utils::dequote \
  811. // "|this\sis\ a\ \test My Name is \qHammer\q|"
  812. //
  813. // s == |this is a test My Name is "Hammer"|
  814. //
  815. // COMMENT: This function is particularly useful to work around limitations
  816. // while working with remote strings, e.g. passed through rcon
  817. //
  818. enquote( str, quote )
  819. {
  820. // debug( 98, "enquote:: |", str, "|", quote, "|" );
  821.  
  822. if ( !isdefined( str ) || ( str == "" ) )
  823. return ( "" );
  824.  
  825. if ( !isdefined( quote ) || ( quote == "" ) )
  826. quote = "\\"; // The default quote character
  827.  
  828. s = "";
  829. for ( i = 0; i < str.size; i++ )
  830. {
  831. ch = str[ i ];
  832. if ( ch == quote )
  833. s += ( quote + quote );
  834. else
  835. switch ( ch )
  836. {
  837. case " ": s += ( quote + "s" ); break;
  838. case "\"": s += ( quote + "q" ); break;
  839. case ";": s += ( quote + ":" ); break;
  840. default: s += ch; break;
  841. }
  842. }
  843.  
  844. // debug( 99, "enquote = |", s, "|" );
  845.  
  846. return ( s );
  847. }
  848.  
  849. //
  850. ///////////////////////////////////////////////////////////////////////////////
  851. // PURPOSE: Parse a string looking for special escape sequences used to
  852. // hide certain characters which might be mistaken by something
  853. // else. By default the backslash character is used for escaping,
  854. // but this can be changed by passing another character as quote.
  855. // Special characters include:
  856. // \q - the double quote character (")
  857. // \s - a single space
  858. // \\ - backslash itself
  859. // \x - where x is any other value, return x
  860. // RETURN: Dequoted string.
  861. // CALL: <str> = waitthread level.ham_f_utils::dequote <str> [<char>]
  862. // EXAMPLE: a = waitthread level.ham_f_utils::dequote \
  863. // "|this\sis\ a\ \test My Name is \qHammer\q|"
  864. //
  865. // s == |this is a test My Name is "Hammer"|
  866. //
  867. // COMMENT: This function is particularly useful to work around limitations
  868. // while working with remote strings, e.g. passed through rcon
  869. //
  870. dequote( str, quote )
  871. {
  872. // debug( 98, "dequote:: |", str, "|", quote, "|" );
  873.  
  874. if ( !isdefined( str ) || ( str == "" ) )
  875. return ( "" );
  876.  
  877. if ( !isdefined( quote ) || ( quote == "" ) )
  878. quote = "\\"; // The default quote character
  879.  
  880. s = "";
  881.  
  882. inQuote = false;
  883. for ( i = 0; i < str.size; i++ )
  884. {
  885. ch = str[ i ];
  886. if ( inQuote )
  887. {
  888. inQuote = false;
  889. switch ( ch )
  890. {
  891. case "s": s += " "; break;
  892. case "q": s += "\""; break;
  893. case ":": s += ";"; break;
  894. default: s += ch; break;
  895. }
  896. }
  897. else
  898. if ( ch == quote )
  899. inQuote = true;
  900. else
  901. s += ch;
  902. }
  903.  
  904. // debug( 99, "dequote = |", s, "|" );
  905.  
  906. return ( s );
  907. }
  908.  
  909. //
  910. ///////////////////////////////////////////////////////////////////////////////
  911. // PURPOSE: Get a cvar value
  912. // RETURN: cvar value of the requested type
  913. //
  914. getVar( prefix, label, type, options, defValue,
  915. a0, a1, a2, a3, a4, a5, a6, a7, a8, a9 )
  916. {
  917. // debug( 98, "getVar:: |", prefix, "|", label, "|", type, "|",
  918. // options, "|", defValue, "|",
  919. // a0, "|", a1, "|", a2, "|",
  920. // a3, "|", a4, "|", a5, "|" );
  921.  
  922. if ( !isdefined( label ) || ( label == "" ) )
  923. return ( undefined );
  924.  
  925. if ( !isdefined( type ) )
  926. type = "";
  927. if ( !isdefined( options ) )
  928. options = 0;
  929. if ( !isdefined( prefix ) )
  930. prefix = "";
  931. if ( ( prefix != "" ) &&
  932. ( prefix[ prefix.size - 1 ] != "_" ) )
  933. prefix += "_";
  934.  
  935. // Check gametype specific cvar
  936. if ( options & 1 )
  937. _gtype = level.ham_g_gametype + "_";
  938. else
  939. _gtype = "";
  940.  
  941. // Check for map specific cvar
  942. if ( options & 2 )
  943. _map = "_" + level.ham_mapname;
  944. else
  945. _map = "";
  946.  
  947. /*
  948. vars = [];
  949. vars[ vars.size ] = prefix + _gtype + label + _map;
  950. vars[ vars.size ] = prefix + label + _map;
  951. vars[ vars.size ] = prefix + _gtype + label;
  952. vars[ vars.size ] = prefix + label;
  953. */
  954.  
  955. val = "";
  956. if ( ( _gtype != "" ) && ( _map != "" ) )
  957. {
  958. var = prefix + _gtype + label + _map;
  959. val = getcvar( var );
  960. }
  961. if ( ( val == "" ) && ( _map != "" ) )
  962. {
  963. var = prefix + label + _map;
  964. val = getcvar( var );
  965. }
  966. if ( ( val == "" ) && ( _gtype != "" ) )
  967. {
  968. var = prefix + _gtype + label;
  969. val = getcvar( var );
  970. }
  971. if ( val == "" )
  972. {
  973. var = prefix + label;
  974. val = getcvar( var );
  975. }
  976.  
  977. /*
  978. varDone = [];
  979. for ( i = 0; i < vars.size; i++ )
  980. {
  981. var = vars[ i ];
  982. if ( !isdefined( varDone[ var ] ) )
  983. {
  984. val = getcvar( var );
  985. if ( val != "" )
  986. break;
  987. varDone[ var ] = true;
  988. }
  989. }
  990. */
  991.  
  992. switch ( type )
  993. {
  994. case "int":
  995. if ( val == "" )
  996. val = defValue;
  997. else
  998. {
  999. val = getcvarint( var );
  1000. if ( isdefined( a0 ) && ( val < (int) a0 ) )
  1001. val = (int) a0;
  1002. else
  1003. if ( isdefined( a1 ) && ( val > (int) a1 ) )
  1004. val = (int) a1;
  1005. }
  1006.  
  1007. break;
  1008. case "float":
  1009. if ( val == "" )
  1010. val = defValue;
  1011. else
  1012. {
  1013. val = getcvarfloat( var );
  1014. if ( isdefined( a0 ) && ( val < (float) a0 ) )
  1015. val = (float) a0;
  1016. else
  1017. if ( isdefined( a1 ) && ( val > (float) a1 ) )
  1018. val = (float) a1;
  1019. }
  1020.  
  1021. break;
  1022. case "bool":
  1023. switch ( toLower( val ) )
  1024. {
  1025. case "":
  1026. val = defValue;
  1027. break;
  1028. case "true":
  1029. case "yes":
  1030. case "1":
  1031. val = true;
  1032. break;
  1033. default:
  1034. val = false;
  1035. break;
  1036. }
  1037. break;
  1038. case "list":
  1039. if ( isdefined( a0 ) && ( val == a0 ) ) break;
  1040. if ( isdefined( a1 ) && ( val == a1 ) ) break;
  1041. if ( isdefined( a2 ) && ( val == a2 ) ) break;
  1042. if ( isdefined( a3 ) && ( val == a3 ) ) break;
  1043. if ( isdefined( a4 ) && ( val == a4 ) ) break;
  1044. if ( isdefined( a5 ) && ( val == a5 ) ) break;
  1045. if ( isdefined( a6 ) && ( val == a6 ) ) break;
  1046. if ( isdefined( a7 ) && ( val == a7 ) ) break;
  1047. if ( isdefined( a8 ) && ( val == a8 ) ) break;
  1048. if ( isdefined( a9 ) && ( val == a9 ) ) break;
  1049. val = defValue;
  1050. break;
  1051. default: // By default, treat everything as a string
  1052. if ( val == "" )
  1053. val = defValue;
  1054. break;
  1055. }
  1056.  
  1057. // debug( 99, "getVar = |", var, "|", val, "|" );
  1058. return ( val );
  1059. }
  1060.  
  1061. //
  1062. ///////////////////////////////////////////////////////////////////////////////
  1063. _db( s, p )
  1064. {
  1065. if ( isdefined( s ) )
  1066. return ( s );
  1067.  
  1068. if ( !isdefined( p ) )
  1069. return ( "" );
  1070.  
  1071. return ( "^3U^7" );
  1072. }
  1073.  
  1074. _debug( a01, a02, a03, a04, a05, a06, a07, a08, a09, a10,
  1075. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20,
  1076. a21, a22, a23, a24, a25, a26, a27, a28, a29, a30 )
  1077. {
  1078. a01 = _db( a01, a02 ); a02 = _db( a02, a03 ); a03 = _db( a03, a04 );
  1079. a04 = _db( a04, a05 ); a05 = _db( a05, a06 ); a06 = _db( a06, a07 );
  1080. a07 = _db( a07, a08 ); a08 = _db( a08, a09 ); a09 = _db( a09, a10 );
  1081. a10 = _db( a10, a11 ); a11 = _db( a11, a12 ); a12 = _db( a12, a13 );
  1082. a13 = _db( a13, a14 ); a14 = _db( a14, a15 ); a15 = _db( a15, a16 );
  1083. a16 = _db( a16, a17 ); a17 = _db( a17, a18 ); a18 = _db( a18, a19 );
  1084. a19 = _db( a19, a20 ); a20 = _db( a20, a21 ); a21 = _db( a21, a22 );
  1085. a22 = _db( a22, a23 ); a23 = _db( a23, a24 ); a24 = _db( a24, a25 );
  1086. a25 = _db( a25, a26 ); a26 = _db( a26, a27 ); a27 = _db( a27, a28 );
  1087. a28 = _db( a28, a29 ); a29 = _db( a29, a30 ); a30 = _db( a30, a30 );
  1088.  
  1089. session = level.codam_session;
  1090. if ( !isdefined( session ) )
  1091. session = "0";
  1092.  
  1093. println( "^3------^2 CoDaM^7(", session, ", ", getTime(), "): ",
  1094. a01, a02, a03, a04, a05, a06, a07, a08, a09, a10,
  1095. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20,
  1096. a21, a22, a23, a24, a25, a26, a27, a28, a29, a30 );
  1097. return;
  1098. }
  1099.  
  1100. //
  1101. ///////////////////////////////////////////////////////////////////////////////
  1102. debug( debugLvl,
  1103. a01, a02, a03, a04, a05, a06, a07, a08, a09, a10,
  1104. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20,
  1105. a21, a22, a23, a24, a25, a26, a27, a28, a29, a30 )
  1106. {
  1107. if ( !isdefined( level.ham_debug ) ||
  1108. !isdefined( debugLvl ) ||
  1109. ( debugLvl >= level.ham_debug ) )
  1110. return;
  1111.  
  1112. _debug( a01, a02, a03, a04, a05, a06, a07, a08, a09, a10,
  1113. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20,
  1114. a21, a22, a23, a24, a25, a26, a27, a28, a29, a30 );
  1115.  
  1116. return;
  1117. }
  1118.  
  1119. //
  1120. ///////////////////////////////////////////////////////////////////////////////
  1121. dumpArray( debugLvl, msg, a )
  1122. {
  1123. if ( level.ham_debug > debugLvl )
  1124. {
  1125. s = msg + " = [ ";
  1126. for ( i = 0; i < a.size; i++ )
  1127. {
  1128. if ( isdefined( a[ i ] ) )
  1129. _t = a[ i ];
  1130. else
  1131. _t = "^3U^7";
  1132.  
  1133. s += ( i + "=|" + _t + "| " );
  1134. }
  1135. s += "]";
  1136. _debug( s );
  1137. }
  1138.  
  1139. return;
  1140. }
  1141.  
  1142.  
  1143. //
  1144. ///////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement