home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / BrushScript.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  14.3 KB  |  652 lines

  1. // BrushScript stuff
  2. //
  3. #include "stdafx.h"
  4. #include "qe3.h"
  5. #include "BSInput.h"
  6. #include "DialogInfo.h"
  7.  
  8.  
  9. //
  10. struct SVariableDef
  11. {
  12.   CString m_strName;
  13.   CString m_strInput;
  14.   float m_fValue;
  15. };
  16.  
  17. struct SVecVariableDef
  18. {
  19.   CString m_strName;
  20.   CString m_strInput;
  21.   vec3_t m_vValue;
  22. };
  23.  
  24.  
  25.  
  26. const int MAX_VARIABLES = 64;
  27.  
  28. brush_t* g_pHold1 = NULL;
  29. brush_t* g_pHold2 = NULL;
  30. brush_t* g_pHold3 = NULL;
  31. bool g_bRotateAroundSelection;
  32. int g_nVariableCount;
  33. int g_nVecVariableCount;
  34. int g_nLoopCounter;
  35. float g_fDefault = 9999.9;
  36. vec3_t g_vDefault;
  37. bool g_bStartLoop;
  38. char* g_pLooper;
  39. bool g_bKeepGoing;
  40.  
  41. SVariableDef g_Variables[MAX_VARIABLES];
  42. SVecVariableDef g_VecVariables[MAX_VARIABLES];
  43.  
  44. void InitForScriptRun()
  45. {
  46.   g_pHold1 = NULL;
  47.   g_pHold2 = NULL;
  48.   g_pHold3 = NULL;
  49.   g_bRotateAroundSelection = true;
  50.   g_nVariableCount = 0;
  51.   g_nVecVariableCount = 0;
  52.   g_nLoopCounter = 0;
  53.   g_bStartLoop = false;
  54.   g_pLooper = NULL;
  55.   g_bKeepGoing = true;
  56. }
  57.  
  58. void AddVariable(const char* pName, float fValue, const char* pInput = NULL)
  59. {
  60.   if (g_nVariableCount < MAX_VARIABLES)
  61.   {
  62.     g_Variables[g_nVariableCount].m_strName = pName;
  63.     g_Variables[g_nVariableCount].m_strName.MakeLower();
  64.     g_Variables[g_nVariableCount].m_fValue = fValue;
  65.     if (pInput)
  66.       g_Variables[g_nVariableCount].m_strInput = pInput;
  67.     g_nVariableCount++;
  68.   }
  69.   else
  70.     g_pParentWnd->MessageBox("Maximum script variable limit reached!");
  71. }
  72.  
  73. float VariableValue(const char* pName)
  74. {
  75.   CString strName = pName;
  76.   strName.MakeLower();
  77.   for (int n = 0; n < g_nVariableCount; n++)
  78.   {
  79.     if (strName == g_Variables[n].m_strName)
  80.       return g_Variables[n].m_fValue;
  81.   }
  82.   //strName.Format("Reference to non-existant varirable %s", pName);
  83.   //g_pParentWnd->MessageBox(strName);
  84.   return g_fDefault;
  85. }
  86.  
  87. void SetVariableValue(const char* pName, float fValue)
  88. {
  89.   CString strName = pName;
  90.   strName.MakeLower();
  91.   for (int n = 0; n < g_nVariableCount; n++)
  92.   {
  93.     if (strName == g_Variables[n].m_strName)
  94.       g_Variables[n].m_fValue = fValue;
  95.   }
  96. }
  97.  
  98.  
  99.  
  100. void AddVectorVariable(const char* pName, const char* pInput = NULL)
  101. {
  102.   if (g_nVecVariableCount < MAX_VARIABLES)
  103.   {
  104.     g_VecVariables[g_nVecVariableCount].m_strName = pName;
  105.     g_VecVariables[g_nVecVariableCount].m_strName.MakeLower();
  106.     if (pInput)
  107.       g_VecVariables[g_nVariableCount].m_strInput = pInput;
  108.     g_nVecVariableCount++;
  109.   }
  110.   else
  111.     g_pParentWnd->MessageBox("Maximum script variable limit reached!");
  112. }
  113.  
  114. void VectorVariableValue(const char* pName, vec3_t& v)
  115. {
  116.   CString strName = pName;
  117.   strName.MakeLower();
  118.   for (int n = 0; n < g_nVecVariableCount; n++)
  119.   {
  120.     if (strName == g_VecVariables[n].m_strName)
  121.     {
  122.       VectorCopy(g_VecVariables[n].m_vValue, v);
  123.       return;
  124.     }
  125.   }
  126.   strName.Format("Reference to non-existant variable %s", pName);
  127.   g_pParentWnd->MessageBox(strName);
  128. }
  129.  
  130. void SetVectorVariableValue(const char* pName, vec3_t v)
  131. {
  132.   CString strName = pName;
  133.   strName.MakeLower();
  134.   for (int n = 0; n < g_nVecVariableCount; n++)
  135.   {
  136.     if (strName == g_VecVariables[n].m_strName)
  137.       VectorCopy(v, g_VecVariables[n].m_vValue);
  138.   }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145. // commands
  146. //
  147. // _CopySelected(nHoldPos)  
  148. // copies selected brush to hold spot 1, 2 or 3
  149. //
  150. // _MoveSelected(x, y, z)
  151. // moves selected brush by coords provided
  152. //
  153. // _RotateSelected(x, y, z)
  154. // rotates selected brush by coords provided
  155. //
  156. // _MoveHold(nHoldPos, x, y, z)
  157. // moves brush in hold pos by coords provided
  158. //
  159. // _RotateHold(nHoldPos, x, y, z)
  160. // rotates brush in hold pos by coords provided
  161. //
  162. // _CopyToMap(nHoldPos)
  163. // copies hold brush to map
  164. //
  165. // _CopyAndSelect(nHoldPos)
  166. // copies hold brush to map and selects it
  167. //
  168. // _Input(VarName1, ... VarNamennn)
  169. // inputs a list of values from the user
  170. //
  171.  
  172. typedef void (PFNScript)(char*&);
  173.  
  174.  
  175. struct SBrushScript
  176. {
  177.   const char* m_pName;
  178.   PFNScript* m_pProc;
  179. };
  180.  
  181.  
  182. const char* GetParam(char*& pBuffer)
  183. {
  184.   static CString strParam;
  185.   bool bStringMode = false;
  186.  
  187.   while (*pBuffer != NULL && isspace(*pBuffer))   // skip and whitespace
  188.     pBuffer++;
  189.  
  190.   if (*pBuffer == '(') // if it's an opening paren, skip it
  191.     pBuffer++;
  192.  
  193.   if (*pBuffer == '\"') // string ?
  194.   {
  195.     pBuffer++;
  196.     bStringMode = true;
  197.   }
  198.  
  199.   strParam = "";
  200.  
  201.   if (bStringMode)
  202.   {
  203.     while (*pBuffer != NULL && *pBuffer != '\"')
  204.       strParam += *pBuffer++;
  205.   }
  206.   else
  207.   {
  208.     while (*pBuffer != NULL && *pBuffer != ' ' && *pBuffer != ')' && *pBuffer != ',')
  209.       strParam += *pBuffer++;
  210.   }
  211.  
  212.   if (*pBuffer != NULL)   // skip last char
  213.     pBuffer++;
  214.  
  215.   if (strParam.GetLength() > 0)
  216.   {
  217.     if (strParam.GetAt(0) == '$') // ? variable name
  218.     {
  219.       float f = VariableValue(strParam);
  220.       if (f != g_fDefault)
  221.         strParam.Format("%f", f);
  222.     }
  223.   }
  224.  
  225.   return strParam;
  226. }
  227.  
  228. brush_t* CopyBrush(brush_t* p)
  229. {                            
  230.   brush_t* pCopy = Brush_Clone(p);
  231.     //Brush_AddToList (pCopy, &active_brushes);
  232.     //Entity_LinkBrush (world_entity, pCopy);
  233.     Brush_Build(pCopy, false);
  234.   return pCopy;
  235. }
  236.  
  237.  
  238. void CopySelected(char*& pBuffer)
  239. {
  240.   // expects one param
  241.   CString strParam = GetParam(pBuffer);
  242.   int n = atoi(strParam);
  243.  
  244.   brush_t* pCopy = NULL;
  245.   if (selected_brushes.next != &selected_brushes && 
  246.       selected_brushes.next->next == &selected_brushes)
  247.     pCopy = selected_brushes.next;
  248.  
  249.   if (pCopy)
  250.   {
  251.     if (n == 1)
  252.     {
  253.       //if (g_pHold1)
  254.         //Brush_Free(g_pHold1);
  255.       g_pHold1 = CopyBrush(pCopy);
  256.     }
  257.     else if (n == 2)
  258.     {
  259.       //if (g_pHold2)
  260.         //Brush_Free(g_pHold2);
  261.       g_pHold2 = CopyBrush(pCopy);
  262.     }
  263.     else
  264.     {
  265.       //if (g_pHold3)
  266.         //Brush_Free(g_pHold3);
  267.       g_pHold3 = CopyBrush(pCopy);
  268.     }
  269.   }
  270. }
  271.  
  272. void MoveSelected(char*& pBuffer)
  273. {
  274.   vec3_t v;
  275.   CString strParam = GetParam(pBuffer);
  276.   v[0] = atof(strParam);
  277.   strParam = GetParam(pBuffer);
  278.   v[1] = atof(strParam);
  279.   strParam = GetParam(pBuffer);
  280.   v[2] = atof(strParam);
  281.   Select_Move(v, false);
  282.   Sys_UpdateWindows(W_ALL);
  283. }
  284.  
  285. void RotateSelected(char*& pBuffer)
  286. {
  287.   vec3_t v;
  288.  
  289.   if (g_bRotateAroundSelection)
  290.   {
  291.     Select_GetTrueMid(v);
  292.     VectorCopy(v, g_pParentWnd->ActiveXY()->RotateOrigin());
  293.   }
  294.  
  295.   CString strParam = GetParam(pBuffer);
  296.   v[0] = atof(strParam);
  297.   strParam = GetParam(pBuffer);
  298.   v[1] = atof(strParam);
  299.   strParam = GetParam(pBuffer);
  300.   v[2] = atof(strParam);
  301.   for (int i = 0; i < 3; i++)
  302.     if (v[i] != 0.0)
  303.       Select_RotateAxis(i, v[i], false , true);
  304.   Sys_UpdateWindows(W_ALL);
  305. }
  306.  
  307. void MoveHold(char*& pBuffer)
  308. {
  309.   CString strParam = GetParam(pBuffer);
  310.   brush_t* pBrush = NULL;
  311.   int nHold = atoi(strParam);
  312.   if (nHold == 1)
  313.     pBrush = g_pHold1;
  314.   else if (nHold == 2)
  315.     pBrush = g_pHold2;
  316.   else 
  317.     pBrush = g_pHold3;
  318.  
  319.   if (pBrush)
  320.   {
  321.     vec3_t v;
  322.     strParam = GetParam(pBuffer);
  323.     v[0] = atof(strParam);
  324.     strParam = GetParam(pBuffer);
  325.     v[1] = atof(strParam);
  326.     strParam = GetParam(pBuffer);
  327.     v[2] = atof(strParam);
  328.         Brush_Move (pBrush, v, false);
  329.   }
  330. }
  331.  
  332. void RotateHold(char*& pBuffer)
  333. {
  334.   CString strParam = GetParam(pBuffer);
  335.   brush_t* pBrush = NULL;
  336.   int nHold = atoi(strParam);
  337.   if (nHold == 1)
  338.     pBrush = g_pHold1;
  339.   else if (nHold == 2)
  340.     pBrush = g_pHold2;
  341.   else 
  342.     pBrush = g_pHold3;
  343.  
  344.   if (pBrush)
  345.   {
  346.     vec3_t v;
  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.     for (int i = 0; i < 3; i++)
  354.       if (v[i] != 0.0)
  355.         Select_RotateAxis(i, v[i]);
  356.   }
  357. }
  358.  
  359. void CopyToMap(char*& pBuffer)
  360. {
  361.   CString strParam = GetParam(pBuffer);
  362.   brush_t* pBrush = NULL;
  363.   int nHold = atoi(strParam);
  364.   if (nHold == 1)
  365.     pBrush = g_pHold1;
  366.   else if (nHold == 2)
  367.     pBrush = g_pHold2;
  368.   else 
  369.     pBrush = g_pHold3;
  370.  
  371.   if (pBrush)
  372.   {
  373.     Brush_AddToList(pBrush, &active_brushes);
  374.         Entity_LinkBrush (world_entity, pBrush);
  375.         Brush_Build(pBrush, false);
  376.     Sys_UpdateWindows(W_ALL);
  377.   }
  378. }
  379.  
  380. void CopyAndSelect(char*& pBuffer)
  381. {
  382.   CString strParam = GetParam(pBuffer);
  383.   brush_t* pBrush = NULL;
  384.   int nHold = atoi(strParam);
  385.   if (nHold == 1)
  386.     pBrush = g_pHold1;
  387.   else if (nHold == 2)
  388.     pBrush = g_pHold2;
  389.   else 
  390.     pBrush = g_pHold3;
  391.  
  392.   if (pBrush)
  393.   {
  394.     Select_Deselect();
  395.     Brush_AddToList(pBrush, &active_brushes);
  396.         Entity_LinkBrush (world_entity, pBrush);
  397.         Brush_Build(pBrush, false);
  398.     Select_Brush(pBrush);
  399.     Sys_UpdateWindows(W_ALL);
  400.   }
  401. }
  402.  
  403. void Input(char*& pBuffer)
  404. {
  405.   CBSInput dlg;
  406.   bool bGo = false;
  407.   for (int n = 0; n < g_nVariableCount; n++)
  408.   {
  409.     if (g_Variables[n].m_strInput.GetLength() > 0)
  410.     {
  411.       bGo = true;
  412.       if (n < 5)
  413.       {
  414.         switch (n)
  415.         {
  416.           case 0 : dlg.m_strField1 = g_Variables[n].m_strInput; break;
  417.           case 1 : dlg.m_strField2 = g_Variables[n].m_strInput; break;
  418.           case 2 : dlg.m_strField3 = g_Variables[n].m_strInput; break;
  419.           case 3 : dlg.m_strField4 = g_Variables[n].m_strInput; break;
  420.           case 4 : dlg.m_strField5 = g_Variables[n].m_strInput; break;
  421.         }
  422.       }
  423.     }
  424.   }
  425.   if (bGo)
  426.   {
  427.     if (dlg.DoModal() == IDOK)
  428.     {
  429.       for (int n = 0; n < g_nVariableCount; n++)
  430.       {
  431.         if (g_Variables[n].m_strInput.GetLength() > 0)
  432.         {
  433.           if (n < 5)
  434.           {
  435.             switch (n)
  436.             {
  437.               case 0 : g_Variables[n].m_fValue = dlg.m_fField1; break;
  438.               case 1 : g_Variables[n].m_fValue = dlg.m_fField2; break;
  439.               case 2 : g_Variables[n].m_fValue = dlg.m_fField3; break;
  440.               case 3 : g_Variables[n].m_fValue = dlg.m_fField4; break;
  441.               case 4 : g_Variables[n].m_fValue = dlg.m_fField5; break;
  442.             }
  443.           }
  444.         }
  445.       }
  446.     }
  447.     else g_bKeepGoing = false;
  448.   }
  449. }
  450.  
  451. bool g_bWaiting;
  452. void _3DPointDone(bool b, int n)
  453. {
  454.   g_bWaiting = false;
  455. }
  456.  
  457. void _3DPointInput(char*& pBuffer)
  458. {
  459.   CString strParam = GetParam(pBuffer);
  460.   CString strParam2 = GetParam(pBuffer);
  461.   ShowInfoDialog(strParam2);
  462.   AddVectorVariable(strParam, strParam2);
  463.   g_bWaiting = true;
  464.   AcquirePath(2, &_3DPointDone);
  465.   while (g_bWaiting)
  466.   {
  467.     MSG msg;
  468.     if (::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )) 
  469.     { 
  470.       TranslateMessage(&msg);
  471.       DispatchMessage(&msg);
  472.     }
  473.   }
  474.   HideInfoDialog();
  475.   SetVectorVariableValue(strParam, g_PathPoints[0]);
  476. }
  477.  
  478. void SetRotateOrigin(char*& pBuffer)
  479. {
  480.   vec3_t v;
  481.   CString strParam = GetParam(pBuffer);
  482.   VectorVariableValue(strParam, v);
  483.   VectorCopy(v, g_pParentWnd->ActiveXY()->RotateOrigin());
  484.   g_bRotateAroundSelection = false;
  485. }
  486.  
  487. void InputVar(char*& pBuffer)
  488. {
  489.   CString strParam = GetParam(pBuffer);
  490.   CString strParam2 = GetParam(pBuffer);
  491.   AddVariable(strParam, 0.0, strParam2);
  492. }
  493.  
  494. void LoopCount(char*& pBuffer)
  495. {
  496.   CString strParam = GetParam(pBuffer);
  497.   g_nLoopCounter = atoi(strParam);
  498.   if (g_nLoopCounter == 0)
  499.     g_nLoopCounter = VariableValue(strParam);
  500.   if (g_nLoopCounter > 0)
  501.     g_pLooper = pBuffer;
  502. }
  503.  
  504. void LoopRun(char*& pBuffer)
  505. {
  506.   if (g_bStartLoop == true)
  507.   {
  508.     g_nLoopCounter--;
  509.     if (g_nLoopCounter == 0)
  510.     {
  511.       g_bStartLoop = false;
  512.       GetParam(pBuffer);
  513.     }
  514.     else
  515.       pBuffer = g_pLooper;
  516.   }
  517.   else
  518.   {
  519.     if (g_pLooper && g_nLoopCounter > 0)
  520.     {
  521.       g_bStartLoop = true;
  522.       pBuffer = g_pLooper;
  523.     }
  524.     else
  525.     {
  526.       GetParam(pBuffer);
  527.     }
  528.   }
  529. }
  530.  
  531.  
  532. void ConfirmMessage(char*& pBuffer)
  533. {
  534.   CString strParam = GetParam(pBuffer);
  535.   if (g_pParentWnd->MessageBox(strParam, "Script Info", MB_OKCANCEL) == IDCANCEL)
  536.     g_bKeepGoing = false;
  537. }
  538.  
  539. void Spherize(char*& pBuffer)
  540. {
  541.   g_bScreenUpdates = false;
  542.   for (int n = 0; n < 120; n += 36)
  543.   {
  544.     for (int i = 0; i < 360; i += 36)
  545.     {
  546.       Select_RotateAxis(0, i, false , true);
  547.       CSG_Subtract();
  548.     }
  549.     Select_RotateAxis(2, n, false , true);
  550.   }
  551.   g_bScreenUpdates = true;
  552. }
  553.  
  554. void RunIt(char*& pBuffer);
  555. SBrushScript g_ScriptCmds[] =
  556. {
  557.   {"_CopySelected", &CopySelected},
  558.   {"_MoveSelected", &MoveSelected},
  559.   {"_RotateSelected", &RotateSelected},
  560.   {"_MoveHold", &MoveHold},
  561.   {"_RotateHold", &RotateHold},
  562.   {"_CopyToMap", &CopyToMap},
  563.   {"_CopyAndSelect", &CopyAndSelect},
  564.   {"_Input", &Input},
  565.   {"_3DPointInput", &_3DPointInput},
  566.   {"_SetRotateOrigin", &SetRotateOrigin},
  567.   {"_InputVar", &InputVar},
  568.   {"_LoopCount", &LoopCount},
  569.   {"_LoopRun", &LoopRun},
  570.   {"_ConfirmMessage", &ConfirmMessage},
  571.   {"_Spherize", &Spherize},
  572.   {"_RunScript", RunIt}
  573. };
  574.  
  575. const int g_nScriptCmdCount = sizeof(g_ScriptCmds) / sizeof(SBrushScript);
  576.  
  577. void RunScript(char* pBuffer)
  578. {
  579.   g_pHold1 = NULL;
  580.   g_pHold2 = NULL;
  581.   g_pHold3 = NULL;
  582.  
  583.   while (g_bKeepGoing && pBuffer && *pBuffer)
  584.   {
  585.     while (*pBuffer != NULL && *pBuffer != '_')
  586.       pBuffer++;
  587.  
  588.     char* pTemp = pBuffer;
  589.     int nLen = 0;
  590.     while (*pTemp != NULL && *pTemp != '(')
  591.     {
  592.       pTemp++;
  593.       nLen++;
  594.     }
  595.     if (*pBuffer != NULL)
  596.     {
  597.       bool bFound = false;
  598.       for (int i = 0; i < g_nScriptCmdCount; i++)
  599.       {
  600.         //if (strnicmp(g_ScriptCmds[i].m_pName, pBuffer, strlen(g_ScriptCmds[i].m_pName)) ==  0)
  601.         if (strnicmp(g_ScriptCmds[i].m_pName, pBuffer, nLen) ==  0)
  602.         {
  603.           pBuffer += strlen(g_ScriptCmds[i].m_pName);
  604.           g_ScriptCmds[i].m_pProc(pBuffer);
  605.           if (g_bStartLoop)
  606.           {
  607.           }
  608.           bFound = true;
  609.           break;
  610.         }
  611.       }
  612.       if (!bFound)
  613.         pBuffer++;
  614.     }
  615.   }
  616. }
  617.  
  618.  
  619. void RunScriptByName(char* pBuffer, bool bInit)
  620. {
  621.   if (bInit)
  622.     InitForScriptRun();
  623.   char* pScript = new char[4096];
  624.   CString strINI = g_strAppPath;
  625.   strINI += "\\scripts.ini";
  626.   GetPrivateProfileSection(pBuffer, pScript, 16384, strINI);
  627.   CString strScript;
  628.   char* pWorkScript = pScript;
  629.   while (*pWorkScript != NULL)
  630.   {
  631.     strScript += pWorkScript;
  632.     pWorkScript += strlen(pWorkScript) + 1;
  633.   }
  634.   RunScript(strScript.GetBuffer(0));
  635. }
  636.  
  637.  
  638. void RunIt(char*& pBuffer)
  639. {
  640.   brush_t* p1 = g_pHold1;
  641.   brush_t* p2 = g_pHold2;
  642.   brush_t* p3 = g_pHold3;
  643.  
  644.   CString strParam = GetParam(pBuffer);
  645.   RunScriptByName(strParam.GetBuffer(0), false);
  646.  
  647.   g_pHold3 = p3;
  648.   g_pHold2 = p2;
  649.   g_pHold1 = p1;
  650. }
  651.  
  652.