Ejemplo n.º 1
0
static int math_pow (lua_State *L) {
  lua_Number x = luaL_checknumber(L, 1);
  lua_Number y = luaL_checknumber(L, 2);
  lua_pushnumber(L, l_mathop(pow)(x, y));
  return 1;
}
Ejemplo n.º 2
0
static int math_frexp (lua_State *L) {
  int e;
  lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
  lua_pushinteger(L, e);
  return 2;
}
Ejemplo n.º 3
0
static int math_tanh (lua_State *L) {
  lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
  return 1;
}
Ejemplo n.º 4
0
static int math_sqrt (lua_State *L) {
  lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
  return 1;
}
Ejemplo n.º 5
0
static int math_log10 (lua_State *L) {
  lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
  return 1;
}
Ejemplo n.º 6
0
ToolCapabilities read_tool_capabilities(
		lua_State *L, int table)
{
	ToolCapabilities toolcap;
	getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
	getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
	lua_getfield(L, table, "groupcaps");
	if(lua_istable(L, -1)){
		int table_groupcaps = lua_gettop(L);
		lua_pushnil(L);
		while(lua_next(L, table_groupcaps) != 0){
			// key at index -2 and value at index -1
			std::string groupname = luaL_checkstring(L, -2);
			if(lua_istable(L, -1)){
				int table_groupcap = lua_gettop(L);
				// This will be created
				ToolGroupCap groupcap;
				// Read simple parameters
				getintfield(L, table_groupcap, "maxlevel", groupcap.maxlevel);
				getintfield(L, table_groupcap, "uses", groupcap.uses);
				// DEPRECATED: maxwear
				float maxwear = 0;
				if(getfloatfield(L, table_groupcap, "maxwear", maxwear)){
					if(maxwear != 0)
						groupcap.uses = 1.0/maxwear;
					else
						groupcap.uses = 0;
					infostream<<script_get_backtrace(L)<<std::endl;
					infostream<<"WARNING: field \"maxwear\" is deprecated; "
							<<"should replace with uses=1/maxwear"<<std::endl;
				}
				// Read "times" table
				lua_getfield(L, table_groupcap, "times");
				if(lua_istable(L, -1)){
					int table_times = lua_gettop(L);
					lua_pushnil(L);
					while(lua_next(L, table_times) != 0){
						// key at index -2 and value at index -1
						int rating = luaL_checkinteger(L, -2);
						float time = luaL_checknumber(L, -1);
						groupcap.times[rating] = time;
						// removes value, keeps key for next iteration
						lua_pop(L, 1);
					}
				}
				lua_pop(L, 1);
				// Insert groupcap into toolcap
				toolcap.groupcaps[groupname] = groupcap;
			}
			// removes value, keeps key for next iteration
			lua_pop(L, 1);
		}
	}
	lua_pop(L, 1);

	lua_getfield(L, table, "damage_groups");
	if(lua_istable(L, -1)){
		int table_damage_groups = lua_gettop(L);
		lua_pushnil(L);
		while(lua_next(L, table_damage_groups) != 0){
			// key at index -2 and value at index -1
			std::string groupname = luaL_checkstring(L, -2);
			u16 value = luaL_checkinteger(L, -1);
			toolcap.damageGroups[groupname] = value;
			// removes value, keeps key for next iteration
			lua_pop(L, 1);
		}
	}
	lua_pop(L, 1);
	return toolcap;
}
Ejemplo n.º 7
0
/**\brief Create a new Planet
 */
