Line data Source code
1 : /**
2 : * @file api_terrain.cpp
3 : * @brief Krig Game Engine Lua Scripting API - Terrain
4 : *
5 : * Terrain API functionality.
6 : */
7 : #include "api.h"
8 : #include "api_terrain.h"
9 : #include "GameLevel.h"
10 :
11 : #if DOXYGEN_ONLY
12 : /**
13 : * Get height of terrain at specified x,z grid coordinates.
14 : * @param int x coordinate.
15 : * @param int z coordinate.
16 : * @return float
17 : */
18 : float get_vertex_height(int, int);
19 : #endif
20 0 : static int get_vertex_height(lua_State *L) {
21 0 : int x = lua_tonumber(L, 1);
22 0 : int z = lua_tonumber(L, 2);
23 :
24 0 : float height = (float)g_KRIG_ENGINE.getCurrentLevel()->getTerrain()->getVertexHeight(x, z);
25 :
26 0 : lua_pushnumber(L, height);
27 0 : return 1;
28 : }
29 :
30 : #if DOXYGEN_ONLY
31 : /**
32 : * Set height of terrain at specified x,z grid coordinates.
33 : * @param int x coordinate.
34 : * @param int z coordinate.
35 : * @param float height
36 : * @return n/a.
37 : */
38 : void set_vertex_height(int, int, float);
39 : #endif
40 0 : static int set_vertex_height(lua_State *L) {
41 0 : int x = lua_tonumber(L, 1);
42 0 : int z = lua_tonumber(L, 2);
43 0 : float height = lua_tonumber(L, 3);
44 :
45 0 : g_KRIG_ENGINE.getCurrentLevel()->getTerrain()->setVertexHeight(x, z, height);
46 0 : return 0;
47 : }
48 :
49 : #if DOXYGEN_ONLY
50 : /**
51 : * Get color of terrain at specified x,z grid coordinates.
52 : * @param int x coordinate.
53 : * @param int z coordinate.
54 : * @return Vector3
55 : */
56 : Vector3 get_vertex_color(int, int);
57 : #endif
58 0 : static int get_vertex_color(lua_State *L) {
59 0 : int x = lua_tonumber(L, 1);
60 0 : int z = lua_tonumber(L, 2);
61 :
62 0 : Vector color = g_KRIG_ENGINE.getCurrentLevel()->getTerrain()->getVertexColor(x, z);
63 :
64 0 : returnVector(L, color);
65 0 : return 1;
66 : }
67 :
68 : #if DOXYGEN_ONLY
69 : /**
70 : * Set type of terrain at specified x,z grid coordinates.
71 : * @param int x coordinate.
72 : * @param int z coordinate.
73 : * @param Vector3 containing r,g,b values.
74 : * @return n/a.
75 : */
76 : void set_vertex_color(int, int, Vector3);
77 : #endif
78 0 : static int set_vertex_color(lua_State *L) {
79 0 : int x = lua_tonumber(L, 1);
80 0 : int z = lua_tonumber(L, 2);
81 0 : int index = 3;
82 0 : Vector color = loadVector(L, index);
83 :
84 0 : g_KRIG_ENGINE.getCurrentLevel()->getTerrain()->setVertexColor(x, z, color);
85 0 : return 0;
86 : }
87 :
88 : #if DOXYGEN_ONLY
89 : /**
90 : * Get type of terrain at specified x,z grid coordinates.
91 : * @param int x coordinate.
92 : * @param int z coordinate.
93 : * @return float - type.
94 : */
95 : float get_vertex_type(int, int);
96 : #endif
97 0 : static int get_vertex_type(lua_State *L) {
98 0 : int x = lua_tonumber(L, 1);
99 0 : int z = lua_tonumber(L, 2);
100 :
101 0 : float type = g_KRIG_ENGINE.getCurrentLevel()->getTerrain()->getVertexType(x, z);
102 :
103 0 : lua_pushnumber(L, type);
104 0 : return 1;
105 : }
106 :
107 : #if DOXYGEN_ONLY
108 : /**
109 : * Set type of terrain at specified x,z grid coordinates.
110 : * @param int x coordinate.
111 : * @param int z coordinate.
112 : * @param int type.
113 : * @return n/a.
114 : */
115 : void set_vertex_type(int, int, int);
116 : #endif
117 0 : static int set_vertex_type(lua_State *L) {
118 0 : int x = lua_tonumber(L, 1);
119 0 : int z = lua_tonumber(L, 2);
120 0 : int type = lua_tonumber(L,3);
121 :
122 0 : g_KRIG_ENGINE.getCurrentLevel()->getTerrain()->setVertexType(x, z, type);
123 0 : return 0;
124 : }
125 :
126 : #if DOXYGEN_ONLY
127 : /**
128 : * Get height of terrain at specified x,z world coordinates.
129 : * @param float x coordinate.
130 : * @param float z coordinate.
131 : * @return float - height.
132 : */
133 : float get_height(float, float);
134 : #endif
135 0 : static int get_height(lua_State *L) {
136 0 : float x = lua_tonumber(L, 1);
137 0 : float z = lua_tonumber(L, 2);
138 :
139 0 : float height = (float)g_KRIG_ENGINE.getCurrentLevel()->getTerrain()->getHeight(x, z);
140 :
141 0 : lua_pushnumber(L, height);
142 0 : return 1;
143 : }
144 :
145 : static const luaL_Reg krigTerrainLib[] = {
146 : {"get_height", get_height},
147 : {"get_vertex_color", get_vertex_color},
148 : {"get_vertex_height", get_vertex_height},
149 : {"get_vertex_type", get_vertex_type},
150 : {"set_vertex_color", set_vertex_color},
151 : {"set_vertex_height", set_vertex_height},
152 : {"set_vertex_type", set_vertex_type},
153 : {NULL, NULL}
154 : };
155 :
156 9 : int luaopen_krigTerrain (lua_State *L) {
157 9 : luaL_openlib(L, "krig.terrain", krigTerrainLib, 0);
158 9 : return 1;
159 : }
|