Skip to content

Commit 9a0122c

Browse files
authored
fix: Command squad auto sorting (#167)
Specialists should auto sort into command squads as originally intended.
1 parent c1669d8 commit 9a0122c

File tree

6 files changed

+181
-98
lines changed

6 files changed

+181
-98
lines changed

scripts/scr_company_order/scr_company_order.gml

Lines changed: 117 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@ function temp_marine_variables(co, unit_num){
44
if (unit.squad != "none"){
55
var squad_member;
66
var found = false;
7-
for (var r=0;r<array_length(squads[unit.squad].members);r++){
8-
squad_member = squads[unit.squad].members[r];
7+
var _squad_members = squads[unit.squad].members;
8+
for (var r=0;r<array_length(_squad_members);r++){
9+
squad_member = _squad_members[r];
910
try{
10-
if (squad_member[0] == unit.company) and (squad_member[1] == unit.marine_number){
11-
squads[unit.squad].members[r] = [co,array_length(temp_name)];
12-
found = true;
13-
break;
11+
if (is_array(squad_member)){
12+
if (squad_member[0] == unit.company) and (squad_member[1] == unit.marine_number){
13+
squads[unit.squad].members[r] = [co,array_length(temp_name)];
14+
found = true;
15+
break;
16+
}
1417
}
1518
} catch( _exception) {
1619
handle_exception(_exception);
1720
unit.squad="none";
1821
}
1922
}
20-
if (!found){unit.squad = "none"}
23+
if (!found){
24+
unit.squad = "none"
25+
}
2126
}
2227
array_push(temp_race,race[co][unit_num]);
2328
array_push(temp_loc,loc[co][unit_num]);
@@ -111,11 +116,20 @@ function scr_company_order(company) {
111116
var role_shuffle_length = array_length(role_orders);
112117
var company_length = array_length(name[co]);
113118
var squadless={};
119+
120+
var _roles = obj_ini.role[100];
114121
// find units not in a squad
115-
for (i=0;i<company_length;i++){
116-
if (!is_struct(TTRPG[co][i])) then TTRPG[co][i] = new TTRPG_stats("chapter", co, i, "blank");
117-
unit = TTRPG[co][i];
118-
if (unit.squad=="none") and (unit.name()!=""){
122+
for (i=company_length-1;i>=0;i--){
123+
unit = fetch_unit([co, i]);
124+
if (!is_struct(unit)){
125+
TTRPG[co][i] = new TTRPG_stats("chapter", co, i, "blank");
126+
unit = fetch_unit([co, i]);
127+
}
128+
if (unit.name()=="") then continue;
129+
130+
unit = fetch_unit([co, i]);
131+
132+
if (unit.squad=="none"){
119133
if (!struct_exists(squadless, unit.role())){
120134
squadless[$ unit.role()] = [i];
121135
} else {
@@ -126,14 +140,14 @@ function scr_company_order(company) {
126140

127141
//at this point check that all squads have the right types and numbers of units in them
128142
var squad, wanted_roles;
129-
for (i=0;i<array_length(squads);i++){
130-
if (squads[i].base_company != co){
131-
if (array_length(squads[i].members)==0){
143+
for (i=0;i<array_length(obj_ini.squads);i++){
144+
if (obj_ini.squads[i].base_company != co){
145+
if (array_length(obj_ini.squads[i].members)==0){
132146
array_push(empty_squads,i);
133147
}
134148
continue;
135149
}
136-
squad = squads[i];
150+
squad = obj_ini.squads[i];
137151
squad.update_fulfilment();
138152

139153
//squad has role spaces to fill
@@ -143,66 +157,93 @@ function scr_company_order(company) {
143157
/* this finds sqauds that are in need of members and checks ot see if there
144158
are any squadless units in the chapter with
145159
the right role to fill the gap*/
146-
for (var r = 0;r < array_length(wanted_roles);r++){
160+
for (var r = array_length(wanted_roles)-1;r >=0;r--){
161+
var _wanted_role = wanted_roles[r];
162+
if (struct_exists(squadless,_wanted_role)){
147163

148-
if (struct_exists(squadless,wanted_roles[r])){
149-
if (!squad.fulfilled){
164+
var _wanted_role_number = squad.space[$ _wanted_role];
165+
var _squadless_with_role = squadless[$ _wanted_role];
166+
var _squadless_with_role_count = array_length(squadless[$ _wanted_role]);
150167

151-
if (struct_exists(squad.required,wanted_roles[r])){
168+
if (!squad.fulfilled){
169+
170+
if (struct_exists(squad.required,_wanted_role)){
152171

153-
while (array_length(squadless[$ wanted_roles[r]])>0) and (squad.required[$ wanted_roles[r]] > 0){
172+
var _needed_role_number = squad.required[$ _wanted_role];
173+
while (_squadless_with_role_count>0) and (_needed_role_number > 0){
154174

155-
array_push(squad.members,[company,squadless[$ wanted_roles[r]][0]]);
175+
var _marine_id = array_pop(_squadless_with_role);
176+
unit = fetch_unit([co, _marine_id]);
156177

157-
TTRPG[co,squadless[$ wanted_roles[r]][0]].squad=i;
178+
unit.add_to_squad(i);
158179

159-
array_delete(squadless[$ wanted_roles[r]],0,1);
180+
_squadless_with_role_count--;
160181

161-
squad.required[$ wanted_roles[r]]--;
162-
163-
squad.space[$ wanted_roles[r]]--;
182+
_needed_role_number--;
183+
_wanted_role_number--;
164184
}
165185
}
166186
}
167-
if (struct_exists(squad.space,wanted_roles[r])){
168-
while (array_length(squadless[$ wanted_roles[r]])> 0) and (squad.space[$ wanted_roles[r]] > 0){
169-
array_push(squad.members,[company,squadless[$ wanted_roles[r]][0]]);
170-
TTRPG[co,squadless[$ wanted_roles[r]][0]].squad=i;
171-
array_delete(squadless[$ wanted_roles[r]],0,1);
172-
squad.space[$ wanted_roles[r]]--;
187+
if (struct_exists(squad.space,_wanted_role)){
188+
while (_squadless_with_role_count> 0) and (_wanted_role_number > 0){
189+
var _marine_id = array_pop(_squadless_with_role);
190+
unit = fetch_unit([co, _marine_id]);
191+
unit.add_to_squad(i)
192+
_wanted_role_number--;
173193
}
174194
}
175195
}
176196
}
177197
//if no new sergeants are found for squad someone gets promoted
178-
//find a new_sergeant
179-
if (struct_exists(squad.required, role[100][18])){
180-
if (squad.required[$ role[100][18]] > 0){
198+
//find a new_sergeant
199+
var _sarge = _roles[Role.SERGEANT]
200+
if (struct_exists(squad.required, _sarge)){
201+
if (squad.required[$ _sarge] > 0){
181202
squad.new_sergeant();
182-
squad.required[$ role[100][18]]--;
203+
squad.required[$ _sarge]--;
183204
}
184205
}
185206
//find a new veteran sergeant
186-
if (struct_exists(squad.required, role[100][19])){
187-
if (squad.required[$ role[100][19]] > 0){
207+
var _vet_sarge = _roles[Role.VETERAN_SERGEANT];
208+
if (struct_exists(squad.required, _vet_sarge)){
209+
if (squad.required[$ _vet_sarge] > 0){
188210
squad.new_sergeant(true);
189-
squad.required[$ role[100][19]]--;
211+
squad.required[$ _vet_sarge]--;
212+
}
213+
}
214+
for (var r = array_length(wanted_roles)-1;r >=0;r--){
215+
var _wanted_role = wanted_roles[r];
216+
if (struct_exists(squad.required,_wanted_role)){
217+
if (squad.required[$struct_exists] > 0){
218+
var _mems = squad.get_members();
219+
squad.empty_squad();
220+
for (var m=0;m<array_length(_mems);m++){
221+
unit = _mems[m];
222+
if (unit.squad=="none"){
223+
if (!struct_exists(squadless, unit.role())){
224+
squadless[$ unit.role()] = [i];
225+
} else {
226+
array_push(squadless[$ unit.role()],i);
227+
}
228+
}
229+
}
230+
}
190231
}
191-
}
232+
}
192233
}
193234
}
194235

195236
var squadless_and_squad_spaces = [squadless,empty_squads];
196237

197238
var squad_builder = [
198-
["tactical_squad",role[100][8],5],
199-
["devastator_squad",role[100][9],5],
200-
["sternguard_veteran_squad",role[100][3],5],
201-
["vanguard_veteran_squad",role[100][3],5],
202-
["terminator_squad",role[100][4],4],
203-
["terminator_assault_squad",role[100][4],4],
204-
["assault_squad",role[100][10],5],
205-
["scout_squad",role[100][12],5],
239+
["tactical_squad",_roles[Role.TACTICAL],5],
240+
["devastator_squad",_roles[Role.DEVASTATOR],5],
241+
["sternguard_veteran_squad",_roles[Role.VETERAN],5],
242+
["vanguard_veteran_squad",_roles[Role.VETERAN],5],
243+
["terminator_squad",_roles[Role.TERMINATOR],4],
244+
["terminator_assault_squad",_roles[Role.TERMINATOR],4],
245+
["assault_squad",_roles[Role.ASSAULT],5],
246+
["scout_squad",_roles[Role.SCOUT],5],
206247
]
207248

208249
for (i=0;i<array_length(squad_builder);i++){
@@ -214,8 +255,8 @@ function scr_company_order(company) {
214255
}
215256

216257
//comand squads only get built to a max of one and are specialist so sit outside of general squad creation
217-
if (struct_exists(squadless,role[100,5])) && (struct_exists(squadless,role[100,7])) && (struct_exists(squadless,role[100][11])){
218-
if (array_length(squadless[$role[100,5]])>0) && (array_length(squadless[$role[100,7]])>0) && (array_length(squadless[$role[100][11]])>0){
258+
if (struct_exists(squadless,_roles[Role.CAPTAIN])) && (struct_exists(squadless,_roles[Role.CHAMPION])) && (struct_exists(squadless,_roles[Role.ANCIENT])){
259+
if (array_length(squadless[$_roles[Role.CAPTAIN]])>0) && (array_length(squadless[$_roles[Role.CHAMPION]])>0) && (array_length(squadless[$_roles[Role.ANCIENT]])>0){
219260
new_squad_index=false;
220261
if (array_length(empty_squads)>0){
221262
new_squad_index = empty_squads[0];
@@ -253,7 +294,7 @@ function scr_company_order(company) {
253294
i--;
254295
sort_length--;
255296
//if unit is part of a squad make sure rest of squad is grouped next to unit
256-
if (unit.squad !="none"){
297+
if (unit.squad != "none"){
257298
var cur_squad = unit.squad;
258299
var r = -1;
259300
while (r < sort_length){
@@ -273,7 +314,7 @@ function scr_company_order(company) {
273314
//position 2 in role order
274315
/*if (global.chapter_name!="Space Wolves") and (global.chapter_name!="Iron Hands"){
275316
i=0;repeat(300){i+=1;
276-
if (role[co][i]=role[100][14]){v+=1;
317+
if (role[co][i]=_roles[Roles.CHAPLAIN]){v+=1;
277318
temp_marine_variables(co, i ,v);
278319
}
279320
}*/
@@ -319,41 +360,41 @@ function scr_company_order(company) {
319360
}
320361

321362
function role_hierarchy(){
322-
363+
var _roles = obj_ini.role[100];
323364
var hierarchy = [
324365
"Chapter Master",
325366
"Forge Master",
326367
"Master of Sanctity",
327368
"Master of the Apothecarion",
328-
string("Chief {0}",obj_ini.role[100,17]),
329-
obj_ini.role[100][2],
330-
obj_ini.role[100][5],
331-
obj_ini.role[100][14],
332-
string("{0} Aspirant",obj_ini.role[100][14]),
369+
string("Chief {0}",_roles[Role.LIBRARIAN]),
370+
_roles[Role.HONOUR_GUARD],
371+
_roles[Role.CAPTAIN],
372+
_roles[Role.CHAPLAIN],
373+
string("{0} Aspirant",_roles[Role.CHAPLAIN]),
333374
"Death Company",
334-
obj_ini.role[100][16],
335-
string("{0} Aspirant",obj_ini.role[100][16]),
375+
_roles[Role.TECHMARINE],
376+
string("{0} Aspirant",_roles[Role.TECHMARINE]),
336377
"Techpriest",
337-
obj_ini.role[100][15],
338-
string("{0} Aspirant",obj_ini.role[100][15]),
378+
_roles[Role.APOTHECARY],
379+
string("{0} Aspirant",_roles[Role.APOTHECARY]),
339380
"Sister Hospitaler",
340-
obj_ini.role[100,17],
381+
_roles[Role.LIBRARIAN],
341382
"Codiciery",
342383
"Lexicanum",
343-
string("{0} Aspirant",obj_ini.role[100,17]),
344-
obj_ini.role[100][11],
345-
obj_ini.role[100][7],
384+
string("{0} Aspirant",_roles[Role.LIBRARIAN]),
385+
_roles[Role.ANCIENT],
386+
_roles[Role.CHAMPION],
346387
"Death Company",
347-
obj_ini.role[100][19],
348-
obj_ini.role[100][18],
349-
obj_ini.role[100][4],
350-
obj_ini.role[100][3],
351-
obj_ini.role[100][8],
352-
obj_ini.role[100][10],
353-
obj_ini.role[100][9],
354-
obj_ini.role[100][12],
355-
"Venerable "+string(obj_ini.role[100][6]),
356-
obj_ini.role[100][6],
388+
_roles[Role.VETERAN_SERGEANT],
389+
_roles[Role.SERGEANT],
390+
_roles[Role.TERMINATOR],
391+
_roles[Role.VETERAN],
392+
_roles[Role.TACTICAL],
393+
_roles[Role.ASSAULT],
394+
_roles[Role.DEVASTATOR],
395+
_roles[Role.SCOUT],
396+
$"Venerable {_roles[Role.DREADNOUGHT]}",
397+
_roles[Role.DREADNOUGHT],
357398
"Skitarii",
358399
"Crusader",
359400
"Ranger",

scripts/scr_company_view/scr_company_view.gml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,9 @@ function filter_and_sort_company(type, specific){
429429
}
430430

431431
function switch_view_company(new_view){
432+
432433
with (obj_controller){
433-
if (new_view<1) then exit;
434+
if (new_view<1) then exit;
434435
filter_mode = false;
435436
text_bar=0;
436437
if (managing<=10 && managing>=0){
@@ -449,6 +450,9 @@ function switch_view_company(new_view){
449450
company_data={};
450451
scr_special_view(new_view);
451452
} else {
453+
with (obj_ini){
454+
scr_company_order(new_view);
455+
}
452456
scr_company_view(new_view);
453457
company_data = new CompanyStruct(managing);
454458
}

scripts/scr_manage_task_selector/scr_manage_task_selector.gml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function scr_manage_task_selector(){
3737
case "captain_promote":
3838
unit = display_unit[i];
3939
unit.update_role(obj_ini.role[100][Role.CAPTAIN]);
40+
unit.squad="none";
4041
var start_company = unit.company;
4142
var end_company = selection_data.system;
4243
var endslot = 0;
@@ -58,6 +59,7 @@ function scr_manage_task_selector(){
5859
case "champion_promote":
5960
unit = display_unit[i];
6061
unit.update_role(obj_ini.role[100][Role.CHAMPION]);
62+
unit.squad="none";
6163

6264
with (obj_ini){
6365
scr_company_order(unit.company);
@@ -70,6 +72,7 @@ function scr_manage_task_selector(){
7072
case "ancient_promote":
7173
unit = display_unit[i];
7274
unit.update_role(obj_ini.role[100][Role.ANCIENT]);
75+
unit.squad="none";
7376

7477

7578
with (obj_ini){

scripts/scr_marine_struct/scr_marine_struct.gml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,15 @@ function TTRPG_stats(faction, comp, mar, class = "marine", other_spawn_data={})
20982098
squad = "none"
20992099
}
21002100
}
2101+
static add_to_squad = function(new_squad){
2102+
if (squad != "none"){
2103+
if (new_squad==squad) then exit;
2104+
remove_from_squad();
2105+
}
2106+
squad = new_squad;
2107+
var _squad = fetch_squad(squad);
2108+
_squad.add_member(company, marine_number);
2109+
}
21012110
static marine_location = function(){
21022111
var location_id,location_name;
21032112
var location_type = planet_location;

0 commit comments

Comments
 (0)