#include #include #include #include "explosion_s.h" #include "bullet_s.h" #include "object_s.h" #include "obstacle_s.h" #include "objhandle.h" #include "parse.h" #include "weapon_s.h" using namespace JFK::server; using std::string; /* Assumes that the same sequence is used in weapon.h */ const struct bulletdata JFK::server::bulletdata[JFK::MAX_WEAPON] = { { 250, 3, 20, 20, 20, 10 }, /* SHOTGUN */ { 125, 3, 12, 10, 15, 5 }, /* REVOLVER */ { 175, 3, 20, 12, 3, 10 }, /* MACHINEGUN */ { 250, 5, 20, 0, 50, 50 }, /* ROCKETLAUNCHER */ { 30, 5, 10, 0, 25, 20 }, /* GRENADE */ { 25, 3, 8, 2, 5, 1 } /* WATERGUN */ }; bullet::bullet(int itype, objhandle ioriginator) : JFK::bullet(itype), explode(false), originator(ioriginator) { assert(itype >= 0 && itype < MAX_WEAPON); ttl = bulletdata[type].ttl; r = bulletdata[type].r; v = bulletdata[type].v; damage = bulletdata[type].damage; } void bullet::assign(const object* obj) { *this = *dynamic_cast(obj); } string bullet::diffstring(const object* oldobj) const { std::ostringstream oss; const bullet* o = dynamic_cast(oldobj); if (v != o->v) oss << "v=" << v << ' '; if (type != o->type) oss << "type=" << type << ' '; if (oss.str().length() > 0) { oss << "x=" << x << "y=" << y << "dx=" << dx << "dy=" << dy; } return oss.str(); } void bullet::move(double t) { if (t > ttl) t = ttl; x += v*dx*t; y += v*dy*t; } object* bullet::action(double t) { object* ret = NULL; ttl -= t; if (ttl < 0.0) { suicide = true; if (type == GRENADE) explode = true; } if (explode) { ret = new explosion(originator); ret->x = x; ret->y = y; } return ret; } void bullet::delegate_collision(object* obj) const { /* grenades fly above other things */ if (type != GRENADE) { obj->collide_with_bullet(this); } } void bullet::collide_with_bullet(const bullet*) { if (type != GRENADE) suicide = true; if (type == ROCKETLAUNCHER) explode = true; } void bullet::collide_with_obstacle(const obstacle* o) { if (o->type != OBS_LAKE1) { if (type != GRENADE) suicide = true; if (type == ROCKETLAUNCHER) explode = true; } } void bullet::collide_with_person(const person* p) { if (type == ROCKETLAUNCHER) explode = true; if (type != GRENADE) { suicide = true; /* Change position to exact position of collision */ double d1 = dx * dx + dy * dy; // != 0ß double d2 = (p->x - x) * dx + (p->y - y) * dy; x += dx * d2 / d1; y += dy * d2 / d1; } }