Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.19 KB | None | 0 0
  1. #include <StdInc.h>
  2. #include "SharedScriptDefs.h"
  3. #include "CScriptTimerManager.h"
  4.  
  5. ScriptVector3::ScriptVector3() : Vector3(), CScriptInstance(SharedScriptDefs::GetModuleManager(), "Vector3", this)
  6. {
  7.  
  8. }
  9.  
  10. namespace SharedScriptDefs
  11. {
  12. void Vector3Defs::Initialize(CModuleManager* manager)
  13. {
  14. SDK::ScriptClass* pVector3Class = manager->CreateClass("Vector3");
  15.  
  16. pVector3Class->SetConstructor(Constructor, "??");
  17. pVector3Class->SetDestructor(Destructor);
  18.  
  19. pVector3Class->AddMetaMethod(META_METHOD_ADD, MetaMethod_Add, "x.");
  20. pVector3Class->AddMetaMethod(META_METHOD_SUB, MetaMethod_Sub, "x.");
  21. pVector3Class->AddMetaMethod(META_METHOD_MUL, MetaMethod_Mul, "x.");
  22. pVector3Class->AddMetaMethod(META_METHOD_DIV, MetaMethod_Div, "x.");
  23. pVector3Class->AddMetaMethod(META_METHOD_SET, MetaMethod_Set, "x.f");
  24. pVector3Class->AddMetaMethod(META_METHOD_GET, MetaMethod_Get, "x.");
  25. pVector3Class->AddMetaMethod(META_METHOD_EQU, MetaMethod_Equ, "xx");
  26. pVector3Class->AddMetaMethod(META_METHOD_CMP, MetaMethod_Cmp, "xx");
  27.  
  28. ADD_INSTANCE_FUNCTION(pVector3Class, ToDegrees, "x");
  29. ADD_INSTANCE_FUNCTION(pVector3Class, Degrees, "x");
  30. ADD_INSTANCE_FUNCTION(pVector3Class, ToRadians, "x");
  31. ADD_INSTANCE_FUNCTION(pVector3Class, Radians, "x");
  32. ADD_INSTANCE_FUNCTION(pVector3Class, IsEmpty, "x");
  33. ADD_INSTANCE_FUNCTION(pVector3Class, Dot, "xx");
  34. ADD_INSTANCE_FUNCTION(pVector3Class, Length, "x");
  35. ADD_INSTANCE_FUNCTION(pVector3Class, Normalize, "x");
  36. ADD_INSTANCE_FUNCTION(pVector3Class, Normalized, "x");
  37. ADD_INSTANCE_FUNCTION(pVector3Class, Lerp, "xfx");
  38. ADD_INSTANCE_FUNCTION(pVector3Class, Normalize360, "x");
  39. ADD_INSTANCE_FUNCTION(pVector3Class, Normalize180, "x");
  40. }
  41.  
  42. ScriptArgument* Vector3Defs::Constructor(ScriptArguments& arguments)
  43. {
  44. if (arguments.Count() == 0)
  45. RETURN(new ScriptVector3());
  46. else if (arguments.Count() == 3)
  47. {
  48. for (int i = 0; i < 3; i++)
  49. {
  50. if (!arguments[i]->IsNumeric())
  51. throw "expected a numeric type";
  52. }
  53.  
  54. RETURN(new ScriptVector3(TO_FLOAT(0), TO_FLOAT(1), TO_FLOAT(2)));
  55. }
  56. else if (arguments.Count() == 1)
  57. {
  58. if (arguments[0]->GetType() == ScriptArgument::INSTANCE)
  59. {
  60. ScriptVector3* pScriptVec = TO_VECTOR3(0);
  61. RETURN(new ScriptVector3(pScriptVec->fX, pScriptVec->fY, pScriptVec->fZ));
  62. }
  63. else if (arguments[0]->GetType() == ScriptArgument::TABLE || arguments[0]->GetType() == ScriptArgument::ARRAY)
  64. {
  65. SDK::ScriptArguments* pArgs = ((arguments[0]->GetType() == ScriptArgument::TABLE) ? arguments[0]->ToTable() : arguments[0]->ToArray());
  66.  
  67. if (pArgs->Count() != 3)
  68. throw "table/array must have 3 elements";
  69.  
  70. RETURN(new ScriptVector3(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat()));
  71. }
  72. }
  73.  
  74. throw "expected 3 floats, a vector3 or a table/array";
  75. }
  76.  
  77. ScriptArgument* Vector3Defs::Destructor(ScriptArguments& arguments)
  78. {
  79. delete TO_VECTOR3(0);
  80. RETURN();
  81. }
  82.  
  83. ScriptArgument* Vector3Defs::MetaMethod_Add(ScriptArguments& arguments)
  84. {
  85. ScriptVector3* pVec1 = TO_VECTOR3(0);
  86.  
  87. if (arguments[1]->IsNumeric())
  88. RETURN(new ScriptVector3(*pVec1 + TO_FLOAT(1)));
  89. else if (arguments[1]->GetType() == ScriptArgument::INSTANCE)
  90. {
  91. ScriptVector3* pVec2 = TO_VECTOR3(1);
  92. RETURN(new ScriptVector3(*pVec1 + *pVec2));
  93. }
  94. else if (arguments[1]->GetType() == ScriptArgument::TABLE || arguments[1]->GetType() == ScriptArgument::ARRAY)
  95. {
  96. SDK::ScriptArguments* pArgs = ((arguments[1]->GetType() == ScriptArgument::TABLE) ? arguments[1]->ToTable() : arguments[1]->ToArray());
  97.  
  98. if (pArgs->Count() != 3)
  99. throw "table/array must have 3 elements";
  100.  
  101. Vector3 vecOther(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat());
  102. RETURN(new ScriptVector3(*pVec1 + vecOther));
  103. }
  104.  
  105. throw "expected a numeric value or a vector3";
  106. }
  107.  
  108. ScriptArgument* Vector3Defs::MetaMethod_Sub(ScriptArguments& arguments)
  109. {
  110. ScriptVector3* pVec1 = TO_VECTOR3(0);
  111.  
  112. if (arguments[1]->IsNumeric())
  113. RETURN(new ScriptVector3(*pVec1 - TO_FLOAT(1)));
  114. else if (arguments[1]->GetType() == ScriptArgument::INSTANCE)
  115. {
  116. ScriptVector3* pVec2 = TO_VECTOR3(1);
  117. RETURN(new ScriptVector3(*pVec1 - *pVec2));
  118. }
  119. else if (arguments[1]->GetType() == ScriptArgument::TABLE || arguments[1]->GetType() == ScriptArgument::ARRAY)
  120. {
  121. SDK::ScriptArguments* pArgs = ((arguments[1]->GetType() == ScriptArgument::TABLE) ? arguments[1]->ToTable() : arguments[1]->ToArray());
  122.  
  123. if (pArgs->Count() != 3)
  124. throw "table/array must have 3 elements";
  125.  
  126. Vector3 vecOther(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat());
  127. RETURN(new ScriptVector3(*pVec1 - vecOther));
  128. }
  129.  
  130. throw "expected a numeric value or a vector3";
  131. }
  132.  
  133. ScriptArgument* Vector3Defs::MetaMethod_Mul(ScriptArguments& arguments)
  134. {
  135. ScriptVector3* pVec1 = TO_VECTOR3(0);
  136.  
  137. if (arguments[1]->IsNumeric())
  138. RETURN(new ScriptVector3(*pVec1 * TO_FLOAT(1)));
  139. else if (arguments[1]->GetType() == ScriptArgument::INSTANCE)
  140. {
  141. ScriptVector3* pVec2 = TO_VECTOR3(1);
  142. RETURN(new ScriptVector3(*pVec1 * *pVec2));
  143. }
  144. else if (arguments[1]->GetType() == ScriptArgument::TABLE || arguments[1]->GetType() == ScriptArgument::ARRAY)
  145. {
  146. SDK::ScriptArguments* pArgs = ((arguments[1]->GetType() == ScriptArgument::TABLE) ? arguments[1]->ToTable() : arguments[1]->ToArray());
  147.  
  148. if (pArgs->Count() != 3)
  149. throw "table/array must have 3 elements";
  150.  
  151. Vector3 vecOther(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat());
  152. RETURN(new ScriptVector3(*pVec1 * vecOther));
  153. }
  154.  
  155. throw "expected a numeric value or a vector3";
  156. }
  157.  
  158. ScriptArgument* Vector3Defs::MetaMethod_Div(ScriptArguments& arguments)
  159. {
  160. ScriptVector3* pVec1 = TO_VECTOR3(0);
  161.  
  162. if (arguments[1]->IsNumeric())
  163. RETURN(new ScriptVector3(*pVec1 / TO_FLOAT(1)));
  164. else if (arguments[1]->GetType() == ScriptArgument::INSTANCE)
  165. {
  166. ScriptVector3* pVec2 = TO_VECTOR3(1);
  167. RETURN(new ScriptVector3(*pVec1 / *pVec2));
  168. }
  169. else if (arguments[1]->GetType() == ScriptArgument::TABLE || arguments[1]->GetType() == ScriptArgument::ARRAY)
  170. {
  171. SDK::ScriptArguments* pArgs = ((arguments[1]->GetType() == ScriptArgument::TABLE) ? arguments[1]->ToTable() : arguments[1]->ToArray());
  172.  
  173. if (pArgs->Count() != 3)
  174. throw "table/array must have 3 elements";
  175.  
  176. Vector3 vecOther(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat());
  177. RETURN(new ScriptVector3(*pVec1 / vecOther));
  178. }
  179.  
  180. throw "expected a numeric value or a vector3";
  181. }
  182.  
  183. ScriptArgument* Vector3Defs::MetaMethod_Set(ScriptArguments& arguments)
  184. {
  185. if (arguments[1]->GetType() == ScriptArgument::INTEGER)
  186. {
  187. switch (TO_INT(1))
  188. {
  189. case 0: TO_VECTOR3(0)->fX = TO_FLOAT(2); break;
  190. case 1: TO_VECTOR3(0)->fY = TO_FLOAT(2); break;
  191. case 2: TO_VECTOR3(0)->fZ = TO_FLOAT(2); break;
  192. default: throw "index does not exist";
  193. }
  194.  
  195. RETURN();
  196. }
  197. else if (arguments[1]->GetType() == ScriptArgument::STRING)
  198. {
  199. switch (TO_S(1)[0])
  200. {
  201. case 'x': TO_VECTOR3(0)->fX = TO_FLOAT(2); break;
  202. case 'y': TO_VECTOR3(0)->fY = TO_FLOAT(2); break;
  203. case 'z': TO_VECTOR3(0)->fZ = TO_FLOAT(2); break;
  204. default: throw "index does not exist";
  205. }
  206.  
  207. RETURN();
  208. }
  209.  
  210. throw "index does not exist";
  211. }
  212.  
  213. ScriptArgument* Vector3Defs::MetaMethod_Get(ScriptArguments& arguments)
  214. {
  215. if (arguments[1]->GetType() == ScriptArgument::INTEGER)
  216. {
  217. switch (TO_INT(1))
  218. {
  219. case 0: RETURN(TO_VECTOR3(0)->fX); break;
  220. case 1: RETURN(TO_VECTOR3(0)->fY); break;
  221. case 2: RETURN(TO_VECTOR3(0)->fZ); break;
  222. default: throw "index does not exist";
  223. }
  224. }
  225. else if (arguments[1]->GetType() == ScriptArgument::STRING)
  226. {
  227. switch (TO_S(1)[0])
  228. {
  229. case 'x': RETURN(TO_VECTOR3(0)->fX); break;
  230. case 'y': RETURN(TO_VECTOR3(0)->fY); break;
  231. case 'z': RETURN(TO_VECTOR3(0)->fZ); break;
  232. default: throw "index does not exist";
  233. }
  234. }
  235.  
  236. throw "index does not exist";
  237. }
  238.  
  239. ScriptArgument* Vector3Defs::MetaMethod_Equ(ScriptArguments& arguments)
  240. {
  241. ScriptVector3* pVec1 = TO_VECTOR3(0);
  242. ScriptVector3* pVec2 = TO_VECTOR3(1);
  243. RETURN(*pVec1 == *pVec2);
  244. }
  245.  
  246. ScriptArgument* Vector3Defs::MetaMethod_Cmp(ScriptArguments& arguments)
  247. {
  248. // TODO: Proper compare (return -1 or 0 or 1 like strcmp (as thats what squirrel uses this for internally))
  249. ScriptVector3* pVec1 = TO_VECTOR3(0);
  250. ScriptVector3* pVec2 = TO_VECTOR3(1);
  251. RETURN(*pVec1 == *pVec2); // this needs to return an integer (see above note)
  252. }
  253.  
  254. ScriptArgument* Vector3Defs::ToDegrees(ScriptArguments& arguments)
  255. {
  256. ScriptVector3* pVec = TO_VECTOR3(0);
  257. pVec->ToDegrees();
  258. RETURN(pVec);
  259. }
  260.  
  261. ScriptArgument* Vector3Defs::Degrees(ScriptArguments& arguments)
  262. {
  263. RETURN(new ScriptVector3(TO_VECTOR3(0)->Degrees()));
  264. }
  265.  
  266. ScriptArgument* Vector3Defs::ToRadians(ScriptArguments& arguments)
  267. {
  268. ScriptVector3* pVec = TO_VECTOR3(0);
  269. pVec->ToDegrees();
  270. RETURN(pVec);
  271. }
  272.  
  273. ScriptArgument* Vector3Defs::Radians(ScriptArguments& arguments)
  274. {
  275. RETURN(new ScriptVector3(TO_VECTOR3(0)->Radians()));
  276. }
  277.  
  278. ScriptArgument* Vector3Defs::IsEmpty(ScriptArguments& arguments)
  279. {
  280. RETURN(TO_VECTOR3(0)->IsEmpty());
  281. }
  282.  
  283. ScriptArgument* Vector3Defs::Dot(ScriptArguments& arguments)
  284. {
  285. RETURN(TO_VECTOR3(0)->Dot(*TO_VECTOR3(1)));
  286. }
  287.  
  288. ScriptArgument* Vector3Defs::Length(ScriptArguments& arguments)
  289. {
  290. RETURN(TO_VECTOR3(0)->Length());
  291. }
  292.  
  293. ScriptArgument* Vector3Defs::Normalize(ScriptArguments& arguments)
  294. {
  295. ScriptVector3* pVec = TO_VECTOR3(0);
  296. pVec->Normalize();
  297. RETURN(pVec);
  298. }
  299.  
  300. ScriptArgument* Vector3Defs::Normalized(ScriptArguments& arguments)
  301. {
  302. RETURN(new ScriptVector3(TO_VECTOR3(0)->Normalized()));
  303. }
  304.  
  305. ScriptArgument* Vector3Defs::Lerp(ScriptArguments& arguments)
  306. {
  307. RETURN(new ScriptVector3(TO_VECTOR3(0)->Lerp(TO_FLOAT(1), *TO_VECTOR3(2))));
  308. }
  309.  
  310. ScriptArgument* Vector3Defs::Normalize360(ScriptArguments& arguments)
  311. {
  312. ScriptVector3* pVec = TO_VECTOR3(0);
  313. pVec->Normalize360();
  314. RETURN(pVec);
  315. }
  316.  
  317. ScriptArgument* Vector3Defs::Normalize180(ScriptArguments& arguments)
  318. {
  319. ScriptVector3* pVec = TO_VECTOR3(0);
  320. pVec->Normalize180();
  321. RETURN(pVec);
  322. }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement