KX_ConvertControllers.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file gameengine/Converter/KX_ConvertControllers.cpp
  28. * \ingroup bgeconv
  29. */
  30. #include "MEM_guardedalloc.h"
  31. #include "KX_BlenderSceneConverter.h"
  32. #include "KX_ConvertControllers.h"
  33. #include "EXP_Python.h"
  34. // Controller
  35. #include "SCA_ANDController.h"
  36. #include "SCA_ORController.h"
  37. #include "SCA_NANDController.h"
  38. #include "SCA_NORController.h"
  39. #include "SCA_XORController.h"
  40. #include "SCA_XNORController.h"
  41. #include "SCA_PythonController.h"
  42. #include "SCA_ExpressionController.h"
  43. #include "SCA_LogicManager.h"
  44. #include "KX_GameObject.h"
  45. #include "EXP_IntValue.h"
  46. /* This little block needed for linking to Blender... */
  47. #ifdef WIN32
  48. #include "BLI_winstuff.h"
  49. #endif
  50. #include "DNA_object_types.h"
  51. #include "DNA_controller_types.h"
  52. #include "DNA_text_types.h"
  53. #include "BKE_text.h"
  54. #include "BLI_blenlib.h"
  55. /* end of blender include block */
  56. static void
  57. LinkControllerToActuators(
  58. SCA_IController *game_controller,
  59. bController* bcontr,
  60. SCA_LogicManager* logicmgr,
  61. KX_BlenderSceneConverter* converter
  62. ) {
  63. // Iterate through the actuators of the game blender
  64. // controller and find the corresponding ketsji actuator.
  65. game_controller->ReserveActuator(bcontr->totlinks);
  66. for (int i=0;i<bcontr->totlinks;i++)
  67. {
  68. bActuator* bact = (bActuator*) bcontr->links[i];
  69. SCA_IActuator *game_actuator = converter->FindGameActuator(bact);
  70. if (game_actuator) {
  71. logicmgr->RegisterToActuator(game_controller, game_actuator);
  72. }
  73. }
  74. }
  75. void BL_ConvertControllers(
  76. struct Object* blenderobject,
  77. class KX_GameObject* gameobj,
  78. SCA_LogicManager* logicmgr,
  79. int activeLayerBitInfo,
  80. bool isInActiveLayer,
  81. KX_BlenderSceneConverter* converter,
  82. bool libloading
  83. ) {
  84. int uniqueint=0;
  85. int count = 0;
  86. int executePriority=0;
  87. bController* bcontr = (bController*)blenderobject->controllers.first;
  88. while (bcontr)
  89. {
  90. bcontr = bcontr->next;
  91. count++;
  92. }
  93. gameobj->ReserveController(count);
  94. bcontr = (bController*)blenderobject->controllers.first;
  95. while (bcontr)
  96. {
  97. SCA_IController* gamecontroller = NULL;
  98. switch (bcontr->type) {
  99. case CONT_LOGIC_AND:
  100. {
  101. gamecontroller = new SCA_ANDController(gameobj);
  102. break;
  103. }
  104. case CONT_LOGIC_OR:
  105. {
  106. gamecontroller = new SCA_ORController(gameobj);
  107. break;
  108. }
  109. case CONT_LOGIC_NAND:
  110. {
  111. gamecontroller = new SCA_NANDController(gameobj);
  112. break;
  113. }
  114. case CONT_LOGIC_NOR:
  115. {
  116. gamecontroller = new SCA_NORController(gameobj);
  117. break;
  118. }
  119. case CONT_LOGIC_XOR:
  120. {
  121. gamecontroller = new SCA_XORController(gameobj);
  122. break;
  123. }
  124. case CONT_LOGIC_XNOR:
  125. {
  126. gamecontroller = new SCA_XNORController(gameobj);
  127. break;
  128. }
  129. case CONT_EXPRESSION:
  130. {
  131. bExpressionCont* bexpcont = (bExpressionCont*) bcontr->data;
  132. STR_String expressiontext = STR_String(bexpcont->str);
  133. if (expressiontext.Length() > 0)
  134. {
  135. gamecontroller = new SCA_ExpressionController(gameobj,expressiontext);
  136. }
  137. break;
  138. }
  139. case CONT_PYTHON:
  140. {
  141. bPythonCont* pycont = (bPythonCont*) bcontr->data;
  142. SCA_PythonController* pyctrl = new SCA_PythonController(gameobj, pycont->mode);
  143. gamecontroller = pyctrl;
  144. #ifdef WITH_PYTHON
  145. // When libloading, this is delayed to KX_Scene::MergeScene_LogicBrick to avoid GIL issues
  146. if (!libloading)
  147. pyctrl->SetNamespace(converter->GetPyNamespace());
  148. if (pycont->mode==SCA_PythonController::SCA_PYEXEC_SCRIPT) {
  149. if (pycont->text)
  150. {
  151. char *buf;
  152. // this is some blender specific code
  153. buf= txt_to_buf(pycont->text);
  154. if (buf)
  155. {
  156. pyctrl->SetScriptText(STR_String(buf));
  157. pyctrl->SetScriptName(pycont->text->id.name+2);
  158. MEM_freeN(buf);
  159. }
  160. }
  161. }
  162. else {
  163. /* let the controller print any warnings here when importing */
  164. pyctrl->SetScriptText(STR_String(pycont->module));
  165. pyctrl->SetScriptName(pycont->module); /* will be something like module.func so using it as the name is OK */
  166. if (pycont->flag & CONT_PY_DEBUG) {
  167. printf("\nDebuging \"%s\", module for object %s\n\texpect worse performance.\n", pycont->module, blenderobject->id.name+2);
  168. pyctrl->SetDebug(true);
  169. }
  170. }
  171. #endif // WITH_PYTHON
  172. break;
  173. }
  174. default:
  175. {
  176. }
  177. }
  178. if (gamecontroller && !(bcontr->flag & CONT_DEACTIVATE))
  179. {
  180. LinkControllerToActuators(gamecontroller,bcontr,logicmgr,converter);
  181. gamecontroller->SetExecutePriority(executePriority++);
  182. gamecontroller->SetBookmark((bcontr->flag & CONT_PRIO) != 0);
  183. gamecontroller->SetState(bcontr->state_mask);
  184. STR_String uniquename = bcontr->name;
  185. uniquename += "#CONTR#";
  186. uniqueint++;
  187. CIntValue* uniqueval = new CIntValue(uniqueint);
  188. uniquename += uniqueval->GetText();
  189. uniqueval->Release();
  190. //unique name was never implemented for sensors and actuators, only for controllers
  191. //and it's producing difference in the keys for the lists: obj.controllers/sensors/actuators
  192. //at some point it should either be implemented globally (and saved as a separate var) or removed.
  193. //gamecontroller->SetName(uniquename);
  194. gamecontroller->SetName(bcontr->name);
  195. gamecontroller->SetLogicManager(logicmgr);
  196. gameobj->AddController(gamecontroller);
  197. converter->RegisterGameController(gamecontroller, bcontr);
  198. #ifdef WITH_PYTHON
  199. // When libloading, this is delayed to KX_Scene::MergeScene_LogicBrick to avoid GIL issues
  200. if (!libloading && bcontr->type==CONT_PYTHON) {
  201. SCA_PythonController *pyctrl= static_cast<SCA_PythonController*>(gamecontroller);
  202. /* not strictly needed but gives syntax errors early on and
  203. * gives more predictable performance for larger scripts */
  204. if (pyctrl->m_mode==SCA_PythonController::SCA_PYEXEC_SCRIPT)
  205. pyctrl->Compile();
  206. else {
  207. /* We cant do this because importing runs the script which could end up accessing
  208. * internal BGE functions, this is unstable while we're converting the scene.
  209. * This is a pity because its useful to see errors at startup but cant help it */
  210. // pyctrl->Import();
  211. }
  212. }
  213. #endif // WITH_PYTHON
  214. //done with gamecontroller
  215. gamecontroller->Release();
  216. }
  217. else if (gamecontroller)
  218. gamecontroller->Release();
  219. bcontr = bcontr->next;
  220. }
  221. }