| 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 |
#include "weapon_class.h" |
| 29 |
|
| 30 |
#include "ammo.h" |
| 31 |
#include "game_events.h" |
| 32 |
#include "json_utils.h" |
| 33 |
#include "log.h" |
| 34 |
#include "net_util.h" |
| 35 |
#include "utils.h" |
| 36 |
|
| 37 |
WeaponClasses gWeaponClasses; |
| 38 |
|
| 39 |
const char *GunTypeStr(const GunType t) |
| 40 |
{ |
| 41 |
switch (t) |
| 42 |
{ |
| 43 |
T2S(GUNTYPE_NORMAL, "Normal"); |
| 44 |
T2S(GUNTYPE_MELEE, "Melee"); |
| 45 |
T2S(GUNTYPE_GRENADE, "Grenade"); |
| 46 |
T2S(GUNTYPE_MULTI, "Multi"); |
| 47 |
default: |
| 48 |
return ""; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
void GunTypeGetSlotStartEnd(const GunType gt, int *start, int *end) |
| 53 |
{ |
| 54 |
switch (gt) |
| 55 |
{ |
| 56 |
case GUNTYPE_MELEE: |
| 57 |
*start = *end = MELEE_SLOT; |
| 58 |
break; |
| 59 |
case GUNTYPE_GRENADE: |
| 60 |
*start = *end = GRENADE_SLOT; |
| 61 |
break; |
| 62 |
default: |
| 63 |
*start = 0; |
| 64 |
*end = 1; |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
#define VERSION 3 |
| 71 |
void WeaponClassesInitialize(WeaponClasses *wcs) |
| 72 |
{ |
| 73 |
memset(wcs, 0, sizeof *wcs); |
| 74 |
CArrayInit(&wcs->Guns, sizeof(WeaponClass)); |
| 75 |
CArrayInit(&wcs->CustomGuns, sizeof(WeaponClass)); |
| 76 |
} |
| 77 |
static void LoadWeaponClass(WeaponClass *wc, json_t *node, const int version); |
| 78 |
static void WeaponClassTerminate(WeaponClass *wc); |
| 79 |
void WeaponClassesLoadJSON(WeaponClasses *wcs, CArray *classes, json_t *root) |
| 80 |
{ |
| 81 |
LOG(LM_MAP, LL_DEBUG, "loading weapons"); |
| 82 |
int version; |
| 83 |
LoadInt(&version, root, "Version"); |
| 84 |
if (version > VERSION || version <= 0) |
| 85 |
{ |
| 86 |
CASSERT(false, "cannot read guns file version"); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if (classes == &wcs->Guns) |
| 91 |
{ |
| 92 |
CASSERT(wcs->Guns.size == 0, "guns not empty"); |
| 93 |
for (int i = 0; i < GUN_COUNT; i++) |
| 94 |
{ |
| 95 |
WeaponClass gd; |
| 96 |
memset(&gd, 0, sizeof gd); |
| 97 |
CArrayPushBack(&wcs->Guns, &gd); |
| 98 |
} |
| 99 |
} |
| 100 |
json_t *gunsNode = json_find_first_label(root, "Guns")->child; |
| 101 |
for (json_t *child = gunsNode->child; child; child = child->next) |
| 102 |
{ |
| 103 |
WeaponClass gd; |
| 104 |
LoadWeaponClass(&gd, child, version); |
| 105 |
int idx = -1; |
| 106 |
|
| 107 |
if (classes == &wcs->Guns) |
| 108 |
{ |
| 109 |
LoadInt(&idx, child, "Index"); |
| 110 |
} |
| 111 |
if (idx >= 0 && idx < GUN_COUNT) |
| 112 |
{ |
| 113 |
WeaponClass *gExisting = CArrayGet(&wcs->Guns, idx); |
| 114 |
WeaponClassTerminate(gExisting); |
| 115 |
memcpy(gExisting, &gd, sizeof gd); |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
CArrayPushBack(classes, &gd); |
| 120 |
} |
| 121 |
} |
| 122 |
json_t *pseudoGunsNode = json_find_first_label(root, "PseudoGuns"); |
| 123 |
if (pseudoGunsNode != NULL) |
| 124 |
{ |
| 125 |
for (json_t *child = pseudoGunsNode->child->child; child; |
| 126 |
child = child->next) |
| 127 |
{ |
| 128 |
WeaponClass gd; |
| 129 |
LoadWeaponClass(&gd, child, version); |
| 130 |
gd.IsRealGun = false; |
| 131 |
CArrayPushBack(classes, &gd); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
static void LoadWeaponClass(WeaponClass *wc, json_t *node, const int version) |
| 136 |
{ |
| 137 |
memset(wc, 0, sizeof *wc); |
| 138 |
|
| 139 |
const json_t *gunsNode = json_find_first_label(node, "Guns"); |
| 140 |
if (gunsNode) |
| 141 |
{ |
| 142 |
wc->Type = GUNTYPE_MULTI; |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
bool canShoot = true; |
| 147 |
LoadBool(&canShoot, node, "CanShoot"); |
| 148 |
bool isGrenade = false; |
| 149 |
LoadBool(&isGrenade, node, "IsGrenade"); |
| 150 |
if (isGrenade) |
| 151 |
{ |
| 152 |
wc->Type = GUNTYPE_GRENADE; |
| 153 |
} |
| 154 |
else if (!canShoot) |
| 155 |
{ |
| 156 |
wc->Type = GUNTYPE_MELEE; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
char *tmp; |
| 161 |
|
| 162 |
if (wc->Type != GUNTYPE_MULTI) |
| 163 |
{ |
| 164 |
|
| 165 |
CSTRDUP(wc->u.Normal.Sprites, "chars/guns/blaster"); |
| 166 |
wc->u.Normal.Grips = 1; |
| 167 |
wc->Icon = PicManagerGetPic(&gPicManager, "peashooter"); |
| 168 |
wc->u.Normal.Sound = StrSound("laserpew"); |
| 169 |
wc->SwitchSound = StrSound("switch"); |
| 170 |
wc->u.Normal.Spread.Count = 1; |
| 171 |
wc->u.Normal.MuzzleHeight = 10 * Z_FACTOR; |
| 172 |
wc->u.Normal.MuzzleFlash = |
| 173 |
StrParticleClass(&gParticleClasses, "muzzle_flash_default"); |
| 174 |
wc->u.Normal.AmmoId = -1; |
| 175 |
wc->CanDrop = true; |
| 176 |
} |
| 177 |
|
| 178 |
const Pic *icon = NULL; |
| 179 |
LoadPic(&icon, node, "Icon"); |
| 180 |
if (icon != NULL) |
| 181 |
{ |
| 182 |
wc->Icon = icon; |
| 183 |
} |
| 184 |
|
| 185 |
tmp = NULL; |
| 186 |
LoadStr(&tmp, node, "Name"); |
| 187 |
if (tmp != NULL) |
| 188 |
{ |
| 189 |
CFREE(wc->name); |
| 190 |
wc->name = tmp; |
| 191 |
} |
| 192 |
|
| 193 |
tmp = NULL; |
| 194 |
LoadStr(&tmp, node, "Description"); |
| 195 |
if (tmp != NULL) |
| 196 |
{ |
| 197 |
CFREE(wc->Description); |
| 198 |
wc->Description = tmp; |
| 199 |
} |
| 200 |
|
| 201 |
tmp = NULL; |
| 202 |
LoadStr(&tmp, node, "Prerequisite"); |
| 203 |
if (tmp != NULL) |
| 204 |
{ |
| 205 |
CFREE(wc->Prerequisite); |
| 206 |
wc->Prerequisite = tmp; |
| 207 |
} |
| 208 |
|
| 209 |
LoadInt(&wc->Lock, node, "Lock"); |
| 210 |
|
| 211 |
LoadSoundFromNode(&wc->SwitchSound, node, "SwitchSound"); |
| 212 |
|
| 213 |
LoadBool(&wc->CanDrop, node, "CanDrop"); |
| 214 |
|
| 215 |
LoadStr(&wc->DropGun, node, "DropGun"); |
| 216 |
|
| 217 |
if (wc->Type == GUNTYPE_MULTI) |
| 218 |
{ |
| 219 |
int i = 0; |
| 220 |
for (const json_t *gunNode = gunsNode->child->child; gunNode; |
| 221 |
gunNode = gunNode->next, i++) |
| 222 |
{ |
| 223 |
wc->u.Guns[i] = json_unescape(gunNode->text); |
| 224 |
} |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
tmp = NULL; |
| 229 |
LoadStr(&tmp, node, "Pic"); |
| 230 |
if (tmp != NULL) |
| 231 |
{ |
| 232 |
CFREE(wc->u.Normal.Sprites); |
| 233 |
wc->u.Normal.Sprites = NULL; |
| 234 |
if (strlen(tmp) > 0) |
| 235 |
{ |
| 236 |
char buf[CDOGS_PATH_MAX]; |
| 237 |
sprintf(buf, "chars/guns/%s", tmp); |
| 238 |
CSTRDUP(wc->u.Normal.Sprites, buf); |
| 239 |
} |
| 240 |
CFREE(tmp); |
| 241 |
} |
| 242 |
|
| 243 |
LoadInt(&wc->u.Normal.Grips, node, "Grips"); |
| 244 |
|
| 245 |
CArrayInit(&wc->u.Normal.Bullets, sizeof(const BulletClass *)); |
| 246 |
tmp = NULL; |
| 247 |
LoadStr(&tmp, node, "Bullet"); |
| 248 |
if (tmp != NULL) |
| 249 |
{ |
| 250 |
const BulletClass *bullet = StrBulletClass(tmp); |
| 251 |
CArrayPushBack(&wc->u.Normal.Bullets, &bullet); |
| 252 |
CFREE(tmp); |
| 253 |
} |
| 254 |
const json_t *bulletsNode = json_find_first_label(node, "Bullets"); |
| 255 |
if (bulletsNode) |
| 256 |
{ |
| 257 |
for (const json_t *bulletNode = bulletsNode->child->child; |
| 258 |
bulletNode; bulletNode = bulletNode->next) |
| 259 |
{ |
| 260 |
const BulletClass *bullet = StrBulletClass(bulletNode->text); |
| 261 |
CArrayPushBack(&wc->u.Normal.Bullets, &bullet); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
tmp = NULL; |
| 266 |
LoadStr(&tmp, node, "Ammo"); |
| 267 |
if (tmp != NULL) |
| 268 |
{ |
| 269 |
wc->u.Normal.AmmoId = StrAmmoId(tmp); |
| 270 |
if (wc->Type == GUNTYPE_GRENADE) |
| 271 |
{ |
| 272 |
|
| 273 |
|
| 274 |
Ammo *ammo = AmmoGetById(&gAmmo, wc->u.Normal.AmmoId); |
| 275 |
CFREE(ammo->DefaultGun); |
| 276 |
CSTRDUP(ammo->DefaultGun, wc->name); |
| 277 |
|
| 278 |
wc->Icon = CPicGetPic(&ammo->Pic, 0); |
| 279 |
} |
| 280 |
CFREE(tmp); |
| 281 |
} |
| 282 |
|
| 283 |
LoadInt(&wc->u.Normal.Cost, node, "Cost"); |
| 284 |
|
| 285 |
LoadInt(&wc->u.Normal.ReloadLead, node, "ReloadLead"); |
| 286 |
|
| 287 |
LoadSoundFromNode(&wc->u.Normal.Sound, node, "Sound"); |
| 288 |
LoadSoundFromNode(&wc->u.Normal.ReloadSound, node, "ReloadSound"); |
| 289 |
|
| 290 |
wc->u.Normal.SoundLockLength = wc->Lock; |
| 291 |
LoadInt(&wc->u.Normal.SoundLockLength, node, "SoundLockLength"); |
| 292 |
|
| 293 |
LoadFloat(&wc->u.Normal.Recoil, node, "Recoil"); |
| 294 |
|
| 295 |
LoadInt(&wc->u.Normal.Spread.Count, node, "SpreadCount"); |
| 296 |
LoadFloat(&wc->u.Normal.Spread.Width, node, "SpreadWidth"); |
| 297 |
LoadFloat(&wc->u.Normal.AngleOffset, node, "AngleOffset"); |
| 298 |
|
| 299 |
if (json_find_first_label(node, "MuzzleHeight")) |
| 300 |
{ |
| 301 |
LoadInt(&wc->u.Normal.MuzzleHeight, node, "MuzzleHeight"); |
| 302 |
wc->u.Normal.MuzzleHeight *= Z_FACTOR; |
| 303 |
} |
| 304 |
if (json_find_first_label(node, "Elevation")) |
| 305 |
{ |
| 306 |
LoadInt(&wc->u.Normal.ElevationLow, node, "Elevation"); |
| 307 |
wc->u.Normal.ElevationHigh = wc->u.Normal.ElevationLow; |
| 308 |
} |
| 309 |
LoadInt(&wc->u.Normal.ElevationLow, node, "ElevationLow"); |
| 310 |
LoadInt(&wc->u.Normal.ElevationHigh, node, "ElevationHigh"); |
| 311 |
wc->u.Normal.ElevationLow = |
| 312 |
MIN(wc->u.Normal.ElevationLow, wc->u.Normal.ElevationHigh); |
| 313 |
wc->u.Normal.ElevationHigh = |
| 314 |
MAX(wc->u.Normal.ElevationLow, wc->u.Normal.ElevationHigh); |
| 315 |
tmp = NULL; |
| 316 |
LoadStr(&tmp, node, "MuzzleFlashParticle"); |
| 317 |
if (tmp != NULL) |
| 318 |
{ |
| 319 |
wc->u.Normal.MuzzleFlash = |
| 320 |
StrParticleClass(&gParticleClasses, tmp); |
| 321 |
CFREE(tmp); |
| 322 |
} |
| 323 |
|
| 324 |
tmp = NULL; |
| 325 |
LoadStr(&tmp, node, "Brass"); |
| 326 |
if (tmp != NULL) |
| 327 |
{ |
| 328 |
wc->u.Normal.Brass = StrParticleClass(&gParticleClasses, tmp); |
| 329 |
CFREE(tmp); |
| 330 |
} |
| 331 |
|
| 332 |
if (version < 3) |
| 333 |
{ |
| 334 |
LoadInt(&wc->u.Normal.Shake.Amount, node, "ShakeAmount"); |
| 335 |
} |
| 336 |
else if (json_find_first_label(node, "Shake")) |
| 337 |
{ |
| 338 |
json_t *shake = json_find_first_label(node, "Shake")->child; |
| 339 |
LoadInt(&wc->u.Normal.Shake.Amount, shake, "Amount"); |
| 340 |
LoadBool( |
| 341 |
&wc->u.Normal.Shake.CameraSubjectOnly, shake, |
| 342 |
"CameraSubjectOnly"); |
| 343 |
} |
| 344 |
|
| 345 |
LoadBool(&wc->u.Normal.Auto, node, "Auto"); |
| 346 |
} |
| 347 |
|
| 348 |
wc->IsRealGun = true; |
| 349 |
|
| 350 |
if (version < 2) |
| 351 |
{ |
| 352 |
CASSERT(wc->Type != GUNTYPE_MULTI, "unexpected gun type"); |
| 353 |
if (wc->Type == GUNTYPE_MELEE) |
| 354 |
{ |
| 355 |
wc->Lock = 0; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
LoadInt(&wc->Price, node, "Price"); |
| 360 |
wc->OverheatTicks = FPS_FRAMELIMIT; |
| 361 |
LoadInt(&wc->OverheatTicks, node, "OverheatTicks"); |
| 362 |
|
| 363 |
LOG(LM_MAP, LL_DEBUG, "loaded %s name(%s) lock(%d)...", |
| 364 |
GunTypeStr(wc->Type), wc->name, wc->Lock); |
| 365 |
LOG(LM_MAP, LL_DEBUG, "...canDrop(%s)", wc->CanDrop ? "true" : "false"); |
| 366 |
if (wc->Type == GUNTYPE_MULTI) |
| 367 |
{ |
| 368 |
LOG(LM_MAP, LL_DEBUG, "...guns{%s, %s}", |
| 369 |
wc->u.Guns[0] ? wc->u.Guns[0] : "", |
| 370 |
wc->u.Guns[1] ? wc->u.Guns[1] : ""); |
| 371 |
} |
| 372 |
else |
| 373 |
{ |
| 374 |
LOG(LM_MAP, LL_DEBUG, "bullets("); |
| 375 |
CA_FOREACH(const BulletClass *, bc, wc->u.Normal.Bullets) |
| 376 |
if (_ca_index > 0) |
| 377 |
{ |
| 378 |
LOG(LM_MAP, LL_DEBUG, ", "); |
| 379 |
} |
| 380 |
LOG(LM_MAP, LL_DEBUG, "%s", (*bc)->Name); |
| 381 |
CA_FOREACH_END() |
| 382 |
LOG(LM_MAP, LL_DEBUG, ") ammo(%d) cost(%d)...", wc->u.Normal.AmmoId, |
| 383 |
wc->u.Normal.Cost); |
| 384 |
LOG(LM_MAP, LL_DEBUG, |
| 385 |
"...reloadLead(%d) soundLockLength(%d) recoil(%f)...", |
| 386 |
wc->u.Normal.ReloadLead, wc->u.Normal.SoundLockLength, |
| 387 |
wc->u.Normal.Recoil); |
| 388 |
LOG(LM_MAP, LL_DEBUG, |
| 389 |
"...spread(%frad x%d) angleOffset(%f) muzzleHeight(%d)...", |
| 390 |
wc->u.Normal.Spread.Width, wc->u.Normal.Spread.Count, |
| 391 |
wc->u.Normal.AngleOffset, wc->u.Normal.MuzzleHeight); |
| 392 |
LOG(LM_MAP, LL_DEBUG, |
| 393 |
"...elevation(%d-%d) muzzleFlash(%s) brass(%s)...", |
| 394 |
wc->u.Normal.ElevationLow, wc->u.Normal.ElevationHigh, |
| 395 |
wc->u.Normal.MuzzleFlash != NULL ? wc->u.Normal.MuzzleFlash->Name |
| 396 |
: "", |
| 397 |
wc->u.Normal.Brass != NULL ? wc->u.Normal.Brass->Name : ""); |
| 398 |
LOG(LM_MAP, LL_DEBUG, "...shake{amount(%d), cameraSubjectOnly(%s)}", |
| 399 |
wc->u.Normal.Shake.Amount, |
| 400 |
wc->u.Normal.Shake.CameraSubjectOnly ? "true" : "false"); |
| 401 |
LOG(LM_MAP, LL_DEBUG, "...auto(%s)", |
| 402 |
wc->u.Normal.Auto ? "true" : "false"); |
| 403 |
} |
| 404 |
|
| 405 |
LOG(LM_MAP, LL_DEBUG, "price(%d) overheatTicks(%d)", wc->Price, wc->OverheatTicks); |
| 406 |
} |
| 407 |
void WeaponClassesTerminate(WeaponClasses *wcs) |
| 408 |
{ |
| 409 |
WeaponClassesClear(&wcs->Guns); |
| 410 |
CArrayTerminate(&wcs->Guns); |
| 411 |
WeaponClassesClear(&wcs->CustomGuns); |
| 412 |
CArrayTerminate(&wcs->CustomGuns); |
| 413 |
} |
| 414 |
void WeaponClassesClear(CArray *classes) |
| 415 |
{ |
| 416 |
CA_FOREACH(WeaponClass, g, *classes) |
| 417 |
WeaponClassTerminate(g); |
| 418 |
CA_FOREACH_END() |
| 419 |
CArrayClear(classes); |
| 420 |
} |
| 421 |
static void WeaponClassTerminate(WeaponClass *wc) |
| 422 |
{ |
| 423 |
CFREE(wc->name); |
| 424 |
CFREE(wc->Description); |
| 425 |
CFREE(wc->Prerequisite); |
| 426 |
CFREE(wc->DropGun); |
| 427 |
if (wc->Type == GUNTYPE_MULTI) |
| 428 |
{ |
| 429 |
for (int i = 0; i < MAX_BARRELS; i++) |
| 430 |
{ |
| 431 |
CFREE(wc->u.Guns[i]); |
| 432 |
} |
| 433 |
} |
| 434 |
else |
| 435 |
{ |
| 436 |
CFREE(wc->u.Normal.Sprites); |
| 437 |
CArrayTerminate(&wc->u.Normal.Bullets); |
| 438 |
} |
| 439 |
memset(wc, 0, sizeof *wc); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
const WeaponClass *StrWeaponClass(const char *s) |
| 444 |
{ |
| 445 |
CA_FOREACH(const WeaponClass, gd, gWeaponClasses.CustomGuns) |
| 446 |
if (strcmp(s, gd->name) == 0) |
| 447 |
{ |
| 448 |
return gd; |
| 449 |
} |
| 450 |
CA_FOREACH_END() |
| 451 |
CA_FOREACH(const WeaponClass, gd, gWeaponClasses.Guns) |
| 452 |
if (strcmp(s, gd->name) == 0) |
| 453 |
{ |
| 454 |
return gd; |
| 455 |
} |
| 456 |
CA_FOREACH_END() |
| 457 |
return NULL; |
| 458 |
} |
| 459 |
WeaponClass *IdWeaponClass(const int i) |
| 460 |
{ |
| 461 |
CASSERT( |
| 462 |
i >= 0 && i < (int)gWeaponClasses.Guns.size + |
| 463 |
(int)gWeaponClasses.CustomGuns.size, |
| 464 |
"Gun index out of bounds"); |
| 465 |
if (i < (int)gWeaponClasses.Guns.size) |
| 466 |
{ |
| 467 |
return CArrayGet(&gWeaponClasses.Guns, i); |
| 468 |
} |
| 469 |
return CArrayGet(&gWeaponClasses.CustomGuns, i - gWeaponClasses.Guns.size); |
| 470 |
} |
| 471 |
int WeaponClassId(const WeaponClass *wc) |
| 472 |
{ |
| 473 |
int idx = 0; |
| 474 |
for (int i = 0; i < (int)gWeaponClasses.Guns.size; i++, idx++) |
| 475 |
{ |
| 476 |
const WeaponClass *wc2 = CArrayGet(&gWeaponClasses.Guns, i); |
| 477 |
if (wc2 == wc) |
| 478 |
{ |
| 479 |
return idx; |
| 480 |
} |
| 481 |
} |
| 482 |
for (int i = 0; i < (int)gWeaponClasses.CustomGuns.size; i++, idx++) |
| 483 |
{ |
| 484 |
const WeaponClass *wc2 = CArrayGet(&gWeaponClasses.CustomGuns, i); |
| 485 |
if (wc2 == wc) |
| 486 |
{ |
| 487 |
return idx; |
| 488 |
} |
| 489 |
} |
| 490 |
CASSERT(false, "cannot find gun"); |
| 491 |
return -1; |
| 492 |
} |
| 493 |
|
| 494 |
void WeaponClassFire( |
| 495 |
const WeaponClass *wc, const struct vec2 pos, const float z, |
| 496 |
const double radians, const int flags, const int actorUID, |
| 497 |
const bool playSound, const bool isGun) |
| 498 |
{ |
| 499 |
CASSERT(wc->Type != GUNTYPE_MULTI, "unexpected gun type"); |
| 500 |
GameEvent e = GameEventNew(GAME_EVENT_GUN_FIRE); |
| 501 |
e.u.GunFire.ActorUID = actorUID; |
| 502 |
strcpy(e.u.GunFire.Gun, wc->name); |
| 503 |
e.u.GunFire.MuzzlePos = Vec2ToNet(pos); |
| 504 |
|
| 505 |
e.u.GunFire.Z = (int)z; |
| 506 |
e.u.GunFire.Angle = (float)radians; |
| 507 |
e.u.GunFire.Sound = playSound; |
| 508 |
e.u.GunFire.Flags = flags; |
| 509 |
e.u.GunFire.IsGun = isGun; |
| 510 |
GameEventsEnqueue(&gGameEvents, e); |
| 511 |
} |
| 512 |
|
| 513 |
void WeaponClassAddBrass( |
| 514 |
const WeaponClass *wc, const direction_e d, const struct vec2 pos) |
| 515 |
{ |
| 516 |
|
| 517 |
if (!ConfigGetBool(&gConfig, "Graphics.Brass")) |
| 518 |
{ |
| 519 |
return; |
| 520 |
} |
| 521 |
CASSERT(wc->Type == GUNTYPE_NORMAL, "gun type can't have brass"); |
| 522 |
CASSERT( |
| 523 |
wc->u.Normal.Brass != NULL, "Cannot create brass for no-brass weapon"); |
| 524 |
GameEvent e = GameEventNew(GAME_EVENT_ADD_PARTICLE); |
| 525 |
e.u.AddParticle.Class = wc->u.Normal.Brass; |
| 526 |
const float radians = dir2radians[d]; |
| 527 |
const struct vec2 ejectionPortOffset = |
| 528 |
svec2_scale(Vec2FromRadiansScaled(radians), 7); |
| 529 |
e.u.AddParticle.Pos = svec2_subtract(pos, ejectionPortOffset); |
| 530 |
e.u.AddParticle.Z = (float)wc->u.Normal.MuzzleHeight; |
| 531 |
e.u.AddParticle.Vel = |
| 532 |
svec2_scale(Vec2FromRadians(radians + MPI_2), 0.333333f); |
| 533 |
e.u.AddParticle.Vel.x += RAND_FLOAT(-0.25f, 0.25f); |
| 534 |
e.u.AddParticle.Vel.y += RAND_FLOAT(-0.25f, 0.25f); |
| 535 |
e.u.AddParticle.Angle = RAND_DOUBLE(0, MPI * 2); |
| 536 |
e.u.AddParticle.DZ = (float)((rand() % 6) + 6); |
| 537 |
e.u.AddParticle.Spin = RAND_DOUBLE(-0.1, 0.1); |
| 538 |
GameEventsEnqueue(&gGameEvents, e); |
| 539 |
} |
| 540 |
|
| 541 |
static struct vec2 GetMuzzleOffset( |
| 542 |
const direction_e d, const gunstate_e state); |
| 543 |
struct vec2 WeaponClassGetBarrelMuzzleOffset( |
| 544 |
const WeaponClass *wc, const CharSprites *cs, const int barrel, |
| 545 |
direction_e dir, const gunstate_e state) |
| 546 |
{ |
| 547 |
if (wc->Type == GUNTYPE_MULTI) |
| 548 |
{ |
| 549 |
wc = StrWeaponClass(wc->u.Guns[barrel]); |
| 550 |
CASSERT(wc != NULL, "cannot find gun"); |
| 551 |
} |
| 552 |
else |
| 553 |
{ |
| 554 |
CASSERT(barrel == 0, "unexpected barrel index"); |
| 555 |
} |
| 556 |
if (!WeaponClassHasMuzzle(wc)) |
| 557 |
{ |
| 558 |
return svec2_zero(); |
| 559 |
} |
| 560 |
CASSERT(wc->u.Normal.Sprites != NULL, "Gun has no pic"); |
| 561 |
CASSERT(barrel < 2, "up to two barrels supported"); |
| 562 |
|
| 563 |
if (barrel == 1) |
| 564 |
{ |
| 565 |
dir = DirectionMirrorX(dir); |
| 566 |
} |
| 567 |
const struct vec2 gunOffset = |
| 568 |
cs->Offsets.Dir[barrel == 0 ? BODY_PART_GUN_R : BODY_PART_GUN_L][dir]; |
| 569 |
const struct vec2 offset = |
| 570 |
svec2_add(gunOffset, GetMuzzleOffset(dir, state)); |
| 571 |
if (barrel == 1) |
| 572 |
{ |
| 573 |
return svec2(-offset.x, offset.y); |
| 574 |
} |
| 575 |
return offset; |
| 576 |
} |
| 577 |
static struct vec2 GetMuzzleOffset(const direction_e d, const gunstate_e state) |
| 578 |
{ |
| 579 |
|
| 580 |
#define BARREL_LENGTH 10 |
| 581 |
#define BARREL_LENGTH_READY 7 |
| 582 |
|
| 583 |
const double barrelLength = |
| 584 |
state == GUNSTATE_FIRING ? BARREL_LENGTH : BARREL_LENGTH_READY; |
| 585 |
return svec2_scale(Vec2FromRadians(dir2radians[d]), (float)barrelLength); |
| 586 |
} |
| 587 |
float WeaponClassGetMuzzleHeight( |
| 588 |
const WeaponClass *wc, const gunstate_e state, const int barrel) |
| 589 |
{ |
| 590 |
|
| 591 |
|
| 592 |
const int muzzleHeight = WC_BARREL_ATTR(*wc, MuzzleHeight, barrel); |
| 593 |
return ( |
| 594 |
float)(muzzleHeight + (state == GUNSTATE_FIRING ? 0 : 4 * Z_FACTOR)); |
| 595 |
} |
| 596 |
|
| 597 |
bool WeaponClassHasMuzzle(const WeaponClass *wc) |
| 598 |
{ |
| 599 |
return wc->Type == GUNTYPE_NORMAL && wc->u.Normal.Sprites != NULL; |
| 600 |
} |
| 601 |
bool WeaponClassIsHighDPS(const WeaponClass *wc) |
| 602 |
{ |
| 603 |
if (wc->Type == GUNTYPE_MULTI) |
| 604 |
{ |
| 605 |
return WeaponClassIsHighDPS(StrWeaponClass(wc->u.Guns[0])) || |
| 606 |
WeaponClassIsHighDPS(StrWeaponClass(wc->u.Guns[1])); |
| 607 |
} |
| 608 |
CA_FOREACH(const BulletClass *, bc, wc->u.Normal.Bullets) |
| 609 |
|
| 610 |
if ((*bc)->Falling.DropGuns.size > 0 || (*bc)->OutOfRangeGuns.size > 0 || |
| 611 |
(*bc)->HitGuns.size > 0) |
| 612 |
{ |
| 613 |
return true; |
| 614 |
} |
| 615 |
CA_FOREACH_END() |
| 616 |
return false; |
| 617 |
} |
| 618 |
float WeaponClassGetRange(const WeaponClass *wc) |
| 619 |
{ |
| 620 |
if (wc->Type == GUNTYPE_MULTI) |
| 621 |
{ |
| 622 |
return (WeaponClassGetRange(StrWeaponClass(wc->u.Guns[0])) + |
| 623 |
WeaponClassGetRange(StrWeaponClass(wc->u.Guns[1]))) / |
| 624 |
2.0f; |
| 625 |
} |
| 626 |
float maxRange = 0; |
| 627 |
CA_FOREACH(const BulletClass *, bc, wc->u.Normal.Bullets) |
| 628 |
const float speed = ((*bc)->SpeedLow + (*bc)->SpeedHigh) / 2; |
| 629 |
const int range = ((*bc)->RangeLow + (*bc)->RangeHigh) / 2; |
| 630 |
float effectiveRange = speed * range; |
| 631 |
if ((*bc)->Falling.GravityFactor != 0 && (*bc)->Falling.DestroyOnDrop) |
| 632 |
{ |
| 633 |
|
| 634 |
|
| 635 |
effectiveRange *= 0.5f; |
| 636 |
} |
| 637 |
maxRange = MAX(maxRange, effectiveRange); |
| 638 |
CA_FOREACH_END() |
| 639 |
return maxRange; |
| 640 |
} |
| 641 |
bool WeaponClassIsLongRange(const WeaponClass *wc) |
| 642 |
{ |
| 643 |
return WeaponClassGetRange(wc) > 130; |
| 644 |
} |
| 645 |
bool WeaponClassIsShortRange(const WeaponClass *wc) |
| 646 |
{ |
| 647 |
return WeaponClassGetRange(wc) < 100; |
| 648 |
} |
| 649 |
bool WeaponClassCanShoot(const WeaponClass *wc) |
| 650 |
{ |
| 651 |
if (wc->Type == GUNTYPE_MULTI) |
| 652 |
{ |
| 653 |
return WeaponClassCanShoot(WeaponClassGetBarrel(wc, 0)) || |
| 654 |
WeaponClassCanShoot(WeaponClassGetBarrel(wc, 1)); |
| 655 |
} |
| 656 |
return wc->Type != GUNTYPE_MELEE; |
| 657 |
} |
| 658 |
int WeaponClassNumBarrels(const WeaponClass *wc) |
| 659 |
{ |
| 660 |
if (wc == NULL) |
| 661 |
{ |
| 662 |
return 0; |
| 663 |
} |
| 664 |
return wc->Type == GUNTYPE_MULTI ? 2 : 1; |
| 665 |
} |
| 666 |
const WeaponClass *WeaponClassGetBarrel( |
| 667 |
const WeaponClass *wc, const int barrel) |
| 668 |
{ |
| 669 |
if (wc->Type == GUNTYPE_MULTI) |
| 670 |
{ |
| 671 |
return StrWeaponClass(wc->u.Guns[barrel]); |
| 672 |
} |
| 673 |
return wc; |
| 674 |
} |
| 675 |
const BulletClass *WeaponClassGetBullet( |
| 676 |
const WeaponClass *wc, const int barrel) |
| 677 |
{ |
| 678 |
if (barrel == -1) |
| 679 |
{ |
| 680 |
return NULL; |
| 681 |
} |
| 682 |
wc = WeaponClassGetBarrel(wc, barrel); |
| 683 |
CA_FOREACH(const BulletClass *, bc, wc->u.Normal.Bullets) |
| 684 |
if (*bc) |
| 685 |
return *bc; |
| 686 |
CA_FOREACH_END() |
| 687 |
return NULL; |
| 688 |
} |
| 689 |
const WeaponClass *WeaponClassGetPrerequisite(const WeaponClass *wc) |
| 690 |
{ |
| 691 |
if (wc == NULL || wc->Prerequisite == NULL) |
| 692 |
{ |
| 693 |
return NULL; |
| 694 |
} |
| 695 |
return StrWeaponClass(wc->Prerequisite); |
| 696 |
} |
| 697 |
static const WeaponClass *FindAncestor(const WeaponClass *wc) |
| 698 |
{ |
| 699 |
for (;;) |
| 700 |
{ |
| 701 |
const WeaponClass *parent = WeaponClassGetPrerequisite(wc); |
| 702 |
if (parent == NULL) |
| 703 |
{ |
| 704 |
return wc; |
| 705 |
} |
| 706 |
wc = parent; |
| 707 |
} |
| 708 |
} |
| 709 |
bool WeaponClassesAreRelated(const WeaponClass *wc1, const WeaponClass *wc2) |
| 710 |
{ |
| 711 |
|
| 712 |
return wc1 != NULL && wc2 != NULL && |
| 713 |
FindAncestor(wc1) == FindAncestor(wc2); |
| 714 |
} |
| 715 |
|
| 716 |
int WeaponClassFullPrice(const WeaponClass *wc) |
| 717 |
{ |
| 718 |
|
| 719 |
if (wc == NULL) |
| 720 |
{ |
| 721 |
return 0; |
| 722 |
} |
| 723 |
return wc->Price + WeaponClassFullPrice(WeaponClassGetPrerequisite(wc)); |
| 724 |
} |
| 725 |
|
| 726 |
void BulletAndWeaponInitialize( |
| 727 |
BulletClasses *b, WeaponClasses *wcs, const char *bpath, const char *gpath) |
| 728 |
{ |
| 729 |
BulletInitialize(b); |
| 730 |
|
| 731 |
FILE *bf = NULL; |
| 732 |
FILE *gf = NULL; |
| 733 |
json_t *broot = NULL; |
| 734 |
json_t *groot = NULL; |
| 735 |
enum json_error e; |
| 736 |
|
| 737 |
|
| 738 |
bool freeBRoot = true; |
| 739 |
char buf[CDOGS_PATH_MAX]; |
| 740 |
GetDataFilePath(buf, bpath); |
| 741 |
bf = fopen(buf, "r"); |
| 742 |
if (bf == NULL) |
| 743 |
{ |
| 744 |
LOG(LM_MAP, LL_ERROR, "Error: cannot load bullets file %s", buf); |
| 745 |
goto bail; |
| 746 |
} |
| 747 |
e = json_stream_parse(bf, &broot); |
| 748 |
if (e != JSON_OK) |
| 749 |
{ |
| 750 |
LOG(LM_MAP, LL_ERROR, "Error parsing bullets file %s [error %d]", buf, |
| 751 |
(int)e); |
| 752 |
goto bail; |
| 753 |
} |
| 754 |
BulletLoadJSON(b, &b->Classes, broot); |
| 755 |
|
| 756 |
WeaponClassesInitialize(wcs); |
| 757 |
GetDataFilePath(buf, gpath); |
| 758 |
gf = fopen(buf, "r"); |
| 759 |
if (gf == NULL) |
| 760 |
{ |
| 761 |
LOG(LM_MAP, LL_ERROR, "Error: cannot load guns file %s", buf); |
| 762 |
goto bail; |
| 763 |
} |
| 764 |
e = json_stream_parse(gf, &groot); |
| 765 |
if (e != JSON_OK) |
| 766 |
{ |
| 767 |
LOG(LM_MAP, LL_ERROR, "Error parsing guns file %s [error %d]", buf, |
| 768 |
(int)e); |
| 769 |
goto bail; |
| 770 |
} |
| 771 |
WeaponClassesLoadJSON(wcs, &wcs->Guns, groot); |
| 772 |
|
| 773 |
BulletLoadWeapons(b); |
| 774 |
freeBRoot = false; |
| 775 |
|
| 776 |
bail: |
| 777 |
if (bf) |
| 778 |
{ |
| 779 |
fclose(bf); |
| 780 |
} |
| 781 |
if (gf) |
| 782 |
{ |
| 783 |
fclose(gf); |
| 784 |
} |
| 785 |
if (freeBRoot) |
| 786 |
{ |
| 787 |
json_free_value(&broot); |
| 788 |
} |
| 789 |
json_free_value(&groot); |
| 790 |
} |