Beispiel #1
0
str *DynamInt::__add__(DynamInt *num) {
    /**
    Overloaded add function
    Adds an existing DynamInt to this one
    @param  num reference to an existing DynamInt
    @return added DynamInt
    */
    str *bottom, *sum, *top;
    int carryover, i, j, tempSum;

    /**
    Basically, this selects what should be added to what
    it is based on primary school addition techniques
    i.e
    to add 1234 to 12345, the code makes a configuration similar to
    
            1  2  3  4  5
            +  1  2  3  4
            -------------
            1  3  5  7  9
            -------------
    The larger number goes on top while the smaller goes below
    */
    if (__gt(this, num)) {
        top = this->data;
        bottom = num->data;
    }
    else {
        top = num->data;
        bottom = this->data;
    }
    /**
    Temporary Variables used in addition process
    tempSum     ----- Stores partial subtraction
    j           ----- Stores length of bottom number
    i           ----- Stores length of top number
    sum         ----- Stores final sum string
    carryover   ----- A flag that determines if there is a carryover to the next digit
    */
    tempSum = 0;
    j = (len(bottom)-1);
    i = (len(top)-1);
    sum = const_0;
    carryover = 0;

    while ((i>=0)) {
        /**
         Have we run out of bottom digits ? 
        */
        if ((j>=0)) {
            tempSum = ((__int(top->__getitem__(i))+__int(bottom->__getitem__(j)))+carryover);
            j = (j-1);
        }
        else {
            /**
             No more bottom digits, add any leftover carryover
            */
            tempSum = (__int(top->__getitem__(i))+carryover);
        }
        if ((tempSum>=10)) {
            /**
             do we have a remainder to carryover ? 
            */
            tempSum = __mods(tempSum, 10);
            carryover = 1;
        }
        else {
            carryover = 0;
        }
        /**
         lets concatenate the main sum outputed 
        */
        sum = (__str(tempSum))->__add__(sum);
        i = (i-1);
    }
    if ((carryover==1)) {
        sum = (__str(carryover))->__add__(sum);
    }
    return sum;
}
Beispiel #2
0
str *DynamInt::__sub__(DynamInt *num) {
    /**
    Overloaded Subtraction function
    subtracts an existing DynamInt from this one
    
    @param  num reference to an existing DynamInt
    @return added DynamInt
    */
    str *bottom, *sub, *sum, *top;
    int borrow, i, j, tempSub;

    /**
    Uses two's compliment on each element of both numbers
    */
    if (__eq(this, num)) {
        return const_1;
    }
    if (__gt(this, num)) {
        top = this->data;
        bottom = num->data;
    }
    else if (__gt(num, this)) {
        top = num->data;
        bottom = this->data;
    }
    /**
    Temporary Variables used in addition process
    tempSub     ----- Stores partial subtraction
    j           ----- Stores length of bottom number
    i           ----- Stores length of top number
    sub         ----- Stores final sub string
    borrow      ----- A flag that determines if it would need to borrow from next digit
    */
    tempSub = 0;
    j = (len(bottom)-1);
    i = (len(top)-1);
    borrow = 0;
    sub = const_0;

    while ((i>=0)) {
        /**
         Have we run out of bottom digits ? 
        */
        if ((j>=0)) {
            tempSub = ((__int(top->__getitem__(i))-__int(bottom->__getitem__(j)))-borrow);
            if ((tempSub<0)) {
                borrow = 1;
                tempSub = (tempSub+10);
            }
            else {
                borrow = 0;
            }
            j = (j-1);
        }
        else {
            /**
             No more bottom digits, subtract any leftover borrow out
            */
            tempSub = (__int(top->__getitem__(i))-borrow);
            if ((tempSub<0)) {
                borrow = 1;
                tempSub = (tempSub+10);
            }
            else {
                borrow = 0;
            }
        }
        /**
         lets concatenate the main sum outputed 
        */
        sub = (__str(tempSub))->__add__(sub);
        i = (i-1);
    }
    if ((borrow==1)) {
        sum = (const_2)->__add__(sub);
    }
    return sub;
}
Beispiel #3
0
 //the setting is by proxy as well
 void SetNucleonList(NucleonCollection& nucleon_collection, list l) {
     nucleon_collection.Reset();
     for(unsigned int i = 0; i < len(l); i++) {
         nucleon_collection.AddNucleon(extract<Nucleon>(l[i]));
     }
 }
