diff --git a/src/controller.c b/src/controller.c index bc2d25c..028ee0d 100644 --- a/src/controller.c +++ b/src/controller.c @@ -265,10 +265,64 @@ bus_query_t *SCANGetQuery(int direction) int SCANDirection(bus_query_t *query) { - return BUS_STOP; + if(query == NULL) + { + return BUS_STOP; + } //如果没有请求,公交车停止 + + else + { + int clockwise = 0; + int counterclockwise = 0; //用于顺,逆时针方向所经站台计数 + + /** + * 公交车当前所在位置 + */ + rail_node_t *now_position = the_bus->rail_node_pos; + /** + * 公交车应该前进的方向 + */ + rail_node_t *target_position = query->node; + + rail_node_t *pos = now_position; + while (pos != target_position) //顺时针计数 + { + clockwise++; + pos = pos->next_node; + } + + pos = now_position; + while (pos != target_position) //逆时针计数 + { + counterclockwise++; + pos = pos->last_node; + } + + if(clockwise <= counterclockwise) + { + return BUS_CLOCK_WISE; + }//若顺时针距离短(或顺逆相等),公交车顺时针运行 + else + { + return BUS_COUNTER_CLOCK_WISE; + }//若逆时针距离短,公交车逆时针运行 + } } bus_query_t *SCANBTWQuery() { + rail_node_t *now_position = the_bus->rail_node_pos; + //获取公交车当前所在站点 + bus_query_t *p = queries; + + while(p != NULL) + { + if(p->node == now_position) + { + return p; + } + p = p->next_node; + }//遍历请求链表,判断是否有可以顺便处理的请求 + return NULL; } \ No newline at end of file