1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include "../include/string.h"
#include "sfm.h"
#include "obj.h"
#include "objutils.h"
#include "sarreality.h"
#include "human.h"
#include "config.h"
sar_human_data_entry_struct *SARHumanMatchEntryByName(
sar_human_data_struct *hd, const char *name
);
sar_human_data_struct *SARHumanPresetsInit(void *core_ptr);
void SARHumanPresetsShutdown(sar_human_data_struct *hd);
void SARHumanEntryDelete(
sar_human_data_struct *hd, sar_human_data_entry_struct *entry
);
int SARHumanCreate(
sar_human_data_struct *hd,
sar_scene_struct *scene,
sar_object_struct ***object, int *total_objects,
sar_obj_flags_t human_flags,
const char *name
);
void SARHumanSetObjectPreset(
sar_human_data_struct *hd,
sar_object_struct *obj_ptr,
const char *name
);
#define ATOI(s) (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s) (((s) != NULL) ? atol(s) : 0)
#define ATOF(s) (((s) != NULL) ? atof(s) : 0.0f)
#define STRDUP(s) (((s) != NULL) ? strdup(s) : NULL)
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h) (MIN(MAX((a),(l)),(h)))
#define STRLEN(s) (((s) != NULL) ? strlen(s) : 0)
#define STRISEMPTY(s) (((s) != NULL) ? (*(s) == '\0') : 1)
#define RADTODEG(r) ((r) * 180.0 / PI)
#define DEGTORAD(d) ((d) * PI / 180.0)
/*
* Returns a pointer to the entry structure on the given hd that
* matches name.
*
* Can return NULL on failed match or error.
*/
sar_human_data_entry_struct *SARHumanMatchEntryByName(
sar_human_data_struct *hd, const char *name
)
{
int i;
sar_human_data_entry_struct *entry;
if((hd == NULL) || STRISEMPTY(name))
return(NULL);
for(i = 0; i < hd->total_presets; i++)
{
entry = hd->preset[i];
if(entry == NULL)
continue;
if(entry->name != NULL)
{
if(!strcasecmp(entry->name, name))
return(entry);
}
}
return(NULL);
}
/*
* Initializes a new sar_human_data_struct structure and returns
* the pointer to it.
*
* This pointer must be freed by the calling function by a call to
* SARHumanPresetsShutdown().
*/
sar_human_data_struct *SARHumanPresetsInit(void *core_ptr)
{
sar_human_data_struct *hd = (sar_human_data_struct *)calloc(
1, sizeof(sar_human_data_struct)
);
if(hd == NULL)
return(hd);
hd->core_ptr = core_ptr;
return(hd);
}
/*
* Deallocates all human preset resources on the given
* sar_human_data_struct structure and the structure itself.
*
* The given pointer must not be referenced again after calling
* this function.
*/
void SARHumanPresetsShutdown(sar_human_data_struct *hd)
{
int i;
if(hd == NULL)
return;
/* Deallocate each preset human entry structure */
for(i = 0; i < hd->total_presets; i++)
SARHumanEntryDelete(
hd, hd->preset[i]
);
free(hd->preset);
hd->preset = NULL;
hd->total_presets = 0;
free(hd);
}
/*
* Deallocates the given human entry structure and all its
* resources.
*/
void SARHumanEntryDelete(
sar_human_data_struct *hd, sar_human_data_entry_struct *entry
)
{
if(entry == NULL)
return;
free(entry->name);
free(entry);
}
/*
* Creates a new object of type SAR_OBJ_TYPE_HUMAN in the given
* scene.
*
* Returns the index to the newly added object or -1 on failure.
*
* If name is not NULL, then the human object will be modeled after
* the one matching name on the given human presets data structure.
*/
int SARHumanCreate(
sar_human_data_struct *hd,
sar_scene_struct *scene,
sar_object_struct ***object, int *total_objects,
sar_obj_flags_t human_flags,
const char *name
)
{
int obj_num;
sar_object_struct *obj_ptr;
sar_object_human_struct *human;
if((scene == NULL) || (object == NULL) || (total_objects == NULL))
return(-1);
/* Create new human object */
obj_num = SARObjNew(
scene, object, total_objects,
SAR_OBJ_TYPE_HUMAN
);
obj_ptr = ((obj_num < 0) ? NULL : (*object)[obj_num]);
if(obj_ptr == NULL)
return(-1);
/* Get pointer to human data substructure */
human = SAR_OBJ_GET_HUMAN(obj_ptr);
if(human == NULL)
return(obj_num);
/* Visual range */
obj_ptr->range = SAR_HUMAN_DEF_RANGE;
/* Set contact bounds, this is needed so we can pick up human
* and do other size detection with it.
*/
SARObjAddContactBoundsCylendrical(
obj_ptr,
0, 0, /* Crash flags, crash type */
(float)SAR_HUMAN_CONTACT_RADIUS,
(float)SAR_HUMAN_CONTACT_ZMIN, (float)SAR_HUMAN_CONTACT_ZMAX
);
/* Temperature */
obj_ptr->temperature = 1.0f;
/* Set human flags */
human->flags = human_flags;
/* Set default animation rate */
human->anim_rate = SAR_HUMAN_ANIM_RATE;
/* Water ripples texture */
human->water_ripple_tex_num = SARGetTextureRefNumberByName(
scene, SAR_STD_TEXNAME_WATER_RIPPLE
);
/* Reset other human object values here */
human->intercepting_object = -1;
human->intercepting_object_distance2d =
human->intercepting_object_distance3d = 0.0;
/* Number of assisting humans */
human->assisting_humans = 0;
/* Model human object values to preset values from the human
* data presets.
*/
SARHumanSetObjectPreset(
hd, obj_ptr, name
);
return(obj_num);
}
/*
* Sets the specified object (which must be of type
* SAR_OBJ_TYPE_HUMAN) to the human presets data entry given by
* name on the given hd.
*
* If obj_ptr is invalid, not of type SAR_OBJ_TYPE_HUMAN, or the
* given name of a human data entry could not be found on the hd
* then no operation will be performed.
*/
void SARHumanSetObjectPreset(
sar_human_data_struct *hd,
sar_object_struct *obj_ptr,
const char *name
)
{
sar_object_human_struct *human;
sar_human_data_entry_struct *entry;
if((hd == NULL) || (obj_ptr == NULL) || (name == NULL))
return;
/* Match human presets data entry by the given name */
entry = SARHumanMatchEntryByName(hd, name);
if(entry == NULL)
return;
human = SAR_OBJ_GET_HUMAN(obj_ptr);
if(human == NULL)
return;
/* Begin setting values from the entry to the human object */
/* Color palette */
memcpy(
&human->color[0],
&entry->color[0],
SAR_HUMAN_COLORS_MAX * sizeof(sar_color_struct)
);
/* Height in meters */
human->height = entry->height;
/* Weight in kg */
human->mass = entry->mass;
/* Number of assisting humans */
human->assisting_humans = entry->assisting_humans;
/* Color palette for assisting humans */
memcpy(
&human->assisting_human_color[0],
&entry->assisting_human_color[0],
SAR_HUMAN_COLORS_MAX * sizeof(sar_color_struct)
);
/* Add support for other things that need to be coppied here */
}
|