Skip to content

Commit

Permalink
th_vo_algo: use all entities for potentials visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
Netdex committed Nov 24, 2018
1 parent b0c1971 commit 3b32132
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 166 deletions.
104 changes: 22 additions & 82 deletions twinhook/algo/th_vo_algo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void th_vo_algo::onTick()
const vec2 enemyCom = enemy.obj->com();
const vec2 playerCom = plyr.obj->com();
if (enemyCom.y < playerCom.y) {
for (int dir = Direction::Left; dir <= Direction::Right; ++dir)
for (int dir = 0; dir <= NUM_DIRS; ++dir)
{
const vec2 pvel = this->getPlayerMovement(dir);

Expand Down Expand Up @@ -289,35 +289,27 @@ vec2 th_vo_algo::getPlayerMovement(int dir)
}

float th_vo_algo::minStaticCollideTick(
const std::vector<bullet> &bullets,
const std::vector<const game_object*> &bullets,
const aabb &area,
std::vector<bullet> &collided) const
std::vector<const game_object*> &collided) const
{
float minTick = FLT_MAX;
for (const bullet& bullet : bullets)
for (const game_object* bullet : bullets)
{
float colTick = area.willCollideWith(*bullet.obj);
/*colTick = vec2::willCollideAABB(
p, bullet.position - bullet.size / 2,
s, bullet.size,
vec2(), bullet.velocity
);
*/
float colTick = area.willCollideWith(*bullet->obj);

if (colTick >= 0) {
minTick = std::min(colTick, minTick);
collided.push_back(bullet);
}
//// don't do more calculations if the density is saturated
//if (minTick != FLT_MAX && minTick / MAX_FRAMES_TILL_COLLISION > 1)
// return minTick;
}
if (minTick != FLT_MAX && minTick >= 0)
return minTick;
return -1.f;
};

