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()
73 g_bRotateAroundSelection = true;
75 g_nVecVariableCount = 0;
82 void AddVariable(const char* pName, float fValue, const char* pInput = NULL)
84 if (g_nVariableCount < MAX_VARIABLES)
86 g_Variables[g_nVariableCount].m_strName = pName;
87 g_Variables[g_nVariableCount].m_strName.MakeLower();
88 g_Variables[g_nVariableCount].m_fValue = fValue;
90 g_Variables[g_nVariableCount].m_strInput = pInput;
94 gtk_MessageBox(g_pParentWnd->m_pWidget, "Maximum script variable limit reached!");
97 float VariableValue(const char* pName)
99 CString strName = pName;
101 for (int n = 0; n < g_nVariableCount; n++)
103 if (strName == g_Variables[n].m_strName)
104 return g_Variables[n].m_fValue;
106 //strName.Format("Reference to non-existant varirable %s", pName);
107 //g_pParentWnd->MessageBox(strName);
111 void SetVariableValue(const char* pName, float fValue)
113 CString strName = pName;
115 for (int n = 0; n < g_nVariableCount; n++)
117 if (strName == g_Variables[n].m_strName)
118 g_Variables[n].m_fValue = fValue;
124 void AddVectorVariable(const char* pName, const char* pInput = NULL)
126 if (g_nVecVariableCount < MAX_VARIABLES)
128 g_VecVariables[g_nVecVariableCount].m_strName = pName;
129 g_VecVariables[g_nVecVariableCount].m_strName.MakeLower();
131 g_VecVariables[g_nVariableCount].m_strInput = pInput;
132 g_nVecVariableCount++;
135 gtk_MessageBox(g_pParentWnd->m_pWidget, "Maximum script variable limit reached!");
138 void VectorVariableValue(const char* pName, vec3_t& v)
140 CString strName = pName;
142 for (int n = 0; n < g_nVecVariableCount; n++)
144 if (strName == g_VecVariables[n].m_strName)
146 VectorCopy(g_VecVariables[n].m_vValue, v);
150 strName.Format("Reference to non-existant variable %s", pName);
151 gtk_MessageBox(g_pParentWnd->m_pWidget, strName);
154 void SetVectorVariableValue(const char* pName, vec3_t v)
156 CString strName = pName;
158 for (int n = 0; n < g_nVecVariableCount; n++)
160 if (strName == g_VecVariables[n].m_strName)
161 VectorCopy(v, g_VecVariables[n].m_vValue);
171 // _CopySelected(nHoldPos)
172 // copies selected brush to hold spot 1, 2 or 3
174 // _MoveSelected(x, y, z)
175 // moves selected brush by coords provided
177 // _RotateSelected(x, y, z)
178 // rotates selected brush by coords provided
180 // _MoveHold(nHoldPos, x, y, z)
181 // moves brush in hold pos by coords provided
183 // _RotateHold(nHoldPos, x, y, z)
184 // rotates brush in hold pos by coords provided
186 // _CopyToMap(nHoldPos)
187 // copies hold brush to map
189 // _CopyAndSelect(nHoldPos)
190 // copies hold brush to map and selects it
192 // _Input(VarName1, ... VarNamennn)
193 // inputs a list of values from the user
196 typedef void (PFNScript)(char*&);
206 const char* GetParam(char*& pBuffer)
208 static CString strParam;
209 bool bStringMode = false;
211 while (*pBuffer != (char)NULL && isspace(*pBuffer)) // skip and whitespace
214 if (*pBuffer == '(') // if it's an opening paren, skip it
217 if (*pBuffer == '\"') // string ?
227 while (*pBuffer != (char)NULL && *pBuffer != '\"')
228 strParam += *pBuffer++;
232 while (*pBuffer != (char)NULL && *pBuffer != ' ' && *pBuffer != ')' && *pBuffer != ',')
233 strParam += *pBuffer++;
236 if (*pBuffer != (char)NULL) // skip last char
239 if (strParam.GetLength() > 0)
241 if (strParam.GetAt(0) == '$') // ? variable name
243 float f = VariableValue(strParam);
245 strParam.Format("%f", f);
252 brush_t* CopyBrush(brush_t* p)
254 brush_t* pCopy = Brush_Clone(p);
255 //Brush_AddToList (pCopy, &active_brushes);
256 //Entity_LinkBrush (world_entity, pCopy);
257 Brush_Build(pCopy, false);
263 void CopySelected(char*& pBuffer)
266 CString strParam = GetParam(pBuffer);
267 int n = atoi(strParam);
269 brush_t* pCopy = NULL;
270 if (selected_brushes.next != &selected_brushes &&
271 selected_brushes.next->next == &selected_brushes)
272 pCopy = selected_brushes.next;
279 //Brush_Free(g_pHold1);
280 g_pHold1 = CopyBrush(pCopy);
285 //Brush_Free(g_pHold2);
286 g_pHold2 = CopyBrush(pCopy);
291 //Brush_Free(g_pHold3);
292 g_pHold3 = CopyBrush(pCopy);
297 void MoveSelected(char*& pBuffer)
300 CString strParam = GetParam(pBuffer);
301 v[0] = atof(strParam);
302 strParam = GetParam(pBuffer);
303 v[1] = atof(strParam);
304 strParam = GetParam(pBuffer);
305 v[2] = atof(strParam);
306 Select_Move(v, false);
307 Sys_UpdateWindows(W_ALL);
310 void RotateSelected(char*& pBuffer)
314 if (g_bRotateAroundSelection)
316 Select_GetTrueMid(v);
317 VectorCopy(v, g_pParentWnd->ActiveXY()->RotateOrigin());
320 CString strParam = GetParam(pBuffer);
321 v[0] = atof(strParam);
322 strParam = GetParam(pBuffer);
323 v[1] = atof(strParam);
324 strParam = GetParam(pBuffer);
325 v[2] = atof(strParam);
326 for (int i = 0; i < 3; i++)
328 Select_RotateAxis(i, v[i], false , true);
329 Sys_UpdateWindows(W_ALL);
332 void MoveHold(char*& pBuffer)
334 CString strParam = GetParam(pBuffer);
335 brush_t* pBrush = NULL;
336 int nHold = atoi(strParam);
347 strParam = GetParam(pBuffer);
348 v[0] = atof(strParam);
349 strParam = GetParam(pBuffer);
350 v[1] = atof(strParam);
351 strParam = GetParam(pBuffer);
352 v[2] = atof(strParam);
353 Brush_Move (pBrush, v, false);
357 void RotateHold(char*& pBuffer)
359 CString strParam = GetParam(pBuffer);
360 brush_t* pBrush = NULL;
361 int nHold = atoi(strParam);
372 strParam = GetParam(pBuffer);
373 v[0] = atof(strParam);
374 strParam = GetParam(pBuffer);
375 v[1] = atof(strParam);
376 strParam = GetParam(pBuffer);
377 v[2] = atof(strParam);
378 for (int i = 0; i < 3; i++)
380 Select_RotateAxis(i, v[i]);
384 void CopyToMap(char*& pBuffer)
386 CString strParam = GetParam(pBuffer);
387 brush_t* pBrush = NULL;
388 int nHold = atoi(strParam);
398 Brush_AddToList(pBrush, &active_brushes);
399 Entity_LinkBrush (world_entity, pBrush);
400 Brush_Build(pBrush, false);
402 Sys_UpdateWindows(W_ALL);
406 void CopyAndSelect(char*& pBuffer)
408 CString strParam = GetParam(pBuffer);
409 brush_t* pBrush = NULL;
410 int nHold = atoi(strParam);
421 Brush_AddToList(pBrush, &active_brushes);
422 Entity_LinkBrush (world_entity, pBrush);
423 Brush_Build(pBrush, false);
425 Select_Brush(pBrush);
426 Sys_UpdateWindows(W_ALL);
430 void Input(char*& pBuffer)
433 const char *fields[5] = { "", "", "", "", "" };
436 for (int n = 0; n < g_nVariableCount; n++)
438 if (g_Variables[n].m_strInput.GetLength() > 0)
445 case 0 : fields[1] = g_Variables[n].m_strInput.GetBuffer (); break;
446 case 1 : fields[2] = g_Variables[n].m_strInput.GetBuffer (); break;
447 case 2 : fields[3] = g_Variables[n].m_strInput.GetBuffer (); break;
448 case 3 : fields[4] = g_Variables[n].m_strInput.GetBuffer (); break;
449 case 4 : fields[5] = g_Variables[n].m_strInput.GetBuffer (); break;
457 if (DoBSInputDlg (fields, values) == IDOK)
459 for (int n = 0; n < g_nVariableCount; n++)
461 if (g_Variables[n].m_strInput.GetLength() > 0)
467 case 0 : g_Variables[n].m_fValue = values[1]; break;
468 case 1 : g_Variables[n].m_fValue = values[2]; break;
469 case 2 : g_Variables[n].m_fValue = values[3]; break;
470 case 3 : g_Variables[n].m_fValue = values[4]; break;
471 case 4 : g_Variables[n].m_fValue = values[5]; break;
477 else g_bKeepGoing = false;
482 void _3DPointDone(bool b, int n)
487 void _3DPointInput(char*& pBuffer)
489 CString strParam = GetParam(pBuffer);
490 CString strParam2 = GetParam(pBuffer);
491 ShowInfoDialog(strParam2);
492 AddVectorVariable(strParam, strParam2);
494 AcquirePath(2, &_3DPointDone);
496 gtk_main_iteration ();
498 SetVectorVariableValue(strParam, g_PathPoints[0]);
501 void SetRotateOrigin(char*& pBuffer)
504 CString strParam = GetParam(pBuffer);
505 VectorVariableValue(strParam, v);
506 VectorCopy(v, g_pParentWnd->ActiveXY()->RotateOrigin());
507 g_bRotateAroundSelection = false;
510 void InputVar(char*& pBuffer)
512 CString strParam = GetParam(pBuffer);
513 CString strParam2 = GetParam(pBuffer);
514 AddVariable(strParam, 0.0, strParam2);
517 void LoopCount(char*& pBuffer)
519 CString strParam = GetParam(pBuffer);
520 g_nLoopCounter = atoi(strParam);
521 if (g_nLoopCounter == 0)
522 g_nLoopCounter = (int)VariableValue(strParam);
523 if (g_nLoopCounter > 0)
527 void LoopRun(char*& pBuffer)
529 if (g_bStartLoop == true)
532 if (g_nLoopCounter == 0)
534 g_bStartLoop = false;
542 if (g_pLooper && g_nLoopCounter > 0)
555 void ConfirmMessage(char*& pBuffer)
557 CString strParam = GetParam(pBuffer);
558 if (gtk_MessageBox(g_pParentWnd->m_pWidget, strParam, "Script Info", MB_OKCANCEL) == IDCANCEL)
559 g_bKeepGoing = false;
562 void Spherize(char*& pBuffer)
564 g_bScreenUpdates = false;
565 for (int n = 0; n < 120; n += 36)
567 for (int i = 0; i < 360; i += 36)
569 Select_RotateAxis(0, i, false , true);
572 Select_RotateAxis(2, n, false , true);
574 g_bScreenUpdates = true;
577 void RunIt(char*& pBuffer);
578 SBrushScript g_ScriptCmds[] =
580 {"_CopySelected", &CopySelected},
581 {"_MoveSelected", &MoveSelected},
582 {"_RotateSelected", &RotateSelected},
583 {"_MoveHold", &MoveHold},
584 {"_RotateHold", &RotateHold},
585 {"_CopyToMap", &CopyToMap},
586 {"_CopyAndSelect", &CopyAndSelect},
588 {"_3DPointInput", &_3DPointInput},
589 {"_SetRotateOrigin", &SetRotateOrigin},
590 {"_InputVar", &InputVar},
591 {"_LoopCount", &LoopCount},
592 {"_LoopRun", &LoopRun},
593 {"_ConfirmMessage", &ConfirmMessage},
594 {"_Spherize", &Spherize},
595 {"_RunScript", RunIt}
598 const int g_nScriptCmdCount = sizeof(g_ScriptCmds) / sizeof(SBrushScript);
600 void RunScript(char* pBuffer)
606 while (g_bKeepGoing && pBuffer && *pBuffer)
608 while (*pBuffer != (char)NULL && *pBuffer != '_')
611 char* pTemp = pBuffer;
613 while (*pTemp != (char)NULL && *pTemp != '(')
618 if (*pBuffer != (char)NULL)
621 for (int i = 0; i < g_nScriptCmdCount; i++)
623 //if (strnicmp(g_ScriptCmds[i].m_pName, pBuffer, strlen(g_ScriptCmds[i].m_pName)) == 0)
624 if (strnicmp(g_ScriptCmds[i].m_pName, pBuffer, nLen) == 0)
626 pBuffer += strlen(g_ScriptCmds[i].m_pName);
627 g_ScriptCmds[i].m_pProc(pBuffer);
642 void RunScriptByName(char* pBuffer, bool bInit)
646 char* pScript = new char[4096];
648 strINI = g_strGameToolsPath;
649 strINI += "/scripts.ini";
653 f = fopen (strINI.GetBuffer(), "rt");
656 char line[1024], *ptr;
658 // read section names
659 while (fgets (line, 1024, f) != 0)
664 ptr = strchr (line, ']');
667 if (strcmp (line, pScript) == 0)
669 while (fgets (line, 1024, f) != 0)
671 if ((strchr (line, '=') == NULL) ||
681 RunScript((char*)strScript.GetBuffer());
685 void RunIt(char*& pBuffer)
687 brush_t* p1 = g_pHold1;
688 brush_t* p2 = g_pHold2;
689 brush_t* p3 = g_pHold3;
691 CString strParam = GetParam(pBuffer);
692 RunScriptByName((char*)strParam.GetBuffer(), false);