Esempio n. 1
0
void NetCity::Packetize(uint8* buf, uint16& size)
{
	buf[0] = 'C';
	buf[1] = 'D';
	uint16 unitSize;

	putlong(&buf[2], m_unitData->m_id);
	NetUnit::PacketizeUnit(&buf[6], unitSize, m_unitData);

	size = 6 + unitSize;
	CityData* cityData = m_unitData->m_city_data;

	PUSHBYTE(m_isInitialPacket);

	PUSHLONG(cityData->m_slaveBits);
	PUSHLONG(cityData->m_shieldstore);
	PUSHLONG(cityData->m_shieldstore_at_begin_turn);
	PUSHLONG(cityData->m_net_gold);
	PUSHLONG(cityData->m_science);
	PUSHLONG(cityData->m_luxury);
	PUSHLONG(cityData->m_city_attitude);
	PUSHLONG64((uint64)cityData->m_built_improvements);

	
	

#ifdef CTP1_TRADE
	uint8 numNonZeroResources = 0;
	
	sint32 resourceCountPosition = size++;

	sint32 i;

	for(i = 0; i < g_theResourceDB->NumRecords(); i++) {
		if(cityData->m_resources.GetResourceCount(i) > 0) {
			numNonZeroResources++;
			PUSHBYTE((uint8)i);
			PUSHSHORT((uint16)cityData->m_resources.GetResourceCount(i));
		}
	}
	buf[resourceCountPosition] = numNonZeroResources;
#endif

	sint32 i;
	PUSHLONG(cityData->m_population);
	for(i = 0; i < (sint32)POP_MAX; i++) {
		PUSHSHORT(cityData->m_numSpecialists[i]);
	}
	PUSHLONG(cityData->m_partialPopulation);
	PUSHLONG(cityData->m_sizeIndex);
	PUSHLONG(cityData->m_workerFullUtilizationIndex);
	PUSHLONG(cityData->m_workerPartialUtilizationIndex);

	PUSHDOUBLE(cityData->m_defensiveBonus);
	PUSHBYTE((uint8)cityData->m_founder);

	PacketizeResources(cityData->m_collectingResources, buf, size);
	PacketizeResources(cityData->m_sellingResources, buf, size);
	PacketizeResources(cityData->m_buyingResources, buf, size);

	sint32 r;
	for(r = 0; r < g_theResourceDB->NumRecords(); r++) {
		PUSHLONG(cityData->m_distanceToGood[r]);
	}
	
}
Esempio n. 2
0
thStatus thExecuteCode(char *blockname,CODEPTR code, CODEPTR codelimit)
{
#ifdef PHILDEBUG
#ifdef NOTPOSIX
#warning Phil says NOTPOSIX!
#else
#warning Phil says not NOTPOSIX! i.e. POSIX!!
#endif
#ifdef POINTER64
#warning Phil says POINTER64!
#else
#warning Phil says not POINTER64!
#endif
#ifdef USEMEMCPY
#warning Phil says USEMEMCPY!
#else
#warning Phil says not USEMEMCPY!
#endif
#endif
  register CODEPTR pc;
  CODE rawopcode,opcode,ltype,rtype,lrtypes;
  DAINT nargs,result;
  register DAINT *sp;
  DAINT i,il,ir,*pi;
  DAFLOAT f,fl,fr,*pf;
  DADOUBLE d,dl,dr,*pd;
  DAINT index;

  sp = stack;
  pc = code;

  while(pc < codelimit){
/*    printf("PC=%x, Op code %x, Stack=%x, SP=%x\n",pc,*pc,stack,sp);*/
    rawopcode = *pc++;
    if(rawopcode >= OPLP){		/* New style */
      ltype = (rawopcode & OPLEFTTYPEMASK) >> 8;
      rtype = (rawopcode & OPRIGHTTYPEMASK) >> 4;
/*      lrtypes = opcode & OPLRTYPEMASK;*/
      opcode = rawopcode & OPCODEMASK;
      switch(opcode & OPGROUPMASK)
	{
	case OPPUSHGROUP:		/* Pushes */
	  switch(opcode)
	    {
#ifdef USEMEMCPY
	      void *tmpptr;
#endif
	    case OPPUSHINT:	/* Float included in pushes */
	      if((rawopcode & OPRESTYPEMASK) == OPRDOUBLE){
/*		printf("sp=%x, pc=%x\n",sp,pc);*/
#ifdef USEMEMCPY
		memcpy((void *)&d,((DADOUBLE *)pc)++,sizeof(DADOUBLE));
		PUSHDOUBLE(d);
#else
#ifdef __sgi
		PUSHDOUBLE(*((DADOUBLE *)pc)); pc++; pc++;
#else
		PUSHDOUBLE(*(DADOUBLE *)pc);/*phil*/
                pc = (CODEPTR) (DADOUBLE *) ((DADOUBLE *)pc + 1);
#endif
#endif
/*		printf("sp=%x, pc=%x\n",sp,pc);*/
	      } else {
		PUSHINT(*pc++);
	      }
	      break;
	    case OPPUSHPINT:	/*Push a pointer*/
#ifdef USEMEMCPY
	      PUSHPOINTER((memcpy(&tmpptr,(((DAINT **)pc)++),sizeof(void *))
			  ,tmpptr));
#else
              PUSHPOINTER(*(DAINT **)pc); /*phil*/
              pc = (CODEPTR)(DAINT **) ((DAINT **)pc + 1);
#endif
	      break;
	    case OPPUSHINTP:    /*Push what a pointer points to */
	      if((rawopcode & OPRESTYPEMASK) == OPRDOUBLE){
#ifdef USEMEMCPY
		memcpy(&tmpptr,(((DAINT **)pc)++),sizeof(void *));
		d = *(DADOUBLE *) tmpptr;
#else
		d = **(DADOUBLE **)pc;/*phil*/
                pc = (CODEPTR) (DADOUBLE **) ((DADOUBLE **)pc + 1);
#endif	      
		PUSHDOUBLE(d);/*phil*/
	      } else {
#ifdef USEMEMCPY
		memcpy(&tmpptr,(((DAINT **)pc)++),sizeof(void *));
		PUSHINT(*(DAINT *) tmpptr);
#else		
		PUSHINT(**(DAINT **)pc);/*phil*/
                pc = (CODEPTR) (DAINT **) ((DAINT **)pc + 1);
#endif
	      }
	      break;
	    case OPPUSHFUNCTION:	/*Push a intrinsic function code */
	      PUSHINT(*pc++);
	      break;
	    }
	  break;
	case OPEOLGROUP:
	  sp--;		/* Should empty the stack */
	  if(rtype == OPRDOUBLE) sp--; /* Double is two entries on stack */
	  break;
	case OPLINDEXGROUP:
	  if(opcode==OPLFARG) {
	    if(rtype==OPRINT) {POPINT(i);}
	    else if(rtype==OPRFLOAT) {POPFLOAT(f);}/*phil*/
	    else {POPDOUBLE(d);}/*phil*/
	    POPINT(index);	/* Pop the function code */
	    switch(index)
	      {
	      case 0:		/* abs */
		if(rtype==OPRINT) {
		  if(i<0) i = -i;
		  PUSHINT(i);
		} else if(rtype==OPRFLOAT) {
		  if(f<0.0) f = -f;
		  PUSHFLOAT(f);/*phil*/
		} else {
		  if(d<0.0) d = -d;
		  PUSHDOUBLE(d);/*phil*/
		}
		break;
	      case 1:		/* sqrt */
		if(rtype==OPRINT) d = i;
		else if(rtype==OPRFLOAT) d = f;
		if(d>=0) d = sqrt(d);
		else {
		  fprintf(STDERR,"Test block %s: sqrt(%f)\n",blockname,d);
		  d = 0;
		}
		PUSHDOUBLE(d);/*phil*/
		break;
	      case 2:		/* exp */
		if(rtype==OPRINT) d = i;
		else if(rtype==OPRFLOAT) d = f;
		d = exp(d);
		PUSHDOUBLE(d);/*phil*/
		break;
	      case 3:		/* sin */
		if(rtype==OPRINT) d = i;
		else if(rtype==OPRFLOAT) d = f;
		d = sin(d);
		PUSHDOUBLE(d);/*phil*/
		break;
	      case 4:		/* cos */
		if(rtype==OPRINT) d = i;
		else if(rtype==OPRFLOAT) d = f;
		d = cos(d);
		PUSHDOUBLE(d);/*phil*/
		break;
	      case 5:		/* tan */
		if(rtype==OPRINT) d = i;
		else if(rtype==OPRFLOAT) d = f;
		d = tan(d);
		PUSHDOUBLE(d);/*phil*/
		break;
	      }
	    break;
	  }
	  if(rtype==OPRFLOAT) {	/* Floating point index */
	    POPFLOAT(f);/*phil*/
	    index = floatToLong(f);
	  } else if(rtype==OPRDOUBLE) {	/* Double */
	    POPDOUBLE(d);/*phil*/
	    index = floatToLong(d);
	  } else {
	    POPINT(index);
	  }
	  index -= ((opcode & 0xF000) == 0x1000 ? 0 : 1);
	  /* ltype should always be == restype */
	  if(opcode == OPLINDEX || opcode == OPLINDEXB){
	    if(ltype == OPRDOUBLE) {
	      FETCHDARRAY(d);/*phil*/
	      PUSHDOUBLE(d);/*phil*/
	    } else if (ltype == OPRFLOAT) {
              FETCHFARRAY(f);/*phil*/
              PUSHFLOAT(f);/*phil*/
	    } else {
              FETCHIARRAY(i);/*phil*/
	      PUSHINT(i);
	    }
	  } else { /*pointer on stack*/
	    sp--;
#ifdef POINTER64
	    sp--;
#endif
	    if(ltype == OPRDOUBLE) {
	      /*	      *((DADOUBLE **)sp)++ =  (*((DADOUBLE **)sp)+index);*/
	      /* The following works better on the alpha */
	      pd = *((DADOUBLE **)sp);
	      pd += index;
	      PUSHPOINTER(pd);/*phil*/
	    } else {		/* Assume INT and FLOAT the same size */
	      /**((DAINT **)sp)++ =  (*((DAINT **)sp)+index);*/
	      /* The following works better on the alpha */
	      pi = *((DAINT **)sp);
	      pi += index;
	      PUSHPOINTER(pi);/*phil*/
	    }
	  }
	  break;
	case OPEQUAL:		/* Big ugly matrix of type conversions */
	  if(rtype==OPRINT) {
	    POPINT(i);
	    if(ltype==OPRINT) {
	      SAVEINT(i); /* Save result in result variable *//*phil*/
	      PUSHINT(i);	/* Put result back on stack */
	    } else if(ltype==OPRFLOAT) {
	      f = i;	/* Convert to floating */
	      SAVEFLOAT(f); /* Save variable *//*phil*/
	      PUSHFLOAT(f); /* Put back on stack *//*phil*/
	    } else {		/* if(ltype==OPRDOUBLE) */
	      d = i;
	      SAVEDOUBLE(d);/*phil*/
	      PUSHDOUBLE(d);/*phil*/
	    }
	  } else if(rtype==OPRFLOAT) {
	    POPFLOAT(f);/*phil*/
	    if(ltype==OPRINT) {
	      i = floatToLong(f);
	      SAVEINT(i); /* Save result in result variable *//*phil*/
	      *sp++ = i;
	    } else if(ltype==OPRFLOAT) {
	      SAVEFLOAT(f); /* Save variable *//*phil*/
	      *sp++ = *(DAINT *)&f;
	    } else {		/* if(ltype==OPRDOUBLE) */
	      d = f;
	      SAVEDOUBLE(d);/*phil*/
	      PUSHDOUBLE(d);/*phil*/
	    }
	  } else {		/* if(rtype==OPRDOUBLE) */
	    POPDOUBLE(d);/*phil*/
	    if(ltype==OPRINT) {
	      i = floatToLong(d);
	      SAVEINT(i); /* Save result in result variable *//*phil*/
	      *sp++ = i;
	    } else if(ltype==OPRFLOAT) {
	      f = d;
	      SAVEFLOAT(f); /* Save variable *//*phil*/
	      *sp++ = *(DAINT *)&f;
	    } else {		/* if(ltype==OPRDOUBLE) */
 	      SAVEDOUBLE(d);/*phil*/
	      PUSHDOUBLE(d);/*phil*/
	    }
	  }
	  break;
	case OPLOGGROUP:		/* Logic and Bit operations */
	case OPSHIFTGROUP:		/* Logic and Bit operations */
	  if(rtype==OPRINT) {
	    POPINT(ir);
	  } else if(rtype==OPRFLOAT) {
	    POPFLOAT(f);/*phil*/
	    ir = floatToLong(f);
	  } else {
	    POPDOUBLE(d);/*phil*/
	    ir = floatToLong(d);
	  }
	  if(ltype==OPRINT) {
	    POPINT(il);
	  } else if(ltype==OPRFLOAT) {
	    POPFLOAT(f);/*phil*/
	    il = floatToLong(f);
	  } else {
	    POPDOUBLE(d);/*phil*/
	    il = floatToLong(d);
	  }
	  switch(opcode)
	    {
	    case OPLOGOR:
	      *sp++ = il || ir;
	      break;
	    case OPLOGXOR:
	      *sp++ = (il != 0) ^ (ir != 0);
	      break;
	    case OPLOGAND:
	      *sp++ = il && ir;
	      break;
	    case OPBITOR:
	      *sp++ = il | ir;
	      break;
	    case OPBITXOR:
	      *sp++ = il ^ ir;
	      break;
	    case OPBITAND:
	      *sp++ = il & ir;
	      break;
	    case OPSHL:
	      *sp++ = il << ir;
	      break;
	    case OPSHR:
	      *sp++ = il >> ir;
	      break;
	    }
	  break;
	case OPCOMPGROUP:		/* Logic comparisons */
/* Result of Add amd MUL groups should now always be double */
	case OPADDGROUP:	/* Add and Subtract */
	case OPMULGROUP:	/* * / and % */
	  if(rtype==OPRINT) {
	    POPINT(ir);
	    dr = ir;
	  } else if (rtype==OPRFLOAT) {
	    POPFLOAT(fr);/*phil*/
	    dr = fr;
	  } else {
	    POPDOUBLE(dr);/*phil*/
	  }
	  if(ltype==OPRINT) {
	    POPINT(il);
	    dl = il;
	  } else if (ltype==OPRFLOAT) {
	    POPFLOAT(fl);/*phil*/
	    dl = fl;
	  } else {
	    POPDOUBLE(dl);/*phil*/
	  }
	  if(rtype!=OPRINT || ltype!=OPRINT){
	    switch(opcode)
	      {
	      case OPISEQUAL:
		*sp++ = dl == dr;
		break;
	      case OPISNOTEQUAL:
		*sp++ = dl != dr;
		break;
	      case OPISLT:
		*sp++ = dl < dr;
		break;
	      case OPISGT:
		*sp++ = dl > dr;
		break;
	      case OPISLE:
		*sp++ = dl <= dr;
		break;
	      case OPISGE:
		*sp++ = dl >= dr;
		break;
	      case OPADD:
		d = dl + dr;
		PUSHDOUBLE(d);/*phil*/
		break;
	      case OPSUB:
		d = dl - dr;
		PUSHDOUBLE(d);/*phil*/
		break;
	      case OPTIMES:
		d = dl * dr;	/* Need to deal with overflow */
		PUSHDOUBLE(d);/*phil*/
		break;
	      case OPIDIV:
/*		printf("OP=%x\n",rawopcode);*/
		if(dr == 0.0) {
		  fprintf(STDERR,"Test block %s: %f/0.0\n",blockname,dl);
		  d = 0.0;
		} else {
		  d = dl / dr;	/* Need to deal with overflow and div 0 */
		}
		*sp++ = floatToLong(d);
		break;
	      case OPDIV:
		if(dr == 0.0) {
		  fprintf(STDERR,"Test block %s: %f/0.0\n",blockname,dl);
		  d = 0.0;
		} else {
		  d = dl / dr;	/* Need to deal with overflow and div 0 */
		}
		PUSHDOUBLE(d);/*phil*/
		break;
	      case OPMOD:
		d = fmod(dl,dr);
		PUSHDOUBLE(d);/*phil*/
		break;
	      }
	  } else {		/* Both left and right are int */
	    switch(opcode)
	      {
	      case OPISEQUAL:
		*sp++ = il == ir;
		break;
	      case OPISNOTEQUAL:
		*sp++ = il != ir;
		break;
	      case OPISLT:
		*sp++ = il < ir;
		break;
	      case OPISGT:
		*sp++ = il > ir;
		break;
	      case OPISLE:
		*sp++ = il <= ir;
		break;
	      case OPISGE:
		*sp++ = il >= ir;
		break;
	      case OPADD:
		*sp++ = il + ir;
		break;
	      case OPSUB:
		*sp++ = il - ir;
		break;
	      case OPTIMES:
		*sp++ = il * ir; /* Need to deal with overflow */
		break;
	      case OPIDIV:
/*		printf("At OPIDIV all int branch\n");*/
		if(ir == 0) {
		  fprintf(STDERR,"Test block %s: %d/0.0\n",blockname,il);
		  *sp++ = 0;
		} else {
		  *sp++ = il / ir;
		}
		break;
	      case OPDIV:
		if(ir == 0) {
		  fprintf(STDERR,"Test block %s: %d/0.0\n",blockname,il);
		  d = 0.0;
		} else
		  d = dl / dr; /* Need to deal with overflow and div 0 */
		PUSHDOUBLE(d);/*phil*/
		break;
	      case OPMOD:
		*sp++ = il % ir; /* Need to deal with overflow and div 0 */
		break;
	      }
	  }
	  break;
	case OPUNARY:		/* Unary Operators */
	  switch(opcode)
	    {
	    case OPNEG:
	      if(rtype==OPRINT) {
		i = -(*--sp);
	        *sp++ = i;
	      } else if (rtype==OPRFLOAT) {
		f = *(DAFLOAT *)(--sp);
		f = -f;
		*sp++ = *(DAINT *)&f;
	      } else {
		POPDOUBLE(d);/*phil*/
		d = -d;
		PUSHDOUBLE(d);/*phil*/
	      }
	      break;
	    case OPNOT:
	    case OPCOMP:
	      if(rtype==OPRINT) {
		POPINT(i);
	      } else if(rtype==OPRFLOAT) {
		POPFLOAT(f);/*phil*/
		i = floatToLong(f);
	      } else {
		POPDOUBLE(d);/*phil*/
		i = floatToLong(d);
	      }
	      i = (opcode == OPNOT ? !i : ~i);
	      *sp++ = i;
	      break;
	    }
	  break;
	default:
	  fprintf(STDERR,"Test block %s: Operator %x not yet implimented\n",
		  blockname,opcode);
	  break;
	} /* Terminates switch */
    } else {	/* terminates if(rawopcode >=OPLP) *//* Old Style, May not work anymore */
      switch(*pc++)
Esempio n. 3
0
void NetCity2::Packetize(uint8* buf, uint16 &size)
{
	size = 0;
	PUSHID(k_PACKET_CITY2_ID);

	PUSHLONG((uint32)m_data->m_home_city);
	
	PUSHLONG(m_data->m_gold_lost_to_crime);
	PUSHLONG(m_data->m_gross_gold);
	PUSHLONG(m_data->m_goldFromTradeRoutes);
	PUSHLONG(m_data->m_gross_production);
	PUSHLONG(m_data->m_net_production);
	PUSHLONG(m_data->m_production_lost_to_crime);
	PUSHLONG64((uint64)m_data->m_builtWonders);
	PUSHDOUBLE(m_data->m_food_delta);
	PUSHDOUBLE(m_data->m_gross_food);
	PUSHDOUBLE(m_data->m_net_food);
	PUSHDOUBLE(m_data->m_food_lost_to_crime);
	PUSHDOUBLE(m_data->m_food_consumed_this_turn);
	PUSHLONG(m_data->m_total_pollution);

	uint8 flags = 
		(uint8)m_data->m_contribute_materials |
		((uint8)m_data->m_contribute_military << 1) |
		((uint8)m_data->m_buildCapitalization << 4) |
		((uint8)m_data->m_walls_nullified << 5) |
		((uint8)m_data->m_isInjoined << 6) |
		((uint8)m_data->m_buildInfrastructure << 7);
	PUSHBYTE(flags);

#if 0
	DPRINTF(k_DBG_NET, ("city: %d, %d, %d, %d, %d, %d, %d, %d, %d\n",
						m_data->m_spied_upon,
						m_data->m_franchise_owner,
						m_data->m_franchiseTurnsRemaining,
						m_data->m_ignore_happiness,
						m_data->m_watchfulTurns,
						m_data->m_bioInfectionTurns,
						m_data->m_nanoInfectionTurns,
						m_data->m_convertedTo,
						m_data->m_convertedBy));
#endif
						
	PUSHBYTE((sint8)m_data->m_spied_upon);

	PUSHBYTE((sint8)m_data->m_franchise_owner);
	PUSHBYTE((sint8)m_data->m_franchiseTurnsRemaining);
	
#ifdef _DEBUG
	
#endif
	PUSHBYTE((sint8)m_data->m_watchfulTurns);
	PUSHBYTE((sint8)m_data->m_bioInfectionTurns);
	PUSHBYTE((sint8)m_data->m_nanoInfectionTurns);
	PUSHBYTE((sint8)m_data->m_convertedTo);
	PUSHBYTE((sint8)m_data->m_convertedBy);
	PUSHLONG(m_data->m_convertedGold);

	PUSHLONG(m_data->m_accumulated_food);
	

	
	

	PUSHLONG(m_data->m_foodVatPollution);
	sint8 govSetting = -1;
	if(m_data->m_useGovernor) {
		govSetting = (sint8)m_data->m_buildListSequenceIndex;
	}
	PUSHBYTE(govSetting);

	PUSHBYTE(m_isInitialPacket);
}