Esempio n. 1
0
void Viewport::draw()
{
	if(worm) // Should not be necessary further on
	{
		if(worm->visible)
		{
			int lifebarWidth = worm->health * 100 / worm->settings->health;
			drawBar(inGameX, 161, lifebarWidth, lifebarWidth/10 + 234);
		}
		else
		{
			int lifebarWidth = 100 - (worm->killedTimer * 25) / 37;
			if(lifebarWidth > 0)
			{
				if(lifebarWidth > 100)
					lifebarWidth = 100;
				drawBar(inGameX, 161, lifebarWidth, lifebarWidth/10 + 234);
			}
		}
	}
	
	// Draw kills status
	
	WormWeapon& ww = worm->weapons[worm->currentWeapon];
	
	if(ww.available)
	{
		if(ww.ammo > 0)
		{
			int ammoBarWidth = ww.ammo * 100 / game.weapons[ww.id].ammo;
			
			if(ammoBarWidth > 0)
				drawBar(inGameX, 166, ammoBarWidth, ammoBarWidth/10 + 245);
		}
	}
	else
	{
		int ammoBarWidth = 0;
		
		if(game.weapons[ww.id].loadingTime != 0)
		{
			ammoBarWidth = 100 - ww.loadingLeft * 100 / game.weapons[ww.id].computedLoadingTime;
		}
		else
		{
			ammoBarWidth = 100 - ww.loadingLeft * 100;
		}
		
		if(ammoBarWidth > 0)
			drawBar(inGameX, 166, ammoBarWidth, ammoBarWidth/10 + 245);
		
		if((game.cycles % 20) > 10
		&& worm->visible)
		{
			gfx.font.drawText(game.texts.reloading, inGameX, 164, 50);
		}
	}
	
	gfx.font.drawText((game.texts.kills + toString(worm->kills)), inGameX, 171, 10);
	
	switch(game.settings.gameMode)
	{
	case Settings::GMKillEmAll:
	{
		gfx.font.drawText((game.texts.lives + toString(worm->lives)), inGameX, 178, 6);
	}
	break;
	
	case Settings::GMGameOfTag:
	{
		int const stateColours[] = {6, 10, 79, 4};
		
		int state = 0;
		
		for(std::size_t i = 0; i < game.worms.size(); ++i)
		{
			Worm& w = *game.worms[i];
			
			if(&w != worm
			&& w.timer >= worm->timer)
				state = 1; // We're better or equal off
		}
		
		int colour;
		if(game.lastKilled == worm)
			colour = stateColours[state];
		else
			colour = stateColours[state + 2];
		
		gfx.font.drawText(timeToString(worm->timer), 5, 106 + 84*worm->index, 161, colour);
	}
	break;
	}	

	PreserveClipRect pcr(gfx.screen);
	
	SDL_Rect viewportClip;
	viewportClip.x = rect.x1;
	viewportClip.y = rect.y1;
	viewportClip.w = rect.width();
	viewportClip.h = rect.height();
	
	SDL_SetClipRect(gfx.screen, &viewportClip);
	
	int offsX = rect.x1 - x;
	int offsY = rect.y1 - y;
	
	blitImageNoKeyColour(gfx.screen, &game.level.data[0], offsX, offsY, game.level.width, game.level.height); // TODO: Unhardcode
	
	if(!worm->visible
	&& worm->killedTimer <= 0
	&& !worm->ready)
	{
		gfx.font.drawText(game.texts.pressFire, rect.center_x() - 30, 76, 0);
		gfx.font.drawText(game.texts.pressFire, rect.center_x() - 31, 75, 50);
	}

	if(bannerY > -8
	&& worm->health <= 0)
	{	
		if(game.settings.gameMode == Settings::GMGameOfTag
		&& game.gotChanged)
		{
			gfx.font.drawText(S[YoureIt], rect.x1 + 3, bannerY + 1, 0);
			gfx.font.drawText(S[YoureIt], rect.x1 + 2, bannerY, 50);
		}
	}
	
	for(std::size_t i = 0; i < game.viewports.size(); ++i)
	{
		Viewport* v = game.viewports[i];
		if(v != this
		&& v->worm->health <= 0
		&& v->bannerY > -8)
		{
			if(v->worm->lastKilledBy == worm)
			{
				std::string msg(S[KilledMsg] + v->worm->settings->name);
				gfx.font.drawText(msg, rect.x1 + 3, v->bannerY + 1, 0);
				gfx.font.drawText(msg, rect.x1 + 2, v->bannerY, 50);
			}
			else
			{
				std::string msg(v->worm->settings->name + S[CommittedSuicideMsg]);
				gfx.font.drawText(msg, rect.x1 + 3, v->bannerY + 1, 0);
				gfx.font.drawText(msg, rect.x1 + 2, v->bannerY, 50);
			}
		}
	}

	for(Game::BonusList::iterator i = game.bonuses.begin(); i != game.bonuses.end(); ++i)
	{
		if(i->timer > C[BonusFlickerTime] || (game.cycles & 3) == 0)
		{
			int f = gfx.bonusFrames[i->frame];
			
			blitImage(
				gfx.screen,
				gfx.smallSprites.spritePtr(f),
				ftoi(i->x) - x - 3 + rect.x1,
				ftoi(i->y) - y - 3 + rect.y1,
				7, 7);
				
			if(game.settings.shadow)
			{
				blitShadowImage(
					gfx.screen,
					gfx.smallSprites.spritePtr(f),
					ftoi(i->x) - x - 5 + rect.x1,
					ftoi(i->y) - y - 1 + rect.y1, // This was - 3 in the original, but that seems wrong
					7, 7);
			}
			
			if(game.settings.namesOnBonuses
			&& i->frame == 0)
			{
				std::string const& name = game.weapons[i->weapon].name;
				int len = int(name.size()) * 4;
				
				gfx.drawTextSmall(
					name.c_str(),
					ftoi(i->x) - x - len/2 + rect.x1,
					ftoi(i->y) - y - 10 + rect.y1);
			}
		}
	}
		
	for(Game::SObjectList::iterator i = game.sobjects.begin(); i != game.sobjects.end(); ++i)
	{
		SObjectType& t = game.sobjectTypes[i->id];
		int frame = i->curFrame + t.startFrame;
		
		// TODO: Check that blitImageR is the correct one to use (probably)
		blitImageR(
			gfx.screen,
			gfx.largeSprites.spritePtr(frame),
			i->x + offsX,
			i->y + offsY,
			16, 16);
			
		if(game.settings.shadow)
		{
			blitShadowImage(
				gfx.screen,
				gfx.largeSprites.spritePtr(frame),
				i->x + offsX - 3,
				i->y + offsY + 3, // TODO: Original doesn't offset the shadow, which is clearly wrong. Check that this offset is correct.
				16, 16);
		}
	}
		
	// TODO: Check order of drawing between bonuses, wobjects, etc.
	
	for(Game::WObjectList::iterator i = game.wobjects.begin(); i != game.wobjects.end(); ++i)
	{
		Weapon& w = game.weapons[i->id];
		
		if(w.startFrame > -1)
		{
			int curFrame = i->curFrame;
			int shotType = w.shotType;
			
			if(shotType == 2)
			{
				curFrame += 4;
				curFrame >>= 3;
				if(curFrame < 0)
					curFrame = 16;
				else if(curFrame > 15)
					curFrame -= 16;
			}
			else if(shotType == 3)
Esempio n. 2
0
void C4Network2IO::HandlePacket(char cStatus, const C4PacketBase *pPacket, C4Network2IOConnection *pConn)
{
    // security
    if (!pConn) return;

#define GETPKT(type, name) \
    assert(pPacket); const type &name = \
     static_cast<const type &>(*pPacket);

    switch (cStatus)
    {

    case PID_Conn: // connection request
    {
        if (!pConn->isOpen()) break;
        // get packet
        GETPKT(C4PacketConn, rPkt)
        // set connection ID
        pConn->SetRemoteID(rPkt.getConnID());
        // check auto-accept
        if (doAutoAccept(rPkt.getCCore(), *pConn))
        {
            // send answer back
            C4PacketConnRe pcr(true, false, "auto accept");
            if (!pConn->Send(MkC4NetIOPacket(PID_ConnRe, pcr)))
                pConn->Close();
            // accept
            pConn->SetStatus(CS_HalfAccepted);
            pConn->SetCCore(rPkt.getCCore());
            pConn->SetAutoAccepted();
        }
        // note that this packet will get processed by C4Network2, too (main thread)
    }
    break;

    case PID_ConnRe: // connection request reply
    {
        if (!pConn->isOpen()) break;
        // conn not sent? That's fishy.
        // FIXME: Note this happens if the peer has exclusive connection mode on.
        if (!pConn->isConnSent())
        {
            pConn->Close();
            break;
        }
        // get packet
        GETPKT(C4PacketConnRe, rPkt)
        // auto accept connection
        if (rPkt.isOK())
        {
            if (pConn->isHalfAccepted() && pConn->isAutoAccepted())
                pConn->SetAccepted();
        }
    }
    break;

    case PID_Ping:
    {
        if (!pConn->isOpen()) break;
        GETPKT(C4PacketPing, rPkt)
        // pong
        C4PacketPing PktPong = rPkt;
        pConn->Send(MkC4NetIOPacket(PID_Pong, PktPong));
        // remove received packets from log
        pConn->ClearPacketLog(rPkt.getPacketCounter());
    }
    break;

    case PID_Pong:
    {
        if (!pConn->isOpen()) break;
        GETPKT(C4PacketPing, rPkt);
        // save
        pConn->SetPingTime(rPkt.getTravelTime());
    }
    break;

    case PID_FwdReq:
    {
        GETPKT(C4PacketFwd, rPkt);
        HandleFwdReq(rPkt, pConn);
    }
    break;

    case PID_Fwd:
    {
        GETPKT(C4PacketFwd, rPkt);
        // only received accidently?
        if (!rPkt.DoFwdTo(LCCore.getID())) break;
        // handle
        C4NetIOPacket Packet(rPkt.getData(), pConn->getPeerAddr());
        HandlePacket(Packet, pConn, true);
    }
    break;

    case PID_PostMortem:
    {
        GETPKT(C4PacketPostMortem, rPkt);
        // Get connection
        C4Network2IOConnection *pConn = GetConnectionByID(rPkt.getConnID());
        if (!pConn) return;
        // Handle all packets
        uint32_t iCounter;
        for (iCounter = pConn->getInPacketCounter(); ; iCounter++)
        {
            // Get packet
            const C4NetIOPacket *pPkt = rPkt.getPacket(iCounter);
            if (!pPkt) break;
            // Handle it
            HandlePacket(*pPkt, pConn, true);
        }
        // Log
        if (iCounter > pConn->getInPacketCounter())
            Application.InteractiveThread.ThreadLogS("Network: Recovered %d packets", iCounter - pConn->getInPacketCounter());
        // Remove the connection from our list
        if (!pConn->isClosed())
            pConn->Close();
        RemoveConnection(pConn);
    }
    break;

    }

#undef GETPKT
}
void fill_matrix_single(mwSize d, mwSize nz, mwSize nij, mwSize na, const mxArray *z_in, mxArray *a_out, mxArray *b_out)
{
  float *zr, *zi;    
  double xr, xi; 
  double yr, yi;
  mwIndex *arow, *acol;
  double *ar, *ai;    
  double *br, *bi;    
  mwIndex ij = 0;
  mwIndex kl;
  mwIndex ia = 0;
  mwIndex i,j,k,l,nd;

  // Get a pointer to the z data
  zr = (float*)mxGetPr(z_in);
  zi = (float*)mxGetPi(z_in);

  // Get a pointer to the data space in allocated memory
  ar = mxGetPr(a_out);
  ai = mxGetPi(a_out);
  arow = mxGetIr(a_out);
  acol = mxGetJc(a_out);
  br = mxGetPr(b_out);
  bi = mxGetPi(b_out);

  for (i=0; i < d; i++) {
    for (j=0; j < d; j++) {
      if (i==j) continue;
      kl = 0;
      if (ij >= nij) mexErrMsgTxt("vector b too small! This should not happen!");
      for (nd=0; nd < nz*d; nd+=d) {
          // b(ij) -= 2.*z(1,i,n)*z(0,j,n);
          br[ij] -= 2.*pcr(zr,zi,j+nd,i+nd);
          bi[ij] -= 2.*pci(zr,zi,j+nd,i+nd);
      }
      acol[ij] = ia;
      ij++;
      for (k=0; k < d; k++) {
        for (l=0; l < d; l++) {
          if (k==l) continue;
          if ((i!=k) && (i!=l) && (j!=k) && (j!=l)) {
            kl++;
            continue;
          }
          if (ia >= na) mexErrMsgTxt("sparse matrix too small! This should not happen!");
          arow[ia] = kl;
          // if (i==k) for (n=0; n < nz; n++) adata(ia) -= .5*z(1,i,n)*z(1,k,n)*z(0,l,n)*z(0,j,n);
          if (i==k) for (nd=0; nd < nz*d; nd+=d) {
              xr = pr(zr,zi,i+nd,k+nd);
              xi = -pi(zr,zi,i+nd,k+nd);
              yr = pr(zr,zi,l+nd,j+nd);
              yi = pi(zr,zi,l+nd,j+nd);
              ar[ia] -= .5*mr(xr,xi,yr,yi);
              ai[ia] -= .5*mi(xr,xi,yr,yi);
            }

          // if (i==l) for (n=0; n < nz; n++) adata(ia) += .5*z(0,j,n)*z(1,k,n);
          if (i==l) for (nd=0; nd < nz*d; nd+=d) {
              ar[ia] += .5*pcr(zr,zi,j+nd,k+nd);
              ai[ia] += .5*pci(zr,zi,j+nd,k+nd);
            }

          // if (j==k) for (n=0; n < nz; n++) adata(ia) += .5*z(1,i,n)*z(0,l,n);
          if (j==k) for (nd=0; nd < nz*d; nd+=d) {
              ar[ia] += .5*pcr(zr,zi,l+nd,i+nd);
              ai[ia] += .5*pci(zr,zi,l+nd,i+nd);
            }

          // if (j==l) for (n=0; n < nz; n++) adata(ia) -= .5*z(1,i,n)*z(1,k,n)*z(0,l,n)*z(0,j,n);
          if (j==l) for (nd=0; nd < nz*d; nd+=d) {
              xr = pr(zr,zi,i+nd,k+nd);
              xi = -pi(zr,zi,i+nd,k+nd);
              yr = pr(zr,zi,l+nd,j+nd);
              yi = pi(zr,zi,l+nd,j+nd);
              ar[ia] -= .5*mr(xr,xi,yr,yi);
              ai[ia] -= .5*mi(xr,xi,yr,yi);           
            }
          kl++;
          ia++;
        }
      }
    }
  }
  acol[nij] = ia;
  return;
}