2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
22 // BrushScript stuff
\r
26 \todo is this still used / in working state?
\r
27 should we cleanup and remove it for good
\r
31 #include "gtkmisc.h"
\r
41 struct SVecVariableDef
\r
50 const int MAX_VARIABLES = 64;
\r
52 brush_t* g_pHold1 = NULL;
\r
53 brush_t* g_pHold2 = NULL;
\r
54 brush_t* g_pHold3 = NULL;
\r
55 bool g_bRotateAroundSelection;
\r
56 int g_nVariableCount;
\r
57 int g_nVecVariableCount;
\r
59 float g_fDefault = 9999.9f;
\r
65 SVariableDef g_Variables[MAX_VARIABLES];
\r
66 SVecVariableDef g_VecVariables[MAX_VARIABLES];
\r
68 void InitForScriptRun()
\r
73 g_bRotateAroundSelection = true;
\r
74 g_nVariableCount = 0;
\r
75 g_nVecVariableCount = 0;
\r
77 g_bStartLoop = false;
\r
79 g_bKeepGoing = true;
\r
82 void AddVariable(const char* pName, float fValue, const char* pInput = NULL)
\r
84 if (g_nVariableCount < MAX_VARIABLES)
\r
86 g_Variables[g_nVariableCount].m_strName = pName;
\r
87 g_Variables[g_nVariableCount].m_strName.MakeLower();
\r
88 g_Variables[g_nVariableCount].m_fValue = fValue;
\r
90 g_Variables[g_nVariableCount].m_strInput = pInput;
\r
94 gtk_MessageBox(g_pParentWnd->m_pWidget, "Maximum script variable limit reached!");
\r
97 float VariableValue(const char* pName)
\r
99 CString strName = pName;
\r
100 strName.MakeLower();
\r
101 for (int n = 0; n < g_nVariableCount; n++)
\r
103 if (strName == g_Variables[n].m_strName)
\r
104 return g_Variables[n].m_fValue;
\r
106 //strName.Format("Reference to non-existant varirable %s", pName);
\r
107 //g_pParentWnd->MessageBox(strName);
\r
111 void SetVariableValue(const char* pName, float fValue)
\r
113 CString strName = pName;
\r
114 strName.MakeLower();
\r
115 for (int n = 0; n < g_nVariableCount; n++)
\r
117 if (strName == g_Variables[n].m_strName)
\r
118 g_Variables[n].m_fValue = fValue;
\r
124 void AddVectorVariable(const char* pName, const char* pInput = NULL)
\r
126 if (g_nVecVariableCount < MAX_VARIABLES)
\r
128 g_VecVariables[g_nVecVariableCount].m_strName = pName;
\r
129 g_VecVariables[g_nVecVariableCount].m_strName.MakeLower();
\r
131 g_VecVariables[g_nVariableCount].m_strInput = pInput;
\r
132 g_nVecVariableCount++;
\r
135 gtk_MessageBox(g_pParentWnd->m_pWidget, "Maximum script variable limit reached!");
\r
138 void VectorVariableValue(const char* pName, vec3_t& v)
\r
140 CString strName = pName;
\r
141 strName.MakeLower();
\r
142 for (int n = 0; n < g_nVecVariableCount; n++)
\r
144 if (strName == g_VecVariables[n].m_strName)
\r
146 VectorCopy(g_VecVariables[n].m_vValue, v);
\r
150 strName.Format("Reference to non-existant variable %s", pName);
\r
151 gtk_MessageBox(g_pParentWnd->m_pWidget, strName);
\r
154 void SetVectorVariableValue(const char* pName, vec3_t v)
\r
156 CString strName = pName;
\r
157 strName.MakeLower();
\r
158 for (int n = 0; n < g_nVecVariableCount; n++)
\r
160 if (strName == g_VecVariables[n].m_strName)
\r
161 VectorCopy(v, g_VecVariables[n].m_vValue);
\r
171 // _CopySelected(nHoldPos)
\r
172 // copies selected brush to hold spot 1, 2 or 3
\r
174 // _MoveSelected(x, y, z)
\r
175 // moves selected brush by coords provided
\r
177 // _RotateSelected(x, y, z)
\r
178 // rotates selected brush by coords provided
\r
180 // _MoveHold(nHoldPos, x, y, z)
\r
181 // moves brush in hold pos by coords provided
\r
183 // _RotateHold(nHoldPos, x, y, z)
\r
184 // rotates brush in hold pos by coords provided
\r
186 // _CopyToMap(nHoldPos)
\r
187 // copies hold brush to map
\r
189 // _CopyAndSelect(nHoldPos)
\r
190 // copies hold brush to map and selects it
\r
192 // _Input(VarName1, ... VarNamennn)
\r
193 // inputs a list of values from the user
\r
196 typedef void (PFNScript)(char*&);
\r
199 struct SBrushScript
\r
201 const char* m_pName;
\r
202 PFNScript* m_pProc;
\r
206 const char* GetParam(char*& pBuffer)
\r
208 static CString strParam;
\r
209 bool bStringMode = false;
\r
211 while (*pBuffer != (char)NULL && isspace(*pBuffer)) // skip and whitespace
\r
214 if (*pBuffer == '(') // if it's an opening paren, skip it
\r
217 if (*pBuffer == '\"') // string ?
\r
220 bStringMode = true;
\r
227 while (*pBuffer != (char)NULL && *pBuffer != '\"')
\r
228 strParam += *pBuffer++;
\r
232 while (*pBuffer != (char)NULL && *pBuffer != ' ' && *pBuffer != ')' && *pBuffer != ',')
\r
233 strParam += *pBuffer++;
\r
236 if (*pBuffer != (char)NULL) // skip last char
\r
239 if (strParam.GetLength() > 0)
\r
241 if (strParam.GetAt(0) == '$') // ? variable name
\r
243 float f = VariableValue(strParam);
\r
244 if (f != g_fDefault)
\r
245 strParam.Format("%f", f);
\r
252 brush_t* CopyBrush(brush_t* p)
\r
254 brush_t* pCopy = Brush_Clone(p);
\r
255 //Brush_AddToList (pCopy, &active_brushes);
\r
256 //Entity_LinkBrush (world_entity, pCopy);
\r
257 Brush_Build(pCopy, false);
\r
263 void CopySelected(char*& pBuffer)
\r
265 // expects one param
\r
266 CString strParam = GetParam(pBuffer);
\r
267 int n = atoi(strParam);
\r
269 brush_t* pCopy = NULL;
\r
270 if (selected_brushes.next != &selected_brushes &&
\r
271 selected_brushes.next->next == &selected_brushes)
\r
272 pCopy = selected_brushes.next;
\r
279 //Brush_Free(g_pHold1);
\r
280 g_pHold1 = CopyBrush(pCopy);
\r
285 //Brush_Free(g_pHold2);
\r
286 g_pHold2 = CopyBrush(pCopy);
\r
291 //Brush_Free(g_pHold3);
\r
292 g_pHold3 = CopyBrush(pCopy);
\r
297 void MoveSelected(char*& pBuffer)
\r
300 CString strParam = GetParam(pBuffer);
\r
301 v[0] = atof(strParam);
\r
302 strParam = GetParam(pBuffer);
\r
303 v[1] = atof(strParam);
\r
304 strParam = GetParam(pBuffer);
\r
305 v[2] = atof(strParam);
\r
306 Select_Move(v, false);
\r
307 Sys_UpdateWindows(W_ALL);
\r
310 void RotateSelected(char*& pBuffer)
\r
314 if (g_bRotateAroundSelection)
\r
316 Select_GetTrueMid(v);
\r
317 VectorCopy(v, g_pParentWnd->ActiveXY()->RotateOrigin());
\r
320 CString strParam = GetParam(pBuffer);
\r
321 v[0] = atof(strParam);
\r
322 strParam = GetParam(pBuffer);
\r
323 v[1] = atof(strParam);
\r
324 strParam = GetParam(pBuffer);
\r
325 v[2] = atof(strParam);
\r
326 for (int i = 0; i < 3; i++)
\r
328 Select_RotateAxis(i, v[i], false , true);
\r
329 Sys_UpdateWindows(W_ALL);
\r
332 void MoveHold(char*& pBuffer)
\r
334 CString strParam = GetParam(pBuffer);
\r
335 brush_t* pBrush = NULL;
\r
336 int nHold = atoi(strParam);
\r
339 else if (nHold == 2)
\r
347 strParam = GetParam(pBuffer);
\r
348 v[0] = atof(strParam);
\r
349 strParam = GetParam(pBuffer);
\r
350 v[1] = atof(strParam);
\r
351 strParam = GetParam(pBuffer);
\r
352 v[2] = atof(strParam);
\r
353 Brush_Move (pBrush, v, false);
\r
357 void RotateHold(char*& pBuffer)
\r
359 CString strParam = GetParam(pBuffer);
\r
360 brush_t* pBrush = NULL;
\r
361 int nHold = atoi(strParam);
\r
364 else if (nHold == 2)
\r
372 strParam = GetParam(pBuffer);
\r
373 v[0] = atof(strParam);
\r
374 strParam = GetParam(pBuffer);
\r
375 v[1] = atof(strParam);
\r
376 strParam = GetParam(pBuffer);
\r
377 v[2] = atof(strParam);
\r
378 for (int i = 0; i < 3; i++)
\r
380 Select_RotateAxis(i, v[i]);
\r
384 void CopyToMap(char*& pBuffer)
\r
386 CString strParam = GetParam(pBuffer);
\r
387 brush_t* pBrush = NULL;
\r
388 int nHold = atoi(strParam);
\r
391 else if (nHold == 2)
\r
398 Brush_AddToList(pBrush, &active_brushes);
\r
399 Entity_LinkBrush (world_entity, pBrush);
\r
400 Brush_Build(pBrush, false);
\r
402 Sys_UpdateWindows(W_ALL);
\r
406 void CopyAndSelect(char*& pBuffer)
\r
408 CString strParam = GetParam(pBuffer);
\r
409 brush_t* pBrush = NULL;
\r
410 int nHold = atoi(strParam);
\r
413 else if (nHold == 2)
\r
421 Brush_AddToList(pBrush, &active_brushes);
\r
422 Entity_LinkBrush (world_entity, pBrush);
\r
423 Brush_Build(pBrush, false);
\r
425 Select_Brush(pBrush);
\r
426 Sys_UpdateWindows(W_ALL);
\r
430 void Input(char*& pBuffer)
\r
433 const char *fields[5] = { "", "", "", "", "" };
\r
436 for (int n = 0; n < g_nVariableCount; n++)
\r
438 if (g_Variables[n].m_strInput.GetLength() > 0)
\r
445 case 0 : fields[1] = g_Variables[n].m_strInput.GetBuffer (); break;
\r
446 case 1 : fields[2] = g_Variables[n].m_strInput.GetBuffer (); break;
\r
447 case 2 : fields[3] = g_Variables[n].m_strInput.GetBuffer (); break;
\r
448 case 3 : fields[4] = g_Variables[n].m_strInput.GetBuffer (); break;
\r
449 case 4 : fields[5] = g_Variables[n].m_strInput.GetBuffer (); break;
\r
457 if (DoBSInputDlg (fields, values) == IDOK)
\r
459 for (int n = 0; n < g_nVariableCount; n++)
\r
461 if (g_Variables[n].m_strInput.GetLength() > 0)
\r
467 case 0 : g_Variables[n].m_fValue = values[1]; break;
\r
468 case 1 : g_Variables[n].m_fValue = values[2]; break;
\r
469 case 2 : g_Variables[n].m_fValue = values[3]; break;
\r
470 case 3 : g_Variables[n].m_fValue = values[4]; break;
\r
471 case 4 : g_Variables[n].m_fValue = values[5]; break;
\r
477 else g_bKeepGoing = false;
\r
482 void _3DPointDone(bool b, int n)
\r
484 g_bWaiting = false;
\r
487 void _3DPointInput(char*& pBuffer)
\r
489 CString strParam = GetParam(pBuffer);
\r
490 CString strParam2 = GetParam(pBuffer);
\r
491 ShowInfoDialog(strParam2);
\r
492 AddVectorVariable(strParam, strParam2);
\r
494 AcquirePath(2, &_3DPointDone);
\r
496 gtk_main_iteration ();
\r
498 SetVectorVariableValue(strParam, g_PathPoints[0]);
\r
501 void SetRotateOrigin(char*& pBuffer)
\r
504 CString strParam = GetParam(pBuffer);
\r
505 VectorVariableValue(strParam, v);
\r
506 VectorCopy(v, g_pParentWnd->ActiveXY()->RotateOrigin());
\r
507 g_bRotateAroundSelection = false;
\r
510 void InputVar(char*& pBuffer)
\r
512 CString strParam = GetParam(pBuffer);
\r
513 CString strParam2 = GetParam(pBuffer);
\r
514 AddVariable(strParam, 0.0, strParam2);
\r
517 void LoopCount(char*& pBuffer)
\r
519 CString strParam = GetParam(pBuffer);
\r
520 g_nLoopCounter = atoi(strParam);
\r
521 if (g_nLoopCounter == 0)
\r
522 g_nLoopCounter = (int)VariableValue(strParam);
\r
523 if (g_nLoopCounter > 0)
\r
524 g_pLooper = pBuffer;
\r
527 void LoopRun(char*& pBuffer)
\r
529 if (g_bStartLoop == true)
\r
532 if (g_nLoopCounter == 0)
\r
534 g_bStartLoop = false;
\r
538 pBuffer = g_pLooper;
\r
542 if (g_pLooper && g_nLoopCounter > 0)
\r
544 g_bStartLoop = true;
\r
545 pBuffer = g_pLooper;
\r
555 void ConfirmMessage(char*& pBuffer)
\r
557 CString strParam = GetParam(pBuffer);
\r
558 if (gtk_MessageBox(g_pParentWnd->m_pWidget, strParam, "Script Info", MB_OKCANCEL) == IDCANCEL)
\r
559 g_bKeepGoing = false;
\r
562 void Spherize(char*& pBuffer)
\r
564 g_bScreenUpdates = false;
\r
565 for (int n = 0; n < 120; n += 36)
\r
567 for (int i = 0; i < 360; i += 36)
\r
569 Select_RotateAxis(0, i, false , true);
\r
572 Select_RotateAxis(2, n, false , true);
\r
574 g_bScreenUpdates = true;
\r
577 void RunIt(char*& pBuffer);
\r
578 SBrushScript g_ScriptCmds[] =
\r
580 {"_CopySelected", &CopySelected},
\r
581 {"_MoveSelected", &MoveSelected},
\r
582 {"_RotateSelected", &RotateSelected},
\r
583 {"_MoveHold", &MoveHold},
\r
584 {"_RotateHold", &RotateHold},
\r
585 {"_CopyToMap", &CopyToMap},
\r
586 {"_CopyAndSelect", &CopyAndSelect},
\r
587 {"_Input", &Input},
\r
588 {"_3DPointInput", &_3DPointInput},
\r
589 {"_SetRotateOrigin", &SetRotateOrigin},
\r
590 {"_InputVar", &InputVar},
\r
591 {"_LoopCount", &LoopCount},
\r
592 {"_LoopRun", &LoopRun},
\r
593 {"_ConfirmMessage", &ConfirmMessage},
\r
594 {"_Spherize", &Spherize},
\r
595 {"_RunScript", RunIt}
\r
598 const int g_nScriptCmdCount = sizeof(g_ScriptCmds) / sizeof(SBrushScript);
\r
600 void RunScript(char* pBuffer)
\r
606 while (g_bKeepGoing && pBuffer && *pBuffer)
\r
608 while (*pBuffer != (char)NULL && *pBuffer != '_')
\r
611 char* pTemp = pBuffer;
\r
613 while (*pTemp != (char)NULL && *pTemp != '(')
\r
618 if (*pBuffer != (char)NULL)
\r
620 bool bFound = false;
\r
621 for (int i = 0; i < g_nScriptCmdCount; i++)
\r
623 //if (strnicmp(g_ScriptCmds[i].m_pName, pBuffer, strlen(g_ScriptCmds[i].m_pName)) == 0)
\r
624 if (strnicmp(g_ScriptCmds[i].m_pName, pBuffer, nLen) == 0)
\r
626 pBuffer += strlen(g_ScriptCmds[i].m_pName);
\r
627 g_ScriptCmds[i].m_pProc(pBuffer);
\r
642 void RunScriptByName(char* pBuffer, bool bInit)
\r
645 InitForScriptRun();
\r
646 char* pScript = new char[4096];
\r
648 strINI = g_strGameToolsPath;
\r
649 strINI += "/scripts.ini";
\r
653 f = fopen (strINI.GetBuffer(), "rt");
\r
656 char line[1024], *ptr;
\r
658 // read section names
\r
659 while (fgets (line, 1024, f) != 0)
\r
661 if (line[0] != '[')
\r
664 ptr = strchr (line, ']');
\r
667 if (strcmp (line, pScript) == 0)
\r
669 while (fgets (line, 1024, f) != 0)
\r
671 if ((strchr (line, '=') == NULL) ||
\r
672 strlen (line) == 0)
\r
681 RunScript((char*)strScript.GetBuffer());
\r
685 void RunIt(char*& pBuffer)
\r
687 brush_t* p1 = g_pHold1;
\r
688 brush_t* p2 = g_pHold2;
\r
689 brush_t* p3 = g_pHold3;
\r
691 CString strParam = GetParam(pBuffer);
\r
692 RunScriptByName((char*)strParam.GetBuffer(), false);
\r