From 4a5bdb90ffdbd9974f86df14893e7287f2faa933 Mon Sep 17 00:00:00 2001
From: nathansmithsmith <nathansmith7@mailfence.com>
Date: Fri, 27 Oct 2023 15:00:19 -0600
Subject: Better targeting

---
 src/entities/antifaShip.c | 38 +++++++++++-----------
 src/entities/antifaShip.h |  6 ++--
 src/gameScreen.c          | 81 ++++++++++++++++++++++++++++++++++++++++-------
 src/gameScreen.h          |  5 ++-
 src/levels/level1.c       |  2 +-
 5 files changed, 97 insertions(+), 35 deletions(-)

diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c
index 7587358..c89c820 100644
--- a/src/entities/antifaShip.c
+++ b/src/entities/antifaShip.c
@@ -43,21 +43,17 @@ void closeAntifaShip(Entity * entity) {
 	entityFreeCollisionModel(entity->transformedCollisionModel);
 }
 
-void getClosestShipToAntifaShip(Game * game, Entity * entity) {
+// Returns closest entity to ship or none.
+EntityId getClosestShipToAntifaShip(Game * game, Entity * entity) {
 	int i;
-	AntifaShip * data = (AntifaShip*)entity->data;
-
-	// Reset closest indeed (:
-	data->targetedEntityId = ENTITY_NONE;
 
 	// Needs at least two entities to work correctly.
 	if (game->world.entitiesCount < 2)
-		return;
+		return ENTITY_NONE;
 
 	// Start out with entity 1 as closest.
 	Entity * currentEntity = &game->world.entities[1];
-	data->targetedEntityId = currentEntity->id;
-	data->targetedEntityFingerprint = currentEntity->fingerprint;
+	EntityId closestId = currentEntity->id;
 	float closestDistance = Vector3Distance(currentEntity->position, entity->position);
 	float distance;
 
@@ -71,11 +67,13 @@ void getClosestShipToAntifaShip(Game * game, Entity * entity) {
 		// Is closest.
 		if (distance < closestDistance) {
 			closestDistance = distance;
-			data->targetedEntityId = currentEntity->id;
-			data->targetedEntityFingerprint = currentEntity->fingerprint;
+			closestId = currentEntity->id;
 		}
 	}
+
+	return closestId;
 }
+
 bool isAntifaShipGunInRange(Entity * ship, Entity * targetEntity) {
 	Vector3 directionAiming = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, ship->rotation);
 	Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(targetEntity->position, ship->position));
@@ -97,11 +95,13 @@ void updateAntifaShipTarget(Game * game, Entity * entity) {
 		} else if (targetEntity->fingerprint != data->targetedEntityFingerprint) {
 			data->doAutoTarget = false;
 			return;
+		} else if (!isAntifaShipGunInRange(entity, targetEntity)) {
+			data->doAutoTarget = false;
+			return;
 		}
 
 		Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(targetEntity->position, entity->position));
-		data->gunTarget = Vector3Lerp(data->gunTarget, directionToTarget, 0.1);
-		data->gunTarget = directionToTarget;
+		data->gunTarget = Vector3Lerp(data->gunTarget, directionToTarget, ANTIFA_AUTO_TARGET_SPEED * t);
 		printf("%d %d\n", isAntifaShipGunInRange(entity, targetEntity), data->targetedEntityId);
 	} else {
 		data->gunTarget = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation);
@@ -117,17 +117,19 @@ void toggleAntifaShipAutoTarget(Game * game, Entity * entity) {
 		return;
 	}
 
-	getClosestShipToAntifaShip(game, entity);
+	// Get closest entity to target on.
+	EntityId closestId = getClosestShipToAntifaShip(game, entity);
 
-	// No closest ):
-	if (data->targetedEntityId == ENTITY_NONE)
+	if (closestId == ENTITY_NONE)
 		return;
 
+	data->targetedEntityId = closestId;
+	Entity * closestEntity = getEntityFromWorld(game->world, closestId);
+	data->targetedEntityFingerprint = closestEntity->fingerprint;
+
 	// Lock on target if can.
-	if (isAntifaShipGunInRange(entity, getEntityFromWorld(game->world, data->targetedEntityId)))
+	if (isAntifaShipGunInRange(entity, closestEntity))
 		data->doAutoTarget = true;
-	else
-		data->targetedEntityId = ENTITY_NONE;
 }
 
 // This fucker will fire a bullet!!!
diff --git a/src/entities/antifaShip.h b/src/entities/antifaShip.h
index 98c85cc..89000c7 100644
--- a/src/entities/antifaShip.h
+++ b/src/entities/antifaShip.h
@@ -10,8 +10,8 @@
 #define ANTIFA_DRAW_BULLET_FOR 0.05
 
 // Auto target shit.
