autobutcher: now accepts list of races in one command

develop
Robert Heinrich 2012-04-07 05:05:42 +02:00
parent e8ccbb4b2c
commit c5fc8aab9f
1 changed files with 187 additions and 212 deletions

@ -172,12 +172,12 @@ const string autobutcher_help =
" sleep X - change timer to sleep X frames between runs.\n" " sleep X - change timer to sleep X frames between runs.\n"
" watch R - start watching race(s)\n" " watch R - start watching race(s)\n"
" R = valid race RAW id (ALPACA, BIRD_TURKEY, etc)\n" " R = valid race RAW id (ALPACA, BIRD_TURKEY, etc)\n"
//" or a list of RAW ids seperated by spaces\n" " or a list of RAW ids seperated by spaces\n"
//" or the keyword 'all' which adds all races with\n" //" or the keyword 'all' which adds all races with\n"
//" at least one owned tame unit in your fortress\n" //" at least one owned tame unit in your fortress\n"
" unwatch R - stop watching race\n" " unwatch R - stop watching race(s)\n"
" the current target settings will be remembered\n" " the current target settings will be remembered\n"
" forget R - unwatch race and forget target settings for it\n" " forget R - unwatch race(s) and forget target settings for it/them\n"
//" autowatch - automatically adds all new races (animals you buy\n" //" autowatch - automatically adds all new races (animals you buy\n"
//" from merchants, tame yourself or get from migrants)\n" //" from merchants, tame yourself or get from migrants)\n"
//" to the watch list using default target count\n" //" to the watch list using default target count\n"
@ -197,13 +197,13 @@ const string autobutcher_help =
const string autobutcher_help_example = const string autobutcher_help_example =
"Examples:\n" "Examples:\n"
" autobutcher target 4 3 2 1 ALPACA\n" " autobutcher target 4 3 2 1 ALPACA BIRD_TURKEY\n"
" autobutcher watch ALPACA\n" " autobutcher watch ALPACA BIRD_TURKEY\n"
" autobutcher start\n" " autobutcher start\n"
" This means you want to have max 7 kids (4 female, 3 male) and max 3 adults\n" " This means you want to have max 7 kids (4 female, 3 male) and max 3 adults\n"
" (2 female, 1 male) of the race alpaca. Once the kids grow up the oldest\n" " (2 female, 1 male) of the races alpaca and turkey. Once the kids grow up the\n"
" adults will get slaughtered. Excess kids will get slaughtered starting with\n" " oldest adults will get slaughtered. Excess kids will get slaughtered starting\n"
" the youngest to allow that the older ones grow into adults.\n" " the the youngest to allow that the older ones grow into adults.\n"
//" autobutcher target 0 0 0 0 all\n" //" autobutcher target 0 0 0 0 all\n"
//" autobutcher autowatch\n" //" autobutcher autowatch\n"
//" autobutcher start\n" //" autobutcher start\n"
@ -2058,7 +2058,9 @@ command_result df_autobutcher(color_ostream &out, vector <string> & parameters)
bool forget_race = false; bool forget_race = false;
bool list_watched = false; bool list_watched = false;
bool change_target = false; bool change_target = false;
string target_racename = ""; vector <string> target_racenames;
vector <int> target_raceids;
int target_fk = default_fk; int target_fk = default_fk;
int target_mk = default_mk; int target_mk = default_mk;
int target_fa = default_fa; int target_fa = default_fa;
@ -2066,166 +2068,118 @@ command_result df_autobutcher(color_ostream &out, vector <string> & parameters)
int32_t target_raceid = -1; int32_t target_raceid = -1;
for (size_t i = 0; i < parameters.size(); i++) if(!parameters.size())
{ {
string & p = parameters[i]; out << "You must specify a command!" << endl;
out << autobutcher_help << endl;
if (p == "help" || p == "?") return CR_OK;
{ }
out << autobutcher_help << endl;
return CR_OK; // parse main command
} string & p = parameters[0];
if (p == "example") if (p == "help" || p == "?")
{ {
out << autobutcher_help_example << endl; out << autobutcher_help << endl;
return CR_OK; return CR_OK;
} }
else if (p == "start") if (p == "example")
{ {
enable_autobutcher = true; out << autobutcher_help_example << endl;
out << "Autobutcher started."; return CR_OK;
return autoButcher(out, verbose); }
} else if (p == "start")
else if (p == "stop") {
{ enable_autobutcher = true;
enable_autobutcher = false; out << "Autobutcher started.";
out << "Autobutcher stopped."; return autoButcher(out, verbose);
return CR_OK; }
} else if (p == "stop")
else if(p == "verbose") {
{ enable_autobutcher = false;
verbose = true; out << "Autobutcher stopped.";
} return CR_OK;
else if(p == "sleep") }
{ else if(p == "sleep")
if(i == parameters.size()-1) {
{ parameters.erase(parameters.begin());
out.printerr("No duration specified!"); if(!parameters.size())
return CR_WRONG_USAGE;
}
else
{
size_t ticks = 0;
stringstream ss(parameters[i+1]);
i++;
ss >> ticks;
if(ticks <= 0)
{
out.printerr("Invalid duration specified (must be > 0)!");
return CR_WRONG_USAGE;
}
sleep_autobutcher = ticks;
out << "New sleep timer for autobutcher: " << ticks << " ticks." << endl;
return CR_OK;
}
}
else if(p == "watch")
{
if(i == parameters.size()-1)
{
out.printerr("No race specified!");
return CR_WRONG_USAGE;
}
else
{
// todo: parse more than one race
target_racename = parameters[i+1];
i++;
out << "Start watching race " << target_racename << endl;
watch_race = true;
}
}
else if(p == "unwatch")
{
if(i == parameters.size()-1)
{
out.printerr("No race specified!");
return CR_WRONG_USAGE;
}
else
{
// todo: parse more than one race
target_racename = parameters[i+1];
i++;
out << "Stop watching race " << target_racename << endl;
unwatch_race = true;
}
}
else if(p == "forget")
{ {
if(i == parameters.size()-1) out.printerr("No duration specified!");
{ return CR_WRONG_USAGE;
out.printerr("No race specified!");
return CR_WRONG_USAGE;
}
else
{
// todo: parse more than one race
target_racename = parameters[i+1];
i++;
out << "Forget settings for race " << target_racename << endl;
forget_race = true;
}
} }
else if(p == "target") else
{ {
// needs at least 5 more parameters: size_t ticks = 0;
// fk mk fa ma R (can have more than 1 R) stringstream ss(parameters.back());
if(parameters.size() < 6) //i++;
ss >> ticks;
if(ticks <= 0)
{ {
out.printerr("Not enough parameters!"); out.printerr("Invalid duration specified (must be > 0)!");
return CR_WRONG_USAGE; return CR_WRONG_USAGE;
} }
else sleep_autobutcher = ticks;
{ out << "New sleep timer for autobutcher: " << ticks << " ticks." << endl;
stringstream fk(parameters[i+1]);
stringstream mk(parameters[i+2]);
stringstream fa(parameters[i+3]);
stringstream ma(parameters[i+4]);
fk >> target_fk;
mk >> target_mk;
fa >> target_fa;
ma >> target_ma;
// todo: parse more than one race, handle 'all' and 'new'
target_racename = parameters[i+5];
i+=5;
out << "Target count for " << target_racename << ":"
<< " fk=" << target_fk
<< " mk=" << target_mk
<< " fa=" << target_fa
<< " ma=" << target_ma
<< endl;
change_target = true;
}
}
else if(p == "autowatch")
{
out << "not supported yet, sorry" << endl;
return CR_OK; return CR_OK;
} }
else if(p == "noautowatch") }
{ else if(p == "watch")
out << "not supported yet, sorry" << endl; {
return CR_OK; parameters.erase(parameters.begin());
} watch_race = true;
else if(p == "list") }
else if(p == "unwatch")
{
parameters.erase(parameters.begin());
unwatch_race = true;
}
else if(p == "forget")
{
parameters.erase(parameters.begin());
forget_race = true;
}
else if(p == "target")
{
// needs at least 5 more parameters:
// fk mk fa ma R (can have more than 1 R)
if(parameters.size() < 6)
{ {
list_watched = true; out.printerr("Not enough parameters!");
return CR_WRONG_USAGE;
} }
else else
{ {
out << "Unknown command: " << p << endl; stringstream fk(parameters[1]);
return CR_WRONG_USAGE; stringstream mk(parameters[2]);
stringstream fa(parameters[3]);
stringstream ma(parameters[4]);
fk >> target_fk;
mk >> target_mk;
fa >> target_fa;
ma >> target_ma;
parameters.erase(parameters.begin(), parameters.begin()+5);
change_target = true;
} }
} }
else if(p == "autowatch")
if( target_racename == "all" ||
target_racename == "new" )
{ {
out << "'all' and 'new' are not supported yet, sorry." << endl; out << "not supported yet, sorry" << endl;
return CR_OK; return CR_OK;
} }
else if(p == "noautowatch")
{
out << "not supported yet, sorry" << endl;
return CR_OK;
}
else if(p == "list")
{
list_watched = true;
}
else
{
out << "Unknown command: " << p << endl;
return CR_WRONG_USAGE;
}
if(list_watched) if(list_watched)
{ {
@ -2248,102 +2202,123 @@ command_result df_autobutcher(color_ostream &out, vector <string> & parameters)
return CR_OK; return CR_OK;
} }
size_t num_races = df::global::world->raws.creatures.all.size(); // parse rest of parameters for commands followed by a list of races
bool found_race = false; if( watch_race
for(size_t i=0; i<num_races; i++) || unwatch_race
|| forget_race
|| change_target )
{ {
df::creature_raw *raw = df::global::world->raws.creatures.all[i]; if(!parameters.size())
if(raw->creature_id == target_racename)
{ {
target_raceid = i; out.printerr("No race(s) specified!");
found_race = true; return CR_WRONG_USAGE;
break; }
while(parameters.size())
{
string tr = parameters.back();
target_racenames.push_back(tr);
parameters.pop_back();
} }
} }
if(!found_race)
if( target_racenames.size() &&
(target_racenames[0] == "all" ||
target_racenames[0] == "new") )
{ {
out << "Race not found!" << endl; out << "'all' and 'new' are not supported yet, sorry." << endl;
return CR_OK; return CR_OK;
} }
if(unwatch_race) size_t num_races = df::global::world->raws.creatures.all.size();
while(target_racenames.size())
{ {
bool found = false; bool found_race = false;
for(size_t i=0; i<watched_races.size(); i++) for(size_t i=0; i<num_races; i++)
{ {
WatchedRace * w = watched_races[i]; df::creature_raw *raw = df::global::world->raws.creatures.all[i];
if(w->raceId == target_raceid) if(raw->creature_id == target_racenames.back())
{ {
found=true; target_raceids.push_back(i);
w->isWatched=false; target_racenames.pop_back();
found_race = true;
break; break;
} }
} }
if(found) if(!found_race)
out << target_racename << " is not watched anymore." << endl; {
else out << "Race not found: " << target_racenames.back() << endl;
out << target_racename << " was not being watched!" << endl; return CR_OK;
return CR_OK; }
} }
if(watch_race || change_target) if(unwatch_race)
{ {
bool watching = false; while(target_raceids.size())
for(size_t i=0; i<watched_races.size(); i++)
{ {
WatchedRace * w = watched_races[i]; for(size_t i=0; i<watched_races.size(); i++)
if(w->raceId == target_raceid)
{ {
if(watch_race) WatchedRace * w = watched_races[i];
if(w->raceId == target_raceids.back())
{ {
if(w->isWatched) w->isWatched=false;
out << target_racename << " is already being watched." << endl; target_raceids.pop_back();
w->isWatched = true; break;
watching = true;
} }
}
}
return CR_OK;
}
if(change_target) if(watch_race || change_target)
{
while(target_raceids.size())
{
bool entry_found = false;
for(size_t i=0; i<watched_races.size(); i++)
{
WatchedRace * w = watched_races[i];
if(w->raceId == target_raceids.back())
{ {
w->fk = target_fk; if(watch_race)
w->mk = target_mk; {
w->fa = target_fa; w->isWatched = true;
w->ma = target_ma; }
else if(change_target)
{
w->fk = target_fk;
w->mk = target_mk;
w->fa = target_fa;
w->ma = target_ma;
}
entry_found = true;
break;
} }
break;
} }
if(!entry_found)
{
WatchedRace * w = new WatchedRace(watch_race, target_raceids.back(), target_fk, target_mk, target_fa, target_ma);
watched_races.push_back(w);
}
target_raceids.pop_back();
} }
if(!watching)
{
WatchedRace * w = new WatchedRace(watch_race, target_raceid, target_fk, target_mk, target_fa, target_ma);
watched_races.push_back(w);
}
out << target_racename << " is now being watched." << endl;
return CR_OK; return CR_OK;
} }
if(forget_race) if(forget_race)
{ {
bool watched = false; while(target_raceids.size())
for(size_t i=0; i<watched_races.size(); i++)
{ {
WatchedRace * w = watched_races[i]; for(size_t i=0; i<watched_races.size(); i++)
if(w->raceId == target_raceid)
{ {
watched_races.erase(watched_races.begin()+i); WatchedRace * w = watched_races[i];
if(w->raceId == target_raceids.back())
if(w->isWatched) {
out << target_racename << " is already being watched." << endl; watched_races.erase(watched_races.begin()+i);
w->isWatched = true; break;
watched = true; }
break;
} }
target_raceids.pop_back();
} }
if(!watched)
{
out << target_racename << " was not on the watchlist." << endl;
return CR_OK;
}
out << target_racename << " was forgotten." << endl;
return CR_OK; return CR_OK;
} }