int Planets_Lua::NewPlanet(lua_State* L){
	int i,n = lua_gettop(L);  // Number of arguments

	// Temporary intermediatary variables
	string imageName, techName;
	Technology* tech;

	// The new planet object and vital attributes
	Planet *p;
	string _name;
	float _x;
	float _y;
	Image* _image;
	string _allianceName;
	Alliance* _alliance;
	bool _landable;
	int _traffic;
	int _militiaSize;
	int _sphereOfInfluence;
	list<Technology*> _technologies;

	if(n<9) {
		return luaL_error(L, "Got %d arguments expected at least 9 (name, x, y, image_name, alliance, landable, traffic, militia, influence, [techs...])", n);
	}

	// Capture the required arguments
	_name = (string)luaL_checkstring(L,1);
	_x = luaL_checknumber(L, 2 );
	_y = luaL_checknumber(L, 3 );
	imageName = (string)luaL_checkstring(L,4);
	_image = Image::Get( imageName );
	_allianceName = (string)luaL_checkstring(L,5);
	_alliance = Alliances::Instance()->GetAlliance(_allianceName);
	_landable = (bool)luaL_checkint(L, 6 );
	_traffic = luaL_checkint(L, 7 );
	_militiaSize = luaL_checkint(L, 8 );
	_sphereOfInfluence = luaL_checkint(L, 9 );

	// Capture all other arguments as technologies
	for(i=10;i<=n;i++) {
		techName = (string)luaL_checkstring(L, 10 );
		tech = Technologies::Instance()->GetTechnology( techName );
		if(tech==NULL) {
			return luaL_error(L, "No Technology '%s'", techName.c_str());
		}
		_technologies.push_back( tech );
	}

	// Check that all pointers are non-NULL.
	if(_image==NULL) {
		return luaL_error(L, "No image '%s'", imageName.c_str());
	}
	if(_alliance==NULL) {
		return luaL_error(L, "No alliance '%s'", _allianceName.c_str());
	}

	// Create the Planet
	p = new Planet(
		_name,
		_x,
		_y,
		_image,
		_alliance,
		_landable,
		_traffic,
		_militiaSize,
		_sphereOfInfluence,
		_technologies
		);

	// Save the Planet!
	Image::Store(_name,_image);
	SpriteManager::Instance()->Add((Sprite*)p);
	Planets::Instance()->AddOrReplace((Component*)p);
    Simulation_Lua::PushSprite(L,p);
	return 1;
}
Ejemplo n.º 8
0
static int math_cos (lua_State *L) {
  lua_pushnumber(L, cos(TORAD(luaL_checknumber(L, 1))));
  return 1;
}
Ejemplo n.º 9
0
static int math_acos (lua_State *L) {
  lua_pushnumber(L, FROMRAD(acos(luaL_checknumber(L, 1))));
  return 1;
}
Ejemplo n.º 10
0
static int math_atan (lua_State *L) {
  lua_Number y = luaL_checknumber(L, 1);
  lua_Number x = luaL_optnumber(L, 2, 1);
  lua_pushnumber(L, l_mathop(atan2)(y, x));
  return 1;
}
Ejemplo n.º 11
0
int vm_hw_lua_cpu_disassemble(lua_State* L)
{
	vm_t* vm = vm_hw_lua_cpu_extract_cpu(L, 1);
	struct inst inst = vm_disassemble(vm, (uint16_t)luaL_checknumber(L, 2), true);
	lua_newtable(L);
	lua_newtable(L);
	lua_pushnumber(L, inst.original.full);
	lua_setfield(L, -2, "full");
	lua_pushnumber(L, inst.original.op);
	lua_setfield(L, -2, "op");
	lua_pushnumber(L, inst.original.a);
	lua_setfield(L, -2, "a");
	lua_pushnumber(L, inst.original.b);
	lua_setfield(L, -2, "b");
	lua_setfield(L, -2, "original");
	lua_newtable(L);
	if (inst.pretty.op != NULL)
		lua_pushstring(L, inst.pretty.op->data);
	else
		lua_pushnil(L);
	lua_setfield(L, -2, "op");
	if (inst.pretty.a != NULL)
		lua_pushstring(L, inst.pretty.a->data);
	else
		lua_pushnil(L);
	lua_setfield(L, -2, "a");
	if (inst.pretty.b != NULL)
		lua_pushstring(L, inst.pretty.b->data);
	else
		lua_pushnil(L);
	lua_setfield(L, -2, "b");
	lua_setfield(L, -2, "pretty");
	lua_pushnumber(L, inst.op);
	lua_setfield(L, -2, "op");
	lua_pushnumber(L, inst.a);
	lua_setfield(L, -2, "a");
	lua_pushnumber(L, inst.b);
	lua_setfield(L, -2, "b");
	lua_pushnumber(L, inst.size);
	lua_setfield(L, -2, "size");
	lua_newtable(L);
	if (inst.size >= 1)
	{
		lua_pushnumber(L, inst.extra[0]);
		lua_rawseti(L, -2, 1);
	}
	if (inst.size >= 2)
	{
		lua_pushnumber(L, inst.extra[1]);
		lua_rawseti(L, -2, 2);
	}
	lua_setfield(L, -2, "extra");
	lua_newtable(L);
	if (inst.used[0])
		lua_pushnumber(L, inst.next[0]);
	else
		lua_pushnil(L);
	lua_rawseti(L, -2, 1);
	if (inst.used[1])
		lua_pushnumber(L, inst.next[1]);
	else
		lua_pushnil(L);
	lua_rawseti(L, -2, 2);
	lua_setfield(L, -2, "next");
	bdestroy(inst.pretty.op);
	bdestroy(inst.pretty.a);
	bdestroy(inst.pretty.b);
	return 1;
}
Ejemplo n.º 12
0
static int math_acos (lua_State *L) {
  lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
  return 1;
}
Ejemplo n.º 13
0
static int math_tan (lua_State *L) {
  lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
  return 1;
}
Ejemplo n.º 14
0
static int math_ldexp (lua_State *L) {
  lua_Number x = luaL_checknumber(L, 1);
  int ep = (int)luaL_checkinteger(L, 2);
  lua_pushnumber(L, l_mathop(ldexp)(x, ep));
  return 1;
}
Ejemplo n.º 15
0
static int l_edit_set_cursor(lua_State* L) {
	int row = luaL_checknumber(L, 1);
	int col = luaL_checknumber(L, 2);
	edit_set_cursor(row, col);
	return 0;
}
Ejemplo n.º 16
0
static int math_atan2 (lua_State *L) {
  lua_pushnumber(L, FROMRAD(atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))));
  return 1;
}
Ejemplo n.º 17
0
void read_object_properties(lua_State *L, int index,
		ObjectProperties *prop)
{
	if(index < 0)
		index = lua_gettop(L) + 1 + index;
	if(!lua_istable(L, index))
		return;

	prop->hp_max = getintfield_default(L, -1, "hp_max", 10);

	getboolfield(L, -1, "physical", prop->physical);
	getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);

	getfloatfield(L, -1, "weight", prop->weight);

	lua_getfield(L, -1, "collisionbox");
	if(lua_istable(L, -1))
		prop->collisionbox = read_aabb3f(L, -1, 1.0);
	lua_pop(L, 1);

	getstringfield(L, -1, "visual", prop->visual);

	getstringfield(L, -1, "mesh", prop->mesh);

	lua_getfield(L, -1, "visual_size");
	if(lua_istable(L, -1))
		prop->visual_size = read_v2f(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, -1, "textures");
	if(lua_istable(L, -1)){
		prop->textures.clear();
		int table = lua_gettop(L);
		lua_pushnil(L);
		while(lua_next(L, table) != 0){
			// key at index -2 and value at index -1
			if(lua_isstring(L, -1))
				prop->textures.push_back(lua_tostring(L, -1));
			else
				prop->textures.push_back("");
			// removes value, keeps key for next iteration
			lua_pop(L, 1);
		}
	}
	lua_pop(L, 1);

	lua_getfield(L, -1, "colors");
	if(lua_istable(L, -1)){
		prop->colors.clear();
		int table = lua_gettop(L);
		lua_pushnil(L);
		while(lua_next(L, table) != 0){
			// key at index -2 and value at index -1
			if(lua_isstring(L, -1))
				prop->colors.push_back(readARGB8(L, -1));
			else
				prop->colors.push_back(video::SColor(255, 255, 255, 255));
			// removes value, keeps key for next iteration
			lua_pop(L, 1);
		}
	}
	lua_pop(L, 1);

	lua_getfield(L, -1, "spritediv");
	if(lua_istable(L, -1))
		prop->spritediv = read_v2s16(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, -1, "initial_sprite_basepos");
	if(lua_istable(L, -1))
		prop->initial_sprite_basepos = read_v2s16(L, -1);
	lua_pop(L, 1);

	getboolfield(L, -1, "is_visible", prop->is_visible);
	getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
	getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
	getfloatfield(L, -1, "stepheight", prop->stepheight);
	prop->stepheight*=BS;
	lua_getfield(L, -1, "automatic_face_movement_dir");
	if (lua_isnumber(L, -1)) {
		prop->automatic_face_movement_dir = true;
		prop->automatic_face_movement_dir_offset = luaL_checknumber(L, -1);
	} else if (lua_isboolean(L, -1)) {
		prop->automatic_face_movement_dir = lua_toboolean(L, -1);
		prop->automatic_face_movement_dir_offset = 0.0;
	}
	lua_pop(L, 1);
}
Ejemplo n.º 18
0
static int math_ceil (lua_State *L) {
  lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
  return 1;
}
Ejemplo n.º 19
0
static int str_format (lua_State *L) {
  int arg = 1;
  size_t sfl;
  const char *strfrmt = luaL_checklstring(L, arg, &sfl);
  const char *strfrmt_end = strfrmt+sfl;
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  while (strfrmt < strfrmt_end) {
    if (*strfrmt != '%')
      luaL_putchar(&b, *strfrmt++);
    else if (*++strfrmt == '%')
      luaL_putchar(&b, *strfrmt++);  /* %% */
    else { /* format item */
      char form[MAX_FORMAT];  /* to store the format (`%...') */
      char buff[MAX_ITEM];  /* to store the formatted item */
      int hasprecision = 0;
      if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == '$')
        return luaL_error(L, "obsolete option (d$) to `format'");
      arg++;
      strfrmt = scanformat(L, strfrmt, form, &hasprecision);
      switch (*strfrmt++) {
        case 'c':  case 'd':  case 'i': {
          sprintf(buff, form, luaL_checkint(L, arg));
          break;
        }
        case 'o':  case 'u':  case 'x':  case 'X': {
          sprintf(buff, form, (unsigned int)(luaL_checknumber(L, arg)));
          break;
        }
        case 'e':  case 'E': case 'f':
        case 'g': case 'G': {
          sprintf(buff, form, luaL_checknumber(L, arg));
          break;
        }
        case 'q': {
          luaI_addquoted(L, &b, arg);
          continue;  /* skip the `addsize' at the end */
        }
        case 's': {
          size_t l;
          const char *s = luaL_checklstring(L, arg, &l);
          if (!hasprecision && l >= 100) {
            /* no precision and string is too long to be formatted;
               keep original string */
            lua_pushvalue(L, arg);
            luaL_addvalue(&b);
            continue;  /* skip the `addsize' at the end */
          }
          else {
            sprintf(buff, form, s);
            break;
          }
        }
        default: {  /* also treat cases `pnLlh' */
          return luaL_error(L, "invalid option to `format'");
        }
      }
      luaL_addlstring(&b, buff, strlen(buff));
    }
  }
  luaL_pushresult(&b);
  return 1;
}
Ejemplo n.º 20
0
static int math_floor (lua_State *L) {
  lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
  return 1;
}
Ejemplo n.º 21
0
static int os_difftime (lua_State *L) {
  lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
                             (time_t)(luaL_optnumber(L, 2, 0))));
  return 1;
}
Ejemplo n.º 22
0
/** Set the preferred size for client icons.
 *
 * The closest equal or bigger size is picked if present, otherwise the closest
 * smaller size is picked. The default is 0 pixels, ie. the smallest icon.
 *
 * @param size The size of the icons in pixels.
 * @function set_preferred_icon_size
 */
