// getVehicleEngineHealth(vehicleid)
SQInteger CVehicleNatives::GetEngineHealth(SQVM * pVM)
{
    EntityId vehicleId;
    sq_getentity(pVM, -1, &vehicleId);

    CNetworkVehicle * pVehicle = g_pClient->GetVehicleManager()->Get(vehicleId);

    if(pVehicle)
    {
        CLogFile::Printf("Function getVehicleEngineHealth is depreciated: please use getVehicleHealth.");
        sq_pushfloat(pVM, (float)pVehicle->GetHealth());
        return 1;
    }

    sq_pushbool(pVM, false);
    return 1;
}
Example #2
0
// getVehicleFuel( vehicleid );
SQInteger CSharedVehicleNatives::GetFuel( SQVM * pVM )
{
	// Get the vehicle id
	SQInteger vehicleId;
	sq_getinteger( pVM, -1, &vehicleId );

	// Is the vehicle active?
	if( pCore->GetVehicleManager()->IsActive( vehicleId ) )
	{
		// Get the vehicle fuel
		sq_pushfloat ( pVM, pCore->GetVehicleManager()->Get( vehicleId )->GetFuel () );
		return 1;
	}

	sq_pushbool( pVM, false );
	return 1;
}
Example #3
0
/*fa MTR_ScriptsRegisterVariable_d yes */
MTR_DCLSPC bool MTR_CALL MTR_ScriptsRegisterVariable_d(const char *name,
 double value)
{
    if ((name != NULL) && (strcmp(name, "") != 0))
    {
        sq_pushroottable(mtrVm);
        sq_pushstring(mtrVm, name, -1);
        sq_pushfloat(mtrVm, value);
        sq_rawset(mtrVm, -3);
        sq_poptop(mtrVm);
        MTR_LogWrite_s("Script const added:", 3, MTR_LMT_INFO, name);
        return true;
    } else {
        MTR_LogWrite("Script const not added. Incorrect const name:", 3,
         MTR_LMT_ERROR);
    }
    return false;
}
Example #4
0
File: util.cpp Project: kyuu/sphere
 //-----------------------------------------------------------------
 bool RegisterConstants(HSQUIRRELVM v, const Constant* constants, bool areStatic)
 {
     assert(constants);
     for (int i = 0; constants[i].name != 0; i++) {
         sq_pushstring(v, constants[i].name, -1);
         switch (constants[i].value.type) {
             case Variant::VT_STRING:  sq_pushstring(v, constants[i].value.value.s, -1);  break;
             case Variant::VT_INTEGER: sq_pushinteger(v, constants[i].value.value.i);     break;
             case Variant::VT_FLOAT:   sq_pushfloat(v, constants[i].value.value.f);       break;
             default:
                 return false;
         }
         if (!SQ_SUCCEEDED(sq_newslot(v, -3, (areStatic ? SQTrue : SQFalse)))) {
             return false;
         }
     }
     return true;
 }
// getConfig()
SQInteger CServerNatives::GetConfig(SQVM * pVM)
{
	// Create a new table
	sq_newtable(pVM);

	for(std::map<String, SettingsValue *>::iterator iter = CSettings::GetValues()->begin(); iter != CSettings::GetValues()->end(); iter++)
	{
		// Get the setting
		SettingsValue * setting = iter->second;

		// Push the setting name onto the stack
		sq_pushstring(pVM, iter->first.Get(), iter->first.GetLength());

		// Push the value onto the stack
		if(setting->IsBool())
			sq_pushbool(pVM, setting->bValue);
		else if(setting->IsInteger())
			sq_pushinteger(pVM, setting->iValue);
		else if(setting->IsFloat())
			sq_pushfloat(pVM, setting->fValue);
		else if(setting->IsString())
			sq_pushstring(pVM, setting->strValue, setting->strValue.GetLength());
		else if(setting->IsList())
		{
			// Create a new array
			sq_newarray(pVM, 0);

			for(std::list<String>::iterator iter2 = setting->listValue.begin(); iter2 != setting->listValue.end(); iter2++)
			{
				// Push the list value onto the stack
				sq_pushstring(pVM, (*iter2).Get(), (*iter2).GetLength());

				// Create a new array slot
				sq_arrayappend(pVM, -2);
			}
		}

		// Create a new table slot
		sq_createslot(pVM, -3);
	}

	return 1;
}
Example #6
0
SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
{
	SQInteger i=0;
	while(mathlib_funcs[i].name!=0)	{
		sq_pushstring(v,mathlib_funcs[i].name,-1);
		sq_newclosure(v,mathlib_funcs[i].f,0);
		sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
		sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
		sq_createslot(v,-3);
		i++;
	}
	sq_pushstring(v,_SC("RAND_MAX"),-1);
	sq_pushinteger(v,RAND_MAX);
	sq_createslot(v,-3);
	sq_pushstring(v,_SC("PI"),-1);
	sq_pushfloat(v,(SQFloat)M_PI);
	sq_createslot(v,-3);
	return SQ_OK;
}
Example #7
0
/*
 * Call Squirrel function with one float parameter
 * Returns SQTrue if sq_call succeeds.
 */
