Skip to content

Commit 07fb135

Browse files
author
Andarix
committed
(sqai_rail) update revision 0.9.5.2
git-svn-id: svn://tron.homeunix.org/simutrans/simutrans/trunk@11733 8aca7d54-2c30-db11-9de9-000461428c89
1 parent 0d9d32a commit 07fb135

File tree

6 files changed

+165
-142
lines changed

6 files changed

+165
-142
lines changed

simutrans/ai/sqai_rail/ai.nut

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* @file ai.nut
23
* Main file of the AI player.
34
*/
45

@@ -10,24 +11,24 @@ ai <- {}
1011
ai.short_description <- "AI player implementation road/ship/rail"
1112

1213
ai.author <- "dwachs/Andarix"
13-
ai.version <- "0.9.5.1"
14+
ai.version <- "0.9.5.2"
1415

1516
// includes
1617
include("basic") // .. definition of basic node classes
1718
include("astar") // .. route search for way building etc
1819
include("save") // .. routines to save class instances
1920

20-
include("factorysearcher") // .. checks factories for available connections
21-
include("industry_connection_planner") // .. plans connection between 2 factories
22-
include("combined_connections") // .. plans connections using water + land transport
23-
include("industry_manager") // .. manages existing connection (buys, sells, upgrades convoys)
24-
include("placefinder") // .. utility functions to find places for stations near factories etc
25-
include("prototyper") // .. plans convoy-type for a connection
26-
include("road_connector") // .. builds road connection
27-
include("rail_connector") // .. builds rail connection
28-
include("ship_connector") // .. creates ship connection
29-
include("station_manager") // .. keeps information about freight station
30-
include("vehicle_constructor") // .. constructs convoy, assign to line, start
21+
include("factorysearcher") /// .. checks factories for available connections
22+
include("industry_connection_planner") /// .. plans connection between 2 factories
23+
include("combined_connections") /// .. plans connections using water + land transport
24+
include("industry_manager") /// .. manages existing connection (buys, sells, upgrades convoys)
25+
include("placefinder") /// .. utility functions to find places for stations near factories etc
26+
include("prototyper") /// .. plans convoy-type for a connection
27+
include("road_connector") /// .. builds road connection
28+
include("rail_connector") /// .. builds rail connection
29+
include("ship_connector") /// .. creates ship connection
30+
include("station_manager") /// .. keeps information about freight station
31+
include("vehicle_constructor") /// .. constructs convoy, assign to line, start
3132

3233
// basic functions
3334
sum <- @(a,b) a+b
@@ -261,8 +262,12 @@ function equal_coord3d(a,b)
261262
return a.x == b.x && a.y == b.y && a.z == b.z
262263
}
263264

264-
265-
function is_cash_available(cost /* in 1/100 cr */)
265+
/**
266+
* check cash <-> build cost
267+
*
268+
* @param[in] cost build cost in 1/100 cr
269+
*/
270+
function is_cash_available(cost)
266271
{
267272
//gui.add_message_at(our_player, " ***** cash : " + our_player.get_current_cash(), world.get_time())
268273
//gui.add_message_at(our_player, " ***** cost : " + cost, world.get_time())
@@ -305,7 +310,7 @@ function myrand(upper)
305310
}
306311

307312
/**
308-
* Returns ticks for today + @p m months
313+
* @return ticks for today + @p m months
309314
*/
310315
function today_plus_months(m)
311316
{
@@ -314,13 +319,14 @@ function today_plus_months(m)
314319
}
315320

316321
/**
317-
* returns pakset name (lower case)
318-
*
322+
* This function read full string from ground.outside.pak.
323+
* The string split by spaces and the first string (name pakset) return as lower case string.
319324
*
325+
* @return pakset name (lower case) or unknown
320326
*/
321327
function get_set_name()
322328
{
323-
local pakset = get_pakset_name() // full string from ground.outside.pak
329+
local pakset = get_pakset_name() //* full string from ground.outside.pak
324330
if ( pakset != null ) {
325331
local s = pakset.find(" ")
326332
pakset = pakset.slice(0, s)
@@ -332,11 +338,11 @@ function get_set_name()
332338
return pakset
333339
}
334340

335-
/*
341+
/**
336342
* set global variables citicar and convoy counts
337343
*
338-
* output = 0 -> no return
339-
* output = 1 -> return road_car_rate
344+
* @param output 0 -> no return : 1 -> return road_car_rate
345+
* @return none or calculate road car rate
340346
*/
341347
function set_map_vehicles_counts(output = 0) {
342348
sleep()

simutrans/ai/sqai_rail/astar.nut

Lines changed: 63 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* @file astar.nut
23
* Classes to help with route-searching.
34
* Based on the A* algorithm.
45
*/
@@ -284,77 +285,6 @@ class astar_route_finder extends astar
284285
}
285286
}
286287

