Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <StdInc.h>
- #include "SharedScriptDefs.h"
- #include "CScriptTimerManager.h"
- ScriptVector3::ScriptVector3() : Vector3(), CScriptInstance(SharedScriptDefs::GetModuleManager(), "Vector3", this)
- {
- }
- namespace SharedScriptDefs
- {
- void Vector3Defs::Initialize(CModuleManager* manager)
- {
- SDK::ScriptClass* pVector3Class = manager->CreateClass("Vector3");
- pVector3Class->SetConstructor(Constructor, "??");
- pVector3Class->SetDestructor(Destructor);
- pVector3Class->AddMetaMethod(META_METHOD_ADD, MetaMethod_Add, "x.");
- pVector3Class->AddMetaMethod(META_METHOD_SUB, MetaMethod_Sub, "x.");
- pVector3Class->AddMetaMethod(META_METHOD_MUL, MetaMethod_Mul, "x.");
- pVector3Class->AddMetaMethod(META_METHOD_DIV, MetaMethod_Div, "x.");
- pVector3Class->AddMetaMethod(META_METHOD_SET, MetaMethod_Set, "x.f");
- pVector3Class->AddMetaMethod(META_METHOD_GET, MetaMethod_Get, "x.");
- pVector3Class->AddMetaMethod(META_METHOD_EQU, MetaMethod_Equ, "xx");
- pVector3Class->AddMetaMethod(META_METHOD_CMP, MetaMethod_Cmp, "xx");
- ADD_INSTANCE_FUNCTION(pVector3Class, ToDegrees, "x");
- ADD_INSTANCE_FUNCTION(pVector3Class, Degrees, "x");
- ADD_INSTANCE_FUNCTION(pVector3Class, ToRadians, "x");
- ADD_INSTANCE_FUNCTION(pVector3Class, Radians, "x");
- ADD_INSTANCE_FUNCTION(pVector3Class, IsEmpty, "x");
- ADD_INSTANCE_FUNCTION(pVector3Class, Dot, "xx");
- ADD_INSTANCE_FUNCTION(pVector3Class, Length, "x");
- ADD_INSTANCE_FUNCTION(pVector3Class, Normalize, "x");
- ADD_INSTANCE_FUNCTION(pVector3Class, Normalized, "x");
- ADD_INSTANCE_FUNCTION(pVector3Class, Lerp, "xfx");
- ADD_INSTANCE_FUNCTION(pVector3Class, Normalize360, "x");
- ADD_INSTANCE_FUNCTION(pVector3Class, Normalize180, "x");
- }
- ScriptArgument* Vector3Defs::Constructor(ScriptArguments& arguments)
- {
- if (arguments.Count() == 0)
- RETURN(new ScriptVector3());
- else if (arguments.Count() == 3)
- {
- for (int i = 0; i < 3; i++)
- {
- if (!arguments[i]->IsNumeric())
- throw "expected a numeric type";
- }
- RETURN(new ScriptVector3(TO_FLOAT(0), TO_FLOAT(1), TO_FLOAT(2)));
- }
- else if (arguments.Count() == 1)
- {
- if (arguments[0]->GetType() == ScriptArgument::INSTANCE)
- {
- ScriptVector3* pScriptVec = TO_VECTOR3(0);
- RETURN(new ScriptVector3(pScriptVec->fX, pScriptVec->fY, pScriptVec->fZ));
- }
- else if (arguments[0]->GetType() == ScriptArgument::TABLE || arguments[0]->GetType() == ScriptArgument::ARRAY)
- {
- SDK::ScriptArguments* pArgs = ((arguments[0]->GetType() == ScriptArgument::TABLE) ? arguments[0]->ToTable() : arguments[0]->ToArray());
- if (pArgs->Count() != 3)
- throw "table/array must have 3 elements";
- RETURN(new ScriptVector3(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat()));
- }
- }
- throw "expected 3 floats, a vector3 or a table/array";
- }
- ScriptArgument* Vector3Defs::Destructor(ScriptArguments& arguments)
- {
- delete TO_VECTOR3(0);
- RETURN();
- }
- ScriptArgument* Vector3Defs::MetaMethod_Add(ScriptArguments& arguments)
- {
- ScriptVector3* pVec1 = TO_VECTOR3(0);
- if (arguments[1]->IsNumeric())
- RETURN(new ScriptVector3(*pVec1 + TO_FLOAT(1)));
- else if (arguments[1]->GetType() == ScriptArgument::INSTANCE)
- {
- ScriptVector3* pVec2 = TO_VECTOR3(1);
- RETURN(new ScriptVector3(*pVec1 + *pVec2));
- }
- else if (arguments[1]->GetType() == ScriptArgument::TABLE || arguments[1]->GetType() == ScriptArgument::ARRAY)
- {
- SDK::ScriptArguments* pArgs = ((arguments[1]->GetType() == ScriptArgument::TABLE) ? arguments[1]->ToTable() : arguments[1]->ToArray());
- if (pArgs->Count() != 3)
- throw "table/array must have 3 elements";
- Vector3 vecOther(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat());
- RETURN(new ScriptVector3(*pVec1 + vecOther));
- }
- throw "expected a numeric value or a vector3";
- }
- ScriptArgument* Vector3Defs::MetaMethod_Sub(ScriptArguments& arguments)
- {
- ScriptVector3* pVec1 = TO_VECTOR3(0);
- if (arguments[1]->IsNumeric())
- RETURN(new ScriptVector3(*pVec1 - TO_FLOAT(1)));
- else if (arguments[1]->GetType() == ScriptArgument::INSTANCE)
- {
- ScriptVector3* pVec2 = TO_VECTOR3(1);
- RETURN(new ScriptVector3(*pVec1 - *pVec2));
- }
- else if (arguments[1]->GetType() == ScriptArgument::TABLE || arguments[1]->GetType() == ScriptArgument::ARRAY)
- {
- SDK::ScriptArguments* pArgs = ((arguments[1]->GetType() == ScriptArgument::TABLE) ? arguments[1]->ToTable() : arguments[1]->ToArray());
- if (pArgs->Count() != 3)
- throw "table/array must have 3 elements";
- Vector3 vecOther(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat());
- RETURN(new ScriptVector3(*pVec1 - vecOther));
- }
- throw "expected a numeric value or a vector3";
- }
- ScriptArgument* Vector3Defs::MetaMethod_Mul(ScriptArguments& arguments)
- {
- ScriptVector3* pVec1 = TO_VECTOR3(0);
- if (arguments[1]->IsNumeric())
- RETURN(new ScriptVector3(*pVec1 * TO_FLOAT(1)));
- else if (arguments[1]->GetType() == ScriptArgument::INSTANCE)
- {
- ScriptVector3* pVec2 = TO_VECTOR3(1);
- RETURN(new ScriptVector3(*pVec1 * *pVec2));
- }
- else if (arguments[1]->GetType() == ScriptArgument::TABLE || arguments[1]->GetType() == ScriptArgument::ARRAY)
- {
- SDK::ScriptArguments* pArgs = ((arguments[1]->GetType() == ScriptArgument::TABLE) ? arguments[1]->ToTable() : arguments[1]->ToArray());
- if (pArgs->Count() != 3)
- throw "table/array must have 3 elements";
- Vector3 vecOther(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat());
- RETURN(new ScriptVector3(*pVec1 * vecOther));
- }
- throw "expected a numeric value or a vector3";
- }
- ScriptArgument* Vector3Defs::MetaMethod_Div(ScriptArguments& arguments)
- {
- ScriptVector3* pVec1 = TO_VECTOR3(0);
- if (arguments[1]->IsNumeric())
- RETURN(new ScriptVector3(*pVec1 / TO_FLOAT(1)));
- else if (arguments[1]->GetType() == ScriptArgument::INSTANCE)
- {
- ScriptVector3* pVec2 = TO_VECTOR3(1);
- RETURN(new ScriptVector3(*pVec1 / *pVec2));
- }
- else if (arguments[1]->GetType() == ScriptArgument::TABLE || arguments[1]->GetType() == ScriptArgument::ARRAY)
- {
- SDK::ScriptArguments* pArgs = ((arguments[1]->GetType() == ScriptArgument::TABLE) ? arguments[1]->ToTable() : arguments[1]->ToArray());
- if (pArgs->Count() != 3)
- throw "table/array must have 3 elements";
- Vector3 vecOther(pArgs->Get(0)->ToFloat(), pArgs->Get(1)->ToFloat(), pArgs->Get(2)->ToFloat());
- RETURN(new ScriptVector3(*pVec1 / vecOther));
- }
- throw "expected a numeric value or a vector3";
- }
- ScriptArgument* Vector3Defs::MetaMethod_Set(ScriptArguments& arguments)
- {
- if (arguments[1]->GetType() == ScriptArgument::INTEGER)
- {
- switch (TO_INT(1))
- {
- case 0: TO_VECTOR3(0)->fX = TO_FLOAT(2); break;
- case 1: TO_VECTOR3(0)->fY = TO_FLOAT(2); break;
- case 2: TO_VECTOR3(0)->fZ = TO_FLOAT(2); break;
- default: throw "index does not exist";
- }
- RETURN();
- }
- else if (arguments[1]->GetType() == ScriptArgument::STRING)
- {
- switch (TO_S(1)[0])
- {
- case 'x': TO_VECTOR3(0)->fX = TO_FLOAT(2); break;
- case 'y': TO_VECTOR3(0)->fY = TO_FLOAT(2); break;
- case 'z': TO_VECTOR3(0)->fZ = TO_FLOAT(2); break;
- default: throw "index does not exist";
- }
- RETURN();
- }
- throw "index does not exist";
- }
- ScriptArgument* Vector3Defs::MetaMethod_Get(ScriptArguments& arguments)
- {
- if (arguments[1]->GetType() == ScriptArgument::INTEGER)
- {
- switch (TO_INT(1))
- {
- case 0: RETURN(TO_VECTOR3(0)->fX); break;
- case 1: RETURN(TO_VECTOR3(0)->fY); break;
- case 2: RETURN(TO_VECTOR3(0)->fZ); break;
- default: throw "index does not exist";
- }
- }
- else if (arguments[1]->GetType() == ScriptArgument::STRING)
- {
- switch (TO_S(1)[0])
- {
- case 'x': RETURN(TO_VECTOR3(0)->fX); break;
- case 'y': RETURN(TO_VECTOR3(0)->fY); break;
- case 'z': RETURN(TO_VECTOR3(0)->fZ); break;
- default: throw "index does not exist";
- }
- }
- throw "index does not exist";
- }
- ScriptArgument* Vector3Defs::MetaMethod_Equ(ScriptArguments& arguments)
- {
- ScriptVector3* pVec1 = TO_VECTOR3(0);
- ScriptVector3* pVec2 = TO_VECTOR3(1);
- RETURN(*pVec1 == *pVec2);
- }
- ScriptArgument* Vector3Defs::MetaMethod_Cmp(ScriptArguments& arguments)
- {
- // TODO: Proper compare (return -1 or 0 or 1 like strcmp (as thats what squirrel uses this for internally))
- ScriptVector3* pVec1 = TO_VECTOR3(0);
- ScriptVector3* pVec2 = TO_VECTOR3(1);
- RETURN(*pVec1 == *pVec2); // this needs to return an integer (see above note)
- }
- ScriptArgument* Vector3Defs::ToDegrees(ScriptArguments& arguments)
- {
- ScriptVector3* pVec = TO_VECTOR3(0);
- pVec->ToDegrees();
- RETURN(pVec);
- }
- ScriptArgument* Vector3Defs::Degrees(ScriptArguments& arguments)
- {
- RETURN(new ScriptVector3(TO_VECTOR3(0)->Degrees()));
- }
- ScriptArgument* Vector3Defs::ToRadians(ScriptArguments& arguments)
- {
- ScriptVector3* pVec = TO_VECTOR3(0);
- pVec->ToDegrees();
- RETURN(pVec);
- }
- ScriptArgument* Vector3Defs::Radians(ScriptArguments& arguments)
- {
- RETURN(new ScriptVector3(TO_VECTOR3(0)->Radians()));
- }
- ScriptArgument* Vector3Defs::IsEmpty(ScriptArguments& arguments)
- {
- RETURN(TO_VECTOR3(0)->IsEmpty());
- }
- ScriptArgument* Vector3Defs::Dot(ScriptArguments& arguments)
- {
- RETURN(TO_VECTOR3(0)->Dot(*TO_VECTOR3(1)));
- }
- ScriptArgument* Vector3Defs::Length(ScriptArguments& arguments)
- {
- RETURN(TO_VECTOR3(0)->Length());
- }
- ScriptArgument* Vector3Defs::Normalize(ScriptArguments& arguments)
- {
- ScriptVector3* pVec = TO_VECTOR3(0);
- pVec->Normalize();
- RETURN(pVec);
- }
- ScriptArgument* Vector3Defs::Normalized(ScriptArguments& arguments)
- {
- RETURN(new ScriptVector3(TO_VECTOR3(0)->Normalized()));
- }
- ScriptArgument* Vector3Defs::Lerp(ScriptArguments& arguments)
- {
- RETURN(new ScriptVector3(TO_VECTOR3(0)->Lerp(TO_FLOAT(1), *TO_VECTOR3(2))));
- }
- ScriptArgument* Vector3Defs::Normalize360(ScriptArguments& arguments)
- {
- ScriptVector3* pVec = TO_VECTOR3(0);
- pVec->Normalize360();
- RETURN(pVec);
- }
- ScriptArgument* Vector3Defs::Normalize180(ScriptArguments& arguments)
- {
- ScriptVector3* pVec = TO_VECTOR3(0);
- pVec->Normalize180();
- RETURN(pVec);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement