t4 refactor

This commit is contained in:
ineed bots
2025-05-21 12:41:21 -06:00
parent 3de51aa71b
commit 3e08ac900d
4 changed files with 128 additions and 121 deletions

View File

@@ -2722,11 +2722,11 @@ fire( what )
if ( what )
{
self BotBuiltinBotAction( "+fire" );
self BotBuiltinBotAction( "+attack" );
}
else
{
self BotBuiltinBotAction( "-fire" );
self BotBuiltinBotAction( "-attack" );
}
}
@@ -2745,14 +2745,14 @@ pressFire( time )
time = 0.05;
}
self BotBuiltinBotAction( "+fire" );
self BotBuiltinBotAction( "+attack" );
if ( time )
{
wait time;
}
self BotBuiltinBotAction( "-fire" );
self BotBuiltinBotAction( "-attack" );
}
/*
@@ -2764,11 +2764,11 @@ ads( what )
if ( what )
{
self BotBuiltinBotAction( "+ads" );
self BotBuiltinBotAction( "+speed_throw" );
}
else
{
self BotBuiltinBotAction( "-ads" );
self BotBuiltinBotAction( "-speed_throw" );
}
}
@@ -2787,14 +2787,14 @@ pressADS( time )
time = 0.05;
}
self BotBuiltinBotAction( "+ads" );
self BotBuiltinBotAction( "+speed_throw" );
if ( time )
{
wait time;
}
self BotBuiltinBotAction( "-ads" );
self BotBuiltinBotAction( "-speed_throw" );
}
/*
@@ -2848,8 +2848,8 @@ jump()
*/
stand()
{
self BotBuiltinBotAction( "-gocrouch" );
self BotBuiltinBotAction( "-goprone" );
self BotBuiltinBotAction( "-crouch" );
self BotBuiltinBotAction( "-prone" );
}
/*
@@ -2857,8 +2857,8 @@ stand()
*/
crouch()
{
self BotBuiltinBotAction( "+gocrouch" );
self BotBuiltinBotAction( "-goprone" );
self BotBuiltinBotAction( "+crouch" );
self BotBuiltinBotAction( "-prone" );
}
/*
@@ -2866,8 +2866,8 @@ crouch()
*/
prone()
{
self BotBuiltinBotAction( "-gocrouch" );
self BotBuiltinBotAction( "+goprone" );
self BotBuiltinBotAction( "-crouch" );
self BotBuiltinBotAction( "+prone" );
}
/*

View File

@@ -38,30 +38,6 @@ BotBuiltinPrintConsole( s )
}
}
/*
Writes to the file, mode can be "append" or "write"
*/
BotBuiltinFileWrite( file, contents, mode )
{
if ( isdefined( level.bot_builtins ) && isdefined( level.bot_builtins[ "filewrite" ] ) )
{
[[ level.bot_builtins[ "filewrite" ] ]]( file, contents, mode );
}
}
/*
Returns the whole file as a string
*/
BotBuiltinFileRead( file )
{
if ( isdefined( level.bot_builtins ) && isdefined( level.bot_builtins[ "fileread" ] ) )
{
return [[ level.bot_builtins[ "fileread" ] ]]( file );
}
return undefined;
}
/*
Test if a file exists
*/
@@ -146,6 +122,54 @@ BotBuiltinBotAngles( angles )
}
}
/*
Opens the file
*/
BotBuiltinFileOpen( file, mode )
{
if ( isdefined( level.bot_builtins ) && isdefined( level.bot_builtins[ "fs_fopen" ] ) )
{
return [[ level.bot_builtins[ "fs_fopen" ] ]]( file, mode );
}
return 0;
}
/*
Closes the file
*/
BotBuiltinFileClose( fh )
{
if ( isdefined( level.bot_builtins ) && isdefined( level.bot_builtins[ "fs_fclose" ] ) )
{
[[ level.bot_builtins[ "fs_fclose" ] ]]( fh );
}
}
/*
Closes the file
*/
BotBuiltinReadLine( fh )
{
if ( isdefined( level.bot_builtins ) && isdefined( level.bot_builtins[ "fs_readline" ] ) )
{
return [[ level.bot_builtins[ "fs_readline" ] ]]( fh );
}
return undefined;
}
/*
Closes the file
*/
BotBuiltinWriteLine( fh, contents )
{
if ( isdefined( level.bot_builtins ) && isdefined( level.bot_builtins[ "fs_writeline" ] ) )
{
[[ level.bot_builtins[ "fs_writeline" ] ]]( fh, contents );
}
}
/*
Returns if player is the host
*/
@@ -1436,61 +1460,6 @@ parseTokensIntoWaypoint( tokens )
return waypoint;
}
/*
Function to extract lines from a file specified by 'filename' and store them in a result structure.
*/
getWaypointLinesFromFile( filename )
{
// Create a structure to store the result, including an array to hold individual lines.
result = spawnstruct();
result.lines = [];
// Read the entire content of the file into the 'waypointStr' variable.
// Note: max string length in GSC is 65535.
waypointStr = BotBuiltinFileRead( filename );
// If the file is empty or not defined, return the empty result structure.
if ( !isdefined( waypointStr ) )
{
return result;
}
// Variables to track the current line's character count and starting position.
linecount = 0;
linestart = 0;
// Iterate through each character in the 'waypointStr'.
for ( i = 0; i < waypointStr.size; i++ )
{
// Check for newline characters '\n' or '\r'.
if ( waypointStr[ i ] == "\n" || waypointStr[ i ] == "\r" )
{
// Extract the current line using 'getsubstr' and store it in the result array.
result.lines[ result.lines.size ] = getsubstr( waypointStr, linestart, linestart + linecount );
// If the newline is '\r\n', skip the next character.
if ( waypointStr[ i ] == "\r" && i < waypointStr.size - 1 && waypointStr[ i + 1 ] == "\n" )
{
i++;
}
// Reset linecount and update linestart for the next line.
linecount = 0;
linestart = i + 1;
continue;
}
// Increment linecount for the current line.
linecount++;
}
// Store the last line (or the only line if there are no newline characters) in the result array.
result.lines[ result.lines.size ] = getsubstr( waypointStr, linestart, linestart + linecount );
// Return the result structure containing the array of extracted lines.
return result;
}
/*
Read from file a csv, and returns an array of waypoints
*/
@@ -1504,26 +1473,40 @@ readWpsFromFile( mapname )
return waypoints;
}
res = getWaypointLinesFromFile( filename );
f = BotBuiltinFileOpen( filename, "read" );
if ( !res.lines.size )
if ( f < 1 )
{
return waypoints;
}
BotBuiltinPrintConsole( "Attempting to read waypoints from " + filename );
waypointCount = int( res.lines[ 0 ] );
line = BotBuiltinReadLine( f );
for ( i = 1; i <= waypointCount; i++ )
if ( isdefined( line ) )
{
tokens = strtok( res.lines[ i ], "," );
waypointCount = int( line );
waypoint = parseTokensIntoWaypoint( tokens );
waypoints[ i - 1 ] = waypoint;
for ( i = 1; i <= waypointCount; i++ )
{
line = BotBuiltinReadLine( f );
if ( !isdefined( line ) )
{
break;
}
tokens = strtok( line, "," );
waypoint = parseTokensIntoWaypoint( tokens );
waypoints[ i - 1 ] = waypoint;
}
}
BotBuiltinFileClose( f );
return waypoints;
}