-#define ANTIFA_START_AUTO_TARGET_MAX 3.0 // How far off auto target can be entail it drops.
-#define ANTIFA_AUTO_TARGET_SPEED 1.0
+#define ANTIFA_START_AUTO_TARGET_MAX 0.5 // How far off auto target can be entail it drops.
+#define ANTIFA_AUTO_TARGET_SPEED 100.0
 
 typedef struct AntifaShip {
 	Vector2 lastMouse;
@@ -21,8 +21,8 @@ typedef struct AntifaShip {
 	double timeSinceLastBullet;
 	Bullet lastBulletShot;
 	Vector3 gunTarget;
-	bool doAutoTarget;
 
+	bool doAutoTarget;
 	EntityId targetedEntityId;
 	EntityFingerprint targetedEntityFingerprint;
 } AntifaShip;
diff --git a/src/gameScreen.c b/src/gameScreen.c
index b956059..70ea351 100644
--- a/src/gameScreen.c
+++ b/src/gameScreen.c
@@ -3,13 +3,19 @@
 #include "world.h"
 #include "bullets.h"
 #include "assets.h"
+#include "entitiesInclude.h"
 
 void initGameScreenGui(GameScreen * gameScreen) {
 	float width = GetScreenWidth();
 	float height = GetScreenHeight();
 
 	// It is kind of terrible but works.
-	gameScreen->infoText = (Vector2){0.0, height / 1.5};
+	gameScreen->infoTextPosition = (Vector2){0.0, height / 1.5};
+
+	gameScreen->targetInfoPosition = (Vector2){
+		width - (GAME_SCREEN_TEXT_SIZE * (GAME_SCREEN_TARGET_INFO_MAX / 2.0)),
+		height / 3.0
+	};
 
 	// Gyroscope indeed
 	initGyroscope(&gameScreen->gyroscope);
@@ -50,21 +56,16 @@ void drawCrossHair(float size, float thick, Color color) {
 	);
 }
 
-void drawGameScreenGui(Game * game) {
-	GameScreen * gameScreen = &game->gameScreen;
+void drawGameScreenInfoText(Game * game, GameScreen * gameScreen) {
 	Entity * player = getEntityFromWorld(game->world, 0);
 
+	Vector3 position = player->position;
+	Vector3 velocity = player->velocity.velocity;
+
 	// Hello reader. I fucking hate you!
 	size_t bufSize = 255;
 	char buf[bufSize];
 
-	// Draw cross hair.
-	if (gameScreen->mainCamera == FIRST_PERSON_CAMERA)
-		drawCrossHair(10.0, 2.0, BLUE);
-
-	Vector3 position = player->position;
-	Vector3 velocity = player->velocity.velocity;
-
 	// Format text.
 	snprintf(
 		buf,
@@ -80,12 +81,68 @@ void drawGameScreenGui(Game * game) {
 	// Draw info text.
 	DrawText(
 		buf,
-		gameScreen->infoText.x,
-		gameScreen->infoText.y,
+		gameScreen->infoTextPosition.x,
+		gameScreen->infoTextPosition.y,
+		GAME_SCREEN_TEXT_SIZE,
+		GREEN
+	);
+}
+
+void drawGameScreenTargetInfo(Game * game, GameScreen * gameScreen) {
+	Entity * player = getEntityFromWorld(game->world, 0);
+	AntifaShip * data = (AntifaShip*)player->data;
+
+	size_t bufSize = 255;
+	char buf[bufSize];
+
+	// Format.
+	snprintf(
+		buf,
+		bufSize,
+		"Auto: %s",
+		data->doAutoTarget ? "On" : "Off"
+	);
+
+	// Is auto targeting.
+	if (data->doAutoTarget) {
+		Entity * targetedEntity = getEntityFromWorld(game->world, data->targetedEntityId);
+
+		if (targetedEntity != NULL) {
+			char bufCopy[bufSize];
+			strncpy(bufCopy, buf, bufSize);
+
+			// Add more formatted text.
+			snprintf(
+				buf,
+				bufSize,
+				"%s\nId: %d@%x\nDistance: %.2f\n",
+				bufCopy,
+				data->targetedEntityId,
+				data->targetedEntityFingerprint,
+				Vector3Distance(player->position, targetedEntity->position)
+			);
+		}
+	}
+
+	// Draw.
+	DrawText(
+		buf,
+		gameScreen->targetInfoPosition.x,
+		gameScreen->targetInfoPosition.y,
 		GAME_SCREEN_TEXT_SIZE,
 		GREEN
 	);
+}
+
+void drawGameScreenGui(Game * game) {
+	GameScreen * gameScreen = &game->gameScreen;
+
+	// Draw cross hair.
+	if (gameScreen->mainCamera == FIRST_PERSON_CAMERA)
+		drawCrossHair(10.0, 2.0, BLUE);
 
+	drawGameScreenInfoText(game, gameScreen);
+	drawGameScreenTargetInfo(game, gameScreen);
 	drawGyroscope(game, &gameScreen->gyroscope);
 	drawRadar(game, &gameScreen->radar);
 }
diff --git a/src/gameScreen.h b/src/gameScreen.h
index 1fdf089..e4f8d20 100644
--- a/src/gameScreen.h
+++ b/src/gameScreen.h
@@ -8,10 +8,13 @@
 
 #define GAME_SCREEN_TEXT_SIZE 20.0
 #define GAME_SCREEN_NEXT_LEVEL_DELAY 2.0
+#define GAME_SCREEN_TARGET_INFO_MAX 20
 
 // Gui stuff and shit.
 typedef struct GameScreen {
-    Vector2 infoText;
+    Vector2 infoTextPosition;
+    Vector2 targetInfoPosition;
+
     Gyroscope gyroscope;
     Radar radar;
     CameraId mainCamera;
diff --git a/src/levels/level1.c b/src/levels/level1.c
index f182ccf..3ef8d64 100644
--- a/src/levels/level1.c
+++ b/src/levels/level1.c
@@ -6,7 +6,7 @@
 void initLevel1(Game * game, Levels * levels) {
      WorldEntry entries[2] = {
 		(WorldEntry){ENTITY_ANTIFA, (Vector3){0.0, 0.0, 0.0}, QuaternionIdentity()},
-		(WorldEntry){ENTITY_SOLDATO, (Vector3){0.0, 0.0, 10.0}, QuaternionIdentity()}
+		(WorldEntry){ENTITY_SOLDATO, (Vector3){0.0, 10.0, 500.0}, QuaternionIdentity()}
 	};
 
     addEntriesToWorld(
-- 
cgit v1.2.3