void MakeAnimals() { // Use the Animal default constructor. unique_ptr<Animal> p1 = make_unique<Animal>(); // Use the constructor that matches these arguments auto p2 = make_unique<Animal>(L"Felis", L"Catus", 12, 16.5); // Create a unique_ptr to an array of 5 Animals unique_ptr<Animal[]> p3 = make_unique<Animal[]>(5); // Initialize the elements p3[0] = Animal(L"Rattus", L"norvegicus", 3, 2.1); p3[1] = Animal(L"Corynorhinus", L"townsendii", 4, 1.08); // auto p4 = p2; //C2280 vector<unique_ptr<Animal>> vec; // vec.push_back(p2); //C2280 // vector<unique_ptr<Animal>> vec2 = vec; // C2280 // OK. p2 no longer points to anything vec.push_back(std::move(p2)); // unique_ptr overloads operator bool wcout << boolalpha << (p2 == false) << endl; // Prints "true" // OK but now you have two pointers to the same memory location Animal* pAnimal = p2.get(); // OK. p2 no longer points to anything Animal* p5 = p2.release(); }
//![0] int main(int argc, char ** argv) { QApplication app(argc, argv); AnimalModel model; model.addAnimal(Animal("Wolf", "Medium")); model.addAnimal(Animal("Polar bear", "Large")); model.addAnimal(Animal("Quoll", "Small")); QDeclarativeView view; QDeclarativeContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", &model); //![0] view.setSource(QUrl("qrc:view.qml")); view.show(); return app.exec(); }
//![0] int main(int argc, char ** argv) { QApplication app(argc, argv); QmlApplicationViewer viewer; AnimalModel model; model.addAnimal(Animal("Wolf", "Medium")); model.addAnimal(Animal("Polar bear", "Large")); model.addAnimal(Animal("Quoll", "Small")); QDeclarativeContext *ctxt = viewer.rootContext(); ctxt->setContextProperty("myModel", &model); //![0] viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape); viewer.setMainQmlFile(QLatin1String("qml/abstractitemmodel/view.qml")); viewer.showExpanded(); return app.exec(); }
int main() { Animal a1("Monkey", 3); Animal a2("Bear" , 8); Animal a3("Turtle", 56); Animal a4("Monkey", 200); std::set<Animal> s; s.insert(a1); s.insert(a2); s.insert(a3); s.insert(a4); std::cout << "Number of animals: " << s.size() << std::endl; std::for_each(s.begin(), s.end(), boost::bind(&Animal::print, _1)); std::cout << std::endl; std::set<Animal>::iterator it(s.find(Animal("Monkey", 200))); if (it != s.end()) { std::cout << "We found the 200 year old monkey!" << std::endl; it->print(); } // Animal("Monkey", 3) as a match it = std::find(s.begin(), s.end(), Animal("Monkey", 200)); if (it == s.end()) { std::cout << "No 200 year old monkey could be find in this set." << std::endl; } }
/* Random initialization */ Population::Population(int size) { for (int i = 0; i < size; ++i) list.push_back(Animal()); }
bool Building::load_data(std::istream& data) { int tmptype; data >> tmptype; if (tmptype <= 0 || tmptype >= BUILD_MAX) { debugmsg("Building loaded type of %d (range is 1 to %d).", tmptype, BUILD_MAX - 1); return false; } type = Building_type(tmptype); data >> open >> pos.x >> pos.y >> construction_left >> workers; crops_grown.clear(); data >> field_output; int num_crops; data >> num_crops; for (int i = 0; i < num_crops; i++) { int tmpcrop, crop_amt; data >> tmpcrop >> crop_amt; if (tmpcrop <= 0 || tmpcrop >= CROP_MAX) { debugmsg("Building loaded crop %d/%d; type %d (range is 1 to %d).", i, num_crops, tmpcrop, CROP_MAX - 1); return false; } crops_grown.push_back( Crop_amount( Crop(tmpcrop), crop_amt ) ); } minerals_mined.clear(); data >> shaft_output; int num_mins; data >> num_mins; for (int i = 0; i < num_mins; i++) { int tmpmin, min_amt; data >> tmpmin >> min_amt; if (tmpmin <= 0 || tmpmin >= MINERAL_MAX) { debugmsg("Building loaded mineral %d/%d; type %d (range is 1 to %d).", i, num_mins, tmpmin, MINERAL_MAX - 1); return false; } minerals_mined.push_back( Mineral_amount( Mineral(tmpmin), min_amt ) ); } data >> hunter_level; int tmptarget, tmpaction; data >> tmptarget >> tmpaction; if (tmptarget >= ANIMAL_MAX) { debugmsg("Building loaded hunting target %d (range is 1 to %d).", tmptarget, ANIMAL_MAX - 1); return false; } hunting_target = Animal(tmptarget); if (tmpaction >= ANIMAL_ACT_MAX) { debugmsg("Building loaded hunting action %d (range is 1 to %d).", tmpaction, ANIMAL_ACT_MAX - 1); return false; } hunting_action = Animal_action(tmpaction); int queue_size; data >> queue_size; for (int i = 0; i < queue_size; i++) { Recipe tmp_recipe; if (!tmp_recipe.load_data(data)) { debugmsg("Building failed to load recipe %d/%d.", i, queue_size); return false; } int recipe_amt; data >> recipe_amt; build_queue.push_back( Recipe_amount( tmp_recipe, recipe_amt ) ); } return true; }
bool City::load_data(std::istream& data) { clear_data(); data >> uid; std::string tmpstr; while (tmpstr != "!") { data >> tmpstr; if (tmpstr != "!") { if (!name.empty()) { name = name + " "; } name = name + tmpstr; } } int tmptype, tmprace; data >> tmptype >> tmprace; if (tmptype >= CITY_TYPE_MAX || tmptype <= 0) { debugmsg("City '%s' has type value of %d (range is 1 - %d)!", name.c_str(), tmptype, CITY_TYPE_MAX - 1); return false; } if (tmprace >= RACE_MAX || tmptype <= 0) { debugmsg("City '%s' has race value of %d (range is 1 - %d)!", name.c_str(), tmprace, RACE_MAX - 1); return false; } type = City_type(tmptype); race = Race(tmprace); data >> location.x >> location.y; if (!map.load_data(data)) { debugmsg("City '%s' failed to load map.", name.c_str()); return false; } int num_routes; data >> num_routes; for (int i = 0; i < num_routes; i++) { int tmpuid; Trade_route tmproute; data >> tmpuid; if (!tmproute.load_data(data)) { debugmsg("City '%s' failed to load trade route to City %d.", name.c_str(), tmpuid); return false; } trade_routes[tmpuid] = tmproute; } for (int i = 0; i < CIT_MAX; i++) { if (!population[i].load_data(data)) { debugmsg("City '%s' failed to load %s data.", name.c_str(), citizen_type_name(Citizen_type(i), true).c_str()); return false; } } for (int i = 0; i < RES_MAX; i++) { data >> resources[i]; } for (int i = 0; i < MINERAL_MAX; i++) { data >> minerals[i]; } int num_animals; data >> num_animals; for (int i = 0; i < num_animals; i++) { int tmpanimal, tmpnum; data >> tmpanimal >> tmpnum; if (tmpanimal <= 0 || tmpanimal >= ANIMAL_MAX) { debugmsg("City '%s' had animal of index %d (Range is 1 to %d)", name.c_str(), tmpanimal, ANIMAL_MAX - 1); return false; } livestock[ Animal(tmpanimal) ] = tmpnum; } return true; }
int main(int argc,char **argv){ Warehouse *wh = new Warehouse(); Zoo *z = new Zoo(); int readFlag=0; int createFlag=0; if(2!=argc){ usage(); } while(argc){ if(!std::strcmp("-r",*argv)) readFlag=1; if(!std::strcmp("-c",*argv)) createFlag=1; argc--; argv++; } if(readFlag){ std::ifstream infile; std::ofstream outfile; outfile.open(WAREHOUSE_BAK,std::ios::out|std::ios::trunc); infile.open(WAREHOUSE_FILE,std::ios::in); // build up FoodItems from file if(infile){ std::string type; std::string units; std::string line; std::string holder; double quantity; while(true){ std::getline(infile,line); if( infile.eof() ){ break; } outfile << line << "\n"; std::istringstream iss(line); std::getline(iss,type,'|'); std::getline(iss,units,'|'); std::getline(iss,holder,'|'); std::istringstream bleck(holder); bleck >> quantity; if(iss.bad()){ std::cout << "error parsing file." << std::endl; exit(1); } FoodItem fi = FoodItem(type,units,quantity); wh->addToInv(fi); } infile.close(); outfile.close(); } else { std::cout << WAREHOUSE_FILE << "could not be opened" << std::endl; } outfile.open(ZOO_BAK,std::ios::out|std::ios::trunc); infile.open(ZOO_FILE,std::ios::in); // build up Animal objects from file if(infile){ std::string line; std::string name; std::string food; std::string type; std::string tmp; double intake; time_t lastFedTime; while(true){ std::getline(infile,line); if( infile.eof() ){ break; } outfile << line << "\n"; std::istringstream iss(line); std::getline(iss,name,'|'); std::getline(iss,food,'|'); std::getline(iss,type,'|'); std::getline(iss,tmp,'|'); std::istringstream blah(tmp); blah >> intake; std::getline(iss,tmp,'|'); std::istringstream blah2(tmp); blah2 >> lastFedTime; if(iss.bad()){ std::cout << "error parsing file." << std::endl; exit(1); } Animal a = Animal(name,food,type,intake,lastFedTime); z->addToHerd(a); } infile.close(); outfile.close(); } else {
Human::Human(string _name, double _energy, double _size, double _weight, Point2D _position, double _strength, State _state) { Animal(_name, _energy, _size, _weight, _position, _strength, _state); }
void AI_city::add_pastures(std::vector<Map_tile*>& tiles, int& food_req) { bool unlimited_food = (food_req == -1); int livestock_skill = Race_data[race]->skill_level[SKILL_LIVESTOCK]; /* First, figure out the three most food-producing animals. * We look at how much food is produced in 10,000 days; this makes calculating * the food we can get by slaughtering animals easier. It also makes our food * amounts work directly with food_req (since that's multiplied by 10,000). * If food_req is -1, then we're not just trying to produce food; other * resources are valuable too. So pick ANY livestock animals. */ std::vector<Animal> food_animals; std::vector<int> food_amounts; int min_food = 0; for (int i = 0; i < ANIMAL_MAX; i++) { Animal_datum* ani_dat = Animal_data[i]; // Only multiply food_livestock by 100 since it's measured per 100 animals int animal_food = ani_dat->food_livestock * 100; // Now, food from slaughtering animals (divided by how long it takes to birth 1) // We multiply by 10,000 to match our required food; then also multiply by 100 // since we have 100 animals (and thus need to divide reproduction_rate by 100) animal_food += (1000000 * ani_dat->food_killed) / ani_dat->reproduction_rate; // At livestock skill of 1, tameness must be >= 88; at skill of 5, >= 40 if (ani_dat->tameness + 12 * livestock_skill >= 100 && (unlimited_food || (animal_food >= min_food || food_animals.size() < 3))) { // Insert it in the proper place. bool inserted = false; for (int n = 0; !inserted && n < food_animals.size(); n++) { if (food_amounts[n] < animal_food) { inserted = true; food_animals.insert( food_animals.begin() + n, Animal(i) ); food_amounts.insert( food_amounts.begin() + n, animal_food ); } } if (!inserted) { // Didn't find a place to put it; push it to the end. food_animals.push_back( Animal(i) ); food_amounts.push_back( animal_food ); } } // Clip our vector to the proper size - unless we're using unlimited food if (!unlimited_food) { while (food_animals.size() > 3) { food_animals.pop_back(); food_amounts.pop_back(); min_food = food_amounts.back(); } } } // Since all tiles support pastures equally, we just randomly pick some from all // tiles that support pastures. std::vector<Map_tile*> pastures; std::vector<int> pasture_indices; // For deleting them from tiles for (int i = 0; i < tiles.size(); i++) { if (tiles[i]->can_build(AREA_PASTURE)) { pastures.push_back( tiles[i] ); pasture_indices.push_back( i ); } } while (!pastures.empty() && (unlimited_food || food_req > 0)) { int index = rng(0, pastures.size() - 1); int orig_index = pasture_indices[index]; pastures.erase( pastures.begin() + index ); tiles.erase( tiles.begin() + orig_index ); // Pick an animal! int animal_index = rng(0, food_animals.size() - 1); Animal new_livestock = food_animals[ animal_index ]; int food_amount = 100 * food_amounts[ animal_index ]; food_req -= food_amount; add_resource_production(RES_FOOD, food_amount / 10000); add_area(AREA_PASTURE); // Add resource production for anything else the animal may produce. Animal_datum* ani_dat = Animal_data[new_livestock]; for (int i = 0; i < ani_dat->resources_livestock.size(); i++) { Resource_amount res_amt = ani_dat->resources_livestock[i]; add_resource_production(res_amt); } for (int i = 0; i < ani_dat->resources_killed.size(); i++) { Resource_amount res_amt = ani_dat->resources_killed[i]; // Multiply by 100 since we have 100 animals birthing - effectively this // divides reproduction_rate by 100. res_amt.amount = (res_amt.amount * 100) / ani_dat->reproduction_rate; if (res_amt.amount == 0) { res_amt.amount = 1; } add_resource_production(res_amt); } if (livestock.count(new_livestock)) { livestock[new_livestock] += 100; } else { livestock[new_livestock] = 100; } } // while (!pastures.empty() && food_req > 0) }