From 8e4e6afa9bf579913256eb350567fad67fb7bc26 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 2 Jun 2022 12:50:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E4=BA=86GetBusPosition?= =?UTF-8?q?=E5=87=BD=E6=95=B0=20FCFS=E7=AD=96=E7=95=A5=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bus.c | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/bus.c b/src/bus.c index 5114608..6f5d566 100644 --- a/src/bus.c +++ b/src/bus.c @@ -16,30 +16,48 @@ void RunBus(int direction) int GetBusPosition() { - int a, b, c; - b = 0; + int distance = 0; + rail_node_t *now_pos = the_bus->rail_node_pos; rail_node_t *p = rails; - a = the_bus->rail_node_pos->id;//指向站点的指针以及这个指针对应的站台id - if (a == 1 && (the_bus->distance < 0)) + + // 先计算当前所在站点距离起始站点的距离 + distance += p ->next_node_distance; + p = p->next_node; + while (p != now_pos) { - while(p->id != p->last_node->id) + distance += p->next_node_distance; + p = p->next_node; + } + + if(now_pos == rails) // 起始点特殊处理 + { + // 公交车偏离起始点的位置 + int length = the_bus->distance; + if(length >= 0) { - b += p->next_node_distance; - p = p->next_node; + return length; + } + else + { + return distance + length; + } + } + else if(now_pos == rails->last_node) + { + int length = the_bus->distance; + if(length == now_pos->next_node_distance) // 处理一种极为特殊的情况 公交车在即将到达起始站时 + { + return 0; + } + else + { + return distance + length; } - b += p->next_node_distance; - c = b + (the_bus->distance); } else { - while (p->id != a){ - b += p->next_node_distance; - p = p->next_node; - } - - c = b + (the_bus->distance); + return distance + the_bus->distance; } - return c; } int JudgeOnStation()