comparison src/order_backup.cpp @ 17109:69b5b1b3eddd draft

(svn r21846) -Codechange: move documentation towards the code to make it more likely to be updated [o-s].
author rubidium <rubidium@openttd.org>
date Tue, 18 Jan 2011 23:09:43 +0000
parents 1b92dbca45cb
children b590eca5d805
comparison
equal deleted inserted replaced
17108:5ae3089defb2 17109:69b5b1b3eddd
18 #include "vehicle_base.h" 18 #include "vehicle_base.h"
19 19
20 OrderBackupPool _order_backup_pool("BackupOrder"); 20 OrderBackupPool _order_backup_pool("BackupOrder");
21 INSTANTIATE_POOL_METHODS(OrderBackup) 21 INSTANTIATE_POOL_METHODS(OrderBackup)
22 22
23 /** Free everything that is allocated. */
23 OrderBackup::~OrderBackup() 24 OrderBackup::~OrderBackup()
24 { 25 {
25 free(this->name); 26 free(this->name);
26 27
27 if (CleaningPool()) return; 28 if (CleaningPool()) return;
32 delete o; 33 delete o;
33 o = next; 34 o = next;
34 } 35 }
35 } 36 }
36 37
38 /**
39 * Create an order backup for the given vehicle.
40 * @param v The vehicle to make a backup of.
41 * @param user The user that is requesting the backup.
42 */
37 OrderBackup::OrderBackup(const Vehicle *v, uint32 user) 43 OrderBackup::OrderBackup(const Vehicle *v, uint32 user)
38 { 44 {
39 this->user = user; 45 this->user = user;
40 this->tile = v->tile; 46 this->tile = v->tile;
41 this->orderindex = v->cur_order_index; 47 this->orderindex = v->cur_order_index;
60 tail = &copy->next; 66 tail = &copy->next;
61 } 67 }
62 } 68 }
63 } 69 }
64 70
71 /**
72 * Restore the data of this order to the given vehicle.
73 * @param v The vehicle to restore to.
74 */
65 void OrderBackup::DoRestore(Vehicle *v) 75 void OrderBackup::DoRestore(Vehicle *v)
66 { 76 {
67 /* If we have a custom name, process that */ 77 /* If we have a custom name, process that */
68 v->name = this->name; 78 v->name = this->name;
69 this->name = NULL; 79 this->name = NULL;
82 92
83 /* Restore vehicle group */ 93 /* Restore vehicle group */
84 DoCommand(0, this->group, v->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP); 94 DoCommand(0, this->group, v->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP);
85 } 95 }
86 96
97 /**
98 * Create an order backup for the given vehicle.
99 * @param v The vehicle to make a backup of.
100 * @param user The user that is requesting the backup.
101 * @note Will automatically remove any previous backups of this user.
102 */
87 /* static */ void OrderBackup::Backup(const Vehicle *v, uint32 user) 103 /* static */ void OrderBackup::Backup(const Vehicle *v, uint32 user)
88 { 104 {
89 /* Don't use reset as that broadcasts over the network to reset the variable, 105 /* Don't use reset as that broadcasts over the network to reset the variable,
90 * which is what we are doing at the moment. */ 106 * which is what we are doing at the moment. */
91 OrderBackup *ob; 107 OrderBackup *ob;
93 if (ob->user == user) delete ob; 109 if (ob->user == user) delete ob;
94 } 110 }
95 new OrderBackup(v, user); 111 new OrderBackup(v, user);
96 } 112 }
97 113
114 /**
115 * Restore the data of this order to the given vehicle.
116 * @param v The vehicle to restore to.
117 * @param user The user that built the vehicle, thus wants to restore.
118 * @note After restoration the backup will automatically be removed.
119 */
98 /* static */ void OrderBackup::Restore(Vehicle *v, uint32 user) 120 /* static */ void OrderBackup::Restore(Vehicle *v, uint32 user)
99 { 121 {
100 OrderBackup *ob; 122 OrderBackup *ob;
101 FOR_ALL_ORDER_BACKUPS(ob) { 123 FOR_ALL_ORDER_BACKUPS(ob) {
102 if (v->tile != ob->tile || ob->user != user) continue; 124 if (v->tile != ob->tile || ob->user != user) continue;
104 ob->DoRestore(v); 126 ob->DoRestore(v);
105 delete ob; 127 delete ob;
106 } 128 }
107 } 129 }
108 130
131 /**
132 * Reset an OrderBackup given a tile and user.
133 * @param tile The tile associated with the OrderBackup.
134 * @param user The user associated with the OrderBackup.
135 * @note Must not be used from the GUI!
136 */
109 /* static */ void OrderBackup::ResetOfUser(TileIndex tile, uint32 user) 137 /* static */ void OrderBackup::ResetOfUser(TileIndex tile, uint32 user)
110 { 138 {
111 OrderBackup *ob; 139 OrderBackup *ob;
112 FOR_ALL_ORDER_BACKUPS(ob) { 140 FOR_ALL_ORDER_BACKUPS(ob) {
113 if (ob->user == user && (ob->tile == tile || tile == INVALID_TILE)) delete ob; 141 if (ob->user == user && (ob->tile == tile || tile == INVALID_TILE)) delete ob;
129 if (flags & DC_EXEC) OrderBackup::ResetOfUser(tile == 0 ? INVALID_TILE : tile, p2); 157 if (flags & DC_EXEC) OrderBackup::ResetOfUser(tile == 0 ? INVALID_TILE : tile, p2);
130 158
131 return CommandCost(); 159 return CommandCost();
132 } 160 }
133 161
162 /**
163 * Reset an user's OrderBackup if needed.
164 * @param user The user associated with the OrderBackup.
165 * @pre _network_server.
166 * @note Must not be used from a command.
167 */
134 /* static */ void OrderBackup::ResetUser(uint32 user) 168 /* static */ void OrderBackup::ResetUser(uint32 user)
135 { 169 {
136 assert(_network_server); 170 assert(_network_server);
137 171
138 OrderBackup *ob; 172 OrderBackup *ob;
143 DoCommandP(0, 0, user, CMD_CLEAR_ORDER_BACKUP); 177 DoCommandP(0, 0, user, CMD_CLEAR_ORDER_BACKUP);
144 return; 178 return;
145 } 179 }
146 } 180 }
147 181
182 /**
183 * Reset the OrderBackups from GUI/game logic.
184 * @param tile The tile of the order backup.
185 * @param from_gui Whether the call came from the GUI, i.e. whether
186 * it must be synced over the network.
187 */
148 /* static */ void OrderBackup::Reset(TileIndex t, bool from_gui) 188 /* static */ void OrderBackup::Reset(TileIndex t, bool from_gui)
149 { 189 {
150 /* The user has CLIENT_ID_SERVER as default when network play is not active, 190 /* The user has CLIENT_ID_SERVER as default when network play is not active,
151 * but compiled it. A network client has its own variable for the unique 191 * but compiled it. A network client has its own variable for the unique
152 * client/user identifier. Finally if networking isn't compiled in the 192 * client/user identifier. Finally if networking isn't compiled in the
175 delete ob; 215 delete ob;
176 } 216 }
177 } 217 }
178 } 218 }
179 219
220 /**
221 * Clear the group of all backups having this group ID.
222 * @param group The group to clear.
223 */
180 /* static */ void OrderBackup::ClearGroup(GroupID group) 224 /* static */ void OrderBackup::ClearGroup(GroupID group)
181 { 225 {
182 OrderBackup *ob; 226 OrderBackup *ob;
183 FOR_ALL_ORDER_BACKUPS(ob) { 227 FOR_ALL_ORDER_BACKUPS(ob) {
184 if (ob->group == group) ob->group = DEFAULT_GROUP; 228 if (ob->group == group) ob->group = DEFAULT_GROUP;
185 } 229 }
186 } 230 }
187 231
232 /**
233 * Clear/update the (clone) vehicle from an order backup.
234 * @param v The vehicle to clear.
235 * @pre v != NULL
236 * @note If it is not possible to set another vehicle as clone
237 * "example", then this backed up order will be removed.
238 */
188 /* static */ void OrderBackup::ClearVehicle(const Vehicle *v) 239 /* static */ void OrderBackup::ClearVehicle(const Vehicle *v)
189 { 240 {
190 assert(v != NULL); 241 assert(v != NULL);
191 OrderBackup *ob; 242 OrderBackup *ob;
192 FOR_ALL_ORDER_BACKUPS(ob) { 243 FOR_ALL_ORDER_BACKUPS(ob) {