Beispiel #4
0
int main(int argc, char *argv[]) {
   if (argc != 2) {
      cout << "Usage: setup outfilename" << endl;
      return(-1);
   }
   string filename = argv[1];

   vector<Cparticle> ps;
   Cparticle p;
   cout << "Creating box with sides: rmax = ["<<RMAX[0]<<" "<<RMAX[1]<<"] rmin = ["<<RMIN[0]<<" "<<RMIN[1]<<"]"<<endl;
   cout << "Reynolds Number = "<<REYNOLDS_NUMBER<<endl;
   cout << "Density = "<<DENS<<endl;
   cout << "number of particles on side = "<<NX<<endl;
   cout << "alpha = "<<ALPHA<<endl;
   cout << "viscosity = "<<VISCOSITY<<endl;
   cout << "maxtime = "<<MAXTIME<<endl;
 
   double tsc = Nsph::courantCondition(H,2*SPSOUND);
   double tsv = Nsph::viscDiffusionCondition(H,VISCOSITY);
   cout <<"simulation will take "<<int((MAXTIME/tsc)+1)<<" steps according to Courant condition, "<<int((MAXTIME/tsv)+1)<<" steps according to visc diffusion condition"<<endl;

   for (int i=0;i<NX;i++) {
         cout << "\rParticle ("<<i<<","<<"0"<<"). Generation "<<((i+2)*(NY+4))/double((NX+4)*(NY+4))*100<<"\% complete"<<flush;
      for (int j=-3;j<=NY+3;j++) {
         p.tag = ps.size()+1;
         p.r = i*PSEP+RMIN[0],j*PSEP+RMIN[1];
         p.dens = DENS;
         p.mass = PSEP*PSEP*DENS;
         p.h = H;
         p.v = 0,0;
         p.vhat = p.v;

         if (j<=0) {
            p.iam = sphBoundary;
            p.norm1[0] = 0;
            p.norm1[1] = -j*PSEP;
            p.dist = len(p.norm1);
            if (p.dist==0) {
               p.norm1[1] = 1.0;
            } else {
               p.norm1 = p.norm1/p.dist;
            }
            p.v[0] = WALL_SPEED;
            p.vhat = p.v;
         } else if (j>=NY) {
            p.iam = sphBoundary;
            p.norm1[0] = 0;
            p.norm1[1] = (NY-j)*PSEP;
            p.dist = len(p.norm1);
            if (p.dist==0) {
               p.norm1[1] = -1.0;
            } else {
               p.norm1 = p.norm1/p.dist;
            }
         } else {
            p.iam = sph;
            //p.v[0] += gsl_cheb_eval(csX,p.r[0]);
            //p.v[1] += gsl_cheb_eval(csY,p.r[1]);
         }
         p.alpha = ALPHA;
         ps.push_back(p);
      }
   }

   cout << "Total number of particles = " << ps.size() << endl;

   CglobalVars globals;


   vector<vector<double> > vprocDomain(globals.mpiSize);
   vector<Array<int,NDIM> > vprocNeighbrs(globals.mpiSize);
   vector<particleContainer > vps;
   vectInt split;
   split = 1,1;
   particleContainer pps;
   for (int i=0;i<ps.size();i++) {
      pps.push_back(ps[i]);
   }
   Nmisc::splitDomain(pps,split,vps,vprocDomain,vprocNeighbrs);



   //CdataLL *data = new CdataLL(ps,globals);
   //CsphIncompress sph(data);
   //CcustomOutput customOutput(data);
   //CcustomSim customSim(data,globals.time);


   cout << "Opening files for writing..."<<endl;
   Cio_data_vtk ioFile(filename.c_str(),&globals);
   cout << "Calculating Output stuff.."<<endl;
   //sph.calcOutputVars();
   //customOutput.calcOutput(0,&customSim,&ioFile);
   cout << "Writing Restart data to file..."<<endl;
   int nProc = product(split);
   for (int i=0;i<nProc;i++) {
      globals.mpiRank = i;
      ioFile.setFilename(filename.c_str(),&globals);
      ioFile.writeGlobals(0,&globals);
      ioFile.writeRestart(0,vps[i],&globals);
      globals.mpiRank = 0;
   }
   cout << "Writing Global data to file..."<<endl;
   //ioFile.writeGlobals(0,&globals);
   ioFile.writeDomain(0,vprocDomain,vprocNeighbrs);

}
// static
LLURI LLURI::buildHTTP(const std::string& prefix,
                       const LLSD& path)
{
    LLURI result;

    // TODO: deal with '/' '?' '#' in host_port
    if (prefix.find("://") != prefix.npos)
    {
        // it is a prefix
        result = LLURI(prefix);
    }
    else
    {
        // it is just a host and optional port
        result.mScheme = "http";
        result.mEscapedAuthority = escapeHostAndPort(prefix);
    }

    if (path.isArray())
    {
        // break out and escape each path component
        for (LLSD::array_const_iterator it = path.beginArray();
                it != path.endArray();
                ++it)
        {
            LL_DEBUGS() << "PATH: inserting " << it->asString() << LL_ENDL;
            result.mEscapedPath += "/" + escapePathComponent(it->asString());
        }
    }
    else if (path.isString())
    {
        std::string pathstr(path);
        // Trailing slash is significant in HTTP land. If caller specified,
        // make a point of preserving.
        std::string last_slash;
        std::string::size_type len(pathstr.length());
        if (len && pathstr[len-1] == '/')
        {
            last_slash = "/";
        }

        // Escape every individual path component, recombining with slashes.
        for (boost::split_iterator<std::string::const_iterator>
                ti(pathstr, boost::first_finder("/")), tend;
                ti != tend; ++ti)
        {
            // Eliminate a leading slash or duplicate slashes anywhere. (Extra
            // slashes show up here as empty components.) This test also
            // eliminates a trailing slash, hence last_slash above.
            if (! ti->empty())
            {
                result.mEscapedPath
                += "/" + escapePathComponent(std::string(ti->begin(), ti->end()));
            }
        }

        // Reinstate trailing slash, if any.
        result.mEscapedPath += last_slash;
    }
    else if(path.isUndefined())
    {
        // do nothing
    }
    else
    {
        LL_WARNS() << "Valid path arguments to buildHTTP are array, string, or undef, you passed type"
                   << path.type() << LL_ENDL;
    }
    result.mEscapedOpaque = "//" + result.mEscapedAuthority +
                            result.mEscapedPath;
    return result;
}
Beispiel #6
0
 __ss_reverse(pyseq<A> *p) {
     this->p = p;
     i = len(p);
 }
