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 < g_nVariableCount; n++ )
431 if ( g_Variables[n].m_strInput.GetLength() > 0 ) {
436 case 0: fields[1] = g_Variables[n].m_strInput.GetBuffer(); break;
437 case 1: fields[2] = g_Variables[n].m_strInput.GetBuffer(); break;
438 case 2: fields[3] = g_Variables[n].m_strInput.GetBuffer(); break;
439 case 3: fields[4] = g_Variables[n].m_strInput.GetBuffer(); break;
440 case 4: fields[5] = g_Variables[n].m_strInput.GetBuffer(); break;
447 if ( DoBSInputDlg( fields, values ) == IDOK ) {
448 for ( int n = 0; n < g_nVariableCount; n++ )
450 if ( g_Variables[n].m_strInput.GetLength() > 0 ) {
454 case 0: g_Variables[n].m_fValue = values[1]; break;
455 case 1: g_Variables[n].m_fValue = values[2]; break;
456 case 2: g_Variables[n].m_fValue = values[3]; break;
457 case 3: g_Variables[n].m_fValue = values[4]; break;
458 case 4: g_Variables[n].m_fValue = values[5]; break;
464 else{ g_bKeepGoing = false; }
469 void _3DPointDone( bool b, int n ){
473 void _3DPointInput( char*& pBuffer ){
474 CString strParam = GetParam( pBuffer );
475 CString strParam2 = GetParam( pBuffer );
476 ShowInfoDialog( strParam2 );
477 AddVectorVariable( strParam, strParam2 );
479 AcquirePath( 2, &_3DPointDone );
481 gtk_main_iteration();
483 SetVectorVariableValue( strParam, g_PathPoints[0] );
486 void SetRotateOrigin( char*& pBuffer ){
488 CString strParam = GetParam( pBuffer );
489 VectorVariableValue( strParam, v );
490 VectorCopy( v, g_pParentWnd->ActiveXY()->RotateOrigin() );
491 g_bRotateAroundSelection = false;
494 void InputVar( char*& pBuffer ){
495 CString strParam = GetParam( pBuffer );
496 CString strParam2 = GetParam( pBuffer );
497 AddVariable( strParam, 0.0, strParam2 );
500 void LoopCount( char*& pBuffer ){
501 CString strParam = GetParam( pBuffer );
502 g_nLoopCounter = atoi( strParam );
503 if ( g_nLoopCounter == 0 ) {
504 g_nLoopCounter = (int)VariableValue( strParam );
506 if ( g_nLoopCounter > 0 ) {
511 void LoopRun( char*& pBuffer ){
512 if ( g_bStartLoop == true ) {
514 if ( g_nLoopCounter == 0 ) {
515 g_bStartLoop = false;
524 if ( g_pLooper && g_nLoopCounter > 0 ) {
536 void ConfirmMessage( char*& pBuffer ){
537 CString strParam = GetParam( pBuffer );
538 if ( gtk_MessageBox( g_pParentWnd->m_pWidget, strParam, "Script Info", MB_OKCANCEL ) == IDCANCEL ) {
539 g_bKeepGoing = false;
543 void Spherize( char*& pBuffer ){
544 g_bScreenUpdates = false;
545 for ( int n = 0; n < 120; n += 36 )
547 for ( int i = 0; i < 360; i += 36 )
549 Select_RotateAxis( 0, i, false, true );
552 Select_RotateAxis( 2, n, false, true );
554 g_bScreenUpdates = true;
557 void RunIt( char*& pBuffer );
558 SBrushScript g_ScriptCmds[] =
560 {"_CopySelected", &CopySelected},
561 {"_MoveSelected", &MoveSelected},
562 {"_RotateSelected", &RotateSelected},
563 {"_MoveHold", &MoveHold},
564 {"_RotateHold", &RotateHold},
565 {"_CopyToMap", &CopyToMap},
566 {"_CopyAndSelect", &CopyAndSelect},
568 {"_3DPointInput", &_3DPointInput},
569 {"_SetRotateOrigin", &SetRotateOrigin},
570 {"_InputVar", &InputVar},
571 {"_LoopCount", &LoopCount},
572 {"_LoopRun", &LoopRun},
573 {"_ConfirmMessage", &ConfirmMessage},
574 {"_Spherize", &Spherize},
575 {"_RunScript", RunIt}
578 const int g_nScriptCmdCount = sizeof( g_ScriptCmds ) / sizeof( SBrushScript );
580 void RunScript( char* pBuffer ){
585 while ( g_bKeepGoing && pBuffer && *pBuffer )
587 while ( *pBuffer != (char)NULL && *pBuffer != '_' )
590 char* pTemp = pBuffer;
592 while ( *pTemp != (char)NULL && *pTemp != '(' )
597 if ( *pBuffer != (char)NULL ) {
599 for ( int i = 0; i < g_nScriptCmdCount; i++ )
601 //if (strnicmp(g_ScriptCmds[i].m_pName, pBuffer, strlen(g_ScriptCmds[i].m_pName)) == 0)
602 if ( strnicmp( g_ScriptCmds[i].m_pName, pBuffer, nLen ) == 0 ) {
603 pBuffer += strlen( g_ScriptCmds[i].m_pName );
604 g_ScriptCmds[i].m_pProc( pBuffer );
605 if ( g_bStartLoop ) {
619 void RunScriptByName( char* pBuffer, bool bInit ){
623 char* pScript = new char[4096];
625 strINI = g_strGameToolsPath;
626 strINI += "/scripts.ini";
630 f = fopen( strINI.GetBuffer(), "rt" );
632 char line[1024], *ptr;
634 // read section names
635 while ( fgets( line, 1024, f ) != 0 )
637 if ( line[0] != '[' ) {
641 ptr = strchr( line, ']' );
644 if ( strcmp( line, pScript ) == 0 ) {
645 while ( fgets( line, 1024, f ) != 0 )
647 if ( ( strchr( line, '=' ) == NULL ) ||
648 strlen( line ) == 0 ) {
658 RunScript( (char*)strScript.GetBuffer() );
662 void RunIt( char*& pBuffer ){
663 brush_t* p1 = g_pHold1;
664 brush_t* p2 = g_pHold2;
665 brush_t* p3 = g_pHold3;
667 CString strParam = GetParam( pBuffer );
668 RunScriptByName( (char*)strParam.GetBuffer(), false );