I've asked in this post earlier how I can read the following out of an conf file:
Example of de_dust2 (Counter-Strike:Source)
Code:
spawnpointslist = (
{
position = [ 448.0, 2464.0, -88.0 ];
angle = [ 0.0, 255.0, 0.0 ];
classname = "info_player_counterterrorist";
},
{
position = [ 352.0, 2464.0, -88.0 ];
angle = [ 0.0, 255.0, 0.0 ];
classname = "info_player_counterterrorist";
},
{
position = [ 256.0, 2464.0, -88.0 ];
angle = [ 0.0, 255.0, 0.0 ];
classname = "info_player_counterterrorist";
},
[...]
{
position = [ -1892.041138, 1261.15564, 32.03125 ];
angle = [ 7.692320347, -155.0625458, 0.0 ];
classname = "info_player_terrorist";
} );
Berni helped me a lot, so thank you Berni!
He pointed out the different types in libconfig (
http://www.hyperrealm.com/libconfig/....html#Settings)
So in the example above, we have a list called spawnpointslist, it contains groups (groups can't have names) and those groups contains values (array, array and string for position, angle and classname).
Load:
To read/load this file I'm using the following code:
PHP Code:
stock LoadSpawnPointsFromFile(){
new Handle:hSpawnPointsList = ConfigLookup(g_hSpawnPoint_MapConfig, "spawnpointslist");
if(hSpawnPointsList == INVALID_HANDLE){
LogError("can't load spawn points from file, it appears to be empty");
return;
}
new length = ConfigSettingLength(hSpawnPointsList);
for (new i=0; i<length; i++) {
new Handle:hSpawnPoint = ConfigSettingGetElement(hSpawnPointsList, i);
new Handle:hPosition = ConfigSettingGetMember(hSpawnPoint, "position");
new Float:position[3];
ConfigSettingGetVector(hPosition,position);
//PrintToServer("LOAD spawnpoint - %fx; %fy; %fz;",position[0],position[1],position[2]);
new Handle:hAngle = ConfigSettingGetMember(hSpawnPoint, "angle");
new Float:angle[3];
ConfigSettingGetVector(hAngle,angle);
new Handle:hClassname = ConfigSettingGetMember(hSpawnPoint, "classname");
new String:classname[MAX_NAME_LENGTH];
ConfigSettingGetString(hClassname, classname, sizeof(classname));
CreateSpawnPoint(position,angle,classname);
//never close setting handles!
}
LogMessage("loaded %d spawn points from file",length);
//dont close the global config handle to keep using it for the hole map.
}
The easy way to get an array with a size of 3 (aka vector):
PHP Code:
stock ConfigSettingGetVector(Handle:Setting, Float:vec[3]){
vec[0] = ConfigSettingGetFloat(ConfigSettingGetElement(Setting, 0));
vec[1] = ConfigSettingGetFloat(ConfigSettingGetElement(Setting, 1));
vec[2] = ConfigSettingGetFloat(ConfigSettingGetElement(Setting, 2));
}
Save:
So I want to save those spawn points too.
I'm using 2 functions for this, since I want to save one spawn point only or all at once.
PHP Code:
stock SaveSpawnPointsToFile(const bool:fetchSpawnPointsFirst=false){
new countSpawnPoints = 0;
if(fetchSpawnPointsFirst){
//'clear' spawn points memory by creating a new config handle
CloseHandle(g_hSpawnPoint_MapConfig);
g_hSpawnPoint_MapConfig = ConfigCreate();
ConfigSettingAdd(ConfigRootSetting(g_hSpawnPoint_MapConfig),"spawnpointslist",ST_List);
for(new i=0;i<sizeof(g_szSpawnPointClassNames);i++){
new entity = -1;
while ((entity = FindEntityByClassname(entity, g_szSpawnPointClassNames[i])) != INVALID_ENT_REFERENCE) {
new Float:position[3];
Entity_GetAbsOrigin(entity,position);
new Float:angle[3];
Entity_GetAbsAngles(entity,angle);
SaveSpawnPoint(position,angle,g_szSpawnPointClassNames[i]);
countSpawnPoints++;
//never close setting handles!
}
}
}
else {
countSpawnPoints = ConfigSettingLength(ConfigLookup(g_hSpawnPoint_MapConfig, "spawnpointslist"));
}
if(ConfigWriteFile(g_hSpawnPoint_MapConfig,g_szConfig_Path)){
LogMessage("successfully written %d spawn points into a file at %s",countSpawnPoints,g_szConfig_Path);
}
else {
SetFailState("can't write file at %s",g_szConfig_Path);
}
//dont close the global config handle to keep using it for the hole map.
}
So this actually does the push into the config handle:
PHP Code:
stock SaveSpawnPoint(const Float:position[3], const Float:angle[3], const String:classname[MAX_NAME_LENGTH]){
new Handle:hSpawnPointsList = ConfigLookup(g_hSpawnPoint_MapConfig, "spawnpointslist");
new Handle:hSpawnPoint = ConfigSettingAdd(hSpawnPointsList,"spawnpoint",ST_Group);
new Handle:hPosition = ConfigSettingAdd(hSpawnPoint,"position",ST_Array);
ConfigSettingSetVector(hPosition, position);
//PrintToServer("spawnpoint %d - %fx; %fy; %fz;",entity,position[0],position[1],position[2]);
new Handle:hAngle = ConfigSettingAdd(hSpawnPoint,"angle",ST_Array);
ConfigSettingSetVector(hAngle, angle);
new Handle:hClassname = ConfigSettingAdd(hSpawnPoint,"classname",ST_String);
ConfigSettingSetString(hClassname,classname);
//DON'T CLOSE SETTING HANDLES!
}
The easy way to set an array with a size of 3 (aka vector):
PHP Code:
stock ConfigSettingSetVector(Handle:Setting, const Float:vec[3]){
ConfigSettingSetFloat(ConfigSettingAdd(Setting,"",ST_Float), vec[0]);
ConfigSettingSetFloat(ConfigSettingAdd(Setting,"",ST_Float), vec[1]);
ConfigSettingSetFloat(ConfigSettingAdd(Setting,"",ST_Float), vec[2]);
}
I hope this helps those who are stuck like I was.
Please don't as questions about this code snipped in this thread, to keep this thread clean!
Contact me via PM and I'll answer your questions and edit this post.
__________________