View File

@@ -354,8 +354,13 @@ watchSaveWaypointsCommand()
println( "********* Start Bot Warfare WPDump *********" );
println( level.waypointcount );
f = BotBuiltinFileOpen( filename, "write" );
BotBuiltinFileWrite( filename, level.waypointcount + "\n", "write" );
if ( f > 0 )
{
BotBuiltinWriteLine( f, level.waypointcount );
}
for ( i = 0; i < level.waypointcount; i++ )
{
@@ -388,7 +393,16 @@ watchSaveWaypointsCommand()
str += ",";
println( str );
BotBuiltinFileWrite( filename, str + "\n", "append" );
if ( f > 0 )
{
BotBuiltinWriteLine( f, str );
}
}
if ( f > 0 )
{
BotBuiltinFileClose( f );
}
println( "\n\n\n\n\n\n" );

View File

@@ -1,8 +1,6 @@
init()
{
level.bot_builtins[ "printconsole" ] = ::do_printconsole;
level.bot_builtins[ "filewrite" ] = ::do_filewrite;
level.bot_builtins[ "fileread" ] = ::do_fileread;
level.bot_builtins[ "fileexists" ] = ::do_fileexists;
level.bot_builtins[ "botaction" ] = ::do_botaction;
level.bot_builtins[ "botstop" ] = ::do_botstop;
@@ -10,29 +8,21 @@ init()
level.bot_builtins[ "botmeleeparams" ] = ::do_botmeleeparams;
level.bot_builtins[ "botangles" ] = ::do_botangles;
level.bot_builtins[ "isbot" ] = ::do_isbot;
level.bot_builtins[ "fs_fopen" ] = ::do_fs_fopen;
level.bot_builtins[ "fs_fclose" ] = ::do_fs_fclose;
level.bot_builtins[ "fs_readline" ] = ::do_fs_readline;
level.bot_builtins[ "fs_writeline" ] = ::do_fs_writeline;
}
do_printconsole( s )
{
printconsole( s );
}
do_filewrite( file, contents, mode )
{
file = "scriptdata/" + file;
filewrite( file, contents, mode );
}
do_fileread( file )
{
file = "scriptdata/" + file;
return fileread( file );
printf( s );
}
do_fileexists( file )
{
file = "scriptdata/" + file;
return true;
return fs_testfile( file );
}
do_botaction( action )
@@ -57,11 +47,31 @@ do_botmeleeparams( yaw, dist )
do_botangles( angles )
{
self setplayerangles( angles );
// self botangles( angles[ 0 ], angles[ 1 ], angles[ 2 ] );
self botangles( angles );
}
do_isbot()
{
return self isbot();
}
do_fs_fopen( file, mode )
{
file = "scriptdata/" + file;
return fs_fopen( file, mode );
}
do_fs_fclose( fh )
{
fs_fclose( fh );
}
do_fs_readline( fh )
{
return fs_readline( fh );
}
do_fs_writeline( fh, contents )
{
fs_writeline( fh, contents );
}