2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 \todo is this still used / in working state?
27 should we cleanup and remove it for good
41 struct SVecVariableDef
50 const int MAX_VARIABLES = 64;
52 brush_t* g_pHold1 = NULL;
53 brush_t* g_pHold2 = NULL;
54 brush_t* g_pHold3 = NULL;
55 bool g_bRotateAroundSelection;
57 int g_nVecVariableCount;
59 float g_fDefault = 9999.9f;
65 SVariableDef g_Variables[MAX_VARIABLES];
66 SVecVariableDef g_VecVariables[MAX_VARIABLES];
68 void InitForScriptRun(){
72 g_bRotateAroundSelection = true;
74 g_nVecVariableCount = 0;
81 void AddVariable( const char* pName, float fValue, const char* pInput = NULL ){
82 if ( g_nVariableCount < MAX_VARIABLES ) {
83 g_Variables[g_nVariableCount].m_strName = pName;
84 g_Variables[g_nVariableCount].m_strName.MakeLower();
85 g_Variables[g_nVariableCount].m_fValue = fValue;
87 g_Variables[g_nVariableCount].m_strInput = pInput;
92 gtk_MessageBox( g_pParentWnd->m_pWidget, "Maximum script variable limit reached!" );
96 float VariableValue( const char* pName ){
97 CString strName = pName;
99 for ( int n = 0; n < g_nVariableCount; n++ )
101 if ( strName == g_Variables[n].m_strName ) {
102 return g_Variables[n].m_fValue;
105 //strName.Format("Reference to non-existant varirable %s", pName);
106 //g_pParentWnd->MessageBox(strName);
110 void SetVariableValue( const char* pName, float fValue ){
111 CString strName = pName;
113 for ( int n = 0; n < g_nVariableCount; n++ )
115 if ( strName == g_Variables[n].m_strName ) {
116 g_Variables[n].m_fValue = fValue;
123 void AddVectorVariable( const char* pName, const char* pInput = NULL ){
124 if ( g_nVecVariableCount < MAX_VARIABLES ) {
125 g_VecVariables[g_nVecVariableCount].m_strName = pName;
126 g_VecVariables[g_nVecVariableCount].m_strName.MakeLower();
128 g_VecVariables[g_nVariableCount].m_strInput = pInput;
130 g_nVecVariableCount++;
133 gtk_MessageBox( g_pParentWnd->m_pWidget, "Maximum script variable limit reached!" );
137 void VectorVariableValue( const char* pName, vec3_t& v ){
138 CString strName = pName;
140 for ( int n = 0; n < g_nVecVariableCount; n++ )
142 if ( strName == g_VecVariables[n].m_strName ) {
143 VectorCopy( g_VecVariables[n].m_vValue, v );
147 strName.Format( "Reference to non-existant variable %s", pName );
148 gtk_MessageBox( g_pParentWnd->m_pWidget, strName );
151 void SetVectorVariableValue( const char* pName, vec3_t v ){
152 CString strName = pName;
154 for ( int n = 0; n < g_nVecVariableCount; n++ )
156 if ( strName == g_VecVariables[n].m_strName ) {
157 VectorCopy( v, g_VecVariables[n].m_vValue );
168 // _CopySelected(nHoldPos)
169 // copies selected brush to hold spot 1, 2 or 3
171 // _MoveSelected(x, y, z)
172 // moves selected brush by coords provided
174 // _RotateSelected(x, y, z)
175 // rotates selected brush by coords provided
177 // _MoveHold(nHoldPos, x, y, z)
178 // moves brush in hold pos by coords provided
180 // _RotateHold(nHoldPos, x, y, z)
181 // rotates brush in hold pos by coords provided
183 // _CopyToMap(nHoldPos)
184 // copies hold brush to map
186 // _CopyAndSelect(nHoldPos)
187 // copies hold brush to map and selects it
189 // _Input(VarName1, ... VarNamennn)
190 // inputs a list of values from the user
193 typedef void ( PFNScript )( char*& );
203 const char* GetParam( char*& pBuffer ){
204 static CString strParam;
205 bool bStringMode = false;
207 while ( *pBuffer != (char)NULL && isspace( *pBuffer ) ) // skip and whitespace
210 if ( *pBuffer == '(' ) { // if it's an opening paren, skip it
214 if ( *pBuffer == '\"' ) { // string ?
222 while ( *pBuffer != (char)NULL && *pBuffer != '\"' )
223 strParam += *pBuffer++;
227 while ( *pBuffer != (char)NULL && *pBuffer != ' ' && *pBuffer != ')' && *pBuffer != ',' )
228 strParam += *pBuffer++;
231 if ( *pBuffer != (char)NULL ) { // skip last char
235 if ( strParam.GetLength() > 0 ) {
236 if ( strParam.GetAt( 0 ) == '$' ) { // ? variable name
237 float f = VariableValue( strParam );
238 if ( f != g_fDefault ) {
239 strParam.Format( "%f", f );
247 brush_t* CopyBrush( brush_t* p ){
248 brush_t* pCopy = Brush_Clone( p );
249 //Brush_AddToList (pCopy, &active_brushes);
250 //Entity_LinkBrush (world_entity, pCopy);
251 Brush_Build( pCopy, false );
257 void CopySelected( char*& pBuffer ){
259 CString strParam = GetParam( pBuffer );
260 int n = atoi( strParam );
262 brush_t* pCopy = NULL;
263 if ( selected_brushes.next != &selected_brushes &&
264 selected_brushes.next->next == &selected_brushes ) {
265 pCopy = selected_brushes.next;
271 //Brush_Free(g_pHold1);
272 g_pHold1 = CopyBrush( pCopy );
276 //Brush_Free(g_pHold2);
277 g_pHold2 = CopyBrush( pCopy );
282 //Brush_Free(g_pHold3);
283 g_pHold3 = CopyBrush( pCopy );
288 void MoveSelected( char*& pBuffer ){
290 CString strParam = GetParam( pBuffer );
291 v[0] = atof( strParam );
292 strParam = GetParam( pBuffer );
293 v[1] = atof( strParam );
294 strParam = GetParam( pBuffer );
295 v[2] = atof( strParam );
296 Select_Move( v, false );
297 Sys_UpdateWindows( W_ALL );
300 void RotateSelected( char*& pBuffer ){
303 if ( g_bRotateAroundSelection ) {
304 Select_GetTrueMid( v );
305 VectorCopy( v, g_pParentWnd->ActiveXY()->RotateOrigin() );
308 CString strParam = GetParam( pBuffer );
309 v[0] = atof( strParam );
310 strParam = GetParam( pBuffer );
311 v[1] = atof( strParam );
312 strParam = GetParam( pBuffer );
313 v[2] = atof( strParam );
314 for ( int i = 0; i < 3; i++ )
316 Select_RotateAxis( i, v[i], false, true );
318 Sys_UpdateWindows( W_ALL );
321 void MoveHold( char*& pBuffer ){
322 CString strParam = GetParam( pBuffer );
323 brush_t* pBrush = NULL;
324 int nHold = atoi( strParam );
328 else if ( nHold == 2 ) {
337 strParam = GetParam( pBuffer );
338 v[0] = atof( strParam );
339 strParam = GetParam( pBuffer );
340 v[1] = atof( strParam );
341 strParam = GetParam( pBuffer );
342 v[2] = atof( strParam );
343 Brush_Move( pBrush, v, false );
347 void RotateHold( char*& pBuffer ){
348 CString strParam = GetParam( pBuffer );
349 brush_t* pBrush = NULL;
350 int nHold = atoi( strParam );
354 else if ( nHold == 2 ) {
363 strParam = GetParam( pBuffer );
364 v[0] = atof( strParam );
365 strParam = GetParam( pBuffer );
366 v[1] = atof( strParam );
367 strParam = GetParam( pBuffer );
368 v[2] = atof( strParam );
369 for ( int i = 0; i < 3; i++ )
371 Select_RotateAxis( i, v[i] );
376 void CopyToMap( char*& pBuffer ){
377 CString strParam = GetParam( pBuffer );
378 brush_t* pBrush = NULL;
379 int nHold = atoi( strParam );
383 else if ( nHold == 2 ) {
391 Brush_AddToList( pBrush, &active_brushes );
392 Entity_LinkBrush( world_entity, pBrush );
393 Brush_Build( pBrush, false );
395 Sys_UpdateWindows( W_ALL );
399 void CopyAndSelect( char*& pBuffer ){
400 CString strParam = GetParam( pBuffer );
401 brush_t* pBrush = NULL;
402 int nHold = atoi( strParam );
406 else if ( nHold == 2 ) {
415 Brush_AddToList( pBrush, &active_brushes );
416 Entity_LinkBrush( world_entity, pBrush );
417 Brush_Build( pBrush, false );
419 Select_Brush( pBrush );
420 Sys_UpdateWindows( W_ALL );
424 void Input( char*& pBuffer ){
426 const char *fields[5] = { "", "", "", "", "" };
429 for ( int n = 0; n < 5 && n < g_nVariableCount; n++ )
431 if ( g_Variables[n].m_strInput.GetLength() > 0 ) {
433 fields[n] = g_Variables[n].m_strInput.GetBuffer();
441 if ( DoBSInputDlg( fields, values ) != IDOK ) {
442 g_bKeepGoing = false;
446 for ( int n = 0; n < 5 && n < g_nVariableCount; n++ )
448 if ( g_Variables[n].m_strInput.GetLength() > 0 ) {
449 g_Variables[n].m_fValue = values[n];
455 void _3DPointDone( bool b, int n ){
459 void _3DPointInput( char*& pBuffer ){
460 CString strParam = GetParam( pBuffer );
461 CString strParam2 = GetParam( pBuffer );
462 ShowInfoDialog( strParam2 );
463 AddVectorVariable( strParam, strParam2 );
465 AcquirePath( 2, &_3DPointDone );
467 gtk_main_iteration();
469 SetVectorVariableValue( strParam, g_PathPoints[0] );
472 void SetRotateOrigin( char*& pBuffer ){
474 CString strParam = GetParam( pBuffer );
475 VectorVariableValue( strParam, v );
476 VectorCopy( v, g_pParentWnd->ActiveXY()->RotateOrigin() );
477 g_bRotateAroundSelection = false;
480 void InputVar( char*& pBuffer ){
481 CString strParam = GetParam( pBuffer );
482 CString strParam2 = GetParam( pBuffer );
483 AddVariable( strParam, 0.0, strParam2 );
486 void LoopCount( char*& pBuffer ){
487 CString strParam = GetParam( pBuffer );
488 g_nLoopCounter = atoi( strParam );
489 if ( g_nLoopCounter == 0 ) {
490 g_nLoopCounter = (int)VariableValue( strParam );
492 if ( g_nLoopCounter > 0 ) {
497 void LoopRun( char*& pBuffer ){
498 if ( g_bStartLoop == true ) {
500 if ( g_nLoopCounter == 0 ) {
501 g_bStartLoop = false;
510 if ( g_pLooper && g_nLoopCounter > 0 ) {
522 void ConfirmMessage( char*& pBuffer ){
523 CString strParam = GetParam( pBuffer );
524 if ( gtk_MessageBox( g_pParentWnd->m_pWidget, strParam, "Script Info", MB_OKCANCEL ) == IDCANCEL ) {
525 g_bKeepGoing = false;
529 void Spherize( char*& pBuffer ){
530 g_bScreenUpdates = false;
531 for ( int n = 0; n < 120; n += 36 )
533 for ( int i = 0; i < 360; i += 36 )
535 Select_RotateAxis( 0, i, false, true );
538 Select_RotateAxis( 2, n, false, true );
540 g_bScreenUpdates = true;
543 void RunIt( char*& pBuffer );
544 SBrushScript g_ScriptCmds[] =
546 {"_CopySelected", &CopySelected},
547 {"_MoveSelected", &MoveSelected},
548 {"_RotateSelected", &RotateSelected},
549 {"_MoveHold", &MoveHold},
550 {"_RotateHold", &RotateHold},
551 {"_CopyToMap", &CopyToMap},
552 {"_CopyAndSelect", &CopyAndSelect},
554 {"_3DPointInput", &_3DPointInput},
555 {"_SetRotateOrigin", &SetRotateOrigin},
556 {"_InputVar", &InputVar},
557 {"_LoopCount", &LoopCount},
558 {"_LoopRun", &LoopRun},
559 {"_ConfirmMessage", &ConfirmMessage},
560 {"_Spherize", &Spherize},
561 {"_RunScript", RunIt}
564 const int g_nScriptCmdCount = sizeof( g_ScriptCmds ) / sizeof( SBrushScript );
566 void RunScript( char* pBuffer ){
571 while ( g_bKeepGoing && pBuffer && *pBuffer )
573 while ( *pBuffer != (char)NULL && *pBuffer != '_' )
576 char* pTemp = pBuffer;
578 while ( *pTemp != (char)NULL && *pTemp != '(' )
583 if ( *pBuffer != (char)NULL ) {
585 for ( int i = 0; i < g_nScriptCmdCount; i++ )
587 //if (strnicmp(g_ScriptCmds[i].m_pName, pBuffer, strlen(g_ScriptCmds[i].m_pName)) == 0)
588 if ( strnicmp( g_ScriptCmds[i].m_pName, pBuffer, nLen ) == 0 ) {
589 pBuffer += strlen( g_ScriptCmds[i].m_pName );
590 g_ScriptCmds[i].m_pProc( pBuffer );
591 if ( g_bStartLoop ) {
605 void RunScriptByName( char* pBuffer, bool bInit ){
609 char* pScript = new char[4096];
611 strINI = g_strGameToolsPath;
612 strINI += "/scripts.ini";
616 f = fopen( strINI.GetBuffer(), "rt" );
618 char line[1024], *ptr;
620 // read section names
621 while ( fgets( line, 1024, f ) != 0 )
623 if ( line[0] != '[' ) {
627 ptr = strchr( line, ']' );
630 if ( strcmp( line, pScript ) == 0 ) {
631 while ( fgets( line, 1024, f ) != 0 )
633 if ( ( strchr( line, '=' ) == NULL ) ||
634 strlen( line ) == 0 ) {
644 RunScript( (char*)strScript.GetBuffer() );
648 void RunIt( char*& pBuffer ){
649 brush_t* p1 = g_pHold1;
650 brush_t* p2 = g_pHold2;
651 brush_t* p3 = g_pHold3;
653 CString strParam = GetParam( pBuffer );
654 RunScriptByName( (char*)strParam.GetBuffer(), false );