EldrickTobin

friar.cpp RC3 - a plugin for DFHack

Apr 4th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. // Friar - For Adding a Super Noble who can handle everything short of Monarchy for your Embark Group. Based on: Fixpositions by Quietust
  2. #include <algorithm> // for std::transform
  3. #include <assert.h>
  4. #include <climits>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <iterator>
  8. #include <list>
  9. #include <set>
  10. #include <stdlib.h> // for rand()
  11. #include <vector>
  12. using namespace std;
  13.  
  14. #include "Console.h"
  15. #include "Core.h"
  16. #include "DataDefs.h"
  17. #include "DFHack.h"
  18. #include "Export.h"
  19. #include "PluginManager.h"
  20.  
  21. #include <df/caste_raw.h>
  22. #include <df/creature_raw.h>
  23. #include "df/entity_position.h"
  24. #include "df/entity_position_assignment.h"
  25. #include "df/entity_position_responsibility.h"
  26. #include "df/entity_raw.h"
  27. #include "df/historical_entity.h"
  28. #include "df/historical_figure.h"
  29. #include "df/ui.h"
  30. #include "df/world.h"
  31.  
  32. using std::set;
  33. using std::string;
  34. using std::vector;
  35.  
  36. using namespace df::enums;
  37. using namespace DFHack;
  38. using df::global::ui;
  39. using df::global::world;
  40.  
  41. DFHACK_PLUGIN("friar");
  42.  
  43. command_result friaradd (color_ostream &out, vector<string> &parameters);
  44.  
  45. DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
  46. {
  47. commands.push_back(PluginCommand(
  48. "friaradd",
  49. "Add a Friar who can handle many management tasks themselves.",
  50. friaradd, false,
  51. "\n"
  52. "\"friaradd\" takes no arguments. Invoke it, and it's done.\n"
  53. "\n"
  54. "Troubleshooting: Currently You must go to the military screen, create a squad off the 'Friar' in the list,\n"
  55. "and assign in the \"Vacant\" spot who-so-ever you wish to be the Friar.\n"
  56. "\n"
  57. "After this has been done you may use the Nobles screen to reassign who is the Friar forever-after.\n"
  58. "When this has been fixed this message will be removed.\n"
  59. ));
  60. return CR_OK;
  61. }
  62.  
  63. DFhackCExport command_result plugin_shutdown ( color_ostream &out )
  64. {
  65. return CR_OK;
  66. }
  67.  
  68. command_result friaradd (color_ostream &out, vector<string> &parameters)
  69. {
  70. if (!parameters.empty())
  71. return CR_WRONG_USAGE;
  72.  
  73. CoreSuspender suspend;
  74. int checked = 0, fixed = 0;
  75. for (int i = 0; i < world->entities.all.size(); i++)
  76. {
  77. df::historical_entity *ent = world->entities.all[i];
  78. checked++;
  79. if (ent->id != ui->main.fortress_entity->id)
  80. continue;
  81. bool update = true;
  82. df::entity_position *pos = NULL;
  83. // see if we need to add a new position or modify an existing one
  84. for (int j = 0; j < ent->positions.own.size(); j++)
  85. {
  86. pos = ent->positions.own[j];
  87. if (pos->responsibilities[entity_position_responsibility::ACCOUNTING] &&
  88. pos->responsibilities[entity_position_responsibility::BUILD_MORALE] &&
  89. pos->responsibilities[entity_position_responsibility::HEALTH_MANAGEMENT] &&
  90. pos->responsibilities[entity_position_responsibility::LAW_ENFORCEMENT] &&
  91. pos->responsibilities[entity_position_responsibility::LAW_MAKING] &&
  92. pos->responsibilities[entity_position_responsibility::MAKE_TOPIC_AGREEMENTS] &&
  93. pos->responsibilities[entity_position_responsibility::MANAGE_PRODUCTION] &&
  94. pos->responsibilities[entity_position_responsibility::MEET_WORKERS] &&
  95. pos->responsibilities[entity_position_responsibility::MILITARY_GOALS] &&
  96. pos->responsibilities[entity_position_responsibility::MILITARY_STRATEGY] &&
  97. pos->responsibilities[entity_position_responsibility::RECEIVE_DIPLOMATS] &&
  98. pos->responsibilities[entity_position_responsibility::RELIGION] &&
  99. pos->responsibilities[entity_position_responsibility::TRADE] &&
  100. (pos->precedence = 130))
  101.  
  102. {
  103. // a friar position exists with the proper responsibilities - skip to the end
  104. update = false;
  105. break;
  106. }
  107. // Friar position already exists, but has the wrong options - modify it instead of creating a new one
  108. if (pos->code == "FRIAR")
  109. break;
  110. pos = NULL;
  111. }
  112. if (update)
  113. {
  114. // either there's no friar, or there is one and it's got the wrong responsibilities
  115. if (!pos)
  116. {
  117. // there was no frair - create it
  118. pos = new df::entity_position;
  119. ent->positions.own.push_back(pos);
  120. pos->code = "FRIAR";
  121. pos->id = ent->positions.next_position_id++;
  122. pos->name[0] = "Friar";
  123. pos->name[1] = "Friars";
  124. pos->flags.set(entity_position_flags::MENIAL_WORK_EXEMPTION);
  125. pos->flags.set(entity_position_flags::COLOR);
  126. pos->flags.set(entity_position_flags::HAS_RESPONSIBILITIES);
  127. pos->flags.set(entity_position_flags::IS_LEADER);
  128. pos->flags.set(entity_position_flags::anon_1);
  129. pos->flags.set(entity_position_flags::anon_2);
  130. pos->flags.set(entity_position_flags::anon_3);
  131. pos->flags.set(entity_position_flags::anon_4);
  132. pos->precedence = 130;
  133. pos->flags.set(entity_position_flags::FLASHES);
  134. pos->flags.set(entity_position_flags::EXPORTED_IN_LEGENDS);
  135. pos->flags.set(entity_position_flags::BRAG_ON_KILL);
  136. pos->flags.set(entity_position_flags::CHAT_WORTHY);
  137. pos->flags.set(entity_position_flags::DO_NOT_CULL);
  138. pos->squad[0]= "congregator";
  139. pos->squad[1]= "congregation";
  140. pos->squad_size = 10;
  141. pos->color[0] = 5;
  142. pos->color[1] = 0;
  143. pos->color[2] = 0;
  144. pos->flags.set(entity_position_flags::DUTY_BOUND);
  145. pos->flags.set(entity_position_flags::RULES_FROM_LOCATION);
  146. }
  147. // assign responsibilities
  148. pos->responsibilities[entity_position_responsibility::ACCOUNTING] = true;
  149. pos->responsibilities[entity_position_responsibility::BUILD_MORALE] = true;
  150. pos->responsibilities[entity_position_responsibility::HEALTH_MANAGEMENT] = true;
  151. pos->responsibilities[entity_position_responsibility::LAW_ENFORCEMENT] = true;
  152. pos->responsibilities[entity_position_responsibility::LAW_MAKING] = true;
  153. pos->responsibilities[entity_position_responsibility::MAKE_TOPIC_AGREEMENTS] = true;
  154. pos->responsibilities[entity_position_responsibility::MANAGE_PRODUCTION] = true;
  155. pos->responsibilities[entity_position_responsibility::MEET_WORKERS] = true;
  156. pos->responsibilities[entity_position_responsibility::MILITARY_GOALS] = true;
  157. pos->responsibilities[entity_position_responsibility::MILITARY_STRATEGY] = true;
  158. pos->responsibilities[entity_position_responsibility::RECEIVE_DIPLOMATS] = true;
  159. pos->responsibilities[entity_position_responsibility::RELIGION] = true;
  160. pos->responsibilities[entity_position_responsibility::TRADE] = true;
  161. }
  162.  
  163. // make sure the friar position, whether we created it or not, is set up for proper assignment
  164. bool assign = true;
  165. for (int j = 0; j < ent->positions.assignments.size(); j++)
  166. {
  167. if (ent->positions.assignments[j]->position_id == pos->id)
  168. {
  169. // it is - nothing more to do here
  170. assign = false;
  171. break;
  172. }
  173. }
  174. if (assign)
  175. {
  176. // it isn't - set it up
  177. df::entity_position_assignment *asn = new df::entity_position_assignment;
  178. ent->positions.assignments.push_back(asn);
  179.  
  180. asn->id = ent->positions.next_assignment_id++;
  181. asn->position_id = pos->id;
  182. asn->flags.extend(0x1F); // make room for 32 flags
  183. asn->flags.set(0); // and set the first one
  184. }
  185. if (update || assign)
  186. fixed++;
  187. }
  188. out.print("Fixed %d of %d civilization, embark, survivor, and religious groups to enable friars.\n", fixed, checked);
  189. return CR_OK;
  190. }
Add Comment
Please, Sign In to add comment