Пример #1
0
void UnitBase::engageTarget() {

    if(target && (target.getObjPointer() == NULL)) {
        // the target does not exist anymore
        releaseTarget();
        return;
    }

    if(target && (target.getObjPointer()->isActive() == false)) {
        // the target changed its state to inactive
        releaseTarget();
        return;
    }

    if(target && !targetFriendly && !canAttack(target.getObjPointer())) {
        // the (non-friendly) target cannot be attacked anymore
        releaseTarget();
        return;
    }

    if(target && !targetFriendly && !forced && !isInAttackRange(target.getObjPointer())) {
        // the (non-friendly) target left the attack mode range (and we were not forced to attack it)
        releaseTarget();
        return;
    }

    if(target) {
        // we have a target unit or structure

        Coord targetLocation = target.getObjPointer()->getClosestPoint(location);

        if(destination != targetLocation) {
            // the location of the target has moved
            // => recalculate path
            clearPath();
        }

        targetDistance = blockDistance(location, targetLocation);

        Sint8 newTargetAngle = lround(8.0f/256.0f*destinationAngle(location, targetLocation));
        if(newTargetAngle == 8) {
            newTargetAngle = 0;
        }

        if(bFollow) {
            // we are following someone
            setDestination(targetLocation);
            return;
        }

        if(targetDistance > getWeaponRange()) {
            // we are not in attack range
            // => follow the target
            setDestination(targetLocation);
            return;
        }

        // we are in attack range

        if(targetFriendly && !forced) {
            // the target is friendly and we only attack these if were forced to do so
            return;
        }

        if(goingToRepairYard) {
            // we are going to the repair yard
            // => we do not need to change the destination
            targetAngle = INVALID;
        } else if(attackMode == CAPTURE) {
            // we want to capture the target building
            setDestination(targetLocation);
            targetAngle = INVALID;
        } else if(isTracked() && target.getObjPointer()->isInfantry() && currentGameMap->tileExists(targetLocation) && !currentGameMap->getTile(targetLocation)->isMountain() && forced) {
            // we squash the infantry unit because we are forced to
            setDestination(targetLocation);
            targetAngle = INVALID;
        } else {
            // we decide to fire on the target thus we can stop moving
            setDestination(location);
            targetAngle = newTargetAngle;
        }

        if(getCurrentAttackAngle() == newTargetAngle) {
            attack();
        }

    } else if(attackPos) {
        // we attack a position

        targetDistance = blockDistance(location, attackPos);

        Sint8 newTargetAngle = lround(8.0f/256.0f*destinationAngle(location, attackPos));
        if(newTargetAngle == 8) {
            newTargetAngle = 0;
        }

        if(targetDistance <= getWeaponRange()) {
            // we are in weapon range thus we can stop moving
            setDestination(location);
            targetAngle = newTargetAngle;

            if(getCurrentAttackAngle() == newTargetAngle) {
                attack();
            }
        } else {
            targetAngle = INVALID;
        }
    }
}
Пример #2
0
void GLTexture::download(Bitmap *bitmap) {
	if (bitmap == NULL)
		bitmap = getBitmap();

	Assert(bitmap != NULL);

	activateTarget();
	GLenum format, dataFormat;

	switch (bitmap->getComponentFormat()) {
		case Bitmap::EUInt8:   dataFormat = GL_UNSIGNED_BYTE; break;
		case Bitmap::EUInt16:  dataFormat = GL_UNSIGNED_SHORT; break;
		case Bitmap::EUInt32:  dataFormat = GL_UNSIGNED_INT; break;
		case Bitmap::EFloat16: dataFormat = GL_HALF_FLOAT_ARB; break;
		case Bitmap::EFloat32: dataFormat = GL_FLOAT; break;
		case Bitmap::EFloat64: dataFormat = GL_DOUBLE; break;
		default:
			Log(EError, "GLTexture::download(): Unknown/unsupported component format %i!",
					(int) bitmap->getComponentFormat());
			return;
	}

	switch (bitmap->getPixelFormat()) {
		case Bitmap::ELuminance:
			if (m_fbType == EDepthBuffer)
				format = GL_DEPTH_COMPONENT;
			else
				format = GL_LUMINANCE;
			break;
		case Bitmap::ELuminanceAlpha: format = GL_LUMINANCE_ALPHA; break;
		case Bitmap::ERGB: format = GL_RGB; break;
		case Bitmap::ERGBA: format = GL_RGBA; break;
		default:
			Log(EError, "GLTexture::download(): Unknown/unsupported pixel format %i!",
					(int) bitmap->getPixelFormat());
			return;
	}

	glPixelStorei(GL_PACK_ALIGNMENT, 1);

	switch (m_type) {
		case ETexture2D:
			glReadPixels(0, 0, bitmap->getWidth(), bitmap->getHeight(), format,
				dataFormat, bitmap->getUInt8Data());
			/* OpenGL associates (0, 0) with the lower left position and
			   the resulting bitmap must thus be vertically flipped. */
			bitmap->flipVertically();
			break;
		case ETextureCubeMap:
			for (int i=0; i<6; ++i) {
				activateSide(i);
				bitmap = getBitmap(i);
				glReadPixels(0, 0, bitmap->getWidth(), bitmap->getHeight(), format,
					dataFormat, bitmap->getUInt8Data());
				bitmap->flipVertically();
			}
			break;
		default:
			Log(EError, "download(): Unsupported texture type!");
	}

	releaseTarget();
}