struct weapontable
{
weapontable();
char *flags; /* Default flag set */
char *name; /* Descriptive name */
float weight; /* Base weight */
float cost; /* Base cost/value */
short type; /* Weapon type */
short wd; /* Base damage */
short skill; /* Skill type */
short damage; /* Damage type */
};
vector<weapontable*> w_table;
weapontable::weapontable()
{
init_memory( &flags, &damage, sizeof( damage ) );
}
void save_weapontable()
{
ofstream stream;
stream.open( WTYPE_FILE );
if( !stream.is_open() )
{
log_string( "Couldn't write to weapontypes file." );
return;
}
for( int x = 0; x < TWTP_MAX; ++x )
{
stream << "#WTYPE" << endl;
stream << "Type " << weapon_type[x].type << endl;
stream << "Name " << weapon_type[x].name << endl;
stream << "WD " << weapon_type[x].wd << endl;
stream << "Weight " << weapon_type[x].weight << endl;
stream << "Cost " << weapon_type[x].cost << endl;
stream << "Skill " << weapon_type[x].skill << endl;
stream << "Damage " << weapon_type[x].damage << endl;
stream << "Flags " << weapon_type[x].flags << endl;
stream << "End" << endl << endl;
}
stream.close();
}
void load_weapontable()
{
ifstream stream;
weapontable *wt;
w_table.clear();
stream.open( WTYPE_FILE );
if( !stream.is_open() )
{
log_string( "Couldn't read from weapontypes file." );
return;
}
do
{
string key, value;
char buf[MSL];
stream >> key;
strip_lspace( key );
stream.getline( buf, MSL );
value = buf;
strip_lspace( value );
if( key == "#WTYPE" )
wt = new weapontable;
else if( key == "Type" )
wt->type = atoi( value.c_str() );
else if( key == "Name" )
wt->name = STRALLOC( value.c_str() );
else if( key == "WD" )
wt->wd = atoi( value.c_str() );
else if( key == "Weight" )
wt->weight = atoi( value.c_str() );
else if( key == "Cost" )
wt->cost = atoi( value.c_str() );
else if( key == "Skill" )
wt->skill = atoi( value.c_str() );
else if( key == "Damage" )
wt->damage = atoi( value.c_str() );
else if( key == "Flags" )
{
wt->flags = STRALLOC( value.c_str() );
log_printf( ".... %s", wt->flags );
}
else if( key == "End" )
w_table.push_back( wt );
}
while( !stream.eof() );
stream.close();
}
CMDF( do_wtsave )
{
save_weapontable();
ch->print( "Done.\r\n" );
}
CMDF( do_wtload )
{
load_weapontable();
for( int x = 0; x < w_table.size(); ++x )
{
weapontable *w = w_table[x];
log_printf( "Type %d, Name %s, WD %d, Weight %d, Cost %d, Skill %d, Damage %d, Flags %s",
w->type, w->name, w->wd, w->weight, w->cost, w->skill, w->damage, w->flags );
ch->printf( "Type %d, Name %s, WD %d, Weight %d, Cost %d, Skill %d, Damage %d, Flags %s\r\n",
w->type, w->name, w->wd, w->weight, w->cost, w->skill, w->damage, w->flags );
}
}
Data file:
#WTYPE
Type 0
Name Not Defined
WD 0
Weight 0
Cost 0
Skill 0
Damage 0
Flags brittle
End
#WTYPE
Type 1
Name Dagger
WD 4
Weight 4
Cost 200
Skill 2
Damage 6
Flags anticleric antimonk metal
End
#WTYPE
Type 2
Name Claw
WD 5
Weight 4
Cost 300
Skill 4
Damage 1
Flags anticleric antimonk metal
End
#WTYPE
Type 3
Name Shortsword
WD 6
Weight 6
Cost 500
Skill 1
Damage 6
Flags anticleric antimonk antimage antinecro antidruid metal
End
#WTYPE
Type 4
Name Longsword
WD 8
Weight 10
Cost 800
Skill 1
Damage 1
Flags anticleric antimonk antimage antinecro antidruid metal
End
#WTYPE
Type 5
Name Claymore
WD 10
Weight 20
Cost 1600
Skill 1
Damage 1
Flags anticleric antimonk antimage antinecro antidruid antibard antirogue twohand metal
End
#WTYPE
Type 6
Name Mace
WD 8
Weight 12
Cost 700
Skill 5
Damage 4
Flags antimonk antimage antinecro antirogue metal
End
#WTYPE
Type 7
Name Maul
WD 10
Weight 24
Cost 1500
Skill 5
Damage 4
Flags antimonk antimage antinecro antirogue twohand metal
End
#WTYPE
Type 8
Name Staff
WD 7
Weight 8
Cost 600
Skill 11
Damage 4
Flags twohand
End
#WTYPE
Type 9
Name Axe
WD 9
Weight 14
Cost 800
Skill 9
Damage 3
Flags anticleric antimonk antimage antinecro antidruid antirogue metal
End
#WTYPE
Type 10
Name War Axe
WD 11
Weight 26
Cost 1600
Skill 9
Damage 3
Flags anticleric antimonk antimage antinecro antidruid antirogue twohand metal
End
#WTYPE
Type 11
Name Spear
WD 7
Weight 10
Cost 600
Skill 10
Damage 7
Flags antimage anticleric antinecro twohand
End
#WTYPE
Type 12
Name Pike
WD 9
Weight 15
Cost 900
Skill 10
Damage 7
Flags anticleric antimonk antimage antinecro antidruid antirogue twohand
End
#WTYPE
Type 13
Name Whip
WD 5
Weight 2
Cost 150
Skill 3
Damage 5
Flags antimonk organic
End
Saavy users might recognize this bit of code, but if not, no worries. Whenever the wtload command is executed, the result is a segmentation fault. The log message displayed last is:
Sun Mar 19, 2006 9:19:46 PM PST :: Samson returns from beyond the void.
Sun Mar 19, 2006 9:19:49 PM PST :: .... brittle
Sun Mar 19, 2006 9:19:49 PM PST :: .... anticleric antimonk metal
Sun Mar 19, 2006 9:19:49 PM PST :: .... anticleric antimonk metal
Sun Mar 19, 2006 9:19:49 PM PST :: .... anticleric antimonk antimage antinecro antidruid metal
Sun Mar 19, 2006 9:19:49 PM PST :: .... anticleric antimonk antimage antinecro antidruid metal
Sun Mar 19, 2006 9:19:49 PM PST :: .... anticleric antimonk antimage antinecro antidruid antibard antirogue twohand metal
Sun Mar 19, 2006 9:19:49 PM PST :: .... antimonk antimage antinecro antirogue metal
Sun Mar 19, 2006 9:19:49 PM PST :: .... antimonk antimage antinecro antirogue twohand metal
Sun Mar 19, 2006 9:19:49 PM PST :: .... twohand
Sun Mar 19, 2006 9:19:49 PM PST :: .... anticleric antimonk antimage antinecro antidruid antirogue metal
Sun Mar 19, 2006 9:19:49 PM PST :: .... anticleric antimonk antimage antinecro antidruid antirogue twohand metal
Sun Mar 19, 2006 9:19:49 PM PST :: .... antimage anticleric antinecro twohand
Sun Mar 19, 2006 9:19:49 PM PST :: .... anticleric antimonk antimage antinecro antidruid antirogue twohand
Sun Mar 19, 2006 9:19:49 PM PST :: .... antimonk organic
Sun Mar 19, 2006 9:19:49 PM PST :: Type 0, Name Not Defined, WD 0, Weight 0, Cost 0, Skill 0, Damage 0, Flags (null)
That is obviously in the wtload command, suggesting it loaded the data in properly. However the log output says otherwise since the flags value turned NULL. Valgrind bitches about address 0x2 not being allocated, no surprise, it's crashing, I figured that much. The stack is smashed, so a core dump is worthless and Valgrind is of no value once the stack is smashed.
My best guess is that I'm using vector improperly, but I can't see how. Anyone have any ideas?