aas_cfg.c (7976B)
1 /* 2 =========================================================================== 3 Copyright (C) 1999-2005 Id Software, Inc. 4 5 This file is part of Quake III Arena source code. 6 7 Quake III Arena source code is free software; you can redistribute it 8 and/or modify it under the terms of the GNU General Public License as 9 published by the Free Software Foundation; either version 2 of the License, 10 or (at your option) any later version. 11 12 Quake III Arena source code is distributed in the hope that it will be 13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with Foobar; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 =========================================================================== 21 */ 22 23 #include "qbsp.h" 24 #include "float.h" 25 #include "../botlib/aasfile.h" 26 #include "aas_store.h" 27 #include "aas_cfg.h" 28 #include "../botlib/l_precomp.h" 29 #include "../botlib/l_struct.h" 30 #include "../botlib/l_libvar.h" 31 32 //structure field offsets 33 #define BBOX_OFS(x) (int)&(((aas_bbox_t *)0)->x) 34 #define CFG_OFS(x) (int)&(((cfg_t *)0)->x) 35 36 //bounding box definition 37 fielddef_t bbox_fields[] = 38 { 39 {"presencetype", BBOX_OFS(presencetype), FT_INT}, 40 {"flags", BBOX_OFS(flags), FT_INT}, 41 {"mins", BBOX_OFS(mins), FT_FLOAT|FT_ARRAY, 3}, 42 {"maxs", BBOX_OFS(maxs), FT_FLOAT|FT_ARRAY, 3}, 43 {NULL, 0, 0, 0} 44 }; 45 46 fielddef_t cfg_fields[] = 47 { 48 {"phys_gravitydirection", CFG_OFS(phys_gravitydirection), FT_FLOAT|FT_ARRAY, 3}, 49 {"phys_friction", CFG_OFS(phys_friction), FT_FLOAT}, 50 {"phys_stopspeed", CFG_OFS(phys_stopspeed), FT_FLOAT}, 51 {"phys_gravity", CFG_OFS(phys_gravity), FT_FLOAT}, 52 {"phys_waterfriction", CFG_OFS(phys_waterfriction), FT_FLOAT}, 53 {"phys_watergravity", CFG_OFS(phys_watergravity), FT_FLOAT}, 54 {"phys_maxvelocity", CFG_OFS(phys_maxvelocity), FT_FLOAT}, 55 {"phys_maxwalkvelocity", CFG_OFS(phys_maxwalkvelocity), FT_FLOAT}, 56 {"phys_maxcrouchvelocity", CFG_OFS(phys_maxcrouchvelocity), FT_FLOAT}, 57 {"phys_maxswimvelocity", CFG_OFS(phys_maxswimvelocity), FT_FLOAT}, 58 {"phys_walkaccelerate", CFG_OFS(phys_walkaccelerate), FT_FLOAT}, 59 {"phys_airaccelerate", CFG_OFS(phys_airaccelerate), FT_FLOAT}, 60 {"phys_swimaccelerate", CFG_OFS(phys_swimaccelerate), FT_FLOAT}, 61 {"phys_maxstep", CFG_OFS(phys_maxstep), FT_FLOAT}, 62 {"phys_maxsteepness", CFG_OFS(phys_maxsteepness), FT_FLOAT}, 63 {"phys_maxwaterjump", CFG_OFS(phys_maxwaterjump), FT_FLOAT}, 64 {"phys_maxbarrier", CFG_OFS(phys_maxbarrier), FT_FLOAT}, 65 {"phys_jumpvel", CFG_OFS(phys_jumpvel), FT_FLOAT}, 66 {"phys_falldelta5", CFG_OFS(phys_falldelta5), FT_FLOAT}, 67 {"phys_falldelta10", CFG_OFS(phys_falldelta10), FT_FLOAT}, 68 {"rs_waterjump", CFG_OFS(rs_waterjump), FT_FLOAT}, 69 {"rs_teleport", CFG_OFS(rs_teleport), FT_FLOAT}, 70 {"rs_barrierjump", CFG_OFS(rs_barrierjump), FT_FLOAT}, 71 {"rs_startcrouch", CFG_OFS(rs_startcrouch), FT_FLOAT}, 72 {"rs_startgrapple", CFG_OFS(rs_startgrapple), FT_FLOAT}, 73 {"rs_startwalkoffledge", CFG_OFS(rs_startwalkoffledge), FT_FLOAT}, 74 {"rs_startjump", CFG_OFS(rs_startjump), FT_FLOAT}, 75 {"rs_rocketjump", CFG_OFS(rs_rocketjump), FT_FLOAT}, 76 {"rs_bfgjump", CFG_OFS(rs_bfgjump), FT_FLOAT}, 77 {"rs_jumppad", CFG_OFS(rs_jumppad), FT_FLOAT}, 78 {"rs_aircontrolledjumppad", CFG_OFS(rs_aircontrolledjumppad), FT_FLOAT}, 79 {"rs_funcbob", CFG_OFS(rs_funcbob), FT_FLOAT}, 80 {"rs_startelevator", CFG_OFS(rs_startelevator), FT_FLOAT}, 81 {"rs_falldamage5", CFG_OFS(rs_falldamage5), FT_FLOAT}, 82 {"rs_falldamage10", CFG_OFS(rs_falldamage10), FT_FLOAT}, 83 {"rs_maxjumpfallheight", CFG_OFS(rs_maxjumpfallheight), FT_FLOAT}, 84 {NULL, 0, 0, 0} 85 }; 86 87 structdef_t bbox_struct = 88 { 89 sizeof(aas_bbox_t), bbox_fields 90 }; 91 structdef_t cfg_struct = 92 { 93 sizeof(cfg_t), cfg_fields 94 }; 95 96 //global cfg 97 cfg_t cfg; 98 99 //=========================================================================== 100 // the default Q3A configuration 101 // 102 // Parameter: - 103 // Returns: - 104 // Changes Globals: - 105 //=========================================================================== 106 void DefaultCfg(void) 107 { 108 int i; 109 110 // default all float values to infinite 111 for (i = 0; cfg_fields[i].name; i++) 112 { 113 if ((cfg_fields[i].type & FT_TYPE) == FT_FLOAT) 114 *(float *)( ((char*)&cfg) + cfg_fields[i].offset ) = FLT_MAX; 115 } //end for 116 // 117 cfg.numbboxes = 2; 118 //bbox 0 119 cfg.bboxes[0].presencetype = PRESENCE_NORMAL; 120 cfg.bboxes[0].flags = 0; 121 cfg.bboxes[0].mins[0] = -15; 122 cfg.bboxes[0].mins[1] = -15; 123 cfg.bboxes[0].mins[2] = -24; 124 cfg.bboxes[0].maxs[0] = 15; 125 cfg.bboxes[0].maxs[1] = 15; 126 cfg.bboxes[0].maxs[2] = 32; 127 //bbox 1 128 cfg.bboxes[1].presencetype = PRESENCE_CROUCH; 129 cfg.bboxes[1].flags = 1; 130 cfg.bboxes[1].mins[0] = -15; 131 cfg.bboxes[1].mins[1] = -15; 132 cfg.bboxes[1].mins[2] = -24; 133 cfg.bboxes[1].maxs[0] = 15; 134 cfg.bboxes[1].maxs[1] = 15; 135 cfg.bboxes[1].maxs[2] = 16; 136 // 137 cfg.allpresencetypes = PRESENCE_NORMAL|PRESENCE_CROUCH; 138 cfg.phys_gravitydirection[0] = 0; 139 cfg.phys_gravitydirection[1] = 0; 140 cfg.phys_gravitydirection[2] = -1; 141 cfg.phys_maxsteepness = 0.7; 142 } //end of the function DefaultCfg 143 //=========================================================================== 144 // 145 // Parameter: - 146 // Returns: - 147 // Changes Globals: - 148 //=========================================================================== 149 char * QDECL va( char *format, ... ) 150 { 151 va_list argptr; 152 static char string[2][32000]; // in case va is called by nested functions 153 static int index = 0; 154 char *buf; 155 156 buf = string[index & 1]; 157 index++; 158 159 va_start (argptr, format); 160 vsprintf (buf, format,argptr); 161 va_end (argptr); 162 163 return buf; 164 } //end of the function va 165 //=========================================================================== 166 // 167 // Parameter: - 168 // Returns: - 169 // Changes Globals: - 170 //=========================================================================== 171 void SetCfgLibVars(void) 172 { 173 int i; 174 float value; 175 176 for (i = 0; cfg_fields[i].name; i++) 177 { 178 if ((cfg_fields[i].type & FT_TYPE) == FT_FLOAT) 179 { 180 value = *(float *)(((char*)&cfg) + cfg_fields[i].offset); 181 if (value != FLT_MAX) 182 { 183 LibVarSet(cfg_fields[i].name, va("%f", value)); 184 } //end if 185 } //end if 186 } //end for 187 } //end of the function SetCfgLibVars 188 //=========================================================================== 189 // 190 // Parameter: - 191 // Returns: - 192 // Changes Globals: - 193 //=========================================================================== 194 int LoadCfgFile(char *filename) 195 { 196 source_t *source; 197 token_t token; 198 int settingsdefined; 199 200 source = LoadSourceFile(filename); 201 if (!source) 202 { 203 Log_Print("couldn't open cfg file %s\n", filename); 204 return false; 205 } //end if 206 207 settingsdefined = false; 208 memset(&cfg, 0, sizeof(cfg_t)); 209 210 while(PC_ReadToken(source, &token)) 211 { 212 if (!stricmp(token.string, "bbox")) 213 { 214 if (cfg.numbboxes >= AAS_MAX_BBOXES) 215 { 216 SourceError(source, "too many bounding box volumes defined"); 217 } //end if 218 if (!ReadStructure(source, &bbox_struct, (char *) &cfg.bboxes[cfg.numbboxes])) 219 { 220 FreeSource(source); 221 return false; 222 } //end if 223 cfg.allpresencetypes |= cfg.bboxes[cfg.numbboxes].presencetype; 224 cfg.numbboxes++; 225 } //end if 226 else if (!stricmp(token.string, "settings")) 227 { 228 if (settingsdefined) 229 { 230 SourceWarning(source, "settings already defined\n"); 231 } //end if 232 settingsdefined = true; 233 if (!ReadStructure(source, &cfg_struct, (char *) &cfg)) 234 { 235 FreeSource(source); 236 return false; 237 } //end if 238 } //end else if 239 } //end while 240 if (VectorLength(cfg.phys_gravitydirection) < 0.9 || VectorLength(cfg.phys_gravitydirection) > 1.1) 241 { 242 SourceError(source, "invalid gravity direction specified"); 243 } //end if 244 if (cfg.numbboxes <= 0) 245 { 246 SourceError(source, "no bounding volumes specified"); 247 } //end if 248 FreeSource(source); 249 SetCfgLibVars(); 250 Log_Print("using cfg file %s\n", filename); 251 return true; 252 } //end of the function LoadCfgFile