Skip to content

Instantly share code, notes, and snippets.

@Chase-san
Last active August 29, 2015 13:56
Show Gist options
  • Save Chase-san/8808333 to your computer and use it in GitHub Desktop.
Save Chase-san/8808333 to your computer and use it in GitHub Desktop.
Cataclysm DDA Power System Rough
bool power_plant::has_power(uint64_t time) {
return shutdown_time > time;
}
void power_plant::powerup(uint64_t time) {
//set next shutdown time to 4-12 days in the future
shutdown_time = time + 24 * (4 + (rand() % 8));
for(int i = 0; i < buildings.size(); ++i) {
buildings[i].powerup();
}
}
void power_plant::shutdown() {
for(int i = 0; i < buildings.size(); ++i) {
buildings[i].shutdown();
}
}
void power_plant::update(uint64_t time) {
if(time >= shutdown_time) {
shutdown();
}
}
bool building::has_power(uint64_t time) {
if(plant == nullptr) //NULL if not using C++11
return false;
return plant->has_power();
}
void building::powerup() {
/* turn on lights and all appliances */
}
void building::shutdown() {
/* turn off lights and all appliances */
}
void find_power_plants(std::vector<power_plant*> &plants) {
/* find power plants (based on map tiles) */
}
void find_buildings(std::vector<building*> &buildings) {
/* find buildings (based on map tiles, not actual buildings) */
}
void recalculate_power_plants(uint64_t current_time) {
std::vector<power_plant> plants;
find_power_plants(plants);
std::vector<building> blds;
find_buildings(blds);
//clear plants buildings
for(int i = 0; i < plants.size(); ++i) {
plants[i]->buildings.clear();
}
while(buildings.size() > 0) {
building *b = blds.back();
blds.pop_back();
bool hadPower = b->has_power(current_time);
float bestDist = 1E+37; //some really large number
//find nearest power plant
for(int i = 0; i < plants.size(); ++i) {
float dist = dda_distance_func(plants[i]->tile.point,b->tile.point);
if(dist < bestDist) {
bestDist = dist;
b->plant = plants[i];
}
}
//after found, add building to list
b->plant->buildings.push_back(b);
if(hadPower != b->has_power(current_time)) {
if(b->has_power()) {
b->powerup();
} else {
b->shutdown();
}
}
}
}
class building;
class power_plant {
public:
map_tile *tile; //the plant's main tile
std::vector<building*> buildings;
uint64_t shutdown_time;
bool has_power(uint64_t);
void powerup(uint64_t);
void shutdown();
void update(uint64_t);
};
class building {
public:
map_tile *tile;
power_plant *plant;
bool has_power(uint64_t);
void powerup();
void shutdown();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment