UnitDef *CUnitDefHandler::GetUnitByID(int id)
{	
	if(!unitDefs[id].loaded)
		ParseUnit(unitDefs[id].filename, id);

	return &unitDefs[id];
}
Пример #2
0
int LuaSyncedMoveCtrl::Disable(lua_State* L)
{
	CUnit* unit = ParseUnit(L, __FUNCTION__, 1);
	if (unit == NULL) {
		return 0;
	}
	unit->DisableScriptMoveType();
	return 0;
}
Пример #3
0
int LuaSyncedMoveCtrl::IsEnabled(lua_State* L)
{
	CUnit* unit = ParseUnit(L, __FUNCTION__, 1);
	if (unit == NULL) {
		return 0;
	}
	lua_pushboolean(L, unit->usingScriptMoveType);
	return 1;
}
Пример #4
0
int CLuaRules::CreateUnitRulesParams(lua_State* L)
{
	CUnit* unit = ParseUnit(L, __FUNCTION__, 1);
	if (unit == NULL) {
		return 0;
	}
	CreateRulesParams(L, __FUNCTION__, 1, unit->modParams, unit->modParamsMap);
	return 0;
}
Пример #5
0
static inline MoveType* ParseMoveType(lua_State* L,
					      const char* caller, int index)
{
	CUnit* unit = ParseUnit(L, caller, index);
	if (unit == NULL) {
		return NULL;
	}

	MoveType* mt = dynamic_cast<MoveType*>(unit->moveType);
	return mt;
}
static inline CScriptMoveType* ParseScriptMoveType(lua_State* L, const char* caller, int index)
{
	CUnit* unit = ParseUnit(L, caller, index);

	if (unit == nullptr)
		return nullptr;
	if (!unit->UsingScriptMoveType())
		return nullptr;

	return (static_cast<CScriptMoveType*>(unit->moveType));
}
Пример #7
0
static inline CScriptMoveType* ParseMoveType(lua_State* L,
                                             const char* caller, int index)
{
	CUnit* unit = ParseUnit(L, caller, index);

	if (unit == NULL)
		return NULL;
	if (!unit->UsingScriptMoveType())
		return NULL;

	return static_cast<CScriptMoveType*>(unit->moveType);
}
static inline DerivedMoveType* ParseDerivedMoveType(lua_State* L, const char* caller, int index)
{
	CUnit* unit = ParseUnit(L, caller, index);

	if (unit == nullptr)
		return nullptr;

	if (unit->moveType == nullptr)
		return nullptr;

	return (dynamic_cast<DerivedMoveType*>(unit->moveType));
}
int LuaSyncedMoveCtrl::SetMoveDef(lua_State* L)
{
	CUnit* unit = ParseUnit(L, __FUNCTION__, 1);
	MoveDef* moveDef = nullptr;

	if (unit == nullptr) {
		lua_pushboolean(L, false);
		return 1;
	}
	if (unit->moveDef == nullptr) {
		// aircraft or structure, not supported
		lua_pushboolean(L, false);
		return 1;
	}

	// MoveType instance must already have been assigned
	assert(unit->moveType != nullptr);

	// parse a MoveDef by number *or* by string (mutually exclusive)
	if (lua_isnumber(L, 2))
		moveDef = moveDefHandler->GetMoveDefByPathType(Clamp(luaL_checkint(L, 2), 0, int(moveDefHandler->GetNumMoveDefs()) - 1));
	if (lua_isstring(L, 2))
		moveDef = moveDefHandler->GetMoveDefByName(lua_tostring(L, 2));

	if (moveDef == nullptr) {
		lua_pushboolean(L, false);
		return 1;
	}

	if (moveDef->udRefCount == 0) {
		// pathfinders contain optimizations that
		// make unreferenced movedef's non-usable
		LOG_L(L_ERROR, "SetMoveDef: Tried to use an unreferenced (:=disabled) MoveDef!");
		lua_pushboolean(L, false);
		return 1;
	}

	// PFS might have cached data by path-type which must be cleared
	if (unit->UsingScriptMoveType()) {
		unit->prevMoveType->StopMoving();
	} else {
		unit->moveType->StopMoving();
	}

	// the case where moveDef->pathType == unit->moveDef->pathType does no harm
	// note: if a unit (ID) is available, then its current MoveDef should always
	// be taken over the MoveDef corresponding to its UnitDef::pathType wherever
	// MoveDef properties are used in decision logic
	lua_pushboolean(L, (unit->moveDef = moveDef) != NULL);
	return 1;
}
UnitDef *CUnitDefHandler::GetUnitByName(std::string name)
{
	std::transform(name.begin(), name.end(), name.begin(), (int (*)(int))std::tolower);

	std::map<std::string, int>::iterator it = unitID.find(name);
	if(it == unitID.end())
		return NULL;

	int id = it->second;
	if(!unitDefs[id].loaded)
		ParseUnit(unitDefs[id].filename, id);

	return &unitDefs[id];
}