Beispiel #7
0
int doStuff(int a, int b, int c, int d, int n){
	best = max(best, a <= d && c <= b ? min(b,d) - max(a,c) + 1 : 0);

	if (n > 1){
		if (a == b && a == len(n-1) + 1) return;
		if (c == d && c == len(n-1) + 1) return;

		if (a <= len(n-1) && b > len(n-1)){
			if (len(n-1) - a + 1 > b - len(n-1) - 1){
				b = len(n-1);
			} else {
				a = 1;
				b = (b-1) - len(n-1);
			}
		} else {
			a = a <= len(n-1) ? a : (a-1) - len(n-1);
			a = max(1,a);
			b = b <= len(n-1) ? b : (b-1) - len(n-1);
			b = max(1,b);
		}

		if (c <= len(n-1) && d > len(n-1)){
			if (len(n-1) - c + 1 > d - len(n-1) - 1){
				d = len(n-1);
			} else {
				c = 1;
				d = (d-1) - len(n-1);
			}
		} else {
			c = c <= len(n-1) ? c : (c-1) - len(n-1);
			c = max(1,c);
			d = d <= len(n-1) ? d : (d-1) - len(n-1);
			d = max(1,d);
		}

		doStuff(a,b,c,d,n-1);
	}
}
Beispiel #8
0
inline void normalize(vec a) { mul(a, 1 / len(a)); }
Beispiel #9
0
/* loads the game from save.dat */
char load()
{
   //LOAD FILE
   int loadversion;
   int l;
   bool dummy_b;
   int dummy;
   long dummy_l;
   FILE *h;

   h=LCSOpenFile("save.dat", "rb", LCSIO_PRE_HOME);
   if(h!=NULL)
   {
      fread(&loadversion,sizeof(int),1,h);

      //NUKE INVALID SAVE GAMES
      if(loadversion<lowestloadversion)
      {
         LCSCloseFile(h);

         reset();

         return 0;
      }

      fread(seed,sizeof(unsigned long),RNG_SIZE,h);

      fread(&mode,sizeof(short),1,h);
      fread(&wincondition,sizeof(short),1,h);
      fread(&fieldskillrate,sizeof(short),1,h);

      fread(&day,sizeof(int),1,h);
      fread(&month,sizeof(int),1,h);
      fread(&year,sizeof(int),1,h);
      fread(&execterm,sizeof(short),1,h);
      fread(&presparty,sizeof(short),1,h);
      fread(&amendnum,sizeof(int),1,h);

      fread(&multipleCityMode,sizeof(bool),1,h);
      fread(&termlimits,sizeof(bool),1,h);
      fread(&deagle,sizeof(bool),1,h);
      fread(&m249,sizeof(bool),1,h);
      fread(&notermlimit,sizeof(bool),1,h);
      fread(&nocourtpurge,sizeof(bool),1,h);
      fread(&stalinmode,sizeof(bool),1,h);

      fread(&stat_recruits,sizeof(int),1,h);
      fread(&stat_dead,sizeof(int),1,h);
      fread(&stat_kills,sizeof(int),1,h);
      fread(&stat_kidnappings,sizeof(int),1,h);
      fread(&stat_buys,sizeof(int),1,h);
      fread(&stat_burns,sizeof(int),1,h);

      fread(&endgamestate,sizeof(char),1,h);
      fread(&ccsexposure,sizeof(char),1,h);
      fread(&ccs_kills,sizeof(char),1,h);

      fread(&Vehicle::curcarid,sizeof(long),1,h);
      fread(&curcreatureid,sizeof(long),1,h);
      fread(&cursquadid,sizeof(long),1,h);
      fread(&police_heat,sizeof(int),1,h);
      fread(&offended_corps,sizeof(short),1,h);
      fread(&offended_cia,sizeof(short),1,h);
      fread(&offended_amradio,sizeof(short),1,h);
      fread(&offended_cablenews,sizeof(short),1,h);
      fread(&offended_firemen,sizeof(short),1,h);
      fread(attorneyseed,sizeof(unsigned long),RNG_SIZE,h);
      //fread(&selectedsiege,sizeof(long),1,h);
      fread(lcityname,sizeof(char),CITY_NAMELEN,h);
      fread(&newscherrybusted,sizeof(char),1,h);

      fread(slogan,sizeof(char),SLOGAN_LEN,h);
      fread(&ledger,sizeof(class Ledger),1,h);
      fread(&party_status,sizeof(short),1,h);

      fread(attitude,sizeof(short),VIEWNUM,h);
      fread(law,sizeof(short),LAWNUM,h);
      fread(house,sizeof(short),HOUSENUM,h);
      fread(senate,sizeof(short),SENATENUM,h);
      fread(court,sizeof(short),COURTNUM,h);
      fread(courtname,sizeof(char)*POLITICIAN_NAMELEN,COURTNUM,h);
      fread(exec,sizeof(char),EXECNUM,h);
      fread(execname,sizeof(char)*POLITICIAN_NAMELEN,EXECNUM,h);
      fread(oldPresidentName,sizeof(char),POLITICIAN_NAMELEN,h);

      //LOCATIONS
      fread(&dummy,sizeof(int),1,h);
      location.resize(dummy);
      for(l=0;l<len(location);l++)
      {
         location[l]=new Location;

         fread(&dummy,sizeof(int),1,h);
         location[l]->loot.resize(dummy);
         for(int l2=0;l2<len(location[l]->loot);l2++)
         {
            int itemLen;
            fread(&itemLen, sizeof(int), 1, h);
            vector<char> vec = vector<char>(itemLen + 1);
            fread(&vec[0], itemLen, 1, h);
            vec[itemLen] = '\0';

            Item* it = create_item(&vec[0]);
            if(it!=NULL)
               location[l]->loot[l2] = it;
         }
         //Remove items of unknown type.
         for(int l2=len(location[l]->loot)-1; l2>=0; l2--)
         {
            bool del = false;
            if(location[l]->loot[l2]->is_loot())
               del = (getloottype(location[l]->loot[l2]->get_itemtypename()) == -1);
            else if(location[l]->loot[l2]->is_clip())
               del = (getcliptype(location[l]->loot[l2]->get_itemtypename()) == -1);
            else if(location[l]->loot[l2]->is_weapon())
               del = (getweapontype(location[l]->loot[l2]->get_itemtypename()) == -1);
            else if(location[l]->loot[l2]->is_armor())
               del = (getarmortype(location[l]->loot[l2]->get_itemtypename()) == -1);

            if(del)
            {
               addstr("Item type ");
               addstr(location[l]->loot[l2]->get_itemtypename());
               addstr(" does not exist. Deleting item.");
               delete_and_remove(location[l]->loot,l2);
            }
         }
         consolidateloot(location[l]->loot); // consolidate loot after loading

         fread(&dummy,sizeof(int),1,h);
         location[l]->changes.resize(dummy);
         for(int l2=0;l2<len(location[l]->changes);l2++)
            fread(&location[l]->changes[l2],sizeof(sitechangest),1,h);

         fread(location[l]->name,sizeof(char),LOCATION_NAMELEN,h);
         fread(location[l]->shortname,sizeof(char),LOCATION_SHORTNAMELEN,h);
         fread(&location[l]->type,sizeof(char),1,h);
         fread(&location[l]->city,sizeof(int),1,h);
         fread(&location[l]->area,sizeof(int),1,h);
         fread(&location[l]->parent,sizeof(int),1,h);
         fread(&location[l]->id,sizeof(int),1,h);

         fread(&location[l]->renting,sizeof(int),1,h);
         fread(&location[l]->newrental,sizeof(char),1,h);
         fread(&location[l]->needcar,sizeof(char),1,h);
         fread(&location[l]->closed,sizeof(int),1,h);
         fread(&location[l]->hidden,sizeof(bool),1,h);
         fread(&location[l]->mapped,sizeof(bool),1,h);
         fread(&location[l]->upgradable,sizeof(bool),1,h);
         fread(&location[l]->highsecurity,sizeof(int),1,h);
         fread(&location[l]->siege,sizeof(siegest),1,h);
         fread(&location[l]->heat,sizeof(int),1,h);
         fread(&location[l]->heat_protection,sizeof(int),1,h);
         fread(&location[l]->compound_walls,sizeof(int),1,h);
         fread(&location[l]->compound_stores,sizeof(int),1,h);
         fread(&location[l]->front_business,sizeof(char),1,h);
         fread(location[l]->front_name,sizeof(char),LOCATION_NAMELEN,h);
         fread(location[l]->front_shortname,sizeof(char),LOCATION_SHORTNAMELEN,h);
         fread(&location[l]->haveflag,sizeof(bool),1,h);

         fread(location[l]->mapseed,sizeof(unsigned long),RNG_SIZE,h);
      }

      //VEHICLES
      fread(&dummy,sizeof(int),1,h);
      vehicle.resize(dummy);
      for(l=0;l<len(vehicle);l++)
      {
         int vehicleLen;
         fread (&vehicleLen, sizeof(int), 1, h);
         vector<char> vec = vector<char> (vehicleLen + 1);
         fread (&vec[0], vehicleLen, 1, h);
         vec[vehicleLen] = '\0';
         vehicle[l] = new Vehicle (&vec[0]);
      }

      //POOL
      fread(&dummy,sizeof(int),1,h);
      pool.resize(dummy);
      for(int pl=0;pl<len(pool);pl++)
      {
         int creatureLen;
         fread (&creatureLen, sizeof(int), 1, h);
         vector<char> vec = vector<char> (creatureLen + 1);
         fread (&vec[0], creatureLen, 1, h);
         vec[creatureLen] = '\0';
         pool[pl] = new Creature(&vec[0]);
         //pool[pl]=new Creature;
         //fread(pool[pl],sizeof(Creature),1,h);
         //read extra interrogation data if applicable
         if(pool[pl]->align==-1 && pool[pl]->alive)
         {
            interrogation* &intr = pool[pl]->activity.intr();
            intr = new interrogation;
            fread(intr->techniques,sizeof(bool[6]),1,h);
            fread(&intr->druguse,sizeof(int),1,h);

            intr->rapport.clear();
            int size;
            fread(&size,sizeof(int),1,h);
            for(int i=0;i<size;i++)
            {
               long id;
               float_zero value;
               fread(&id,sizeof(long),1,h);
               fread(&value,sizeof(float_zero),1,h);
               intr->rapport[id]=value;
            }
         }
         /*
         //read equipment
         vector<Item*> dump; //Used to catch invalid pointers from creature so they aren't deleted.
         pool[pl]->drop_weapon(&dump);
         pool[pl]->strip(&dump);
         pool[pl]->clips = deque<Clip*>();
         pool[pl]->extra_throwing_weapons = deque<Weapon*>();
         int itemLen;
         fread(&itemLen, sizeof(int), 1, h);
         if(itemLen != 0)
         {
            vector<char> vec = vector<char>(itemLen + 1);
            fread(&vec[0], itemLen, 1, h);
            vec[itemLen] = '\0';

            Weapon w(&vec[0]);
            if(getweapontype(w.get_itemtypename())!=-1) //Check it is a valid weapon type.
               pool[pl]->give_weapon(w,&dump);
         }
         //pool[pl]->clips.clear();
         fread(&dummy,sizeof(int),1,h);
         for(int nc=0; nc<dummy; nc++)
         {
            fread(&itemLen, sizeof(itemLen), 1, h);
            vector<char> vec = vector<char>(itemLen + 1);
            fread(&vec[0], itemLen, 1, h);
            vec[itemLen] = '\0';

            Clip c(&vec[0]);
            if(getcliptype(c.get_itemtypename())!=-1) //Check it is a valid clip type.
               pool[pl]->take_clips(c,len(c));
         }
         //pool[pl]->extra_throwing_weapons.clear();
         fread(&dummy,sizeof(int),1,h);
         for(int ne=0; ne<dummy; ne++)
         {
            fread(&itemLen, sizeof(itemLen), 1, h);
            vector<char> vec = vector<char>(itemLen + 1);
            fread(&vec[0], itemLen, 1, h);
            vec[itemLen] = '\0';

            Weapon w(&vec[0]);
            if(getweapontype(w.get_itemtypename())!=-1) //Check it is a valid weapon type.
               pool[pl]->give_weapon(w,NULL);
         }
         fread(&itemLen, sizeof(itemLen), 1, h);
         if(itemLen != 0)
         {
            vector<char> vec = vector<char>(itemLen + 1);
            fread(&vec[0], itemLen, 1, h);
            vec[itemLen] = '\0';

            Armor a(&vec[0]);
            if(getarmortype(a.get_itemtypename())!=-1) //Check it is a valid armor type.
               pool[pl]->give_armor(a,&dump);
         }*/
      }

      //Unique Creatures
      {
         int uniquecreaturesLen;
         fread (&uniquecreaturesLen, sizeof(int), 1, h);
         vector<char> vec = vector<char> (uniquecreaturesLen + 1);
         fread (&vec[0], uniquecreaturesLen, 1, h);
         vec[uniquecreaturesLen] = '\0';
         uniqueCreatures = UniqueCreatures(&vec[0]);
         //fread(&uniqueCreatures,sizeof(UniqueCreatures),1,h);
      }

      //SQUADS
      fread(&dummy,sizeof(int),1,h);
      squad.resize(dummy);
      for(int sq=0;sq<len(squad);sq++)
      {
         squad[sq]=new squadst;

         fread(squad[sq]->name,sizeof(char),SQUAD_NAMELEN,h);
         fread(&squad[sq]->activity,sizeof(activityst),1,h);
         fread(&squad[sq]->id,sizeof(int),1,h);

         for(int pos=0;pos<6;pos++)
         {
            //REBUILD SQUAD FROM POOL
            squad[sq]->squad[pos]=NULL;
            fread(&dummy_b,sizeof(bool),1,h);
            if(dummy_b)
            {
               int dummy_i;
               fread(&dummy_i,sizeof(int),1,h);
               for(int pl=0;pl<len(pool);pl++)
                  if(pool[pl]->id==dummy_i)
                     squad[sq]->squad[pos]=pool[pl];
            }
         }

         fread(&dummy,sizeof(int),1,h);
         squad[sq]->loot.resize(dummy);
         for(int l2=0;l2<len(squad[sq]->loot);l2++)
         {
            int itemLen;
            fread(&itemLen, sizeof(int), 1, h);
            vector<char> vec = vector<char>(itemLen + 1);
            fread(&vec[0], itemLen, 1, h);
            vec[itemLen] = '\0';

            Item* it = create_item(&vec[0]);
            //if(it!=NULL) //Assume save file is correct? -XML
               squad[sq]->loot[l2] = it;
            /*else
               squad[sq]->loot.erase(loot.begin()+l2--);*/
         }
         //Remove items of unknown type.
         for(int l2=len(squad[sq]->loot)-1; l2>=0; l2--)
         {
            bool del = false;
            if(squad[sq]->loot[l2]->is_loot())
               del = (getloottype(squad[sq]->loot[l2]->get_itemtypename()) == -1);
            else if(squad[sq]->loot[l2]->is_clip())
               del = (getcliptype(squad[sq]->loot[l2]->get_itemtypename()) == -1);
            else if(squad[sq]->loot[l2]->is_weapon())
               del = (getweapontype(squad[sq]->loot[l2]->get_itemtypename()) == -1);
            else if(squad[sq]->loot[l2]->is_armor())
               del = (getarmortype(squad[sq]->loot[l2]->get_itemtypename()) == -1);

            if(del)
            {
               addstr("Item type ");
               addstr(squad[sq]->loot[l2]->get_itemtypename());
               addstr(" does not exist. Deleting item.");
               delete_and_remove(squad[sq]->loot,l2);
            }
         }
         consolidateloot(squad[sq]->loot); // consolidate loot after loading
      }

      activesquad=NULL;
      fread(&dummy_b,sizeof(bool),1,h);
      if(dummy_b)
      {
         int dummy_i;
         fread(&dummy_i,sizeof(int),1,h);
         for(int sq=0;sq<len(squad);sq++)
            if(squad[sq]->id==dummy_i)
            {
               activesquad=squad[sq];
               break;
            }
      }

      //DATES
      fread(&dummy,sizeof(int),1,h);
      date.resize(dummy);
      for(int dt=0;dt<len(date);dt++)
      {
         date[dt]=new datest;

         fread(&date[dt]->mac_id,sizeof(long),1,h);
         fread(&date[dt]->timeleft,sizeof(short),1,h);
         fread(&date[dt]->city,sizeof(int),1,h);

         fread(&dummy,sizeof(int),1,h);
         date[dt]->date.resize(dummy);
         for(int dt2=0;dt2<len(date[dt]->date);dt2++)
         {
            int creatureLen;
            fread (&creatureLen, sizeof(int), 1, h);
            vector<char> vec = vector<char> (creatureLen + 1);
            fread (&vec[0], creatureLen, 1, h);
            vec[creatureLen] = '\0';
            date[dt]->date[dt2] = new Creature(&vec[0]);

            //date[dt]->date[dt2]=new Creature;
            //fread(date[dt]->date[dt2],sizeof(Creature),1,h);
         }
      }

      //RECRUITS
      fread(&dummy,sizeof(int),1,h);
      recruit.resize(dummy);
      for(int rt=0;rt<len(recruit);rt++)
      {
         recruit[rt]=new recruitst;
         fread(&recruit[rt]->recruiter_id,sizeof(long),1,h);
         fread(&recruit[rt]->timeleft,sizeof(short),1,h);
         fread(&recruit[rt]->level,sizeof(char),1,h);
         fread(&recruit[rt]->eagerness1,sizeof(char),1,h);
         fread(&recruit[rt]->task,sizeof(char),1,h);

         int creatureLen;
         fread (&creatureLen, sizeof(int), 1, h);
         vector<char> vec = vector<char> (creatureLen + 1);
         fread (&vec[0], creatureLen, 1, h);
         vec[creatureLen] = '\0';
         recruit[rt]->recruit = new Creature(&vec[0]);
         //recruit[rt]->recruit = new Creature;
         //fread(recruit[rt]->recruit,sizeof(Creature),1,h);
      }

      //NEWS STORIES
      fread(&dummy,sizeof(int),1,h);
      newsstory.resize(dummy);
      for(int ns=0;ns<len(newsstory);ns++)
      {
         newsstory[ns]=new newsstoryst;

         fread(&newsstory[ns]->type,sizeof(short),1,h);
         fread(&newsstory[ns]->view,sizeof(short),1,h);

         fread(&newsstory[ns]->loc,sizeof(long),1,h);
         fread(&newsstory[ns]->priority,sizeof(long),1,h);
         fread(&newsstory[ns]->page,sizeof(long),1,h);
         fread(&newsstory[ns]->positive,sizeof(char),1,h);
         fread(&newsstory[ns]->siegetype,sizeof(short),1,h);

         newsstory[ns]->cr=NULL;
         fread(&dummy_b,sizeof(bool),1,h);
         if(dummy_b)
         {
            fread(&dummy_l,sizeof(long),1,h);
            for(int pl=0;pl<len(pool);pl++)
               if(pool[pl]->id==dummy_l)
               {
                  newsstory[ns]->cr=pool[pl];
                  break;
               }
         }

         fread(&dummy,sizeof(int),1,h);
         newsstory[ns]->crime.resize(dummy);
         for(int dt2=0;dt2<len(newsstory[ns]->crime);dt2++)
            fread(&newsstory[ns]->crime[dt2],sizeof(int),1,h);
      }

      // Liberal Media
      fread(public_interest,sizeof(public_interest),1,h);
      fread(background_liberal_influence,sizeof(background_liberal_influence),1,h);

      // Site mode options
      fread(&encounterwarnings,sizeof(bool),1,h);
      bool musicenabled;
      fread(&musicenabled,sizeof(bool),1,h);
      music.enableIf(musicenabled);

      LCSCloseFile(h);

      // Check that vehicles are of existing types.
      for(int v=0;v<len(vehicle);v++)
      {
         if(getvehicletype(vehicle[v]->vtypeidname())==-1)
         { //Remove vehicle of non-existing type.
            addstr("Vehicle type "+vehicle[v]->vtypeidname()+" does not exist. Deleting vehicle.");
            delete_and_remove(vehicle,v--);
         }
      }

      return 1;
   }

   return 0;
}
Beispiel #10
0
/* handles saving */
void savegame(const char *str)
{
#ifdef NOSAVE
   return;
#endif

   bool dummy_b;
   int dummy;
   FILE *h;
   int l;

   h=LCSOpenFile(str, "wb", LCSIO_PRE_HOME);

   if(h!=NULL)
   {
      int lversion=version;
      fwrite(&lversion,sizeof(int),1,h);

      fwrite(seed,sizeof(unsigned long),RNG_SIZE,h);

      fwrite(&mode,sizeof(short),1,h);
      fwrite(&wincondition,sizeof(short),1,h);
      fwrite(&fieldskillrate,sizeof(short),1,h);

      fwrite(&day,sizeof(int),1,h);
      fwrite(&month,sizeof(int),1,h);
      fwrite(&year,sizeof(int),1,h);
      fwrite(&execterm,sizeof(short),1,h);
      fwrite(&presparty,sizeof(short),1,h);
      fwrite(&amendnum,sizeof(int),1,h);

      fwrite(&multipleCityMode,sizeof(bool),1,h);
      fwrite(&termlimits,sizeof(bool),1,h);
      fwrite(&deagle,sizeof(bool),1,h);
      fwrite(&m249,sizeof(bool),1,h);
      fwrite(&notermlimit,sizeof(bool),1,h);
      fwrite(&nocourtpurge,sizeof(bool),1,h);
      fwrite(&stalinmode,sizeof(bool),1,h);

      fwrite(&stat_recruits,sizeof(int),1,h);
      fwrite(&stat_dead,sizeof(int),1,h);
      fwrite(&stat_kills,sizeof(int),1,h);
      fwrite(&stat_kidnappings,sizeof(int),1,h);
      fwrite(&stat_buys,sizeof(int),1,h);
      fwrite(&stat_burns,sizeof(int),1,h);

      fwrite(&endgamestate,sizeof(char),1,h);
      fwrite(&ccsexposure,sizeof(char),1,h);
      fwrite(&ccs_kills,sizeof(char),1,h);

      fwrite(&Vehicle::curcarid,sizeof(long),1,h);
      fwrite(&curcreatureid,sizeof(long),1,h);
      fwrite(&cursquadid,sizeof(long),1,h);
      fwrite(&police_heat,sizeof(int),1,h);
      fwrite(&offended_corps,sizeof(short),1,h);
      fwrite(&offended_cia,sizeof(short),1,h);
      fwrite(&offended_amradio,sizeof(short),1,h);
      fwrite(&offended_cablenews,sizeof(short),1,h);
      fwrite(&offended_firemen,sizeof(short),1,h);
      fwrite(attorneyseed,sizeof(unsigned long),RNG_SIZE,h);
      //fwrite(&selectedsiege,sizeof(long),1,h);
      fwrite(lcityname,sizeof(char),CITY_NAMELEN,h);
      fwrite(&newscherrybusted,sizeof(char),1,h);

      fwrite(slogan,sizeof(char),SLOGAN_LEN,h);
      fwrite(&ledger,sizeof(class Ledger),1,h);
      fwrite(&party_status,sizeof(short),1,h);

      fwrite(attitude,sizeof(short),VIEWNUM,h);
      fwrite(law,sizeof(short),LAWNUM,h);
      fwrite(house,sizeof(short),HOUSENUM,h);
      fwrite(senate,sizeof(short),SENATENUM,h);
      fwrite(court,sizeof(short),COURTNUM,h);
      fwrite(courtname,sizeof(char)*POLITICIAN_NAMELEN,9,h);
      fwrite(exec,sizeof(char),EXECNUM,h);
      fwrite(execname,sizeof(char)*POLITICIAN_NAMELEN,EXECNUM,h);
      fwrite(oldPresidentName,sizeof(char),POLITICIAN_NAMELEN,h);

      //LOCATIONS
      dummy=len(location);
      fwrite(&dummy,sizeof(int),1,h);
      for(l=0;l<len(location);l++)
      {
         consolidateloot(location[l]->loot); // consolidate loot before saving
         dummy=len(location[l]->loot);
         fwrite(&dummy,sizeof(int),1,h);
         for(int l2=0;l2<len(location[l]->loot);l2++)
         {
            std::string itemStr = location[l]->loot[l2]->showXml();
            int itemSize = len(itemStr);

            fwrite(&itemSize,sizeof(int),1,h);
            fwrite(itemStr.c_str(),itemSize,1,h);
         }
         dummy=len(location[l]->changes);
         fwrite(&dummy,sizeof(int),1,h);
         for(int l2=0;l2<len(location[l]->changes);l2++)
            fwrite(&location[l]->changes[l2],sizeof(sitechangest),1,h);

         fwrite(location[l]->name,sizeof(char),LOCATION_NAMELEN,h);
         fwrite(location[l]->shortname,sizeof(char),LOCATION_SHORTNAMELEN,h);
         fwrite(&location[l]->type,sizeof(char),1,h);
         fwrite(&location[l]->city,sizeof(int),1,h);
         fwrite(&location[l]->area,sizeof(int),1,h);
         fwrite(&location[l]->parent,sizeof(int),1,h);
         fwrite(&location[l]->id,sizeof(int),1,h);

         fwrite(&location[l]->renting,sizeof(int),1,h);
         fwrite(&location[l]->newrental,sizeof(char),1,h);
         fwrite(&location[l]->needcar,sizeof(char),1,h);
         fwrite(&location[l]->closed,sizeof(int),1,h);
         fwrite(&location[l]->hidden,sizeof(bool),1,h);
         fwrite(&location[l]->mapped,sizeof(bool),1,h);
         fwrite(&location[l]->upgradable,sizeof(bool),1,h);
         fwrite(&location[l]->highsecurity,sizeof(int),1,h);
         fwrite(&location[l]->siege,sizeof(siegest),1,h);
         fwrite(&location[l]->heat,sizeof(int),1,h);
         fwrite(&location[l]->heat_protection,sizeof(int),1,h);
         fwrite(&location[l]->compound_walls,sizeof(int),1,h);
         fwrite(&location[l]->compound_stores,sizeof(int),1,h);
         fwrite(&location[l]->front_business,sizeof(char),1,h);
         fwrite(location[l]->front_name,sizeof(char),LOCATION_NAMELEN,h);
         fwrite(location[l]->front_shortname,sizeof(char),LOCATION_SHORTNAMELEN,h);
         fwrite(&location[l]->haveflag,sizeof(bool),1,h);

         fwrite(location[l]->mapseed,sizeof(unsigned long),RNG_SIZE,h);
      }

      //VEHICLES
      dummy=len(vehicle);
      fwrite(&dummy,sizeof(int),1,h);
      for(l=0;l<len(vehicle);l++)
      {
         std::string vehicleStr = vehicle[l]->showXml();
         int vehicleSize = len(vehicleStr);

         fwrite(&vehicleSize,sizeof(int),1,h);
         fwrite(vehicleStr.c_str(),vehicleSize,1,h);
      }

      //POOL
      dummy=len(pool);
      fwrite(&dummy,sizeof(int),1,h);
      for(int pl=0;pl<len(pool);pl++)
      {
         std::string creatureStr = pool[pl]->showXml();
         int creatureSize = len(creatureStr);

         fwrite(&creatureSize,sizeof(int),1,h);
         fwrite(creatureStr.c_str(),creatureSize,1,h);
         //fwrite(pool[pl],sizeof(Creature),1,h);
         //write extra interrogation data if applicable
         if(pool[pl]->align==-1 && pool[pl]->alive)
         {
            interrogation* &intr = pool[pl]->activity.intr();
            fwrite(intr->techniques,sizeof(bool[6]),1,h);
            fwrite(&intr->druguse,sizeof(int),1,h);

            //deep write rapport map
            int size = len(intr->rapport);
            fwrite(&size,sizeof(int),1,h);
            for(map<long,float_zero>::iterator i=intr->rapport.begin();i!=intr->rapport.end();i++)
            {
               fwrite(&i->first,sizeof(long),1,h);
               fwrite(&i->second,sizeof(float_zero),1,h);
            }
         }
      }

      //Unique Creatures
      {
         std::string uniquecreaturesStr = uniqueCreatures.showXml();
         int uniquecreaturesSize = len(uniquecreaturesStr);

         fwrite(&uniquecreaturesSize,sizeof(int),1,h);
         fwrite(uniquecreaturesStr.c_str(),uniquecreaturesSize,1,h);
         //fwrite(&uniqueCreatures,sizeof(UniqueCreatures),1,h);
      }

      //SQUADS
      dummy=len(squad);
      fwrite(&dummy,sizeof(int),1,h);
      for(int sq=0;sq<len(squad);sq++)
      {
         fwrite(squad[sq]->name,sizeof(char),SQUAD_NAMELEN,h);
         fwrite(&squad[sq]->activity,sizeof(activityst),1,h);
         fwrite(&squad[sq]->id,sizeof(int),1,h);

         for(int pos=0;pos<6;pos++)
         {
            dummy_b=squad[sq]->squad[pos];
            fwrite(&dummy_b,sizeof(bool),1,h);
            if(dummy_b)
               fwrite(&squad[sq]->squad[pos]->id,sizeof(int),1,h);
         }

         consolidateloot(squad[sq]->loot); // consolidate loot before saving
         dummy=len(squad[sq]->loot);
         fwrite(&dummy,sizeof(int),1,h);
         for(int l2=0;l2<len(squad[sq]->loot);l2++)
         {
            std::string itemStr = squad[sq]->loot[l2]->showXml();
            int itemSize = len(itemStr);

            fwrite(&itemSize,sizeof(int),1,h);
            fwrite(itemStr.c_str(),itemSize,1,h);
         }
      }

      dummy_b=activesquad;
      fwrite(&dummy_b,sizeof(bool),1,h);
      if(dummy_b)
         fwrite(&activesquad->id,sizeof(int),1,h);

      //DATES
      dummy=len(date);
      fwrite(&dummy,sizeof(int),1,h);
      for(int dt=0;dt<len(date);dt++)
      {
         fwrite(&date[dt]->mac_id,sizeof(long),1,h);
         fwrite(&date[dt]->timeleft,sizeof(short),1,h);
         fwrite(&date[dt]->city,sizeof(int),1,h);

         dummy=len(date[dt]->date);
         fwrite(&dummy,sizeof(int),1,h);
         for(int dt2=0;dt2<len(date[dt]->date);dt2++)
         {
            std::string creatureStr = date[dt]->date[dt2]->showXml();
            int creatureSize = len(creatureStr);

            fwrite(&creatureSize,sizeof(int),1,h);
            fwrite(creatureStr.c_str(),creatureSize,1,h);
            //fwrite(date[dt]->date[dt2],sizeof(Creature),1,h);
         }
      }

      //RECRUITS
      dummy=len(recruit);
      fwrite(&dummy,sizeof(int),1,h);
      for(int rt=0;rt<len(recruit);rt++)
      {
         fwrite(&recruit[rt]->recruiter_id,sizeof(long),1,h);
         fwrite(&recruit[rt]->timeleft,sizeof(short),1,h);
         fwrite(&recruit[rt]->level,sizeof(char),1,h);
         fwrite(&recruit[rt]->eagerness1,sizeof(char),1,h);
         fwrite(&recruit[rt]->task,sizeof(char),1,h);

         std::string creatureStr = recruit[rt]->recruit->showXml();
         int creatureSize = len(creatureStr);

         fwrite(&creatureSize,sizeof(int),1,h);
         fwrite(creatureStr.c_str(),creatureSize,1,h);
         //fwrite(recruit[rt]->recruit,sizeof(Creature),1,h);
      }

      //NEWS STORIES
      dummy=len(newsstory);
      fwrite(&dummy,sizeof(int),1,h);
      for(int ns=0;ns<len(newsstory);ns++)
      {
         fwrite(&newsstory[ns]->type,sizeof(short),1,h);
         fwrite(&newsstory[ns]->view,sizeof(short),1,h);

         fwrite(&newsstory[ns]->loc,sizeof(long),1,h);
         fwrite(&newsstory[ns]->priority,sizeof(long),1,h);
         fwrite(&newsstory[ns]->page,sizeof(long),1,h);
         fwrite(&newsstory[ns]->positive,sizeof(char),1,h);
         fwrite(&newsstory[ns]->siegetype,sizeof(short),1,h);

         dummy_b=newsstory[ns]->cr;
         fwrite(&dummy_b,sizeof(bool),1,h);
         if(dummy_b)
            fwrite(&newsstory[ns]->cr->id,sizeof(long),1,h);

         dummy=len(newsstory[ns]->crime);
         fwrite(&dummy,sizeof(int),1,h);
         for(int dt2=0;dt2<len(newsstory[ns]->crime);dt2++)
            fwrite(&newsstory[ns]->crime[dt2],sizeof(int),1,h);
      }

      // Liberal Media
      fwrite(public_interest,sizeof(public_interest),1,h);
      fwrite(background_liberal_influence,sizeof(background_liberal_influence),1,h);

      // Site mode options
      fwrite(&encounterwarnings,sizeof(bool),1,h);
      bool musicenabled=music.isEnabled();
      fwrite(&musicenabled,sizeof(bool),1,h);

      LCSCloseFile(h);
   }
}
void System::print_ent(std::string filename,boost::python::list atoms_list,int fold,std::string pbc_type){
/** 
  \param[in] filename The name of the file where the info will be trinted out
  \param[in] atoms_list The list of the atom IDs for the atoms to be printed out 
  \param[in] fold Controlls the folding of the coordinates into the unit-cell 
  if fold==1 - will output coordinates folded into simulation box
  \param[in] pbc_type The parameter controlling the periodicity (when and if folding) of the unit cell
  Can take values: "a", "b", "c", "ab", "ac", "bc", and "abc"

  Print the state of the system into file in Brookhaven PDB format (ENT). Write only specified atoms which IDs are in the atomlist
*/

  FILE* fp;
  fp = fopen(filename.c_str(),"w");

// Crystal structure information, CRYST1 record see:
//http://deposit.rcsb.org/adit/docs/pdb_atom_format.html
  if(is_Box){
    VECTOR tv1,tv2,tv3;
    Box.get_vectors(tv1,tv2,tv3);
    double a,b,c,alp,bet,gam;
    a = tv1.length();
    b = tv2.length();
    c = tv3.length();
    tv1 = tv1/a;
    tv2 = tv2/b;
    tv3 = tv3/c;
    alp = acos(tv2*tv3)*radians_to_degrees;
    bet = acos(tv3*tv1)*radians_to_degrees;
    gam = acos(tv1*tv2)*radians_to_degrees;
    fprintf(fp,"CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f    \n",a * bohr, b * bohr, c * bohr, alp,bet,gam); // convert from internal units
     // (Bohrs) to Angstroms

  }


  vector<int> at_indexes;
  int sz = len(atoms_list);
  for(int i=0;i<sz;i++){
    int id = extract<int>(atoms_list[i]);
    int j = get_atom_index_by_atom_id(id);
    if(j!=-1){ at_indexes.push_back(j); }
  }

  sz = at_indexes.size();
  for(i=0;i<sz;i++){
    int indx = at_indexes[i];
    int at_ser_num = 1;
    std::string at_name = "H";
    int grp_num;
    VECTOR r;
    double occup = 1.0;
    double b_fact = 0.0;

    if(Atoms[indx].is_Atom_id){at_ser_num = Atoms[indx].Atom_id;}
    if(Atoms[indx].is_Atom_element){at_name = Atoms[indx].Atom_element;}
    grp_num = Atoms[indx].globGroup_Index;
    if(Atoms[indx].is_Atom_RB){  if(Atoms[indx].Atom_RB.is_rb_cm){   r = Atoms[indx].Atom_RB.rb_cm;  }  }
    if(Atoms[indx].is_Atom_charge){ b_fact = Atoms[indx].Atom_charge; }

    if(fold && is_Box){
      MATRIX3x3 invBox; invBox = Box.inverse();
      VECTOR borig; borig = 0.0; // box origin

      r = invBox*(r - borig);

      if((pbc_type=="a")||(pbc_type=="ab")||(pbc_type=="ac")||(pbc_type=="abc")) {
        r.x = r.x - floor(r.x);
      }
      if((pbc_type=="b")||(pbc_type=="ab")||(pbc_type=="bc")||(pbc_type=="abc")) {
        r.y = r.y - floor(r.y);
      }
      if((pbc_type=="c")||(pbc_type=="ac")||(pbc_type=="bc")||(pbc_type=="abc")) {
        r.z = r.z - floor(r.z);
      }
      r = Box * r + borig;
    }// fold && is_Box

    r *= bohr; // convert from internal units (a.u.) to conventional units (Angstrom)

    fprintf(fp,"HETATM%5i %4s MOL C%4i    %8.3f%8.3f%8.3f%6.2f%6.2f    \n",at_ser_num,at_name.c_str(),grp_num,r.x,r.y,r.z,occup,b_fact);
  }
  fclose(fp);
}
String InlineTextBox::text() const
{
    return getLineLayoutItem().text().substring(start(), len());
}
bool InlineTextBox::isLineBreak() const
{
    return getLineLayoutItem().isBR() || (getLineLayoutItem().style()->preserveNewline() && len() == 1 && (*getLineLayoutItem().text().impl())[start()] == '\n');
}