Clean code is Good code

This commit is contained in:
intns 2022-10-17 20:00:02 +01:00 committed by EpochFlame
parent 1a533ef39a
commit 90af7259e8
14 changed files with 85 additions and 40 deletions

14
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "F5 Anything: Custom Command",
"type": "f5anything",
"request": "launch",
"command": "make -j"
}
]
}

View File

@ -62,6 +62,19 @@ pikmin2.usa.dol: `sha1: 90d328bf8f190c90472e8c19e7e53c6ad0fe0d1a`
- The exception is only when adding extra folders becomes useless, for example having to add multiple folders that are empty, you can instead opt for creating a header file with the namespace's definitions inside.
- The project is actively formatted using .clang-format, for a consistent code style.
## Writing Clean Code
The size of the project requires a lot of repeated code, here are some commonly used examples to help you practice clean and concise code.
### General
- ```randFloat()``` generates a random float from 0 to 1.
- ```randWeightFloat(f32 range)``` is shorthand for ```(range * (f32)rand()) / 32768.0f;```, which generates a random number from 0 to `range`.
### Enemies
- ```CG_PARMS(x)``` is shorthand for ```static_cast<Parms*>(x->m_parms)```.
- ```C_PARMS``` is shorthand for ```static_cast<Parms*>(this->m_parms)```.
- ```CG_PROPERPARMS(x)``` is shorthand for ```static_cast<Parms*>(x->m_parms)->m_properParms```.
- ```C_PROPERPARMS(x)``` is shorthand for ```static_cast<Parms*>(this->m_parms)->m_properParms```.
## Progress
### DONE
- sysGCU

View File

@ -18,7 +18,8 @@ extern "C" {
#define fabs(x) __fabs(x)
// #define __frsqrtes opword
#define SQUARE(v) ((v) * (v))
#define SQUARE(v) ((v) * (v))
#define IS_WITHIN_CIRCLE(x, z, radius) ((SQUARE(x) + SQUARE(z)) < SQUARE(radius))
#define VECTOR_SQUARE_MAG(v) (SQUARE(v.x) + SQUARE(v.y) + SQUARE(v.z))

View File

@ -12,7 +12,7 @@ int rand();
inline f32 randFloat() { return (float)rand() / 32768.0f; }
inline f32 randWeightFloat(f32 x) { return (x * (f32)rand()) / 32768.0f; }
inline f32 randWeightFloat(f32 range) { return (range * (f32)rand()) / 32768.0f; }
#ifdef __cplusplus
};

View File

