RigsofRods
Soft-body Physics Simulation
ProceduralRoadAngelscript.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2022 Petr Ohlidal
4 
5  For more information, see http://www.rigsofrods.org/
6 
7  Rigs of Rods is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License version 3, as
9  published by the Free Software Foundation.
10 
11  Rigs of Rods is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
22 
23 #include "ProceduralManager.h"
24 #include "ProceduralRoad.h"
25 #include "ScriptEngine.h"
26 
27 using namespace RoR;
28 using namespace AngelScript;
29 
31 {
32  return new ProceduralPoint();
33 }
34 
36 {
37  return new ProceduralObject();
38 }
39 
41 {
42  return new ProceduralRoad();
43 }
44 
45 void RoR::RegisterProceduralRoad(asIScriptEngine* engine)
46 {
47  int result = 0;
48 
49  // enum RoadType
50  result = engine->RegisterEnum("RoadType"); ROR_ASSERT(result >= 0);
51  result = engine->RegisterEnumValue("RoadType", "ROAD_AUTOMATIC", (int)RoadType::ROAD_AUTOMATIC); ROR_ASSERT(result >= 0);
52  result = engine->RegisterEnumValue("RoadType", "ROAD_FLAT", (int)RoadType::ROAD_FLAT); ROR_ASSERT(result >= 0);
53  result = engine->RegisterEnumValue("RoadType", "ROAD_LEFT", (int)RoadType::ROAD_LEFT); ROR_ASSERT(result >= 0);
54  result = engine->RegisterEnumValue("RoadType", "ROAD_RIGHT", (int)RoadType::ROAD_RIGHT); ROR_ASSERT(result >= 0);
55  result = engine->RegisterEnumValue("RoadType", "ROAD_BOTH", (int)RoadType::ROAD_BOTH); ROR_ASSERT(result >= 0);
56  result = engine->RegisterEnumValue("RoadType", "ROAD_BRIDGE", (int)RoadType::ROAD_BRIDGE); ROR_ASSERT(result >= 0);
57  result = engine->RegisterEnumValue("RoadType", "ROAD_MONORAIL", (int)RoadType::ROAD_MONORAIL); ROR_ASSERT(result >= 0);
58 
59  // enum TextureFit
60  result = engine->RegisterEnum("TextureFit"); ROR_ASSERT(result >= 0);
61  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_NONE", (int)TextureFit::TEXFIT_NONE); ROR_ASSERT(result >= 0);
62  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_BRICKWALL", (int)TextureFit::TEXFIT_BRICKWALL); ROR_ASSERT(result >= 0);
63  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_ROADS1", (int)TextureFit::TEXFIT_ROADS1); ROR_ASSERT(result >= 0);
64  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_ROADS2", (int)TextureFit::TEXFIT_ROADS2); ROR_ASSERT(result >= 0);
65  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_ROAD", (int)TextureFit::TEXFIT_ROAD); ROR_ASSERT(result >= 0);
66  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_ROADS3", (int)TextureFit::TEXFIT_ROADS3); ROR_ASSERT(result >= 0);
67  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_ROADS4", (int)TextureFit::TEXFIT_ROADS4); ROR_ASSERT(result >= 0);
68  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_CONCRETEWALL", (int)TextureFit::TEXFIT_CONCRETEWALL); ROR_ASSERT(result >= 0);
69  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_CONCRETEWALLI", (int)TextureFit::TEXFIT_CONCRETEWALLI); ROR_ASSERT(result >= 0);
70  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_CONCRETETOP", (int)TextureFit::TEXFIT_CONCRETETOP); ROR_ASSERT(result >= 0);
71  result = engine->RegisterEnumValue("TextureFit", "TEXFIT_CONCRETEUNDER", (int)TextureFit::TEXFIT_CONCRETEUNDER); ROR_ASSERT(result >= 0);
72 
73  // struct ProceduralPoint (ref)
74  // NOTE: Using property-accessors because `offsetof()` cannot be used with derived classes (see https://stackoverflow.com/q/1129894)
75  // NOTE: Using lambdas to define the property-accessor functions because #lazy.
76  ProceduralPoint::RegisterRefCountingObject(engine, "ProceduralPointClass");
77  ProceduralPointPtr::RegisterRefCountingObjectPtr(engine, "ProceduralPointClassPtr", "ProceduralPointClass");
78  result = engine->RegisterObjectBehaviour("ProceduralPointClass", asBEHAVE_FACTORY, "ProceduralPointClass@+ f()", asFUNCTION(ProceduralPointFactory), asCALL_CDECL); ROR_ASSERT(result >= 0);
79  //get (note: for compound data types like vector3 we must return non-const references so that expressions like `ppoint.position.y = 100.f` still compile and work):
80  result = engine->RegisterObjectMethod("ProceduralPointClass", "vector3& get_position() property", asFUNCTIONPR([](ProceduralPoint* self) -> Ogre::Vector3& { return self->position; }, (ProceduralPoint*), Ogre::Vector3&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
81  result = engine->RegisterObjectMethod("ProceduralPointClass", "quaternion& get_rotation() property", asFUNCTIONPR([](ProceduralPoint* self) -> Ogre::Quaternion& { return self->rotation; }, (ProceduralPoint*), Ogre::Quaternion&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
82  result = engine->RegisterObjectMethod("ProceduralPointClass", "float get_width() property", asFUNCTIONPR([](ProceduralPoint* self) { return self->width; }, (ProceduralPoint*), float), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
83  result = engine->RegisterObjectMethod("ProceduralPointClass", "float get_border_width() property", asFUNCTIONPR([](ProceduralPoint* self) { return self->bwidth; }, (ProceduralPoint*), float), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
84  result = engine->RegisterObjectMethod("ProceduralPointClass", "float get_border_height() property", asFUNCTIONPR([](ProceduralPoint* self) { return self->bheight; }, (ProceduralPoint*), float), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
85  result = engine->RegisterObjectMethod("ProceduralPointClass", "RoadType get_type() property", asFUNCTIONPR([](ProceduralPoint* self) { return self->type; }, (ProceduralPoint*), RoadType), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
86  result = engine->RegisterObjectMethod("ProceduralPointClass", "int get_pillar_type() property", asFUNCTIONPR([](ProceduralPoint* self) { return self->pillartype; }, (ProceduralPoint*), int), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
87  //set:
88  result = engine->RegisterObjectMethod("ProceduralPointClass", "void set_position(const vector3& in pos) property", asFUNCTIONPR([](ProceduralPoint* self, const Ogre::Vector3& pos) { self->position = pos; }, (ProceduralPoint*, const Ogre::Vector3&), void), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
89  result = engine->RegisterObjectMethod("ProceduralPointClass", "void set_rotation(const quaternion& in rot) property", asFUNCTIONPR([](ProceduralPoint* self, const Ogre::Quaternion& rot) { self->rotation = rot; }, (ProceduralPoint*, const Ogre::Quaternion&), void), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
90  result = engine->RegisterObjectMethod("ProceduralPointClass", "void set_width(float width) property", asFUNCTIONPR([](ProceduralPoint* self, float width) { self->width = width; }, (ProceduralPoint*, float), void), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
91  result = engine->RegisterObjectMethod("ProceduralPointClass", "void set_border_width(float bwidth) property", asFUNCTIONPR([](ProceduralPoint* self, float bwidth) { self->bwidth = bwidth; }, (ProceduralPoint*, float), void), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
92  result = engine->RegisterObjectMethod("ProceduralPointClass", "void set_border_height(float bheight) property", asFUNCTIONPR([](ProceduralPoint* self, float bheight) { self->bheight = bheight; }, (ProceduralPoint*, float), void), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
93  result = engine->RegisterObjectMethod("ProceduralPointClass", "void set_type(RoadType type) property", asFUNCTIONPR([](ProceduralPoint* self, RoadType type) { self->type = type; }, (ProceduralPoint*, RoadType), void), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
94  result = engine->RegisterObjectMethod("ProceduralPointClass", "void set_pillar_type(int type) property", asFUNCTIONPR([](ProceduralPoint* self, int type) { self->pillartype = type; }, (ProceduralPoint*, int), void), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
95 
96  // class ProceduralRoad (ref)
97  ProceduralRoad::RegisterRefCountingObject(engine, "ProceduralRoadClass");
98  ProceduralRoadPtr::RegisterRefCountingObjectPtr(engine, "ProceduralRoadClassPtr", "ProceduralRoadClass");
99  result = engine->RegisterObjectBehaviour("ProceduralRoadClass", asBEHAVE_FACTORY, "ProceduralRoadClass@+ f()", asFUNCTION(ProceduralRoadFactory), asCALL_CDECL); ROR_ASSERT(result >= 0);
100  result = engine->RegisterObjectMethod("ProceduralRoadClass", "void addBlock(vector3 pos, quaternion rot, RoadType type, float width, float border_width, float border_height, int pillar_type = 1)", asMETHOD(RoR::ProceduralRoad, addBlock), asCALL_THISCALL); ROR_ASSERT(result >= 0);
101  result = engine->RegisterObjectMethod("ProceduralRoadClass", "void addQuad(vector3 p1, vector3 p2, vector3 p3, vector3 p4, TextureFit texfit, vector3 pos, vector3 lastpos, float width, bool flip = false)", asMETHOD(RoR::ProceduralRoad, addQuad), asCALL_THISCALL); ROR_ASSERT(result >= 0);
102  result = engine->RegisterObjectMethod("ProceduralRoadClass", "void addCollisionQuad(vector3 p1, vector3 p2, vector3 p3, vector3 p4, const string&in gm_name, bool flip = false)", asMETHODPR(RoR::ProceduralRoad, addCollisionQuad, (Ogre::Vector3, Ogre::Vector3, Ogre::Vector3, Ogre::Vector3, std::string const&, bool), void), asCALL_THISCALL); ROR_ASSERT(result >= 0);
103  result = engine->RegisterObjectMethod("ProceduralRoadClass", "void createMesh()", asMETHOD(RoR::ProceduralRoad, createMesh), asCALL_THISCALL); ROR_ASSERT(result >= 0);
104  result = engine->RegisterObjectMethod("ProceduralRoadClass", "void finish()", asMETHOD(RoR::ProceduralRoad, finish), asCALL_THISCALL); ROR_ASSERT(result >= 0);
105  result = engine->RegisterObjectMethod("ProceduralRoadClass", "void setCollisionEnabled(bool v)", asMETHOD(RoR::ProceduralRoad, setCollisionEnabled), asCALL_THISCALL); ROR_ASSERT(result >= 0);
106 
107  // class ProceduralObject (ref)
108  ProceduralObject::RegisterRefCountingObject(engine, "ProceduralObjectClass");
109  ProceduralObjectPtr::RegisterRefCountingObjectPtr(engine, "ProceduralObjectClassPtr", "ProceduralObjectClass");
110  result = engine->RegisterObjectBehaviour("ProceduralObjectClass", asBEHAVE_FACTORY, "ProceduralObjectClass@+ f()", asFUNCTION(ProceduralObjectFactory), asCALL_CDECL); ROR_ASSERT(result >= 0);
111  result = engine->RegisterObjectMethod("ProceduralObjectClass", "string getName()", asMETHOD(RoR::ProceduralObject, getName), asCALL_THISCALL); ROR_ASSERT(result >= 0);
112  result = engine->RegisterObjectMethod("ProceduralObjectClass", "void setName(const string&in)", asMETHOD(RoR::ProceduralObject, setName), asCALL_THISCALL); ROR_ASSERT(result >= 0);
113  result = engine->RegisterObjectMethod("ProceduralObjectClass", "void addPoint(ProceduralPointClassPtr @)", asMETHOD(RoR::ProceduralObject, addPoint), asCALL_THISCALL); ROR_ASSERT(result >= 0);
114  result = engine->RegisterObjectMethod("ProceduralObjectClass", "void insertPoint(int pos, ProceduralPointClassPtr @)", asMETHOD(RoR::ProceduralObject, insertPoint), asCALL_THISCALL); ROR_ASSERT(result >= 0);
115  result = engine->RegisterObjectMethod("ProceduralObjectClass", "void deletePoint(int pos)", asMETHOD(RoR::ProceduralObject, deletePoint), asCALL_THISCALL); ROR_ASSERT(result >= 0);
116  result = engine->RegisterObjectMethod("ProceduralObjectClass", "ProceduralPointClassPtr @getPoint(int pos)", asMETHOD(RoR::ProceduralObject, getPoint), asCALL_THISCALL); ROR_ASSERT(result >= 0);
117  result = engine->RegisterObjectMethod("ProceduralObjectClass", "int getNumPoints()", asMETHOD(RoR::ProceduralObject, getNumPoints), asCALL_THISCALL); ROR_ASSERT(result >= 0);
118  result = engine->RegisterObjectMethod("ProceduralObjectClass", "ProceduralRoadClassPtr @getRoad()", asMETHOD(ProceduralObject, getRoad), asCALL_THISCALL); ROR_ASSERT(result >= 0);
119  result = engine->RegisterObjectMethod("ProceduralObjectClass", "int get_smoothing_num_splits() property", asFUNCTIONPR([](ProceduralObject* self) { return self->smoothing_num_splits; },(ProceduralObject*),int ), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
120  result = engine->RegisterObjectMethod("ProceduralObjectClass", "void set_smoothing_num_splits(int) property", asFUNCTIONPR([](ProceduralObject* self, int n) { self->smoothing_num_splits = n; }, (ProceduralObject*, int), void), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result >= 0);
121 
122  // class ProceduralManager (ref)
123  ProceduralManager::RegisterRefCountingObject(engine, "ProceduralManagerClass");
124  ProceduralManagerPtr::RegisterRefCountingObjectPtr(engine, "ProceduralManagerClassPtr", "ProceduralManagerClass");
125  result = engine->RegisterObjectMethod("ProceduralManagerClass", "void addObject(ProceduralObjectClassPtr@)", asMETHOD(ProceduralManager, addObject), asCALL_THISCALL); ROR_ASSERT(result>=0);
126  result = engine->RegisterObjectMethod("ProceduralManagerClass", "void removeObject(ProceduralObjectClassPtr@)", asMETHOD(ProceduralManager, removeObject), asCALL_THISCALL); ROR_ASSERT(result >= 0);
127  result = engine->RegisterObjectMethod("ProceduralManagerClass", "int getNumObjects()", asMETHOD(RoR::ProceduralManager, getNumObjects), asCALL_THISCALL); ROR_ASSERT(result >= 0);
128  result = engine->RegisterObjectMethod("ProceduralManagerClass", "ProceduralObjectClassPtr @getObject(int pos)", asMETHOD(ProceduralManager, getObject), asCALL_THISCALL); ROR_ASSERT(result >= 0);
129 }
ROR_ASSERT
#define ROR_ASSERT(_EXPR)
Definition: Application.h:40
RoR::RoadType::ROAD_BOTH
@ ROAD_BOTH
RefCountingObjectPtr::RegisterRefCountingObjectPtr
static void RegisterRefCountingObjectPtr(AS_NAMESPACE_QUALIFIER asIScriptEngine *engine, const char *handle_name, const char *obj_name)
Definition: RefCountingObjectPtr.h:84
RoR::TextureFit::TEXFIT_CONCRETEUNDER
@ TEXFIT_CONCRETEUNDER
RoR::RoadType::ROAD_AUTOMATIC
@ ROAD_AUTOMATIC
RefCountingObject< ProceduralPoint >::RegisterRefCountingObject
static void RegisterRefCountingObject(AS_NAMESPACE_QUALIFIER asIScriptEngine *engine, const char *name)
Definition: RefCountingObject.h:65
RoR::TextureFit::TEXFIT_CONCRETEWALL
@ TEXFIT_CONCRETEWALL
RoR::TextureFit::TEXFIT_ROADS1
@ TEXFIT_ROADS1
RoR::ProceduralManager
Definition: ProceduralManager.h:79
RoR::TextureFit::TEXFIT_BRICKWALL
@ TEXFIT_BRICKWALL
RoR::RegisterProceduralRoad
void RegisterProceduralRoad(AngelScript::asIScriptEngine *engine)
defined in ProceduralRoadAngelscript.cpp
ProceduralRoad.h
RoR::ProceduralObject
Definition: ProceduralManager.h:59
RoR::TextureFit::TEXFIT_CONCRETEWALLI
@ TEXFIT_CONCRETEWALLI
RoR::RoadType
RoadType
Definition: ProceduralRoad.h:33
ProceduralManager.h
ScriptEngine.h
RoR::RoadType::ROAD_BRIDGE
@ ROAD_BRIDGE
RoR::TextureFit::TEXFIT_ROADS2
@ TEXFIT_ROADS2
RoR::TextureFit::TEXFIT_ROAD
@ TEXFIT_ROAD
RoR::RoadType::ROAD_FLAT
@ ROAD_FLAT
RoR::RoadType::ROAD_MONORAIL
@ ROAD_MONORAIL
RoR::RoadType::ROAD_RIGHT
@ ROAD_RIGHT
RoR::ProceduralRoad
Definition: ProceduralRoad.h:60
RoR::TextureFit::TEXFIT_NONE
@ TEXFIT_NONE
RoR::RoadType::ROAD_LEFT
@ ROAD_LEFT
RoR::TextureFit::TEXFIT_CONCRETETOP
@ TEXFIT_CONCRETETOP
RoR::ProceduralPoint
Definition: ProceduralManager.h:34
RoR::TextureFit::TEXFIT_ROADS3
@ TEXFIT_ROADS3
RoR::TextureFit::TEXFIT_ROADS4
@ TEXFIT_ROADS4
ProceduralObjectFactory
static ProceduralObject * ProceduralObjectFactory()
Definition: ProceduralRoadAngelscript.cpp:35
ProceduralRoadFactory
static ProceduralRoad * ProceduralRoadFactory()
Definition: ProceduralRoadAngelscript.cpp:40
RoR
Definition: AppContext.h:36
ProceduralPointFactory
static ProceduralPoint * ProceduralPointFactory()
Definition: ProceduralRoadAngelscript.cpp:30