void th_vo_algo::vizPotentialQuadtree(
const std::vector<bullet> &bullets,
const std::vector<const game_object*> &bullets,
const aabb &area,
float minRes) const
{
Expand Down Expand Up @@ -347,7 +339,7 @@ void th_vo_algo::vizPotentialQuadtree(

for (int i = 0; i < 4; i++)
{
std::vector<bullet> collided;
std::vector<const game_object*> collided;
float colTick = minStaticCollideTick(
bullets,
aabb{ colDomains[i], vec2(), vec2(sqsz) },
Expand Down Expand Up @@ -377,6 +369,18 @@ void th_vo_algo::vizPotentialQuadtree(

}

std::vector<const game_object*> th_vo_algo::constructDangerObjectUnion()
{
std::vector<const game_object*> objs;
for (const laser &l : player->lasers)
objs.push_back(&l);
for (const bullet &b : player->bullets)
objs.push_back(&b);
for (const enemy &e : player->enemies)
objs.push_back(&e);
return objs;
}

void th_vo_algo::visualize(IDirect3DDevice9* d3dDev)
{
if (player->render)
Expand All @@ -387,7 +391,7 @@ void th_vo_algo::visualize(IDirect3DDevice9* d3dDev)
{
// draw vector field (laggy)
vizPotentialQuadtree(
player->bullets,
constructDangerObjectUnion(),
aabb{ vec2(), vec2(), vec2(th_param.GAME_WIDTH, th_param.GAME_HEIGHT) },
VEC_FIELD_MIN_RESOLUTION);
}
Expand All @@ -400,71 +404,7 @@ void th_vo_algo::visualize(IDirect3DDevice9* d3dDev)
for (const powerup &p : player->powerups)
p.render();
plyr.render();
//// draw laser points
//for (auto i = player->lasers.begin(); i != player->lasers.end(); ++i)
//{
// cdraw::fillRect(
// th_param.GAME_X_OFFSET + i->obj->.x - 3,
// th_param.GAME_Y_OFFSET + i->position.y - 3,
// 6, 6,
// D3DCOLOR_ARGB(255, 0, 0, 255));

// vec2 proj = (*i).position + (*i).velocity * 10;
// cdraw::line((*i).position.x + th_param.GAME_X_OFFSET, (*i).position.y + th_param.GAME_Y_OFFSET,
// proj.x + th_param.GAME_X_OFFSET, proj.y + th_param.GAME_Y_OFFSET, D3DCOLOR_ARGB(255, 255, 0, 0));

// // voodoo witchcraft magic

// float rex = i->radius * (float)cos(M_PI / 2 + i->angle);
// float rey = i->radius * (float)sin(M_PI / 2 + i->angle);
// cdraw::line(
// th_param.GAME_X_OFFSET + i->position.x - rex,
// th_param.GAME_Y_OFFSET + i->position.y - rey,
// th_param.GAME_X_OFFSET + i->position.x + i->extent.x - rex,
// th_param.GAME_Y_OFFSET + i->position.y + i->extent.y - rey,
// D3DCOLOR_ARGB(255, 0, 0, 255));
// cdraw::line(
// th_param.GAME_X_OFFSET + i->position.x + rex,
// th_param.GAME_Y_OFFSET + i->position.y + rey,
// th_param.GAME_X_OFFSET + i->position.x + i->extent.x + rex,
// th_param.GAME_Y_OFFSET + i->position.y + i->extent.y + rey,
// D3DCOLOR_ARGB(255, 0, 0, 255));
//}

//// bullet markers
//for (auto i = player->bullets.begin(); i != player->bullets.end(); ++i)
//{
// if ((*i).meta)
// {
// cdraw::rect((*i).position.x - 7 + th_param.GAME_X_OFFSET, (*i).position.y - 7 + th_param.GAME_Y_OFFSET, 14, 14, D3DCOLOR_HSV((double)(16 * (*i).meta), 1, 1)));
// }
// else {
// cdraw::rect(
// (*i).position.x - (*i).size.x / 2 + th_param.GAME_X_OFFSET,
// (*i).position.y - (*i).size.y / 2 + th_param.GAME_Y_OFFSET,
// (*i).size.x, (*i).size.y, D3DCOLOR_ARGB(255, 255, 2, 200));
// }
// vec2 proj = (*i).position + (*i).velocity * 10;

// cdraw::line((*i).position.x + th_param.GAME_X_OFFSET, (*i).position.y + th_param.GAME_Y_OFFSET,
// proj.x + th_param.GAME_X_OFFSET, proj.y + th_param.GAME_Y_OFFSET, D3DCOLOR_ARGB(255, 0, 255, 0));
//}

//for (auto b : player->powerups)
//{
// cdraw::rect(
// th_param.GAME_X_OFFSET + b.position.x - b.size.x / 2,
// th_param.GAME_Y_OFFSET + b.position.y - b.size.y / 2,
// b.size.x, b.size.y, D3DCOLOR_ARGB(255, 255, 0, 0));
// vec2 proj = b.position + b.velocity * 10;

// cdraw::line(b.position.x + th_param.GAME_X_OFFSET, b.position.y + th_param.GAME_Y_OFFSET,
// proj.x + th_param.GAME_X_OFFSET, proj.y + th_param.GAME_Y_OFFSET, D3DCOLOR_ARGB(255, 0, 255, 0));
//}
//cdraw::fillRect(
// plyr.position.x - plyr.size.x / 2 + th_param.GAME_X_OFFSET,
// plyr.position.y - plyr.size.y / 2 + th_param.GAME_Y_OFFSET,
// plyr.size.x, plyr.size.y, D3DCOLOR_ARGB(255, 0, 255, 0));

}
}

Expand Down
9 changes: 5 additions & 4 deletions twinhook/algo/th_vo_algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static const float MAX_FRAMES_TILL_COLLISION = 10.f; // used for coloring vector

/* Algorithmic Constants */
static const float SQRT_2 = sqrt(2.f);
static const float MIN_SAFETY_TICK = 10.0f;
static const float MIN_SAFETY_TICK = 20.0f;

enum Direction
{
Expand Down Expand Up @@ -152,9 +152,9 @@ class th_vo_algo : public th_algorithm
* \return The minimum collison tick
*/
float minStaticCollideTick(
const std::vector<bullet> &bullets,
const std::vector<const game_object*> &bullets,
const aabb &area,
std::vector<bullet> &collided) const;
std::vector<const game_object*> &collided) const;
/**
* \brief Draw collision potentials at a specified resolution
* \param bullets The bullets to check collision against
Expand All @@ -163,10 +163,11 @@ class th_vo_algo : public th_algorithm
* \param minRes Minimum allowable resolution for visualization
*/
void vizPotentialQuadtree(
const std::vector<bullet> &bullets,
const std::vector<const game_object*> &bullets,
const aabb &area,
float minRes) const;

std::vector<const game_object*> constructDangerObjectUnion();
/* IMGUI Integration */
static const int RISK_HISTORY_SIZE = 90;
float riskHistory[RISK_HISTORY_SIZE] = {0};
Expand Down
59 changes: 29 additions & 30 deletions twinhook/control/th11_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ void th11_player::doBulletPoll()
float y = *((float *)pBase + 272);
float dx = *((float *)pBase + 274);
float dy = *((float *)pBase + 275);
vec2 sz(w, h);
aabb a{
vec2(x + th_param.GAME_WIDTH / 2,y),
vec2(x + th_param.GAME_WIDTH / 2,y) - sz / 2,
vec2(dx,dy),
vec2(w,h)
sz
};
bullet b{ a };
bullets.push_back(b);
Expand Down Expand Up @@ -95,34 +96,32 @@ void th11_player::doLaserPoll()

player th11_player::getPlayerEntity()
{
///*
// * (PLYR BASE PTR)
// * [0x87C] ?
// * [0x8] POS (VEC2)
// * [0x48] ?
// * [0xC] BOUND TOP LEFT (VEC3)
// * [0xC] BOUND BOTTOM RIGHT (VEC3)
// */
//PBYTE *PlayerPtrAddr = (PBYTE*)gs_ptr.plyr_pos;
//if (*PlayerPtrAddr) {
// PBYTE plyrAddr = *PlayerPtrAddr;

// float w = (*(float*)(plyrAddr + 0x87C + 0x8 + 0x48 + 0xC)
// - *(float*)(plyrAddr + 0x87C + 0x8 + 0x48)) * 2;
// float h = (*(float*)(plyrAddr + 0x87C + 0x8 + 0x48 + 0xC + 4)
// - *(float*)(plyrAddr + 0x87C + 0x8 + 0x48 + 4)) * 2;
// entity e = {
// vec2(
// *(float*)(plyrAddr + 0x87C) + th_param.GAME_WIDTH / 2,
// *(float*)(plyrAddr + 0x87C + 4)),
// vec2(),
// vec2(w, h),
// 0
// };
// return e;
//}
//return {};
// TODO
/*
* (PLYR BASE PTR)
* [0x87C] ?
* [0x8] POS (VEC2)
* [0x48] ?
* [0xC] BOUND TOP LEFT (VEC3)
* [0xC] BOUND BOTTOM RIGHT (VEC3)
*/
PBYTE *PlayerPtrAddr = (PBYTE*)gs_ptr.plyr_pos;
if (*PlayerPtrAddr) {
PBYTE plyrAddr = *PlayerPtrAddr;

float w = (*(float*)(plyrAddr + 0x87C + 0x8 + 0x48 + 0xC)
- *(float*)(plyrAddr + 0x87C + 0x8 + 0x48)) * 2;
float h = (*(float*)(plyrAddr + 0x87C + 0x8 + 0x48 + 0xC + 4)
- *(float*)(plyrAddr + 0x87C + 0x8 + 0x48 + 4)) * 2;
vec2 sz(w, h);
aabb e = {
vec2(
*(float*)(plyrAddr + 0x87C) + th_param.GAME_WIDTH / 2,
*(float*)(plyrAddr + 0x87C + 4)) - sz / 2,
vec2(),
sz,
};
return e;
}
return player{ aabb() };
}

Expand Down
50 changes: 0 additions & 50 deletions twinhook/control/th_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,6 @@

#include "model/game_object.h"

///*
// * Representing an entity. This struct has two interpretations depending on
// * whether the game using hitcircles or hitboxes because I was lazy
// * (yes, this is confusing, I'll fix this)
// *
// * If hitcircles:
// * p: center of circle
// * v: velocity of circle
// * sz: radius of circle (both components are the same)
// * me: game specific
// *
// * If hitboxes:
// * p: top left corner of box
// * v: velocity of box
// * sz: dimensions (width, height) of box
// * me: game specific
// */
//struct entity
//{
// vec2 position; // position
// vec2 velocity; // velocity
// vec2 size; // bullet size
// DWORD meta; // metadata
//};
//
//struct laser
//{
// vec2 position; // position
// vec2 velocity; // velocity
// vec2 extent; // laser extent
// float length;
// float radius; // perpendicular radius
// float angle; // angle
//
// std::vector<vec2> laser::getVertices() const
// {
// std::vector<vec2> vertices;
// // note: these vertices must be in VERTEX ORDER
// vec2 points[] = {
// vec2(0, radius), vec2(0, -radius),
// vec2(length, -radius), vec2(length, radius)
// };
// for (int i = 0; i < 4; ++i)
// {
// vertices.push_back(points[i].rotate(angle) + position);
// }
// return vertices;
// }
//};

// game-specific addresses for common behaviour
struct gs_addr
{
Expand Down

0 comments on commit 3b32132

Please sign in to comment.