/** * @brief Subtracts two vectors or a vector and some cartesian coordinates. * * If x is a vector it subtracts both vectors, otherwise it subtracts cartesian * coordinates to the vector. * * @usage my_vec = my_vec - your_vec * @usage my_vec:sub( your_vec ) * @usage my_vec:sub( 5, 3 ) * * @luaparam v Vector getting stuff subtracted from. * @luaparam x X coordinate or vector to subtract. * @luaparam y Y coordinate or nil to subtract. * @luareturn The result of the vector operation. * @luafunc sub( v, x, y ) */ static int vectorL_sub( lua_State *L ) { LuaVector vout, *v1, *v2; double x, y; /* Get self. */ v1 = luaL_checkvector(L,1); /* Get rest of parameters. */ v2 = NULL; if (lua_isvector(L,2)) { v2 = lua_tovector(L,2); x = v2->vec.x; y = v2->vec.y; } else if ((lua_gettop(L) > 2) && lua_isnumber(L,2) && lua_isnumber(L,3)) { x = lua_tonumber(L,2); y = lua_tonumber(L,3); } else NLUA_INVALID_PARAMETER(L); /* Actually add it */ vect_cset( &vout.vec, v1->vec.x - x, v1->vec.y - y ); lua_pushvector( L, vout ); return 1; }
static int vectorL_sub__( lua_State *L ) { Vector2d *v1, *v2; double x, y; /* Get self. */ v1 = luaL_checkvector(L,1); /* Get rest of parameters. */ v2 = NULL; if (lua_isvector(L,2)) { v2 = lua_tovector(L,2); x = v2->x; y = v2->y; } else if ((lua_gettop(L) > 2) && lua_isnumber(L,2) && lua_isnumber(L,3)) { x = lua_tonumber(L,2); y = lua_tonumber(L,3); } else { NLUA_INVALID_PARAMETER(L); return 0; } /* Actually add it */ vect_cset( v1, v1->x - x, v1->y - y ); lua_pushvector( L, *v1 ); return 1; }
/** * @brief Gets vector at index making sure type is valid. * * @param L Lua state to get vector from. * @param ind Index position of vector. * @return The LuaVector at ind. */ LuaVector* luaL_checkvector( lua_State *L, int ind ) { if (lua_isvector(L,ind)) return (LuaVector*) lua_touserdata(L,ind); luaL_typerror(L, ind, VECTOR_METATABLE); return NULL; }
struct model *luaL_checkmodel(lua_State *L, int index) { int n, i; struct mesh *group; vector offset; struct model *m; n = lua_objlen(L, index); if (n <= 0) { luaL_error(L, "invalid table length at index %d", index); } m = model_create(NULL); for (i=0; i<n; i++) { lua_getfieldi(L, index, i+1); group = luaL_checkmesh(L, -1); lua_getfield(L, -1, "offset"); if (lua_isvector(L, -1)) lua_tovector(L, -1, &offset); else offset.x = offset.y = offset.z = 0.0f; lua_pop(L, 2); model_addgroup(m, group, &offset); mesh_destroy(group); } return m; }
static int l_atom_setcolor(lua_State* L) { Atom* a = lua_toatom(L, 1); if(!a) return 0; if(lua_istable(L, 2)) { for(int i=0; i<4; i++) { double c = 1.0; lua_pushinteger(L, i+1); lua_gettable(L, 2); if(lua_isnumber(L, -1)) c = lua_tonumber(L, -1); lua_pop(L, 1); a->color.setComponent(i, c); } return 0; } if(lua_isvector(L, 2)) { Vector v; lua_makevector(L, 2, v); a->color = v; return 0; } for(int i=0; i<4; i++) { double c = 1.0; if(lua_isnumber(L, i+2)) c = lua_tonumber(L, i+2); a->color.setComponent(i, c); } return 0; }