static int
luaA_set_preferred_icon_size(lua_State *L)
{
    globalconf.preferred_icon_size = luaL_checknumber(L, 1);
    return 0;
}
Ejemplo n.º 23
0
static int math_pow (lua_State *L) {
  lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
  return 1;
}
Ejemplo n.º 24
0
static int make_lua_call(lua_State *L, mrp_funcbridge_t *fb, int f)
{
#define ARG_MAX 256

    int ret;
    int i, n, m, b, e;
    const char *s;
    char t;
    mrp_funcbridge_value_t args[ARG_MAX];
    mrp_funcbridge_value_t *a, r;

    e = lua_gettop(L);
    f = (f < 0) ? e + f + 1 : f;
    b = f + 1;
    n = e - b + 1;

    switch (fb->type) {

    case MRP_C_FUNCTION:
        m = strlen(fb->c.signature);

        if (n >= ARG_MAX - 1 || n > m)
            return luaL_error(L, "too many arguments");
        if (n < m)
            return luaL_error(L, "too few arguments");

        for (i = b, s = fb->c.signature, a= args;    i <= e;    i++, s++, a++){
            switch (*s) {
            case MRP_FUNCBRIDGE_STRING:
                a->string = luaL_checklstring(L, i, NULL);
                break;
            case MRP_FUNCBRIDGE_INTEGER:
                a->integer = luaL_checkinteger(L, i);
                break;
            case MRP_FUNCBRIDGE_FLOATING:
                a->floating = luaL_checknumber(L, i);
                break;
            case MRP_FUNCBRIDGE_OBJECT:
                a->pointer = mrp_lua_check_object(L, NULL, i);
                break;
            default:
                return luaL_error(L, "argument %d has unsupported type '%c'",
                                  (i - b) + 1, i);
            }
        }
        memset(a, 0, sizeof(*a));

        if (!fb->c.func(L, fb->c.data, fb->c.signature, args, &t, &r))
            return luaL_error(L, "c function invocation failed");

        switch (t) {
        case MRP_FUNCBRIDGE_NO_DATA:
            ret = 0;
            break;
        case MRP_FUNCBRIDGE_STRING:
            ret = 1;
            lua_pushstring(L, r.string);
            break;
        case MRP_FUNCBRIDGE_INTEGER:
            ret = 1;
            lua_pushinteger(L, r.integer);
            break;
        case MRP_FUNCBRIDGE_FLOATING:
            ret = 1;
            lua_pushnumber(L, r.floating);
            break;
        default:
            ret = 0;
            lua_pushnil(L);
        }
        break;

    case MRP_LUA_FUNCTION:
        lua_rawgeti(L, f, 1);
        luaL_checktype(L, -1, LUA_TFUNCTION);
        lua_replace(L, f);
        lua_pcall(L, n, 1, 0);
        ret = 1;
        break;

    default:
        return luaL_error(L, "internal error");
    }

    return ret;

#undef ARG_MAX
}
Ejemplo n.º 25
0
static int math_rad (lua_State *L) {
  lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
  return 1;
}
Ejemplo n.º 26
0
int LuaPacket::SetReadPosition(lua_State* L)
{
    uint32_t position = static_cast<uint32_t>(luaL_checknumber(L, 1));
    rpos(position);
    return 0;
}
Ejemplo n.º 27
0
static int math_ldexp (lua_State *L) {
  lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
  return 1;
}
Ejemplo n.º 28
0
static int lua_Curve_evaluate(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 3:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    lua_type(state, 2) == LUA_TNUMBER &&
                    (lua_type(state, 3) == LUA_TTABLE || lua_type(state, 3) == LUA_TLIGHTUSERDATA))
                {
                    // Get parameter 1 off the stack.
                    float param1 = (float)luaL_checknumber(state, 2);

                    // Get parameter 2 off the stack.
                    gameplay::ScriptUtil::LuaArray<float> param2 = gameplay::ScriptUtil::getFloatPointer(3);

                    Curve* instance = getInstance(state);
                    instance->evaluate(param1, param2);
                    
                    return 0;
                }
            } while (0);

            lua_pushstring(state, "lua_Curve_evaluate - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        case 6:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    lua_type(state, 2) == LUA_TNUMBER &&
                    lua_type(state, 3) == LUA_TNUMBER &&
                    lua_type(state, 4) == LUA_TNUMBER &&
                    lua_type(state, 5) == LUA_TNUMBER &&
                    (lua_type(state, 6) == LUA_TTABLE || lua_type(state, 6) == LUA_TLIGHTUSERDATA))
                {
                    // Get parameter 1 off the stack.
                    float param1 = (float)luaL_checknumber(state, 2);

                    // Get parameter 2 off the stack.
                    float param2 = (float)luaL_checknumber(state, 3);

                    // Get parameter 3 off the stack.
                    float param3 = (float)luaL_checknumber(state, 4);

                    // Get parameter 4 off the stack.
                    float param4 = (float)luaL_checknumber(state, 5);

                    // Get parameter 5 off the stack.
                    gameplay::ScriptUtil::LuaArray<float> param5 = gameplay::ScriptUtil::getFloatPointer(6);

                    Curve* instance = getInstance(state);
                    instance->evaluate(param1, param2, param3, param4, param5);
                    
                    return 0;
                }
            } while (0);

            lua_pushstring(state, "lua_Curve_evaluate - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 3 or 6).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Ejemplo n.º 29
0
static int math_asin (lua_State *L) {
  lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
  return 1;
}
Ejemplo n.º 30
0
static int math_sinh (lua_State *L) {
  lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
  return 1;
}