Also scroll tile separators in the train depot
[openttd/fttd.git] / src / group_cmd.cpp
bloba249adae9cf62be4f1962e890d772fd96eae5054
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file group_cmd.cpp Handling of the engine groups */
12 #include "stdafx.h"
13 #include "cmd_helper.h"
14 #include "command_func.h"
15 #include "train.h"
16 #include "vehiclelist.h"
17 #include "vehicle_func.h"
18 #include "autoreplace_base.h"
19 #include "autoreplace_func.h"
20 #include "string.h"
21 #include "company_func.h"
22 #include "core/pool_func.hpp"
23 #include "order_backup.h"
25 #include "table/strings.h"
27 GroupID _new_group_id;
29 template<> Group::Pool Group::PoolItem::pool ("Group");
30 INSTANTIATE_POOL_METHODS(Group)
32 GroupStatistics::GroupStatistics()
34 this->num_engines = xcalloct<uint16>(Engine::GetPoolSize());
37 GroupStatistics::~GroupStatistics()
39 free(this->num_engines);
42 /**
43 * Clear all caches.
45 void GroupStatistics::Clear()
47 this->num_vehicle = 0;
48 this->num_profit_vehicle = 0;
49 this->profit_last_year = 0;
51 /* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */
52 free(this->num_engines);
53 this->num_engines = xcalloct<uint16>(Engine::GetPoolSize());
56 /**
57 * Returns the GroupStatistics for a specific group.
58 * @param company Owner of the group.
59 * @param id_g GroupID of the group.
60 * @param type VehicleType of the vehicles in the group.
61 * @return Statistics for the group.
63 /* static */ GroupStatistics &GroupStatistics::Get(CompanyID company, GroupID id_g, VehicleType type)
65 if (Group::IsValidID(id_g)) {
66 Group *g = Group::Get(id_g);
67 assert(g->owner == company);
68 assert(g->vehicle_type == type);
69 return g->statistics;
72 if (IsDefaultGroupID(id_g)) return Company::Get(company)->group_default[type];
73 if (IsAllGroupID(id_g)) return Company::Get(company)->group_all[type];
75 NOT_REACHED();
78 /**
79 * Returns the GroupStatistic for the group of a vehicle.
80 * @param v Vehicle.
81 * @return GroupStatistics for the group of the vehicle.
83 /* static */ GroupStatistics &GroupStatistics::Get(const Vehicle *v)
85 return GroupStatistics::Get(v->owner, v->group_id, v->type);
88 /**
89 * Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
90 * @param v Vehicle.
91 * @return GroupStatistics for the ALL_GROUP of the vehicle type.
93 /* static */ GroupStatistics &GroupStatistics::GetAllGroup(const Vehicle *v)
95 return GroupStatistics::Get(v->owner, ALL_GROUP, v->type);
98 /**
99 * Update all caches after loading a game, changing NewGRF etc..
101 /* static */ void GroupStatistics::UpdateAfterLoad()
103 /* Set up the engine count for all companies */
104 Company *c;
105 FOR_ALL_COMPANIES(c) {
106 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
107 c->group_all[type].Clear();
108 c->group_default[type].Clear();
112 /* Recalculate */
113 Group *g;
114 FOR_ALL_GROUPS(g) {
115 g->statistics.Clear();
118 const Vehicle *v;
119 FOR_ALL_VEHICLES(v) {
120 if (!v->IsEngineCountable()) continue;
122 GroupStatistics::CountEngine(v, 1);
123 if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
126 FOR_ALL_COMPANIES(c) {
127 GroupStatistics::UpdateAutoreplace(c->index);
132 * Update num_vehicle when adding or removing a vehicle.
133 * @param v Vehicle to count.
134 * @param delta +1 to add, -1 to remove.
136 /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta)
138 assert(delta == 1 || delta == -1);
140 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
141 GroupStatistics &stats = GroupStatistics::Get(v);
143 stats_all.num_vehicle += delta;
144 stats.num_vehicle += delta;
146 if (v->age > VEHICLE_PROFIT_MIN_AGE) {
147 stats_all.num_profit_vehicle += delta;
148 stats_all.profit_last_year += v->GetDisplayProfitLastYear() * delta;
149 stats.num_profit_vehicle += delta;
150 stats.profit_last_year += v->GetDisplayProfitLastYear() * delta;
155 * Update num_engines when adding/removing an engine.
156 * @param v Engine to count.
157 * @param delta +1 to add, -1 to remove.
159 /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta)
161 assert(delta == 1 || delta == -1);
162 GroupStatistics::GetAllGroup(v).num_engines[v->engine_type] += delta;
163 GroupStatistics::Get(v).num_engines[v->engine_type] += delta;
167 * Add a vehicle to the profit sum of its group.
169 /* static */ void GroupStatistics::VehicleReachedProfitAge(const Vehicle *v)
171 GroupStatistics &stats_all = GroupStatistics::GetAllGroup(v);
172 GroupStatistics &stats = GroupStatistics::Get(v);
174 stats_all.num_profit_vehicle++;
175 stats_all.profit_last_year += v->GetDisplayProfitLastYear();
176 stats.num_profit_vehicle++;
177 stats.profit_last_year += v->GetDisplayProfitLastYear();
181 * Recompute the profits for all groups.
183 /* static */ void GroupStatistics::UpdateProfits()
185 /* Set up the engine count for all companies */
186 Company *c;
187 FOR_ALL_COMPANIES(c) {
188 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
189 c->group_all[type].ClearProfits();
190 c->group_default[type].ClearProfits();
194 /* Recalculate */
195 Group *g;
196 FOR_ALL_GROUPS(g) {
197 g->statistics.ClearProfits();
200 const Vehicle *v;
201 FOR_ALL_VEHICLES(v) {
202 if (v->IsPrimaryVehicle() && v->age > VEHICLE_PROFIT_MIN_AGE) GroupStatistics::VehicleReachedProfitAge(v);
207 * Update autoreplace_defined and autoreplace_finished of all statistics of a company.
208 * @param company Company to update statistics for.
210 /* static */ void GroupStatistics::UpdateAutoreplace(CompanyID company)
212 /* Set up the engine count for all companies */
213 Company *c = Company::Get(company);
214 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
215 c->group_all[type].ClearAutoreplace();
216 c->group_default[type].ClearAutoreplace();
219 /* Recalculate */
220 Group *g;
221 FOR_ALL_GROUPS(g) {
222 if (g->owner != company) continue;
223 g->statistics.ClearAutoreplace();
226 for (EngineRenewList erl = c->engine_renew_list; erl != NULL; erl = erl->next) {
227 const Engine *e = Engine::Get(erl->from);
228 GroupStatistics &stats = GroupStatistics::Get(company, erl->group_id, e->type);
229 if (!stats.autoreplace_defined) {
230 stats.autoreplace_defined = true;
231 stats.autoreplace_finished = true;
233 if (stats.num_engines[erl->from] > 0) stats.autoreplace_finished = false;
238 * Update the num engines of a groupID. Decrease the old one and increase the new one
239 * @note called in SetTrainGroupID and UpdateTrainGroupID
240 * @param v Vehicle we have to update
241 * @param old_g index of the old group
242 * @param new_g index of the new group
244 static inline void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
246 if (old_g != new_g) {
247 /* Decrease the num engines in the old group */
248 GroupStatistics::Get(v->owner, old_g, v->type).num_engines[v->engine_type]--;
250 /* Increase the num engines in the new group */
251 GroupStatistics::Get(v->owner, new_g, v->type).num_engines[v->engine_type]++;
257 Group::Group(Owner owner)
259 this->owner = owner;
262 Group::~Group()
264 free(this->name);
269 * Create a new vehicle group.
270 * @param vt vehicle type
271 * @param flags type of operation
272 * @return the cost of this operation or an error
274 static CommandCost CreateNewGroup (VehicleType vt, DoCommandFlag flags)
276 if (!Group::CanAllocateItem()) return CMD_ERROR;
278 if (flags & DC_EXEC) {
279 Group *g = new Group(_current_company);
280 g->replace_protection = false;
281 g->vehicle_type = vt;
282 g->parent = INVALID_GROUP;
284 _new_group_id = g->index;
286 InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
289 return CommandCost();
293 * Create a new vehicle group.
294 * @param tile unused
295 * @param flags type of operation
296 * @param p1 vehicle type
297 * @param p2 unused
298 * @param text unused
299 * @return the cost of this operation or an error
301 CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
303 VehicleType vt = Extract<VehicleType, 0, 3>(p1);
304 if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR;
306 return CreateNewGroup (vt, flags);
311 * Add all vehicles in the given group to the default group and then deletes the group.
312 * @param tile unused
313 * @param flags type of operation
314 * @param p1 index of array group
315 * - p1 bit 0-15 : GroupID
316 * @param p2 unused
317 * @param text unused
318 * @return the cost of this operation or an error
320 CommandCost CmdDeleteGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
322 Group *g = Group::GetIfValid(p1);
323 if (g == NULL || g->owner != _current_company) return CMD_ERROR;
325 /* Remove all vehicles from the group */
326 DoCommand(0, p1, 0, flags, CMD_REMOVE_ALL_VEHICLES_GROUP);
328 /* Delete sub-groups */
329 Group *gp;
330 FOR_ALL_GROUPS(gp) {
331 if (gp->parent == g->index) {
332 DoCommand(0, gp->index, 0, flags, CMD_DELETE_GROUP);
336 if (flags & DC_EXEC) {
337 /* Update backupped orders if needed */
338 OrderBackup::ClearGroup(g->index);
340 /* If we set an autoreplace for the group we delete, remove it. */
341 if (_current_company < MAX_COMPANIES) {
342 Company *c;
343 EngineRenew *er;
345 c = Company::Get(_current_company);
346 FOR_ALL_ENGINE_RENEWS(er) {
347 if (er->group_id == g->index) RemoveEngineReplacementForCompany(c, er->from, g->index, flags);
351 VehicleType vt = g->vehicle_type;
353 /* Delete the Replace Vehicle Windows */
354 DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
355 delete g;
357 InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack());
360 return CommandCost();
363 static bool IsUniqueGroupNameForVehicleType(const char *name, VehicleType type)
365 const Group *g;
367 FOR_ALL_GROUPS(g) {
368 if (g->name != NULL && g->vehicle_type == type && strcmp(g->name, name) == 0) return false;
371 return true;
375 * Alter a group
376 * @param tile unused
377 * @param flags type of operation
378 * @param p1 index of array group
379 * - p1 bit 0-15 : GroupID
380 * - p1 bit 16: 0 - Rename group
381 * 1 - Set group parent
382 * @param p2 parent group index
383 * @param text the new name or an empty string when resetting to the default
384 * @return the cost of this operation or an error
386 CommandCost CmdAlterGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
388 Group *g = Group::GetIfValid(GB(p1, 0, 16));
389 if (g == NULL || g->owner != _current_company) return CMD_ERROR;
391 if (!HasBit(p1, 16)) {
392 /* Rename group */
393 bool reset = StrEmpty(text);
395 if (!reset) {
396 if (Utf8StringLength(text) >= MAX_LENGTH_GROUP_NAME_CHARS) return CMD_ERROR;
397 if (!IsUniqueGroupNameForVehicleType(text, g->vehicle_type)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
400 if (flags & DC_EXEC) {
401 /* Delete the old name */
402 free(g->name);
403 /* Assign the new one */
404 g->name = reset ? NULL : xstrdup(text);
406 } else {
407 /* Set group parent */
408 const Group *pg = Group::GetIfValid(GB(p2, 0, 16));
410 if (pg != NULL) {
411 if (pg->owner != _current_company) return CMD_ERROR;
412 if (pg->vehicle_type != g->vehicle_type) return CMD_ERROR;
414 /* Ensure request parent isn't child of group.
415 * This is the only place that loops are prevented. */
416 if (GroupIsInGroup(pg->index, g->index)) return CMD_ERROR;
419 if (flags & DC_EXEC) {
420 g->parent = (pg == NULL) ? INVALID_GROUP : pg->index;
424 if (flags & DC_EXEC) {
425 SetWindowDirty(WC_REPLACE_VEHICLE, g->vehicle_type);
426 InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
429 return CommandCost();
434 * Do add a vehicle to a group.
435 * @param v Vehicle to add.
436 * @param new_g Group to add to.
438 static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
440 GroupStatistics::CountVehicle(v, -1);
442 switch (v->type) {
443 default: NOT_REACHED();
444 case VEH_TRAIN:
445 SetTrainGroupID(Train::From(v), new_g);
446 break;
448 case VEH_ROAD:
449 case VEH_SHIP:
450 case VEH_AIRCRAFT:
451 if (v->IsEngineCountable()) UpdateNumEngineGroup(v, v->group_id, new_g);
452 v->group_id = new_g;
453 break;
456 GroupStatistics::CountVehicle(v, 1);
460 * Add a vehicle to a group
461 * @param tile unused
462 * @param flags type of operation
463 * @param p1 index of array group
464 * - p1 bit 0-15 : GroupID
465 * @param p2 vehicle to add to a group
466 * - p2 bit 0-19 : VehicleID
467 * - p2 bit 31 : Add shared vehicles as well.
468 * @param text unused
469 * @return the cost of this operation or an error
471 CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
473 Vehicle *v = Vehicle::GetIfValid(GB(p2, 0, 20));
474 GroupID new_g = p1;
476 if (v == NULL || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return CMD_ERROR;
478 if (Group::IsValidID(new_g)) {
479 Group *g = Group::Get(new_g);
480 if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR;
483 if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR;
485 if (new_g == NEW_GROUP) {
486 /* Create new group. */
487 CommandCost ret = CreateNewGroup (v->type, flags);
488 if (ret.Failed()) return ret;
490 new_g = _new_group_id;
493 if (flags & DC_EXEC) {
494 AddVehicleToGroup(v, new_g);
496 if (HasBit(p2, 31)) {
497 /* Add vehicles in the shared order list as well. */
498 for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
499 if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
503 GroupStatistics::UpdateAutoreplace(v->owner);
505 /* Update the Replace Vehicle Windows */
506 SetWindowDirty(WC_REPLACE_VEHICLE, v->type);
507 InvalidateWindowData(GetWindowClassForVehicleType(v->type), VehicleListIdentifier(VL_GROUP_LIST, v->type, _current_company).Pack());
510 return CommandCost();
514 * Add all shared vehicles of all vehicles from a group
515 * @param tile unused
516 * @param flags type of operation
517 * @param p1 index of group array
518 * - p1 bit 0-15 : GroupID
519 * @param p2 type of vehicles
520 * @param text unused
521 * @return the cost of this operation or an error
523 CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
525 VehicleType type = Extract<VehicleType, 0, 3>(p2);
526 GroupID id_g = p1;
527 if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR;
529 if (flags & DC_EXEC) {
530 Vehicle *v;
532 /* Find the first front engine which belong to the group id_g
533 * then add all shared vehicles of this front engine to the group id_g */
534 FOR_ALL_VEHICLES(v) {
535 if (v->type == type && v->IsPrimaryVehicle()) {
536 if (v->group_id != id_g) continue;
538 /* For each shared vehicles add it to the group */
539 for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
540 if (v2->group_id != id_g) DoCommand(tile, id_g, v2->index, flags, CMD_ADD_VEHICLE_GROUP, text);
545 InvalidateWindowData(GetWindowClassForVehicleType(type), VehicleListIdentifier(VL_GROUP_LIST, type, _current_company).Pack());
548 return CommandCost();
553 * Remove all vehicles from a group
554 * @param tile unused
555 * @param flags type of operation
556 * @param p1 index of group array
557 * - p1 bit 0-15 : GroupID
558 * @param p2 unused
559 * @param text unused
560 * @return the cost of this operation or an error
562 CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
564 GroupID old_g = p1;
565 Group *g = Group::GetIfValid(old_g);
567 if (g == NULL || g->owner != _current_company) return CMD_ERROR;
569 if (flags & DC_EXEC) {
570 Vehicle *v;
572 /* Find each Vehicle that belongs to the group old_g and add it to the default group */
573 FOR_ALL_VEHICLES(v) {
574 if (v->IsPrimaryVehicle()) {
575 if (v->group_id != old_g) continue;
577 /* Add The Vehicle to the default group */
578 DoCommand(tile, DEFAULT_GROUP, v->index, flags, CMD_ADD_VEHICLE_GROUP, text);
582 InvalidateWindowData(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
585 return CommandCost();
589 * Set replace protection for a group and its sub-groups.
590 * @param g initial group.
591 * @param protect 1 to set or 0 to clear protection.
593 static void SetGroupReplaceProtection(Group *g, bool protect)
595 g->replace_protection = protect;
597 Group *pg;
598 FOR_ALL_GROUPS(pg) {
599 if (pg->parent == g->index) SetGroupReplaceProtection(pg, protect);
604 * (Un)set global replace protection from a group
605 * @param tile unused
606 * @param flags type of operation
607 * @param p1 index of group array
608 * - p1 bit 0-15 : GroupID
609 * @param p2
610 * - p2 bit 0 : 1 to set or 0 to clear protection.
611 * - p2 bit 1 : 1 to apply to sub-groups.
612 * @param text unused
613 * @return the cost of this operation or an error
615 CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
617 Group *g = Group::GetIfValid(p1);
618 if (g == NULL || g->owner != _current_company) return CMD_ERROR;
620 if (flags & DC_EXEC) {
621 if (HasBit(p2, 1)) {
622 SetGroupReplaceProtection(g, HasBit(p2, 0));
623 } else {
624 g->replace_protection = HasBit(p2, 0);
627 SetWindowDirty(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack());
628 InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type);
631 return CommandCost();
635 * Decrease the num_vehicle variable before delete an front engine from a group
636 * @note Called in CmdSellRailWagon and DeleteLasWagon,
637 * @param v FrontEngine of the train we want to remove.
639 void RemoveVehicleFromGroup(const Vehicle *v)
641 if (!v->IsPrimaryVehicle()) return;
643 if (!IsDefaultGroupID(v->group_id)) GroupStatistics::CountVehicle(v, -1);
648 * Affect the groupID of a train to new_g.
649 * @note called in CmdAddVehicleGroup and CmdMoveRailVehicle
650 * @param v First vehicle of the chain.
651 * @param new_g index of array group
653 void SetTrainGroupID(Train *v, GroupID new_g)
655 if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g)) return;
657 assert(v->IsFrontEngine() || IsDefaultGroupID(new_g));
659 for (Vehicle *u = v; u != NULL; u = u->Next()) {
660 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
662 u->group_id = new_g;
665 /* Update the Replace Vehicle Windows */
666 GroupStatistics::UpdateAutoreplace(v->owner);
667 SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
672 * Recalculates the groupID of a train. Should be called each time a vehicle is added
673 * to/removed from the chain,.
674 * @note this needs to be called too for 'wagon chains' (in the depot, without an engine)
675 * @note Called in CmdBuildRailVehicle, CmdBuildRailWagon, CmdMoveRailVehicle, CmdSellRailWagon
676 * @param v First vehicle of the chain.
678 void UpdateTrainGroupID(Train *v)
680 assert(v->IsFrontEngine() || v->IsFreeWagon());
682 GroupID new_g = v->IsFrontEngine() ? v->group_id : (GroupID)DEFAULT_GROUP;
683 for (Vehicle *u = v; u != NULL; u = u->Next()) {
684 if (u->IsEngineCountable()) UpdateNumEngineGroup(u, u->group_id, new_g);
686 u->group_id = new_g;
689 /* Update the Replace Vehicle Windows */
690 GroupStatistics::UpdateAutoreplace(v->owner);
691 SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
695 * Get the number of engines with EngineID id_e in the group with GroupID
696 * id_g and its sub-groups.
697 * @param company The company the group belongs to
698 * @param id_g The GroupID of the group used
699 * @param id_e The EngineID of the engine to count
700 * @return The number of engines with EngineID id_e in the group
702 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
704 uint count = 0;
705 const Engine *e = Engine::Get(id_e);
706 const Group *g;
707 FOR_ALL_GROUPS(g) {
708 if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
710 return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
713 void RemoveAllGroupsForCompany(const CompanyID company)
715 Group *g;
717 FOR_ALL_GROUPS(g) {
718 if (company == g->owner) delete g;
724 * Test if GroupID needle is a descendant of (or is) GroupID haystack
725 * @param needle The GroupID to search for
726 * @param haystack The GroupID to search in
727 * @return True iff needle is haystack or a descendant of haystack
729 bool GroupIsInGroup (GroupID needle, GroupID haystack)
731 if (needle == haystack) return true;
733 if (!Group::IsValidID(needle)) return false;
735 for (;;) {
736 needle = Group::Get(needle)->parent;
737 if (needle == INVALID_GROUP) return false;
738 if (needle == haystack) return true;