SQBool callSqFunction_Bool_Float(HSQUIRRELVM v, const SQChar* nname, const SQChar* name, SQFloat value, SQBool defaultValue) {
	SQBool   result = SQFalse;

	SQInteger top = sq_gettop(v);
	sq_pushroottable(v);
	sq_pushstring(v, nname, -1);
	if (SQ_SUCCEEDED(sq_get(v, -2))) {
		sq_pushstring(v, name, -1);
		if(SQ_SUCCEEDED(sq_get(v, -2))) {
			sq_pushroottable(v);
			sq_pushfloat(v, value);
			if (SQ_SUCCEEDED(sq_call(v, 2, SQTrue, SQTrue))) {
				sq_getbool(v, sq_gettop(v), &result);
			}
		}
	}
	sq_settop(v,top);

	return result;
}
Example #8
0
void Sq_DecodeJSONTable(HSQUIRRELVM v, cJSON *Item) {
  if(!Item)
    return;
  while(Item) {
    if(Item->string)
      sq_pushstring(v, Item->string, -1);
    switch(Item->type) {
      case cJSON_False:
        sq_pushbool(v, SQFalse);
        break;
      case cJSON_True:
        sq_pushbool(v, SQTrue);
        break;
      case cJSON_NULL:
        sq_pushnull(v);
        break;
      case cJSON_Number:
        if(Item->valueint == Item->valuedouble)
          sq_pushinteger(v, Item->valueint);
        else
          sq_pushfloat(v, Item->valuedouble);
        break;
      case cJSON_String:
        sq_pushstring(v, Item->valuestring, -1);
        break;
      case cJSON_Array:
        sq_newarray(v, 0);
        Sq_DecodeJSONTable(v, Item->child);
        break;
      case cJSON_Object:
        sq_newtable(v);
        Sq_DecodeJSONTable(v, Item->child);
        break;
    }
    if(Item->string)
      sq_newslot(v,-3,SQFalse);
    else
      sq_arrayappend(v, -2);
    Item = Item->next;
  }
}
//------------------------------------------------------------------------------
void pushValue(HSQUIRRELVM vm, const Variant & v)
{
	switch (v.getType().id)
	{
	case SN_VT_STRING:
		{
			const std::string & s = v.getString();
			sq_pushstring(vm, s.c_str(), s.size());
		}
		break;

	case SN_VT_INT:
		sq_pushinteger(vm, v.getInt());
		break;

	case SN_VT_FLOAT:
		sq_pushfloat(vm, static_cast<float>(v.getFloat()));
		break;

	case SN_VT_DICTIONARY:
		pushTable(vm, v.getDictionary());
		break;

	case SN_VT_ARRAY:
		pushArray(vm, v.getArray());
		break;

	case SN_VT_BOOL:
		sq_pushbool(vm, v.getBool());
		break;

	case SN_VT_NIL:
		sq_pushnull(vm);
		break;

	default:
		// TODO Handle special objects (color, vectors, matrix, instances etc)
		// UNKNOWN
		break;
	}
}
Example #10
0
SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
{
	SQInteger i=0;
	while(mathlib_funcs[i].name!=0)	{
		sq_pushstring(v,mathlib_funcs[i].name,-1);
		sq_newclosure(v,mathlib_funcs[i].f,0);
		sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
		sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
		sq_createslot(v,-3);
		i++;
	}
#ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS
	sq_pushstring(v,"RAND_MAX",-1);
	sq_pushinteger(v,RAND_MAX);
	sq_createslot(v,-3);
#endif /* EXPORT_DEFAULT_SQUIRREL_FUNCTIONS */
	sq_pushstring(v,"PI",-1);
	sq_pushfloat(v,(SQFloat)M_PI);
	sq_createslot(v,-3);
	return SQ_OK;
}
Example #11
0
bool GameWorld::callVariadricSquirrelFunction(const std::string name,
                                              const std::vector<SQParam> &message) {
  HSQUIRRELVM v = _vm.GetVM();
  sq_pushroottable(v);
  sq_pushstring(v,name.c_str(),-1);
  sq_get(v,-2);
  sq_pushroottable(v);
  for (const auto &p : message) {
    switch (p.type) {
      case OT_NULL:
        sq_pushnull(v);
        break;
      case OT_BOOL:
        sq_pushbool(v, p.as_bool);
        break;
      case OT_INTEGER:
        sq_pushinteger(v, p.as_int);
        break;
      case OT_FLOAT:
        sq_pushfloat(v, p.as_float);
        break;
      case OT_STRING:
        sq_pushstring(v, p.as_string, -1);
        break;
      default:
        std::cout<<"Wrong argument";
    }
  }
  SQBool ret = SQFalse;
  sq_call(v, message.size() + 1, SQTrue, SQTrue);
  SQInteger top = sq_gettop(v);
  if (top == 3) {  // Return value, Closure and Roottable
    if (sq_gettype(v, top) == OT_BOOL) {
      sq_getbool(v, top, &ret);
      std::cout<<"Got bool:"<<ret<<std::endl;
    }
  }
  sq_pop(v, top);
  return ret;
}
Example #12
0
SQInteger SFVectorDirection(HSQUIRRELVM v)
{
    SQFloat x1;
    SQFloat y1;
    SQFloat x2;
    SQFloat y2;
    SQFloat dir;
    sq_getfloat(Scripts.vm, 2, &x1);
    sq_getfloat(Scripts.vm, 3, &y1);
    sq_getfloat(Scripts.vm, 4, &x2);
    sq_getfloat(Scripts.vm, 5, &y2);

    dir = acos((x2 - x1) / sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)));

    if (y2 > y1)
    {
        dir = M_PI * 2 - dir;
    }

    sq_pushfloat(Scripts.vm, dir);
    return 1;
}
Example #13
0
/*
 * Call Squirrel function with multiple float parameters, returns boolean
 * Returns default value if sq_call failed.
 */
