src/waypoint.cpp
changeset 8258 9fa31acb07bc
parent 8254 1496654ca5e7
child 8264 b1e85998c7d3
equal deleted inserted replaced
8257:3151fdbc73f9 8258:9fa31acb07bc
    90 
    90 
    91 	/* Find an unused waypoint number belonging to this town */
    91 	/* Find an unused waypoint number belonging to this town */
    92 	FOR_ALL_WAYPOINTS(local_wp) {
    92 	FOR_ALL_WAYPOINTS(local_wp) {
    93 		if (wp == local_wp) continue;
    93 		if (wp == local_wp) continue;
    94 
    94 
    95 		if (local_wp->xy && local_wp->string == STR_NULL && local_wp->town_index == wp->town_index)
    95 		if (local_wp->xy && local_wp->name == NULL && local_wp->town_index == wp->town_index)
    96 			used_waypoint[local_wp->town_cn] = true;
    96 			used_waypoint[local_wp->town_cn] = true;
    97 	}
    97 	}
    98 
    98 
    99 	/* Find an empty spot */
    99 	/* Find an empty spot */
   100 	for (i = 0; used_waypoint[i] && i < MAX_WAYPOINTS_PER_TOWN; i++) {}
   100 	for (i = 0; used_waypoint[i] && i < MAX_WAYPOINTS_PER_TOWN; i++) {}
   101 
   101 
   102 	wp->string = STR_NULL;
   102 	wp->name = NULL;
   103 	wp->town_cn = i;
   103 	wp->town_cn = i;
   104 }
   104 }
   105 
   105 
   106 /**
   106 /**
   107  * Find a deleted waypoint close to a tile.
   107  * Find a deleted waypoint close to a tile.
   195 		if (wp == NULL) return CMD_ERROR;
   195 		if (wp == NULL) return CMD_ERROR;
   196 
   196 
   197 		wp_auto_delete = wp;
   197 		wp_auto_delete = wp;
   198 
   198 
   199 		wp->town_index = 0;
   199 		wp->town_index = 0;
   200 		wp->string = STR_NULL;
   200 		wp->name = NULL;
   201 		wp->town_cn = 0;
   201 		wp->town_cn = 0;
   202 	} else if (flags & DC_EXEC) {
   202 	} else if (flags & DC_EXEC) {
   203 		/* Move existing (recently deleted) waypoint to the new location */
   203 		/* Move existing (recently deleted) waypoint to the new location */
   204 
   204 
   205 		/* First we update the destination for all vehicles that
   205 		/* First we update the destination for all vehicles that
   348 	if (!CheckTileOwnership(wp->xy)) return CMD_ERROR;
   348 	if (!CheckTileOwnership(wp->xy)) return CMD_ERROR;
   349 
   349 
   350 	if (!StrEmpty(_cmd_text)) {
   350 	if (!StrEmpty(_cmd_text)) {
   351 		if (!IsUniqueWaypointName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
   351 		if (!IsUniqueWaypointName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
   352 
   352 
   353 		StringID str = AllocateName(_cmd_text, 0);
       
   354 
       
   355 		if (str == 0) return CMD_ERROR;
       
   356 
       
   357 		if (flags & DC_EXEC) {
   353 		if (flags & DC_EXEC) {
   358 			if (wp->string != STR_NULL) DeleteName(wp->string);
   354 			free(wp->name);
   359 
   355 			wp->name = strdup(_cmd_text);
   360 			wp->string = str;
       
   361 			wp->town_cn = 0;
   356 			wp->town_cn = 0;
   362 
   357 
   363 			UpdateWaypointSign(wp);
   358 			UpdateWaypointSign(wp);
   364 			MarkWholeScreenDirty();
   359 			MarkWholeScreenDirty();
   365 		} else {
       
   366 			DeleteName(str);
       
   367 		}
   360 		}
   368 	} else {
   361 	} else {
   369 		if (flags & DC_EXEC) {
   362 		if (flags & DC_EXEC) {
   370 			if (wp->string != STR_NULL) DeleteName(wp->string);
   363 			free(wp->name);
   371 
   364 
   372 			MakeDefaultWaypointName(wp);
   365 			MakeDefaultWaypointName(wp);
   373 			UpdateWaypointSign(wp);
   366 			UpdateWaypointSign(wp);
   374 			MarkWholeScreenDirty();
   367 			MarkWholeScreenDirty();
   375 		}
   368 		}
   391 	static byte stat_raw[sizeof(Station)];
   384 	static byte stat_raw[sizeof(Station)];
   392 	static Station &stat = *(Station*)stat_raw;
   385 	static Station &stat = *(Station*)stat_raw;
   393 
   386 
   394 	stat.train_tile = stat.xy = wp->xy;
   387 	stat.train_tile = stat.xy = wp->xy;
   395 	stat.town = GetTown(wp->town_index);
   388 	stat.town = GetTown(wp->town_index);
   396 	stat.string_id = wp->string;
       
   397 	stat.build_date = wp->build_date;
   389 	stat.build_date = wp->build_date;
   398 
   390 
   399 	return &stat;
   391 	return &stat;
   400 }
   392 }
   401 
   393 
   421 	this->xy = tile;
   413 	this->xy = tile;
   422 }
   414 }
   423 
   415 
   424 Waypoint::~Waypoint()
   416 Waypoint::~Waypoint()
   425 {
   417 {
   426 	if (this->string != STR_NULL) DeleteName(this->string);
   418 	free(this->name);
   427 
   419 
   428 	if (CleaningPool()) return;
   420 	if (CleaningPool()) return;
   429 
   421 
   430 	RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, this->index);
   422 	RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, this->index);
   431 
   423 
   460 static const SaveLoad _waypoint_desc[] = {
   452 static const SaveLoad _waypoint_desc[] = {
   461 	SLE_CONDVAR(Waypoint, xy,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
   453 	SLE_CONDVAR(Waypoint, xy,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
   462 	SLE_CONDVAR(Waypoint, xy,         SLE_UINT32,                  6, SL_MAX_VERSION),
   454 	SLE_CONDVAR(Waypoint, xy,         SLE_UINT32,                  6, SL_MAX_VERSION),
   463 	SLE_CONDVAR(Waypoint, town_index, SLE_UINT16,                 12, SL_MAX_VERSION),
   455 	SLE_CONDVAR(Waypoint, town_index, SLE_UINT16,                 12, SL_MAX_VERSION),
   464 	SLE_CONDVAR(Waypoint, town_cn,    SLE_UINT8,                  12, SL_MAX_VERSION),
   456 	SLE_CONDVAR(Waypoint, town_cn,    SLE_UINT8,                  12, SL_MAX_VERSION),
   465 	    SLE_VAR(Waypoint, string,     SLE_UINT16),
   457 	SLE_CONDVAR(Waypoint, string,     SLE_STRINGID,                0, 83),
       
   458 	SLE_CONDSTR(Waypoint, name,       SLE_STR, 0,                 84, SL_MAX_VERSION),
   466 	    SLE_VAR(Waypoint, deleted,    SLE_UINT8),
   459 	    SLE_VAR(Waypoint, deleted,    SLE_UINT8),
   467 
   460 
   468 	SLE_CONDVAR(Waypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32,  3, 30),
   461 	SLE_CONDVAR(Waypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32,  3, 30),
   469 	SLE_CONDVAR(Waypoint, build_date, SLE_INT32,                  31, SL_MAX_VERSION),
   462 	SLE_CONDVAR(Waypoint, build_date, SLE_INT32,                  31, SL_MAX_VERSION),
   470 	SLE_CONDVAR(Waypoint, localidx,   SLE_UINT8,                   3, SL_MAX_VERSION),
   463 	SLE_CONDVAR(Waypoint, localidx,   SLE_UINT8,                   3, SL_MAX_VERSION),