Alright, here's an actual coding question for all you smart people.
Originally when creating a new module I was just using LINK to throw the new one onto the end of the linked list for the ships installed mods. I had also created a function that would be used to sort all the installed modules in the order I wanted them. It's just one big mess of for loop after for loop and I wasn't very happy with it. So after talking with my dad the other night and telling him about it, he suggested using something to insert the new module exactly where I wanted it during creation instead of just throwing it onto the end and having to run that stupid sorting function. So I took a look at the INSERT macro in mud.h and I'd like someone who has more experience than me to take a look at my updated creation function and tell me if it looks good, or what I can do to get it to work better.
MODULE_DATA *new_module( SHIP_DATA *ship, int type )
{
MODULE_DATA *module = NULL;
MODULE_DATA *insert = NULL;
if ( ship == NULL )
{
bug( "new_module: null ship passed to function!" );
return NULL;
}
CREATE( module, MODULE_DATA, 1 );
module->mod_val[0] = type;
module->mod_val[1] = 0; // condition - 100 = perfect condition
module->mod_val[2] = 0; // size
module->mod_val[3] = 1; // state - 1 = in working order but turned off
module->mod_val[4] = 0; // atype - 0 is default of base system
module->mod_val[5] = 0; // varies
module->mod_val[6] = 0; // varies
module->mod_vnum = 0; // varies, naturally
// alright, now comes the fun part. linking the new module into the ships module list
// hmm, is this the first module?
if ( !ship->first_module )
LINK( module, ship->first_module, ship->last_module, next, prev ); // yep
else
{ // guess not..
insert = ship->first_module;
do // let's run through this at least once
{
if ( insert == ship->first_module && insert->mod_val[MOD_TYPE] > type ) // ok, if we're pointing at the first ship mod,
{ // and it's type is greater than what we're installing...
INSERT( module, insert, ship->first_module, next, prev ); // stick the new mod into the list before the old one
break; // and the new one becomes ship->first_module
}
if ( insert->mod_val[MOD_TYPE] < type ) // is the current mods type less than the one we're installing?
{
if ( insert->next == NULL ) // if there's only the one already installed
{
LINK( module, ship->first_module, ship->last_module, next, prev ); // just throw the new one onto the end
break;
}
if ( insert->next->mod_val[MOD_TYPE] != type ) // ok, there's more than one, but is the next one a different type?
{
INSERT( module, insert->next, ship->first_module, next, prev ); // throw it into the list before the next one
break;
}
// if we've gotten this far, then the current mod is the same as the one we're installing,
// and so is the next. So we'll just let the mod we're pointing at get set the the next in
// the list of installed mods and let everything run through again, until we hit the end.
}
insert = insert->next;
}while( insert->next != NULL );
}
// now then, just to make sure that everything was done correctly up above, lets make sure that the list of installed mods is good
CHECK_LINKS( ship->first_module, ship->last_module, next, prev, MODULE_DATA );
return module;
}
And this is in the module_install function, which calls new_module.
if ( ( module = new_module( ship, obj->value[MOD_TYPE] ) == NULL ) )
{
bug( "module_install: could not create new module" );
return;
}
Well, there you have it. What have I screwed up?