SQBool callSqFunction_Bool_Floats(HSQUIRRELVM v, const SQChar* nname, const SQChar* name, SQFloat param[], int count, SQBool defaultValue) {
	SQBool   result = defaultValue;

	SQInteger top = sq_gettop(v);
	sq_pushroottable(v);
	sq_pushstring(v, nname, -1);
	if (SQ_SUCCEEDED(sq_get(v, -2))) {
		sq_pushstring(v, name, -1);
		if(SQ_SUCCEEDED(sq_get(v, -2))) {
			sq_pushroottable(v);
			for (int i = 0; i < count; i++) {
				sq_pushfloat(v, param[i]);
			}
			if (SQ_SUCCEEDED(sq_call(v, count + 1, SQTrue, SQTrue))) {
				sq_getbool(v, sq_gettop(v), &result);
			}
		}
	}
	sq_settop(v,top);

	return result;
}
void ScriptBehavior::Update( float deltaTime )
{
    if( !m_start )
    {
        Start();
        m_start = true;
    }

    if( !m_initialize || !m_haveUpdate )
    {
        return;
    }

    int top = sq_gettop(m_vm);

    sq_pushobject(m_vm, m_instance);
    sq_pushobject(m_vm, m_updateFunction);

    sq_push( m_vm,-3 );
    sq_pushfloat( m_vm,deltaTime );
    sq_call( m_vm,2,SQFalse,SQTrue );

    sq_settop( m_vm,top );
}
Example #15
0
BOOL SquirrelObject::SetValue(int key,float f)
{
	_SETVALUE_INT_BEGIN
	sq_pushfloat(m_Vm.GetVMPtr(),f);
	_SETVALUE_INT_END
}
Example #16
0
/*
** Copies values from State src to State dst.
*/
static SQRESULT copy_values_between_vms (HSQUIRRELVM dst, HSQUIRRELVM src, int argc, int argIdx)
{
    SQRESULT _rc_;
    sq_reservestack(dst, argc + 20);
    argc += argIdx; //we will work with argc args starting at argIdx
    for (; argIdx < argc; argIdx++)
    {
        switch (sq_gettype(src, argIdx))
        {
        case OT_INTEGER:
            SQ_GET_INTEGER(src, argIdx, vint);
            sq_pushinteger(dst, vint);
            break;

        case OT_FLOAT:
            SQ_GET_FLOAT(src, argIdx, vfloat);
            sq_pushfloat (dst, vfloat);
            break;

        case OT_BOOL:
            SQ_GET_BOOL(src, argIdx, vbool);
            sq_pushbool (dst, vbool);
            break;

        case OT_STRING:
        {
            SQ_GET_STRING(src, argIdx, vstr)
            sq_pushstring (dst, vstr, vstr_size);
        }
        break;

        case OT_ARRAY:
        {
            SQInteger size = sq_getsize(src, argIdx);
            sq_newarray(dst, size);
            for(SQInteger i=0; i<size; ++i)
            {
                sq_pushinteger(src, i);
                sq_get(src, -2);
                sq_pushinteger(dst, i);
                if(copy_values_between_vms(dst, src, 1, sq_gettop(src)) != SQ_OK) return SQ_ERROR;
                sq_poptop(src);
                sq_set(dst, -3);
            }
        }
        break;

        case OT_TABLE:
        {
            sq_newtable(dst);
            sq_pushnull(src);
            while(sq_next(src, -2) == SQ_OK)
            {
                SQInteger src_top = sq_gettop(src);
                if(copy_values_between_vms(dst, src, 1, src_top-1) != SQ_OK
                        || copy_values_between_vms(dst, src, 1, src_top) != SQ_OK) return SQ_ERROR;
                sq_newslot(dst, -3, SQFalse);
                sq_pop(src, 2);
            }
            sq_pop(src,1);
        }
        break;

        case OT_USERPOINTER:
        {
            SQUserPointer ptr;
            sq_getuserpointer(src, argIdx, &ptr);
            sq_pushuserpointer(dst, ptr);
        }
        break;

        case OT_NULL:
            sq_pushnull(dst);
            break;

        default:
            return SQ_ERROR;
        }
    }
    return SQ_OK;
}
Example #17
0
BOOL SquirrelObject::SetValue(const SQChar *key,float f)
{
	_SETVALUE_STR_BEGIN
	sq_pushfloat(m_Vm.GetVMPtr(),f);
	_SETVALUE_STR_END
}
Example #18
0
static SQInteger _system_clock(HSQUIRRELVM v)
{
    sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC);
    return 1;
}
bool CSquirrelArgument::push(SQVM * pVM)
{
	switch(type)
	{
		case OT_NULL:
			sq_pushnull(pVM);
			break;
		case OT_INTEGER:
			sq_pushinteger(pVM, data.i);
			break;
		case OT_BOOL:
			sq_pushbool(pVM, data.b);
			break;
		case OT_FLOAT:
			sq_pushfloat(pVM, data.f);
			break;
		case OT_STRING:
			sq_pushstring(pVM, data.str->Get(), data.str->GetLength());
			break;
		case OT_ARRAY:
			{
				sq_newarray(pVM, 0);

				for(CSquirrelArguments::iterator iter = data.pArray->begin(); iter != data.pArray->end(); iter++)
				{
					(*iter)->push(pVM);
					sq_arrayappend(pVM, -2);
				}

				break;
			}
		case OT_TABLE:
			{
				assert(data.pArray->size() % 2 == 0);
				sq_newtable(pVM);

				for(CSquirrelArguments::iterator iter = data.pArray->begin(); iter != data.pArray->end(); iter++)
				{
					(*iter)->push(pVM);
					++iter;
					(*iter)->push(pVM);
					sq_createslot(pVM, -3);
				}

				break;
			}
			break;
		case OT_CLOSURE:
		case OT_NATIVECLOSURE:
			sq_pushobject(pVM, data.sqObject);
			break;
		case OT_INSTANCE:
			{
				SQObject obj;
				obj._type = OT_INSTANCE;
				obj._unVal.pInstance = data.pInstance;
				sq_pushobject(pVM, obj);
			}
			break;
		default:
			sq_pushnull(pVM); // whatsoever. do not even care. fix it if you dare.
			assert(0);
			return false;
	}

	return true;
}
Example #20
0
void RegisterVariable(HSQUIRRELVM pVM, const char * szVarName, float fValue)
{
    sq_pushstring(pVM, szVarName, -1);
    sq_pushfloat(pVM, fValue);
    sq_createslot(pVM, -3);
}
Example #21
0
void SquirrelVM::PushParam(SQFloat f)
{
	_CHECK_CALL_STATE
	sq_pushfloat(_VM,f);
	_CallState++;
}
Example #22
0
void CSquirrelVM::Push(const float& f)
{
	sq_pushfloat(m_pVM, f);
}
Example #23
0
void CScripts::Call(RakNet::BitStream * bitStream)
{
	for(int i = 0; i < MAX_SCRIPTS; i++) {
		if(m_pScripts[i]) {

			int iArgCount = 0;
			int funcLen = 0;
			CHAR szFunc[256];
			bitStream->Read(funcLen);
			bitStream->Read(iArgCount);
			bitStream->Read(szFunc, funcLen);
			szFunc[funcLen] = '\0';

			// get the script vm pointer
			SQVM * pVM = m_pScripts[i]->GetVM();

			// Get the stack top
			int iTop = sq_gettop(pVM);

			// Push the root table onto the stack
			sq_pushroottable(pVM);

			// Push the function name onto the stack
			sq_pushstring(pVM, szFunc, -1);

			if(SQ_SUCCEEDED(sq_get(pVM, -2)))
			{
				// Push the root table onto the stack
				sq_pushroottable(pVM);

				if(iArgCount > 0)
				{
					for(int j = 0; i < iArgCount; i++)
					{
						int type;
						bitStream->Read(type);
						if(type == OT_INTEGER)
						{
							int val;
							bitStream->Read(val);
							sq_pushinteger(pVM, val);
						}
						else if(type == OT_FLOAT)
						{
							float val;
							bitStream->Read(val);
							sq_pushfloat(pVM, val);
						}
						else if(type == OT_BOOL)
						{
							bool val;
							bitStream->Read(val);
							sq_pushbool(pVM, val);
						}
						else if(type == OT_STRING)
						{
							int len;
							bitStream->Read(len);
							CHAR szStr[256];
							bitStream->Read(szStr, len);
							szStr[len] = '\0';
							std::string str = szStr;
							sq_pushstring(pVM, str.c_str(), -1);
						}
					}
				}
				sq_call(pVM, iArgCount + 1, true, true);
			}
			// Restore the stack top
			sq_settop(pVM, iTop);
			bitStream->ResetReadPointer();
		}
	}
}
Example #24
0
SQInteger CSystemNatives::Clock(SQVM * pVM)
{
	sq_pushfloat(pVM, (((SQFloat)clock()) / (SQFloat)CLOCKS_PER_SEC));
	return 1;
}
Example #25
0
/*
 * returns min audio volume (always 0)
 */
SQInteger emoGetAudioChannelMinVolume(HSQUIRRELVM v) {
    sq_pushfloat(v, 0);
    return 1;
}