bool
NetGameClient::DoJoinBacklog(NetJoinAnnounce* join_ann)
{
	bool finished = false;

	if (!join_ann)
	return finished;

	Sim* sim = Sim::GetSim();
	if (!sim)
	return finished;

	DWORD nid       = join_ann->GetNetID();
	DWORD oid       = join_ann->GetObjID();
	Text  name      = join_ann->GetName();
	Text  elem_name = join_ann->GetElement();
	Text  region    = join_ann->GetRegion();
	Point loc       = join_ann->GetLocation();
	Point velocity  = join_ann->GetVelocity();
	int   index     = join_ann->GetIndex();
	int   shld_lvl  = join_ann->GetShield();
	Ship* ship      = 0;
	char  ship_name[128];

	strcpy_s(ship_name, Game::GetText("NetGameClient.no-ship").data());

	if (nid && oid) {
		NetPlayer* remote_player = FindPlayerByObjID(oid);
		if (remote_player) {
			remote_player->SetName(name);
			remote_player->SetObjID(oid);

			if (index > 0)
			sprintf_s(ship_name, "%s %d", elem_name.data(), index);
			else
			sprintf_s(ship_name, "%s", elem_name.data());
		}
		else {
			Element* element = sim->FindElement(elem_name);

			if (element) {
				ship = element->GetShip(index);
			}

			if (ship) {
				strcpy_s(ship_name, ship->Name());

				SimRegion* rgn = ship->GetRegion();
				if (rgn && region != rgn->Name()) {
					SimRegion* dst = sim->FindRegion(region);
					if (dst) dst->InsertObject(ship);
				}

				ship->MoveTo(loc);
				ship->SetVelocity(velocity);

				Shield* shield = ship->GetShield();
				if (shield)
				shield->SetNetShieldLevel(shld_lvl);

				NetPlayer* remote_player = new(__FILE__,__LINE__) NetPlayer(nid);
				remote_player->SetName(name);
				remote_player->SetObjID(oid);
				remote_player->SetShip(ship);

				players.append(remote_player);
				finished = true;

				if (name == "Server A.I. Ship") {
					Print("NetGameClient::DoJoinBacklog() Remote Player '%s' has joined as '%s' with ID %d\n", name.data(), ship_name, oid);
				}
				else {
					HUDView::Message(Game::GetText("NetGameClient.remote-join").data(), name.data(), ship_name);
				}
			}
		}
	}

	return finished;
}
Exemple #2
0
Shot*
Weapon::FireBarrel(int n)
{
    const Point&   base_vel = ship->Velocity();
    Shot*          shot     = 0;
    SimRegion*     region   = ship->GetRegion();

    if (!region || n < 0 || n >= nbarrels || Game::Paused())
    return 0;

    firing = 1;
    Aim();

    Camera rail_cam;
    rail_cam.Clone(aim_cam);

    Point shotpos = muzzle_pts[n];
    if (design->length > 0)
    shotpos = shotpos + aim_cam.vpn() * design->length;

    // guns may be slewed towards target:
    if (design->primary) {
        shot = CreateShot(shotpos, aim_cam, design, ship);

        if (shot) {
            shot->SetVelocity(shot->Velocity() + base_vel);
        }
    }

    // missiles always launch in rail direction:
    else {
        // unless they are on a mobile launcher
        if (turret && design->self_aiming) {
            shot = CreateShot(shotpos, aim_cam, design, ship);
            shot->SetVelocity(base_vel);
        }
        else {
            shot = CreateShot(shotpos, rail_cam, design, ship);
            if (shot /* && !turret */) {
                Matrix orient = ship->Cam().Orientation();
                if (aim_azimuth != 0)   orient.Yaw(aim_azimuth);
                if (aim_elevation != 0) orient.Pitch(aim_elevation);

                Point eject = design->eject * orient;
                shot->SetVelocity(base_vel + eject);
            }
        }

        if (shot && visible_stores[n]) {
            GRAPHIC_DESTROY(visible_stores[n]);
        }
    }

    if (shot) {
        if (ammo > 0)
        ammo--;

        if (guided && target)
        shot->SeekTarget(target, subtarget);

        float shot_load;
        if (energy > design->charge)
        shot_load = design->charge;
        else
        shot_load = energy;   

        energy -= shot_load;
        shot->SetCharge(shot_load * availability);

        if (target && design->flak && !design->guided) {
            double speed = shot->Velocity().length();
            double range = (target->Location() - shot->Location()).length();

            if (range > design->min_range && range < design->max_range) {
                shot->SetFuse(range / speed);
            }
        }

        region->InsertObject(shot);

        if (beams) {
            beams[n] = shot;
            Observe(beams[n]);

            // aim beam at target:
            SetBeamPoints(true);
        }

        if (ship) {
            ShipStats* stats = ShipStats::Find(ship->Name());

            if (design->primary)
            stats->AddGunShot();
            else if (design->decoy_type == 0 && design->damage > 0)
            stats->AddMissileShot();
        }
    }

    return shot;
}
void
NetGameClient::DoJoinAnnounce(NetMsg* msg)
{
	if (!msg) return;

	Sim* sim = Sim::GetSim();
	if (!sim) return;

	NetJoinAnnounce*  join_ann = new(__FILE__,__LINE__) NetJoinAnnounce;
	bool              saved    = false;

	if (join_ann->Unpack(msg->Data())) {
		DWORD nid       = msg->NetID();
		DWORD oid       = join_ann->GetObjID();
		Text  name      = join_ann->GetName();
		Text  elem_name = join_ann->GetElement();
		Text  region    = join_ann->GetRegion();
		Point loc       = join_ann->GetLocation();
		Point velocity  = join_ann->GetVelocity();
		int   index     = join_ann->GetIndex();
		int   shld_lvl  = join_ann->GetShield();
		join_ann->SetNetID(nid);
		Ship* ship      = 0;
		char  ship_name[128];

		strcpy_s(ship_name, Game::GetText("NetGameClient.no-ship").data());

		if (local_player && player_name == name) {
			HUDView::Message(Game::GetText("NetGameClient.local-accept"), name.data(), local_player->Name());

			objid = oid;
			netid = nid;
			local_player->SetObjID(oid);
			local_player->SetNetObserver(false);
			Observe(local_player);

			SimRegion* rgn = local_player->GetRegion();
			if (rgn && region != rgn->Name()) {
				SimRegion* dst = sim->FindRegion(region);
				if (dst) dst->InsertObject(local_player);
			}

			local_player->MoveTo(loc);
			local_player->SetVelocity(velocity);

			Shield* shield = local_player->GetShield();
			if (shield)
			shield->SetNetShieldLevel(shld_lvl);
		}
		else {
			NetPlayer* remote_player = FindPlayerByObjID(oid);
			if (remote_player) {
				remote_player->SetName(name);
				remote_player->SetObjID(oid);

				if (index > 0)
				sprintf_s(ship_name, "%s %d", elem_name.data(), index);
				else
				sprintf_s(ship_name, "%s", elem_name.data());
			}
			else {
				Element* element = sim->FindElement(elem_name);

				if (element) {
					ship = element->GetShip(index);
				}
				else {
					Print("NetGameClient::DoJoinAnnounce() could not find elem %s for player '%s' objid %d\n",
					elem_name.data(), name.data(), oid);

					NetUtil::SendElemRequest(elem_name.data());
				}

				if (!ship) {
					// save it for later:
					join_backlog.append(join_ann);
					saved = true;
				}
				else {
					strcpy_s(ship_name, ship->Name());

					SimRegion* rgn = ship->GetRegion();
					if (rgn && region != rgn->Name()) {
						SimRegion* dst = sim->FindRegion(region);
						if (dst) dst->InsertObject(ship);
					}

					ship->MoveTo(loc);
					ship->SetVelocity(velocity);

					Shield* shield = ship->GetShield();
					if (shield)
					shield->SetNetShieldLevel(shld_lvl);

					NetPlayer* remote_player = new(__FILE__,__LINE__) NetPlayer(nid);
					remote_player->SetName(name);
					remote_player->SetObjID(oid);
					remote_player->SetShip(ship);

					players.append(remote_player);

					if (name == "Server A.I. Ship") {
						Print("Remote Player '%s' has joined as '%s' with ID %d\n", name.data(), ship_name, oid);
					}
					else {
						HUDView::Message(Game::GetText("NetGameClient.remote-join").data(), name.data(), ship_name);
					}
				}
			}
		}
	}

	if (!saved)
	delete join_ann;
}