@ -87,6 +87,14 @@ struct CreatureKillArg {
int _04; // _04
};
// Shorthand parms casting
#define CG_PARMS(x) (static_cast<Parms*>(x->m_parms))
#define C_PARMS (CG_PARMS(this))
// Shorthand parms casting + proper parms
#define CG_PROPERPARMS(x) (CG_PARMS(x)->m_properParms)
#define C_PROPERPARMS (CG_PROPERPARMS(this))
struct Creature : public CellObject {
struct CheckHellArg {
inline CheckHellArg()

View File

@ -461,10 +461,22 @@ struct EnemyBase : public Creature, public SysShape::MotionListener, virtual pub
void becomeCarcass(bool);
inline void getSeparation(Creature* creature, Vector2f& sep)
inline void getDistanceTo(Creature* creature, Vector2f& distanceResult)
{
sep.x = getPosition().x - creature->getPosition().x;
sep.y = getPosition().z - creature->getPosition().z;
distanceResult.x = getPosition().x - creature->getPosition().x;
distanceResult.y = getPosition().z - creature->getPosition().z;
}
inline bool isCreatureWithinRange(Creature* c, f32 range)
{
Vector2f delta;
getDistanceTo(c, delta);
if (IS_WITHIN_CIRCLE(delta.x, delta.y, range)) {
return true;
}
return false;
}
inline void setEnemyIndexForType(u8 idx) { m_enemyIndexForType = idx; }

View File

@ -100,7 +100,7 @@ struct Obj : public EnemyBase {
bool isFinalFloor();
void appearFanfare();
inline Parms* getParms() { return static_cast<Parms*>(m_parms); }
inline Parms* getParms() { return C_PARMS; }
// _00 = VTBL
// _00-_2BC = EnemyBase

View File

@ -116,9 +116,9 @@ struct Parms : public EnemyParmsBase {
struct ProperParms : public Parameters {
inline ProperParms(); // likely
Parm<f32> _808; // _804, type unsure
Parm<f32> _830; // _82C, type unsure
Parm<f32> m_fp03; // _854
Parm<f32> _808; // _804, type unsure
Parm<f32> _830; // _82C, type unsure
Parm<f32> m_bulborbWakeRadius; // _854
};
Parms();

View File

@ -72,7 +72,7 @@ struct Obj : public EnemyBase {
virtual f32 getDownSmokeScale() { return 0.4f; } // _2EC (weak)
//////////////// VTABLE END
inline Parms* getParms() { return static_cast<Parms*>(m_parms); }
inline Parms* getParms() { return C_PARMS; }
// _00 = VTBL
// _00-_2BC = EnemyBase

View File

@ -123,7 +123,7 @@ struct Obj : public EnemyBase {
void effectDrawOn();
void effectDrawOff();
inline Parms* getParms() { return static_cast<Parms*>(m_parms); }
inline Parms* getParms() { return C_PARMS; }
// _00 = VTBL
// _00-_2BC = EnemyBase

View File

@ -86,9 +86,8 @@ struct AABBWaterBox : public WaterBox {
f32 _30; // _30
f32 _34; // _34
f32 _38; // _38
f32 _3C; // _3C
f32 _40; // _40
Vector3f _44; // _44
Vector2f m_xzPieceSize; // _3C, length from one side to another, divided by some number (so as to split into pieces)
Vector3f m_centrePoint; // _44
SysShape::Model* m_model; // _50
Sys::MatLoopAnimator m_matAnimator; // _54
J3DTexture* _60; // _60

View File

@ -73,7 +73,7 @@ struct Model : MtxObject {
}
// VTBL _00
u8 _04; // _04
bool m_isAnimating; // _04, assumed name
u8 _05; // _05
bool m_isVisible; // _06
J3DModel* m_j3dModel; // _08

View File

@ -44,7 +44,7 @@ void Obj::onInit(CreatureInitArg* args)
m_isAlive = true;
setupLodParms();
f32 r = randWeightFloat(static_cast<Parms*>(m_parms)->m_properParms.m_waitTime.m_value);
f32 r = randWeightFloat(C_PROPERPARMS.m_waitTime.m_value);
WaitStateArg arg;
arg.m_waitTimer = r;
m_FSM->start(this, HIBA_Wait, &arg);

View File

@ -251,39 +251,37 @@ WalkSmokeEffect::Mgr* Obj::getWalkSmokeEffectMgr() { return &m_walkSmokeMgr; }
*/
bool Obj::isWakeup()
{
bool check = false;
bool shouldWakeup = false;
switch (getEnemyTypeID()) {
case EnemyTypeID::EnemyID_BlueChappy:
Sys::Sphere ball;
f32 radius = static_cast<Parms*>(m_parms)->m_properParms.m_fp03.m_value;
ball.m_position = m_position;
ball.m_radius = radius;
case EnemyTypeID::EnemyID_BlueChappy: // Orange bulborb
f32 radius = C_PROPERPARMS.m_bulborbWakeRadius.m_value;
Sys::Sphere detectionSphere(m_position, radius);
CellIteratorArg iterArg(ball);
CellIterator iterator(iterArg);
iterator.first();
while (!iterator.isDone()) {
Creature* creature = static_cast<Creature*>(*iterator);
if (creature->isAlive() && (creature->isNavi() || creature->isPiki())) {
float privateRadius = static_cast<Parms*>(m_parms)->m_properParms.m_fp03.m_value;
Vector2f delta;
getSeparation(creature, delta);
CellIteratorArg iterArg(detectionSphere);
CellIterator i(iterArg);
for (i.first(); !i.isDone(); i.next()) {
Creature* c = static_cast<Creature*>(*i);
if ((SQUARE(delta.x) + SQUARE(delta.y)) < SQUARE(privateRadius)) {
check = true;
// If the creature is alive and a navigator or Piki
if (c->isAlive() && (c->isNavi() || c->isPiki())) {
radius = C_PROPERPARMS.m_bulborbWakeRadius.m_value;
if (isCreatureWithinRange(c, radius)) {
// And within the private radius, then wakeup
shouldWakeup = true;
break;
}
}
iterator.next();
}
break;
default:
if (isEvent(0, EB_Damage) || isEvent(0, EB_Collision)) {
check = true;
shouldWakeup = true;
}
break;
}
return check;
return shouldWakeup;
}
/*
@ -310,15 +308,15 @@ void Obj::setCollEvent(CollEvent& collEvent)
*/
void Obj::flickStatePikmin()
{
Parms* stickParms = static_cast<Parms*>(m_parms);
Parms* stickParms = C_PARMS;
EnemyFunc::flickStickPikmin(this, stickParms->m_general.m_shakeRateMaybe.m_value, stickParms->m_general.m_shakeKnockback.m_value,
stickParms->m_general.m_shakeDamage.m_value, getFaceDir(), nullptr);
Parms* nearPikiParms = static_cast<Parms*>(m_parms);
Parms* nearPikiParms = C_PARMS;
EnemyFunc::flickNearbyPikmin(this, nearPikiParms->m_general.m_shakeRange.m_value, nearPikiParms->m_general.m_shakeKnockback.m_value,
nearPikiParms->m_general.m_shakeDamage.m_value, getFaceDir(), nullptr);
Parms* nearNaviParms = static_cast<Parms*>(m_parms);
Parms* nearNaviParms = C_PARMS;
EnemyFunc::flickNearbyNavi(this, nearNaviParms->m_general.m_shakeRange.m_value, nearNaviParms->m_general.m_shakeKnockback.m_value,
nearNaviParms->m_general.m_shakeDamage.m_value, getFaceDir(), nullptr);
}
@ -355,7 +353,7 @@ void Obj::flickAttackBomb()
*/
void Obj::flickAttackFail()
{
Parms* parms = static_cast<Parms*>(m_parms);
Parms* parms = C_PARMS;
EnemyFunc::flickStickPikmin(this, parms->m_general.m_shakeRateMaybe.m_value, parms->m_general.m_shakeKnockback.m_value,
parms->m_general.m_shakeDamage.m_value, getFaceDir(), nullptr);
}