287-
/**
288-
* Class to search a route along existing ways.
289-
*/
290-
class astar_route_finder extends astar
291-
{
292-
wt = wt_all
293-
294-
constructor(wt_)
295-
{
296-
base.constructor()
297-
wt = wt_
298-
if ( [wt_all, wt_invalid, wt_water, wt_air].find(wt) ) {
299-
throw("Using this waytype is going to be inefficient. Use at own risk.")
300-
}
301-
cost_curve = cost_straight
302-
}
303-
304-
function process_node(cnode)
305-
{
306-
local from = tile_x(cnode.x, cnode.y, cnode.z)
307-
local back = dir.backward(cnode.dir)
308-
// allowed directions
309-
local dirs = from.get_way_dirs_masked(wt)
310-
311-
for(local d = 1; d<16; d*=2) {
312-
// do not go backwards, only along existing ways
313-
if ( d == back || ( (dirs & d) == 0) ) {
314-
continue
315-
}
316-
317-
local to = from.get_neighbour(wt, d)
318-
if (to) {
319-
if (!is_closed(to)) {
320-
// estimate moving cost
321-
local move = cnode.is_straight_move(d) ? cost_straight : cost_curve
322-
local dist = estimate_distance(to)
323-
local cost = cnode.cost + move
324-
local weight = cost //+ dist
325-
local node = ab_node(to, cnode, cost, dist, d)
326-
327-
add_to_open(node, weight)
328-
}
329-
}
330-
}
331-
}
332-
333-
// start and end have to be arrays of objects with 3d-coordinates
334-
function search_route(start, end)
335-
{
336-
prepare_search()
337-
foreach (e in end) {
338-
targets.append(e);
339-
}
340-
compute_bounding_box()
341-
342-
foreach (s in start)
343-
{
344-
local dist = estimate_distance(s)
345-
add_to_open(ab_node(s, null, 1, dist+1, 0, 0), dist+1)
346-
}
347-
348-
search()
349-
350-
if (route.len() > 0) {
351-
return { start = route.top(), end = route[0], routes = route }
352-
}
353-
print("No route found")
354-
return { err = "No route" }
355-
}
356-
}
357-
358288
class ab_node extends ::astar_node
359289
{
360290
dir = 0 // direction to reach this node
@@ -1118,13 +1048,14 @@ function check_ground(pos_s, pos_e, way) {
11181048
}
11191049

11201050
/**
1051+
* @fn check_tile_end_of_station(direction, count, s_tile)
11211052
* check tile end of station
11221053
*
1123-
* direction = 1, 2, 4, 8
1124-
* count
1125-
* s_tile = station tile : tile_x
1054+
* @param direction = 1, 2, 4, 8
1055+
* @param count
1056+
* @param s_tile = station tile : tile_x
11261057
*
1127-
* return tile_x or null
1058+
* @return tile_x or null
11281059
*/
11291060
function check_tile_end_of_station(direction, count, s_tile) {
11301061

@@ -1543,13 +1474,13 @@ function remove_tile_to_empty(tiles, wt, t_array = 1) {
15431474
/**
15441475
* function for check station lenght
15451476
*
1546-
* pl = player
1547-
* starts_field = tile station from plan_simple_connection
1548-
* st_lenght = stations fields count
1549-
* wt = waytype
1550-
* select_station = station object
1551-
* build = 0 -> test ; 1 -> build
1552-
* combined_halt = true -> yes ; false -> no
1477+
* @param pl = player
1478+
* @param starts_field = tile station from plan_simple_connection
1479+
* @param st_lenght = stations fields count
1480+
* @param wt = waytype
1481+
* @param select_station = station object
1482+
* @param build = 0 -> test ; 1 -> build
1483+
* @param combined_halt = true -> yes ; false -> no
15531484
*
15541485
* returns false (something failed) or array of station tiles (success)
15551486
* in case of success, the value of starts_field maybe changed
@@ -1808,12 +1739,12 @@ function test_tile_is_empty(t_tile) {
18081739
/**
18091740
* function expand station()
18101741
*
1811-
* pl = player
1812-
* fields = array fields
1813-
* wt = waytype
1814-
* select_station = station object
1815-
* start_fld = c_start or c_end
1816-
* combined_halt = true -> yes ; false -> no
1742+
* @param pl = player_x
1743+
* @param fields = array fields
1744+
* @param wt = waytype
1745+
* @param select_station = station object
1746+
* @param start_fld = c_start or c_end
1747+
* @param combined_halt = true -> yes ; false -> no
18171748
*/
18181749
function expand_station(pl, fields, wt, select_station, start_fld, combined_halt) {
18191750

@@ -2255,16 +2186,16 @@ function expand_station(pl, fields, wt, select_station, start_fld, combined_halt
22552186
}
22562187
}
22572188

2258-
/*
2189+
/**
22592190
* function build_extensions_connect_factory()
22602191
*
2261-
* pl = player
2262-
* st_field = start field
2263-
* hlt_field = halt field
2264-
* tiles[] = test tiles for extensions to connect factory
2265-
* extension = extensions object
2192+
* @param pl = player
2193+
* @param st_field = start field
2194+
* @param hlt_field = halt field
2195+
* @param tiles[] = test tiles for extensions to connect factory
2196+
* @param extension = extensions object
22662197
*
2267-
* return
2198+
* @return
22682199
* 1 = connect factory
22692200
* 0 = not connect factory
22702201
*/
@@ -2368,8 +2299,8 @@ function build_station(tiles, station_obj) {
23682299
/**
23692300
* find signal tool
23702301
*
2371-
* sig_type = signal type (is_signal, is_presignal ... )
2372-
* wt = waytype
2302+
* @param sig_type = signal type (is_signal, is_presignal ... )
2303+
* @param wt = waytype
23732304
*/
23742305
function find_signal(sig_type, wt) {
23752306

@@ -2621,9 +2552,9 @@ function find_extension(wt, tile_size = 1) {
26212552
/**
26222553
* search existing depot on range to station
26232554
*
2624-
* field_pos = start field
2625-
* wt = waytype
2626-
* range = search range
2555+
* @param field_pos = start field
2556+
* @param wt = waytype
2557+
* @param range = search range
26272558
*
26282559
*/
26292560
function search_depot(field_pos, wt, range = 10) {
@@ -2649,9 +2580,9 @@ function search_depot(field_pos, wt, range = 10) {
26492580
/**
26502581
* search existing station on range to field
26512582
*
2652-
* field_pos = start field
2653-
* wt = waytype
2654-
* range = search range
2583+
* @param field_pos = start field
2584+
* @param wt = waytype
2585+
* @param range = search range
26552586
*
26562587
*/
26572588
function search_station(field_pos, wt, range) {
@@ -3550,13 +3481,19 @@ function build_double_track(start_field, wt, station_len) {
35503481
}
35513482
}
35523483

3553-
/*
3554-
* start = start field line
3555-
* end = end field line
3556-
* wt = waytype
3557-
* l = stations distance
3558-
* c = count of double ways
3559-
* c=0 -> no build double ways - return route array
3484+
/// @publicsection
3485+
/**
3486+
*
3487+
* check the route for build double ways
3488+
*
3489+
* @param start = start field line
3490+
* @param end = end field line
3491+
* @param wt = waytype
3492+
* @param l = stations distance
3493+
* @param c = count of double ways
3494+
* @param c=0 -> no build double ways - return route array
3495+
*
3496+
* @retur tile_x array start tiles for double ways
35603497
*/
35613498
function check_way_line(start, end, wt, l, c, r_line) {
35623499
/*
@@ -4314,6 +4251,15 @@ function check_way_line(start, end, wt, l, c, r_line) {
43144251

43154252
}
43164253

4254+
/**
4255+
* check the route and optimized this
4256+
*
4257+
* @param route tiles array from route
4258+
* @param wt waytype from route
4259+
* @param int_run count the run this function for selcted line
4260+
* @param o_line line_x object
4261+
*
4262+
*/
43174263
function optimize_way_line(route, wt, int_run, o_line) {
43184264

43194265
// 0 = off
@@ -4983,10 +4929,10 @@ function optimize_way_line(route, wt, int_run, o_line) {
49834929

49844930
}
49854931

4986-
/*
4932+
/**
49874933
* check tiles free for terraform
49884934
*
4989-
* tiles = array tiles for check tiles left and right is free
4935+
* @param tiles array tiles for check tiles left and right is free
49904936
*/
49914937
function check_tiles_for_terraform(tiles) {
49924938
local tiles_free = true
@@ -5008,11 +4954,12 @@ function check_tiles_for_terraform(tiles) {
50084954
return tiles_free
50094955
}
50104956

5011-
/*
4957+
/**
50124958
* check double ways in new line
50134959
* waytype: wt_rail
50144960
*
5015-
*
4961+
* @param route tiles array from route
4962+
* @param wt waytype from route
50164963
*/
50174964
function check_doubleway_in_line(route, wt) {
50184965
//gui.add_message_at(our_player, " check_doubleway_in_line(route, wt) ", world.get_time())
@@ -5721,6 +5668,7 @@ function destroy_line(line_obj, good, link_obj) {
57215668
return true
57225669
}
57235670

5671+
/// @publicsection
57245672
/*
57255673
* check waytypes from halt
57265674
* tile = one tile from halt
@@ -5762,6 +5710,7 @@ function test_halt_waytypes(tile) {
57625710
return test_way
57635711
}
57645712

5713+
/// @publicsection
57655714
/*
57665715
* check depot as home for other vehicles
57675716
* tile = depot coord

0 commit comments

Comments
 (0)