A lot of the time when I'm sifting through the forums I see questions being asked that are so vague, only a psychic would have a better chance. And then there's those that post a code snippet!!!
Why can't people format their code!?!?
Please will magazines, other website, even "TEACHERS" teach people how to indent their code... It's annoying...
Simple things like:
. Using the TAB, Indenting functions, loops, etc.
. Comments, comments, comments, comments (by the way more comments on large snippets)
IT REALLY IS THE DIFFERENCE BETWEEN THIS: (make NO difference about the language)
(from a piece of my own code)
bool PROC_MAP::add_room()
{
int sides = ( rand() % ( rseg_max - rseg_min ) ) + rseg_min;
int edge_size = ( rand() % ( room_max - room_min ) ) + room_min;
_VERT* v = new _VERT[sides+1];
_EDGE* curr_edge;
do {
curr_edge = &edges[ ( rand() % (int)edges.size() ) ];
} while ( curr_edge->type != PM_COUTER && curr_edge->type != PM_ROUTER );
int v1 = curr_edge->v1, v2 = curr_edge->v2;
v[0].x = verts[v2].x;
v[0].y = verts[v2].z;
v[1].x = verts[v1].x;
v[1].y = verts[v1].z;
float curr_angle = atan2( v[1].y - v[0].y, v[1].x - v[0].x );
float diff_angle = Deg2Rad( 360.0f / sides );
v[1].z = curr_angle;
int loop;
for ( loop = 2; loop < sides; loop++ )
{
curr_angle = curr_angle + diff_angle;
v[loop].x = v[loop-1].x + cos( curr_angle ) * edge_size;
v[loop].y = v[loop-1].y + sin( curr_angle ) * edge_size;
v[loop].z = curr_angle;
}
vvv_circle( v[0].x, v[0].y,
v[1].x, v[1].y,
v[2].x, v[2].y,
&v[sides].x, &v[sides].y );
float x, y, inner_angle, z;
for ( loop = 1; loop < sides - 1; loop++ )
{
x = v[loop].x + cos( v[loop+1].z ) * 0.5f;
y = v[loop].y + sin( v[loop+1].z ) * 0.5f;
if ( edge_intersect( x, y, v[loop+1].x, v[loop+1].y ) )
{
delete[] v;
return 0;
}
}
for ( loop = 0; loop < sides; loop++ )
{
inner_angle = atan2( v[sides].y - v[loop].y, v[sides].x - v[loop].x );
x = v[loop].x + cos( inner_angle ) * 0.5f;
y = v[loop].y + sin( inner_angle ) * 0.5f;
if ( edge_intersect( x, y, v[sides].x, v[sides].y ) )
{
delete[] v;
return 0;
}
}
inner_angle = atan2( v[sides-1].y - v[0].y, v[sides-1].x - v[0].x );
x = v[0].x + cos( inner_angle ) * 0.5f;
y = v[0].y + sin( inner_angle ) * 0.5f;
if ( edge_intersect( x, y, v[sides-1].x, v[sides-1].y ) )
{
delete[] v;
return 0;
}
int vsize = (int)verts.size();
int esize = (int)edges.size();
int fsize = (int)faces.size();
curr_edge->type = PM_RENTRY;
x = v[sides].x;
y = v[sides].y;
verts.push_back( _VERT( x, 0, y ) );
verts.push_back( _VERT( verts[v1].x, verts[v1].y, verts[v1].z ) );
verts.push_back( _VERT( verts[v2].x, verts[v2].y, verts[v2].z ) );
edges.push_back( _EDGE( vsize + 1, vsize + 2, PM_RENTRY, curr_segment ) );
faces.push_back( _FACE( vsize + 2, vsize + 1, vsize, PM_RFACE, curr_segment ) );
edges.push_back( _EDGE( vsize + 1, vsize, PM_RINNER, curr_segment ) );
edges.push_back( _EDGE( vsize + 2, vsize, PM_RINNER, curr_segment ) );
int last_vert = vsize + 1;
for ( loop = 2; loop < sides; loop++ )
{
x = v[loop].x;
z = v[loop].y;
y = 0;
int pos = vsize + loop - 1 + 2;
verts.push_back( _VERT( x, y, z ) );
faces.push_back( _FACE( vsize, last_vert, pos, PM_RFACE, curr_segment ) );
edges.push_back( _EDGE( last_vert, vsize, PM_RINNER, curr_segment ) );
edges.push_back( _EDGE( last_vert, pos, PM_ROUTER, curr_segment ) );
last_vert = pos;
}
last_vert = (int)verts.size() - 1;
edges.push_back( _EDGE( last_vert, vsize, PM_RINNER, curr_segment ) );
edges.push_back( _EDGE( last_vert, vsize + 2, PM_ROUTER, curr_segment ) );
faces.push_back( _FACE( last_vert, vsize + 2, vsize, PM_RFACE, curr_segment ) );
delete[] v;
return 1;
}
And THIS:
bool PROC_MAP::add_room()
{
// get the number of sides that make up the new room (within the range)
int sides = ( rand() % ( rseg_max - rseg_min ) ) + rseg_min;
// use same calc to find the edge size
int edge_size = ( rand() % ( room_max - room_min ) ) + room_min;
// setup an array to store the temp room verts
_VERT* v = new _VERT[sides+1]; // extra vert for centre of the room
_EDGE* curr_edge;
// find an outer edge to attach the room to
do {
curr_edge = &edges[ ( rand() % (int)edges.size() ) ];
} while ( curr_edge->type != PM_COUTER && curr_edge->type != PM_ROUTER );
// get vert ID's for the original edge
int v1 = curr_edge->v1, v2 = curr_edge->v2;
// get coords of original edge (using reverse direction)
v[0].x = verts[v2].x;
v[0].y = verts[v2].z;
v[1].x = verts[v1].x;
v[1].y = verts[v1].z;
// define the starting angle and the step angle
float curr_angle = atan2( v[1].y - v[0].y, v[1].x - v[0].x );
float diff_angle = Deg2Rad( 360.0f / sides );
// store edges angle in z
v[1].z = curr_angle;
int loop;
// store new verts for room
for ( loop = 2; loop < sides; loop++ )
{
curr_angle = curr_angle + diff_angle;
v[loop].x = v[loop-1].x + cos( curr_angle ) * edge_size;
v[loop].y = v[loop-1].y + sin( curr_angle ) * edge_size;
v[loop].z = curr_angle;
}
// now calc the centre of the room for the last vert
vvv_circle( v[0].x, v[0].y,
v[1].x, v[1].y,
v[2].x, v[2].y,
&v[sides].x, &v[sides].y );
float x, y, inner_angle, z;
// check new edges (not the last one though yet)
for ( loop = 1; loop < sides - 1; loop++ )
{
// nudge the beginning coords a tad
x = v[loop].x + cos( v[loop+1].z ) * 0.5f;
y = v[loop].y + sin( v[loop+1].z ) * 0.5f;
// if edge collides with map then exit
if ( edge_intersect( x, y, v[loop+1].x, v[loop+1].y ) )
{
delete[] v;
return 0;
}
}
// now check all inner edges (using the centre coord of the room)
for ( loop = 0; loop < sides; loop++ )
{
// get angle of current inner edge
inner_angle = atan2( v[sides].y - v[loop].y, v[sides].x - v[loop].x );
// nudge the start coords a little
x = v[loop].x + cos( inner_angle ) * 0.5f;
y = v[loop].y + sin( inner_angle ) * 0.5f;
// if the edge collides with the map then exit
if ( edge_intersect( x, y, v[sides].x, v[sides].y ) )
{
delete[] v;
return 0;
}
}
// now check the last outer edge of the room
inner_angle = atan2( v[sides-1].y - v[0].y, v[sides-1].x - v[0].x );
x = v[0].x + cos( inner_angle ) * 0.5f;
y = v[0].y + sin( inner_angle ) * 0.5f;
// if collides with map then just exit
if ( edge_intersect( x, y, v[sides-1].x, v[sides-1].y ) )
{
delete[] v;
return 0;
}
// Now it is safe to add the room to the main map
int vsize = (int)verts.size();
int esize = (int)edges.size();
int fsize = (int)faces.size();
// change current edge to a room entry point (doorway to room)
curr_edge->type = PM_RENTRY;
// add centre of rooms coordinate
x = v[sides].x;
y = v[sides].y;
verts.push_back( _VERT( x, 0, y ) );
//
// now add the original edge for the limb addition
// NB: Temp taking this out
//
verts.push_back( _VERT( verts[v1].x, verts[v1].y, verts[v1].z ) );
verts.push_back( _VERT( verts[v2].x, verts[v2].y, verts[v2].z ) );
// add this new edge
edges.push_back( _EDGE( vsize + 1, vsize + 2, PM_RENTRY, curr_segment ) );
// add first face ( current edge with centre of room )
faces.push_back( _FACE( vsize + 2, vsize + 1, vsize, PM_RFACE, curr_segment ) );
// now add the new edges to the list
edges.push_back( _EDGE( vsize + 1, vsize, PM_RINNER, curr_segment ) );
edges.push_back( _EDGE( vsize + 2, vsize, PM_RINNER, curr_segment ) );
// vsize refers to centre room vert as well
// this is used to record the last vert used in the next loop
int last_vert = vsize + 1;
//int v2 = curr_edge->v2;
// now add the rest of the verts, edges and faces
for ( loop = 2; loop < sides; loop++ )
{
x = v[loop].x;
z = v[loop].y;
y = 0;
int pos = vsize + loop - 1 + 2;
// add the new vert
verts.push_back( _VERT( x, y, z ) );
// add the face
faces.push_back( _FACE( vsize, last_vert, pos, PM_RFACE, curr_segment ) );
// add the edges
edges.push_back( _EDGE( last_vert, vsize, PM_RINNER, curr_segment ) );
edges.push_back( _EDGE( last_vert, pos, PM_ROUTER, curr_segment ) );
// update last_vert
last_vert = pos;
}
// fix the last face now
last_vert = (int)verts.size() - 1;
// The bug here was the verts array had been moved in memory so the curr_edge pointer
// was now invalid at this point...
edges.push_back( _EDGE( last_vert, vsize, PM_RINNER, curr_segment ) );
edges.push_back( _EDGE( last_vert, vsize + 2, PM_ROUTER, curr_segment ) );
faces.push_back( _FACE( last_vert, vsize + 2, vsize, PM_RFACE, curr_segment ) );
delete[] v;
return 1; // 1 means success / 0 means failed
}
DBP SNIPPETS are so much worse off without formatting...
Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!