Beispiel #1
0
BasicSymbol* BasicScope::resolve(string name){
	if(isVariable(name)){
		return this->variables[name];
	}

	if(isFunction(name)){
		return this->functions[name][0];
	}

	if(isStructure(name)){
		return this->structures[name];
	}

	throw TypeException("What are you trying to resolve!?");
}
void ObjectData::loadFromINIFile(std::string filename)
{
    SDL_RWops *file = pFileManager->OpenFile(filename);
    INIFile objectDataFile(file);
    SDL_RWclose(file);

    // load default structure values
    ObjectDataStruct structureDefaultData;
    structureDefaultData.hitpoints = objectDataFile.getIntValue("default structure", "HitPoints");
    structureDefaultData.price = objectDataFile.getIntValue("default structure", "Price");
    structureDefaultData.power = objectDataFile.getIntValue("default structure", "Power");
    structureDefaultData.viewrange = objectDataFile.getIntValue("default structure", "ViewRange");
    structureDefaultData.capacity = objectDataFile.getIntValue("default structure", "Capacity");
    structureDefaultData.weapondamage = objectDataFile.getIntValue("default structure", "WeaponDamage");
    structureDefaultData.weaponrange = objectDataFile.getIntValue("default structure", "WeaponRange");
    structureDefaultData.maxspeed = objectDataFile.getDoubleValue("default structure", "MaxSpeed");
    structureDefaultData.turnspeed = objectDataFile.getDoubleValue("default structure", "TurnSpeed");
    structureDefaultData.radius = objectDataFile.getIntValue("default structure", "Radius");
    structureDefaultData.buildtime = objectDataFile.getIntValue("default structure", "BuildTime");

    // load default unit values
    ObjectDataStruct unitDefaultData;
    unitDefaultData.hitpoints = objectDataFile.getIntValue("default unit", "HitPoints");
    unitDefaultData.price = objectDataFile.getIntValue("default unit", "Price");
    unitDefaultData.power = objectDataFile.getIntValue("default unit", "Power");
    unitDefaultData.viewrange = objectDataFile.getIntValue("default unit", "ViewRange");
    unitDefaultData.capacity = objectDataFile.getIntValue("default unit", "Capacity");
    unitDefaultData.weapondamage = objectDataFile.getIntValue("default unit", "WeaponDamage");
    unitDefaultData.weaponrange = objectDataFile.getIntValue("default unit", "WeaponRange");
    unitDefaultData.maxspeed = objectDataFile.getDoubleValue("default unit", "MaxSpeed");
    unitDefaultData.turnspeed = objectDataFile.getDoubleValue("default unit", "TurnSpeed");
    unitDefaultData.radius = objectDataFile.getIntValue("default unit", "Radius");
    unitDefaultData.buildtime = objectDataFile.getIntValue("default unit", "BuildTime");

    // set default values
    for(int i=0;i<ItemID_max;i++) {
        if(isStructure(i)) {
            data[i] = structureDefaultData;
        } else {
            data[i] = unitDefaultData;
        }
    }

    INIFile::SectionIterator iter;
    for(iter = objectDataFile.begin(); iter != objectDataFile.end(); ++iter) {
        std::string sectionName = iter->getSectionName();

        if(sectionName == "" || sectionName == "default structure" || sectionName == "default unit") {
            continue;
        }

        Uint32 itemID = getItemIDByName(sectionName);

        if(itemID == ItemID_Invalid) {
            std::cerr << "ObjectData::ObjectData(): \"" << sectionName << "\" is no valid unit/structure name!" << std::endl;
            continue;
        }

        ObjectDataStruct& defaultData = isStructure(itemID) ? structureDefaultData : unitDefaultData;

        data[itemID].hitpoints = objectDataFile.getIntValue(sectionName, "HitPoints", defaultData.hitpoints);
        data[itemID].price = objectDataFile.getIntValue(sectionName, "Price", defaultData.price);
        data[itemID].power = objectDataFile.getIntValue(sectionName, "Power", defaultData.power);
        data[itemID].viewrange = objectDataFile.getIntValue(sectionName, "ViewRange", defaultData.viewrange);
        data[itemID].capacity = objectDataFile.getIntValue(sectionName, "Capacity", defaultData.capacity);
        data[itemID].weapondamage = objectDataFile.getIntValue(sectionName, "WeaponDamage", defaultData.weapondamage);
        data[itemID].weaponrange = objectDataFile.getIntValue(sectionName, "WeaponRange", defaultData.weaponrange);
        data[itemID].maxspeed = objectDataFile.getDoubleValue(sectionName, "MaxSpeed", defaultData.maxspeed);
        data[itemID].turnspeed = objectDataFile.getDoubleValue(sectionName, "TurnSpeed", defaultData.turnspeed);
        data[itemID].radius = objectDataFile.getIntValue(sectionName, "Radius", defaultData.radius);
        data[itemID].buildtime = objectDataFile.getIntValue(sectionName, "BuildTime", defaultData.buildtime);
    }
}
/**
    This method loads the structures specified by the [Structures] section.
*/
void INIMapEditorLoader::loadStructures()
{
    int genID = -2;

    INIFile::KeyIterator iter;

    for(iter = inifile->begin("STRUCTURES"); iter != inifile->end("STRUCTURES"); ++iter) {
		std::string tmpkey = iter->getKeyName();
		std::string tmp = iter->getStringValue();

		if(tmpkey.find("GEN") == 0) {
			// Gen Object/Structure
			std::string PosStr = tmpkey.substr(3,tmpkey.size()-3);
            int pos;
            if(!parseString(PosStr, pos) || (pos < 0)) {
                logWarning(iter->getLineNumber(), "Invalid position string: '" + PosStr + "'!");
                continue;
            }

			std::string HouseStr, BuildingStr;
			splitString(tmp,2,&HouseStr,&BuildingStr);

			HOUSETYPE houseID = getHouseID(HouseStr);
			if(houseID == HOUSE_UNUSED) {
                // skip structure for unused house
                continue;
            } else if(houseID == HOUSE_INVALID) {
				logWarning(iter->getLineNumber(), "Invalid house string for '" + BuildingStr + "': '" + HouseStr + "'!");
				continue;
			}

			int itemID = getItemIDByName(BuildingStr);

			if((itemID == Structure_Slab1) || (itemID == Structure_Slab4) || (itemID == Structure_Wall)) {
			    pMapEditor->structures.push_back(MapEditor::Structure(genID, houseID, itemID, 256, Coord(getXPos(pos),getYPos(pos))));
			    genID--;
			} else {
			    logWarning(iter->getLineNumber(), "Invalid building string: '" + BuildingStr + "' for GEN-Placement!");
			    continue;
			}

		} else if(tmpkey.find("ID") == 0) {
		    // other structure
			int structureID = 0;
            parseString(tmpkey.substr(2), structureID);

			std::string HouseStr, BuildingStr, health, PosStr;
			splitString(tmp,6,&HouseStr,&BuildingStr,&health,&PosStr);

            int pos;
            if(!parseString(PosStr, pos) || (pos < 0)) {
                logWarning(iter->getLineNumber(), "Invalid position string for '" + BuildingStr + "': '" + PosStr + "'!");
                continue;
            }

			HOUSETYPE houseID = getHouseID(HouseStr);
            if(houseID == HOUSE_UNUSED) {
                // skip structure for unused house
                continue;
            } else if(houseID == HOUSE_INVALID) {
				logWarning(iter->getLineNumber(), "Invalid house string for '" + BuildingStr + "': '" + HouseStr + "'!");
				continue;
			}

            int iHealth;
            if(!parseString(health, iHealth) || (iHealth < 0) || (iHealth > 256)) {
                logWarning(iter->getLineNumber(), "Invalid health string: '" + health + "'!");
                iHealth = 256;
            }

			int itemID = getItemIDByName(BuildingStr);

			if((itemID == ItemID_Invalid) || !isStructure(itemID)) {
			    logWarning(iter->getLineNumber(), "Invalid building string: '" + BuildingStr + "'!");
			    continue;
			}

			pMapEditor->structures.push_back(MapEditor::Structure(structureID, houseID, itemID, iHealth, Coord(getXPos(pos),getYPos(pos))));
		} else {
            logWarning(iter->getLineNumber(), "Invalid structure key: '" + tmpkey + "'!");
            continue;
		}
	}
}
Beispiel #4
0
/* See if there is a target in range */
bool aiChooseTarget(BASE_OBJECT *psObj, BASE_OBJECT **ppsTarget, int weapon_slot, bool bUpdateTarget, UWORD *targetOrigin)
{
	BASE_OBJECT		*psTarget = NULL;
	DROID			*psCommander;
	SDWORD			curTargetWeight=-1;
	UWORD 			tmpOrigin = ORIGIN_UNKNOWN;

	if (targetOrigin)
	{
		*targetOrigin = ORIGIN_UNKNOWN;
	}
		
	/* Get the sensor range */
	switch (psObj->type)
	{
	case OBJ_DROID:
		if (((DROID *)psObj)->asWeaps[weapon_slot].nStat == 0)
		{
			return false;
		}

		if (((DROID *)psObj)->asWeaps[0].nStat == 0 &&
			((DROID *)psObj)->droidType != DROID_SENSOR)
		{
			// Can't attack without a weapon
			return false;
		}
		break;
	case OBJ_STRUCTURE:
		if (((STRUCTURE *)psObj)->numWeaps == 0 || ((STRUCTURE *)psObj)->asWeaps[0].nStat == 0)
		{
			// Can't attack without a weapon
			return false;
		}
		break;
	default:
		break;
	}

	/* See if there is a something in range */
	if (psObj->type == OBJ_DROID)
	{
		BASE_OBJECT *psCurrTarget = ((DROID *)psObj)->psActionTarget[0];

		/* find a new target */
		int newTargetWeight = aiBestNearestTarget((DROID *)psObj, &psTarget, weapon_slot);

		/* Calculate weight of the current target if updating; but take care not to target
		 * ourselves... */
		if (bUpdateTarget && psCurrTarget != psObj)
		{
			curTargetWeight = targetAttackWeight(psCurrTarget, psObj, weapon_slot);
		}

		if (newTargetWeight >= 0		// found a new target
		    && (!bUpdateTarget			// choosing a new target, don't care if current one is better
		        || curTargetWeight <= 0		// attacker had no valid target, use new one
			|| newTargetWeight > curTargetWeight + OLD_TARGET_THRESHOLD)	// updating and new target is better
		    && validTarget(psObj, psTarget, weapon_slot)
		    && (aiDroidHasRange((DROID *)psObj, psTarget, weapon_slot) || (secondaryGetState((DROID *)psObj, DSO_HALTTYPE) != DSS_HALT_HOLD)))
		{
			ASSERT(!isDead(psTarget), "aiChooseTarget: Droid found a dead target!");
			*ppsTarget = psTarget;
			return true;
		}
	}
	else if (psObj->type == OBJ_STRUCTURE)
	{
		WEAPON_STATS	*psWStats = NULL;
		bool	bCommanderBlock = false;

		ASSERT(((STRUCTURE *)psObj)->asWeaps[weapon_slot].nStat > 0, "no weapons on structure");

		psWStats = ((STRUCTURE *)psObj)->asWeaps[weapon_slot].nStat + asWeaponStats;
		int longRange = proj_GetLongRange(psWStats);

		// see if there is a target from the command droids
		psTarget = NULL;
		psCommander = cmdDroidGetDesignator(psObj->player);
		if (!proj_Direct(psWStats) && (psCommander != NULL) &&
			aiStructHasRange((STRUCTURE *)psObj, (BASE_OBJECT *)psCommander, weapon_slot))
		{
			// there is a commander that can fire designate for this structure
			// set bCommanderBlock so that the structure does not fire until the commander
			// has a target - (slow firing weapons will not be ready to fire otherwise).
			bCommanderBlock = true;

			// I do believe this will never happen, check for yourself :-)
			debug(LOG_NEVER,"Commander %d is good enough for fire designation", psCommander->id);

			if (psCommander->action == DACTION_ATTACK
			    && psCommander->psActionTarget[0] != NULL
			    && !aiObjectIsProbablyDoomed(psCommander->psActionTarget[0]))
			{
				// the commander has a target to fire on
				if (aiStructHasRange((STRUCTURE *)psObj, psCommander->psActionTarget[0], weapon_slot))
				{
					// target in range - fire on it
					tmpOrigin = ORIGIN_COMMANDER;
					psTarget = psCommander->psActionTarget[0];
				}
				else
				{
					// target out of range - release the commander block
					bCommanderBlock = false;
				}
			}
		}

		// indirect fire structures use sensor towers first
		if (psTarget == NULL && !bCommanderBlock && !proj_Direct(psWStats))
		{
			psTarget = aiSearchSensorTargets(psObj, weapon_slot, psWStats, &tmpOrigin);
		}

		if (psTarget == NULL && !bCommanderBlock)
		{
			int targetValue = -1;
			int tarDist = INT32_MAX;

			gridStartIterate(psObj->pos.x, psObj->pos.y, std::min(longRange, PREVIOUS_DEFAULT_GRID_SEARCH_RADIUS));
			for (BASE_OBJECT *psCurr = gridIterate(); psCurr != NULL; psCurr = gridIterate())
			{
				// Prefer targets that aren't walls, then prefer finished targets to unfinished targets.
				int newTargetValue = 1*!(isStructure(psCurr) && castStructure(psCurr)->status != SS_BUILT)
				                   + 2*!aiObjIsWall(psCurr);
				// See if in sensor range and visible
				int distSq = objPosDiffSq(psCurr->pos, psObj->pos);
				if (newTargetValue < targetValue || (newTargetValue == targetValue && distSq >= tarDist))
				{
					continue;
				}

				/* Check that it is a valid target */
				if (psCurr->type != OBJ_FEATURE && !aiObjectIsProbablyDoomed(psCurr)
				    && !aiCheckAlliances(psCurr->player, psObj->player)
				    && validTarget(psObj, psCurr, weapon_slot) && psCurr->visible[psObj->player] == UBYTE_MAX
				    && aiStructHasRange((STRUCTURE *)psObj, psCurr, weapon_slot))
				{

					tmpOrigin = ORIGIN_VISUAL;
					psTarget = psCurr;
					tarDist = distSq;
					targetValue = newTargetValue;
				}
			}
		}

		if (psTarget)
		{
			ASSERT(!psTarget->died, "aiChooseTarget: Structure found a dead target!");
			if (targetOrigin)
			{
				*targetOrigin = tmpOrigin;
			}
			*ppsTarget = psTarget;
			return true;
		}
	}

	return false;
}
Beispiel #5
0
ir::Type* TypeParser::_parseType()
{
	std::string nextToken = _lexer->peek();
	
	ir::Type* type = nullptr;
	
	if(isFunction(nextToken))
	{
		type = _parseFunction();
	}
	else if(isStructure(nextToken))
	{
		type = _parseStructure();
	}
	else if(isPrimitive(_compiler, nextToken))
	{
		type = _parsePrimitive();
		
		nextToken = _lexer->peek();
		
		if(isFunction(nextToken))
		{
			type = _parseFunction(type);
		}
	}
	else if(isArray(nextToken))
	{
		type = _parseArray();
	}
	else if(isVariadic(nextToken))
	{
		_lexer->scan("...");
		type = *_compiler->getOrInsertType(ir::VariadicType(_compiler));
	}
	else if(isTypeAlias(nextToken))
	{
		type = _parseTypeAlias();
	}
	else if(isOpaqueType(nextToken))
	{
		_lexer->scan("opaque");
		type = *_compiler->getOrInsertType(ir::OpaqueType(_compiler));
	}

	nextToken = _lexer->peek();

	while(isPointer(nextToken))
	{
		_lexer->scan("*");
		type = *_compiler->getOrInsertType(ir::PointerType(_compiler, type));
	
		nextToken = _lexer->peek();
	}
	
	if(type == nullptr)
	{
		throw std::runtime_error("Failed to parse type.");
	}
	
	hydrazine::log("TypeParser::Parser") << "Parsed type " << type->name
		<< ".\n";
	
	return type;
}