Add:Core:Adding estimate of duration of speech
[navit-package] / navit / navigation.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include <glib.h>
25 #include "debug.h"
26 #include "profile.h"
27 #include "navigation.h"
28 #include "coord.h"
29 #include "item.h"
30 #include "route.h"
31 #include "transform.h"
32 #include "mapset.h"
33 #include "projection.h"
34 #include "map.h"
35 #include "navit.h"
36 #include "callback.h"
37 #include "plugin.h"
38 #include "navit_nls.h"
39
40 /* #define DEBUG */
41
42 struct suffix {
43         char *fullname;
44         char *abbrev;
45         int sex;
46 } suffixes[]= {
47         {"weg",NULL,1},
48         {"platz","pl.",1},
49         {"ring",NULL,1},
50         {"allee",NULL,2},
51         {"gasse",NULL,2},
52         {"straße","str.",2},
53         {"strasse",NULL,2},
54 };
55
56 struct navigation {
57         struct map *map;
58         struct item_hash *hash;
59         struct navigation_itm *first;
60         struct navigation_itm *last;
61         struct navigation_command *cmd_first;
62         struct navigation_command *cmd_last;
63         struct callback_list *callback_speech;
64         struct callback_list *callback;
65         struct navit *navit;
66         int level_last;
67         struct item item_last;
68         int turn_around;
69         int turn_around_limit;
70         int distance_turn;
71         int distance_last;
72         int announce[route_item_last-route_item_first+1][3];
73 };
74
75
76 struct navigation_command {
77         struct navigation_itm *itm;
78         struct navigation_command *next;
79         struct navigation_command *prev;
80         int delta;
81         int roundabout_delta;
82         int length;
83 };
84
85 /**
86  * @brief Calculates the delta between two angles
87  * @param angle1 The first angle
88  * @param angle2 The second angle
89  * @return The difference between the angles: -179..-1=angle2 is left of angle1,0=same,1..179=angle2 is right of angle1,180=angle1 is opposite of angle2
90  */ 
91
92 static int
93 angle_delta(int angle1, int angle2)
94 {
95         int delta=angle2-angle1;
96         if (delta <= -180)
97                 delta+=360;
98         if (delta > 180)
99                 delta-=360;
100         return delta;
101 }
102
103 struct navigation *
104 navigation_new(struct attr *parent, struct attr **attrs)
105 {
106         int i,j;
107         struct navigation *ret=g_new0(struct navigation, 1);
108         ret->hash=item_hash_new();
109         ret->callback=callback_list_new();
110         ret->callback_speech=callback_list_new();
111         ret->level_last=-2;
112         ret->distance_last=-2;
113         ret->distance_turn=50;
114         ret->turn_around_limit=3;
115         ret->navit=parent->u.navit;
116
117         for (j = 0 ; j <= route_item_last-route_item_first ; j++) {
118                 for (i = 0 ; i < 3 ; i++) {
119                         ret->announce[j][i]=-1;
120                 }
121         }
122
123         return ret;     
124 }
125
126 int
127 navigation_set_announce(struct navigation *this_, enum item_type type, int *level)
128 {
129         int i;
130         if (type < route_item_first || type > route_item_last) {
131                 dbg(0,"street type %d out of range [%d,%d]", type, route_item_first, route_item_last);
132                 return 0;
133         }
134         for (i = 0 ; i < 3 ; i++) 
135                 this_->announce[type-route_item_first][i]=level[i];
136         return 1;
137 }
138
139 static int
140 navigation_get_announce_level(struct navigation *this_, enum item_type type, int dist)
141 {
142         int i;
143
144         if (type < route_item_first || type > route_item_last)
145                 return -1;
146         for (i = 0 ; i < 3 ; i++) {
147                 if (dist <= this_->announce[type-route_item_first][i])
148                         return i;
149         }
150         return i;
151 }
152
153 /**
154  * @brief Holds a way that one could possibly drive from a navigation item
155  */
156 struct navigation_way {
157         struct navigation_way *next;            /**< Pointer to a linked-list of all navigation_ways from this navigation item */ 
158         short dir;                      /**< The direction -1 or 1 of the way */
159         short angle2;                   /**< The angle one has to steer to drive from the old item to this street */
160         int flags;                      /**< The flags of the way */
161         struct item item;               /**< The item of the way */
162         char *name1;
163         char *name2;
164 };
165
166 struct navigation_itm {
167         char *name1;
168         char *name2;
169         struct item item;
170         int direction;
171         int angle_start;
172         int angle_end;
173         struct coord start,end;
174         int time;
175         int length;
176         int dest_time;
177         int dest_length;
178         int told;                                                       /**< Indicates if this item's announcement has been told earlier and should not be told again*/
179         int streetname_told;                            /**< Indicates if this item's streetname has been told in speech navigation*/
180         int dest_count;
181         int flags;
182         struct navigation_itm *next;
183         struct navigation_itm *prev;
184         struct navigation_way *ways;            /**< Pointer to all ways one could drive from here */
185 };
186
187 /* 0=N,90=E */
188 static int
189 road_angle(struct coord *c1, struct coord *c2, int dir)
190 {
191         int ret=transform_get_angle_delta(c1, c2, dir);
192         dbg(1, "road_angle(0x%x,0x%x - 0x%x,0x%x)=%d\n", c1->x, c1->y, c2->x, c2->y, ret);
193         return ret;
194 }
195
196 static char
197 *get_count_str(int n) 
198 {
199         switch (n) {
200         case 0:
201                 return _("zeroth"); // Not shure if this exists, neither if it will ever be needed
202         case 1:
203                 return _("first");
204         case 2:
205                 return _("second");
206         case 3:
207                 return _("third");
208         case 4:
209                 return _("fourth");
210         case 5:
211                 return _("fifth");
212         case 6:
213                 return _("sixth");
214         default: 
215                 return NULL;
216         }
217 }
218
219 static int
220 round_distance(int dist)
221 {
222         if (dist < 100) {
223                 dist=(dist+5)/10;
224                 return dist*10;
225         }
226         if (dist < 250) {
227                 dist=(dist+13)/25;
228                 return dist*25;
229         }
230         if (dist < 500) {
231                 dist=(dist+25)/50;
232                 return dist*50;
233         }
234         if (dist < 1000) {
235                 dist=(dist+50)/100;
236                 return dist*100;
237         }
238         if (dist < 5000) {
239                 dist=(dist+50)/100;
240                 return dist*100;
241         }
242         if (dist < 100000) {
243                 dist=(dist+500)/1000;
244                 return dist*1000;
245         }
246         dist=(dist+5000)/10000;
247         return dist*10000;
248 }
249
250 static char *
251 get_distance(int dist, enum attr_type type, int is_length)
252 {
253         if (type == attr_navigation_long) {
254                 if (is_length)
255                         return g_strdup_printf(_("%d m"), dist);
256                 else
257                         return g_strdup_printf(_("in %d m"), dist);
258         }
259         if (dist < 1000) {
260                 if (is_length)
261                         return g_strdup_printf(_("%d meters"), dist);
262                 else
263                         return g_strdup_printf(_("in %d meters"), dist);
264         }
265         if (dist < 5000) {
266                 int rem=(dist/100)%10;
267                 if (rem) {
268                         if (is_length)
269                                 return g_strdup_printf(_("%d.%d kilometer"), dist/1000, rem);
270                         else
271                                 return g_strdup_printf(_("in %d.%d kilometers"), dist/1000, rem);
272                 }
273         }
274         if (is_length) 
275                 return g_strdup_printf(ngettext("one kilometer","%d kilometers", dist/1000), dist/1000);
276         else
277                 return g_strdup_printf(ngettext("in one kilometer","in %d kilometers", dist/1000), dist/1000);
278 }
279
280
281 /**
282  * @brief This calculates the angle with which an item starts or ends
283  *
284  * This function can be used to get the angle an item (from a route graph map)
285  * starts or ends with. Note that the angle will point towards the inner of
286  * the item.
287  *
288  * This is meant to be used with items from a route graph map
289  * With other items this will probably not be optimal...
290  *
291  * @param w The way which should be calculated
292  */ 
293 static void
294 calculate_angle(struct navigation_way *w)
295 {
296         struct coord cbuf[2];
297         struct item *ritem; // the "real" item
298         struct coord c;
299         struct map_rect *mr;
300         struct attr attr;
301
302         w->angle2=361;
303         mr = map_rect_new(w->item.map, NULL);
304         if (!mr)
305                 return;
306
307         ritem = map_rect_get_item_byid(mr, w->item.id_hi, w->item.id_lo);
308         if (!ritem) {
309                 dbg(1,"Item from segment not found on map!\n");
310                 map_rect_destroy(mr);
311                 return;
312         }
313
314         if (ritem->type < type_line || ritem->type >= type_area) {
315                 map_rect_destroy(mr);
316                 return;
317         }
318         if (item_attr_get(ritem, attr_flags, &attr))
319                 w->flags=attr.u.num;
320         else
321                 w->flags=0;
322         if (item_attr_get(ritem, attr_street_name, &attr))
323                 w->name1=map_convert_string(ritem->map,attr.u.str);
324         else
325                 w->name1=NULL;
326         if (item_attr_get(ritem, attr_street_name_systematic, &attr))
327                 w->name2=map_convert_string(ritem->map,attr.u.str);
328         else
329                 w->name2=NULL;
330                 
331         if (w->dir < 0) {
332                 if (item_coord_get(ritem, cbuf, 2) != 2) {
333                         dbg(1,"Using calculate_angle() with a less-than-two-coords-item?\n");
334                         map_rect_destroy(mr);
335                         return;
336                 }
337                         
338                 while (item_coord_get(ritem, &c, 1)) {
339                         cbuf[0] = cbuf[1];
340                         cbuf[1] = c;
341                 }
342                 
343         } else {
344                 if (item_coord_get(ritem, cbuf, 2) != 2) {
345                         dbg(1,"Using calculate_angle() with a less-than-two-coords-item?\n");
346                         map_rect_destroy(mr);
347                         return;
348                 }
349                 c = cbuf[0];
350                 cbuf[0] = cbuf[1];
351                 cbuf[1] = c;
352         }
353
354         map_rect_destroy(mr);
355
356         w->angle2=road_angle(&cbuf[1],&cbuf[0],0);
357 }
358
359 /**
360  * @brief Returns the time (in seconds) one will drive between two navigation items
361  *
362  * This function returns the time needed to drive between two items, including both of them,
363  * in seconds.
364  *
365  * @param from The first item
366  * @param to The last item
367  * @return The travel time in seconds, or -1 on error
368  */
369 static int
370 navigation_time(struct navigation_itm *from, struct navigation_itm *to)
371 {
372         struct navigation_itm *cur;
373         int time;
374
375         time = 0;
376         cur = from;
377         while (cur) {
378                 time += cur->time;
379
380                 if (cur == to) {
381                         break;
382                 }
383                 cur = cur->next;
384         }
385
386         if (!cur) {
387                 return -1;
388         }
389
390         return time;
391 }
392
393 /**
394  * @brief Clears the ways one can drive from itm
395  *
396  * @param itm The item that should have its ways cleared
397  */
398 static void
399 navigation_itm_ways_clear(struct navigation_itm *itm)
400 {
401         struct navigation_way *c,*n;
402
403         c = itm->ways;
404         while (c) {
405                 n = c->next;
406                 map_convert_free(c->name1);
407                 map_convert_free(c->name2);
408                 g_free(c);
409                 c = n;
410         }
411
412         itm->ways = NULL;
413 }
414
415 /**
416  * @brief Updates the ways one can drive from itm
417  *
418  * This updates the list of possible ways to drive to from itm. The item "itm" is on
419  * and the next navigation item are excluded.
420  *
421  * @param itm The item that should be updated
422  * @param graph_map The route graph's map that these items are on 
423  */
424 static void
425 navigation_itm_ways_update(struct navigation_itm *itm, struct map *graph_map) 
426 {
427         struct map_selection coord_sel;
428         struct map_rect *g_rect; // Contains a map rectangle from the route graph's map
429         struct item *i,*sitem;
430         struct attr sitem_attr,direction_attr;
431         struct navigation_way *w,*l;
432
433         navigation_itm_ways_clear(itm);
434
435         // These values cause the code in route.c to get us only the route graph point and connected segments
436         coord_sel.next = NULL;
437         coord_sel.u.c_rect.lu = itm->start;
438         coord_sel.u.c_rect.rl = itm->start;
439         // the selection's order is ignored
440         
441         g_rect = map_rect_new(graph_map, &coord_sel);
442         
443         i = map_rect_get_item(g_rect);
444         if (!i || i->type != type_rg_point) { // probably offroad? 
445                 return ;
446         }
447
448         w = NULL;
449         
450         while (1) {
451                 i = map_rect_get_item(g_rect);
452
453                 if (!i) {
454                         break;
455                 }
456                 
457                 if (i->type != type_rg_segment) {
458                         continue;
459                 }
460                 
461                 if (!item_attr_get(i,attr_street_item,&sitem_attr)) {
462                         dbg(1, "Got no street item for route graph item in entering_straight()\n");
463                         continue;
464                 }               
465
466                 if (!item_attr_get(i,attr_direction,&direction_attr)) {
467                         continue;
468                 }
469
470                 sitem = sitem_attr.u.item;
471                 if (item_is_equal(itm->item,*sitem) || ((itm->prev) && item_is_equal(itm->prev->item,*sitem))) {
472                         continue;
473                 }
474
475                 l = w;
476                 w = g_new(struct navigation_way, 1);
477                 w->dir = direction_attr.u.num;
478                 w->item = *sitem;
479                 w->next = l;
480                 calculate_angle(w);
481         }
482
483         map_rect_destroy(g_rect);
484         
485         itm->ways = w;
486 }
487
488 static void
489 navigation_destroy_itms_cmds(struct navigation *this_, struct navigation_itm *end)
490 {
491         struct navigation_itm *itm;
492         struct navigation_command *cmd;
493         dbg(2,"enter this_=%p this_->first=%p this_->cmd_first=%p end=%p\n", this_, this_->first, this_->cmd_first, end);
494         if (this_->cmd_first)
495                 dbg(2,"this_->cmd_first->itm=%p\n", this_->cmd_first->itm);
496         while (this_->first && this_->first != end) {
497                 itm=this_->first;
498                 dbg(3,"destroying %p\n", itm);
499                 item_hash_remove(this_->hash, &itm->item);
500                 this_->first=itm->next;
501                 if (this_->first)
502                         this_->first->prev=NULL;
503                 if (this_->cmd_first && this_->cmd_first->itm == itm->next) {
504                         cmd=this_->cmd_first;
505                         this_->cmd_first=cmd->next;
506                         if (cmd->next) {
507                                 cmd->next->prev = NULL;
508                         }
509                         g_free(cmd);
510                 }
511                 map_convert_free(itm->name1);
512                 map_convert_free(itm->name2);
513                 navigation_itm_ways_clear(itm);
514                 g_free(itm);
515         }
516         if (! this_->first)
517                 this_->last=NULL;
518         if (! this_->first && end) 
519                 dbg(0,"end wrong\n");
520         dbg(2,"ret this_->first=%p this_->cmd_first=%p\n",this_->first, this_->cmd_first);
521 }
522
523 static void
524 navigation_itm_update(struct navigation_itm *itm, struct item *ritem)
525 {
526         struct attr length, time;
527
528         if (! item_attr_get(ritem, attr_length, &length)) {
529                 dbg(0,"no length\n");
530                 return;
531         }
532         if (! item_attr_get(ritem, attr_time, &time)) {
533                 dbg(0,"no time\n");
534                 return;
535         }
536
537         dbg(1,"length=%d time=%d\n", length.u.num, time.u.num);
538         itm->length=length.u.num;
539         itm->time=time.u.num;
540 }
541
542 /**
543  * @brief This check if an item is part of a roundabout
544  *
545  * @param itm The item to be checked
546  * @return True if the item is part of a roundabout
547  */ 
548 static int
549 check_roundabout(struct navigation_itm *itm, struct map *graph_map)
550 {
551         struct map_selection coord_sel;
552         struct map_rect *g_rect; // Contains a map rectangle from the route graph's map
553         struct item *i,*sitem;
554         struct attr sitem_attr,flags_attr;
555
556         // These values cause the code in route.c to get us only the route graph point and connected segments
557         coord_sel.next = NULL;
558         coord_sel.u.c_rect.lu = itm->start;
559         coord_sel.u.c_rect.rl = itm->start;
560         // the selection's order is ignored
561         
562         g_rect = map_rect_new(graph_map, &coord_sel);
563         
564         i = map_rect_get_item(g_rect);
565         if (!i || i->type != type_rg_point) { // probably offroad? 
566                 return 0;
567         }
568
569         while (1) {
570                 i = map_rect_get_item(g_rect);
571
572                 if (!i) {
573                         break;
574                 }
575                 
576                 if (i->type != type_rg_segment) {
577                         continue;
578                 }
579                 
580                 if (!item_attr_get(i,attr_street_item,&sitem_attr)) {
581                         continue;
582                 }               
583
584                 sitem = sitem_attr.u.item;
585                 if (item_is_equal(itm->item,*sitem)) {
586                         if (item_attr_get(i,attr_flags,&flags_attr) && (flags_attr.u.num & AF_ROUNDABOUT)) {
587                                 map_rect_destroy(g_rect);
588                                 return 1;
589                         }
590                 }
591         }
592
593         map_rect_destroy(g_rect);
594         return 0;
595 }
596
597 static struct navigation_itm *
598 navigation_itm_new(struct navigation *this_, struct item *ritem)
599 {
600         struct navigation_itm *ret=g_new0(struct navigation_itm, 1);
601         int i=0;
602         struct item *sitem;
603         struct map *graph_map = NULL;
604         struct attr street_item,direction,route_attr;
605         struct map_rect *mr;
606         struct attr attr;
607         struct coord c[5];
608
609         if (ritem) {
610                 ret->streetname_told=0;
611                 if (! item_attr_get(ritem, attr_street_item, &street_item)) {
612                         dbg(0,"no street item\n");
613                         return NULL;
614                 }
615                 if (item_attr_get(ritem, attr_direction, &direction))
616                         ret->direction=direction.u.num;
617                 else
618                         ret->direction=0;
619
620                 sitem=street_item.u.item;
621                 ret->item=*sitem;
622                 item_hash_insert(this_->hash, sitem, ret);
623                 mr=map_rect_new(sitem->map, NULL);
624                 sitem=map_rect_get_item_byid(mr, sitem->id_hi, sitem->id_lo);
625                 if (item_attr_get(sitem, attr_street_name, &attr))
626                         ret->name1=map_convert_string(sitem->map,attr.u.str);
627                 if (item_attr_get(sitem, attr_street_name_systematic, &attr))
628                         ret->name2=map_convert_string(sitem->map,attr.u.str);
629                 navigation_itm_update(ret, ritem);
630
631                 while (item_coord_get(ritem, &c[i], 1)) {
632                         dbg(1, "coord %d 0x%x 0x%x\n", i, c[i].x ,c[i].y);
633
634                         if (i < 4) 
635                                 i++;
636                         else {
637                                 c[2]=c[3];
638                                 c[3]=c[4];
639                         }
640                 }
641                 dbg(1,"count=%d\n", i);
642                 i--;
643
644                 ret->angle_start=road_angle(&c[0], &c[1], 0);
645                 ret->angle_end=road_angle(&c[i-1], &c[i], 0);
646
647                 ret->start=c[0];
648                 ret->end=c[i];
649
650                 item_attr_get(ritem, attr_route, &route_attr);
651                 graph_map = route_get_graph_map(route_attr.u.route);
652                 if (check_roundabout(ret, graph_map)) {
653                         ret->flags |= AF_ROUNDABOUT;
654                 }
655
656                 dbg(1,"i=%d start %d end %d '%s' '%s'\n", i, ret->angle_start, ret->angle_end, ret->name1, ret->name2);
657                 map_rect_destroy(mr);
658         } else {
659                 if (this_->last)
660                         ret->start=ret->end=this_->last->end;
661         }
662         if (! this_->first)
663                 this_->first=ret;
664         if (this_->last) {
665                 this_->last->next=ret;
666                 ret->prev=this_->last;
667                 if (graph_map) {
668                         navigation_itm_ways_update(ret,graph_map);
669                 }
670         }
671         dbg(1,"ret=%p\n", ret);
672         this_->last=ret;
673         return ret;
674 }
675
676 /**
677  * @brief Counts how many times a driver could turn right/left 
678  *
679  * This function counts how many times the driver theoretically could
680  * turn right/left between two navigation items, not counting the final
681  * turn itself.
682  *
683  * @param from The navigation item which should form the start
684  * @param to The navigation item which should form the end
685  * @param direction Set to < 0 to count turns to the left >= 0 for turns to the right
686  * @return The number of possibilities to turn or -1 on error
687  */
688 static int
689 count_possible_turns(struct navigation_itm *from, struct navigation_itm *to, int direction)
690 {
691         int count;
692         struct navigation_itm *curr;
693         struct navigation_way *w;
694
695         count = 0;
696         curr = from->next;
697         while (curr && (curr != to)) {
698                 w = curr->ways;
699
700                 while (w) {
701                         if (direction < 0) {
702                                 if (angle_delta(curr->prev->angle_end, w->angle2) < 0) {
703                                         count++;
704                                         break;
705                                 }
706                         } else {
707                                 if (angle_delta(curr->prev->angle_end, w->angle2) > 0) {
708                                         count++;
709                                         break;
710                                 }                               
711                         }
712                         w = w->next;
713                 }
714                 curr = curr->next;
715         }
716
717         if (!curr) { // from does not lead to to?
718                 return -1;
719         }
720
721         return count;
722 }
723
724 /**
725  * @brief Calculates distance and time to the destination
726  *
727  * This function calculates the distance and the time to the destination of a
728  * navigation. If incr is set, this is only calculated for the first navigation
729  * item, which is a lot faster than re-calculation the whole destination, but works
730  * only if the rest of the navigation already has been calculated.
731  *
732  * @param this_ The navigation whose destination / time should be calculated
733  * @param incr Set this to true to only calculate the first item. See description.
734  */
735 static void
736 calculate_dest_distance(struct navigation *this_, int incr)
737 {
738         int len=0, time=0, count=0;
739         struct navigation_itm *next,*itm=this_->last;
740         dbg(1, "enter this_=%p, incr=%d\n", this_, incr);
741         if (incr) {
742                 if (itm)
743                         dbg(2, "old values: (%p) time=%d lenght=%d\n", itm, itm->dest_length, itm->dest_time);
744                 else
745                         dbg(2, "old values: itm is null\n");
746                 itm=this_->first;
747                 next=itm->next;
748                 dbg(2, "itm values: time=%d lenght=%d\n", itm->length, itm->time);
749                 dbg(2, "next values: (%p) time=%d lenght=%d\n", next, next->dest_length, next->dest_time);
750                 itm->dest_length=next->dest_length+itm->length;
751                 itm->dest_count=next->dest_count+1;
752                 itm->dest_time=next->dest_time+itm->time;
753                 dbg(2, "new values: time=%d lenght=%d\n", itm->dest_length, itm->dest_time);
754                 return;
755         }
756         while (itm) {
757                 len+=itm->length;
758                 time+=itm->time;
759                 itm->dest_length=len;
760                 itm->dest_time=time;
761                 itm->dest_count=count++;
762                 itm=itm->prev;
763         }
764         dbg(1,"len %d time %d\n", len, time);
765 }
766
767 /**
768  * @brief Checks if two navigation items are on the same street
769  *
770  * This function checks if two navigation items are on the same street. It returns
771  * true if either their name or their "systematic name" (e.g. "A6" or "B256") are the
772  * same.
773  *
774  * @param old The first item to be checked
775  * @param new The second item to be checked
776  * @return True if both old and new are on the same street
777  */
778 static int
779 is_same_street2(char *old_name1, char *old_name2, char *new_name1, char *new_name2)
780 {
781         if (old_name1 && new_name1 && !strcmp(old_name1, new_name1)) {
782                 dbg(1,"is_same_street: '%s' '%s' vs '%s' '%s' yes (1.)\n", old_name2, new_name2, old_name1, new_name1);
783                 return 1;
784         }
785         if (old_name2 && new_name2 && !strcmp(old_name2, new_name2)) {
786                 dbg(1,"is_same_street: '%s' '%s' vs '%s' '%s' yes (2.)\n", old_name2, new_name2, old_name1, new_name1);
787                 return 1;
788         }
789         dbg(1,"is_same_street: '%s' '%s' vs '%s' '%s' no\n", old_name2, new_name2, old_name1, new_name1);
790         return 0;
791 }
792
793 #if 0
794 /**
795  * @brief Checks if two navigation items are on the same street
796  *
797  * This function checks if two navigation items are on the same street. It returns
798  * true if the first part of their "systematic name" is equal. If the "systematic name" is
799  * for example "A352/E3" (a german highway which at the same time is part of the international
800  * E-road network), it would only search for "A352" in the second item's systematic name.
801  *
802  * @param old The first item to be checked
803  * @param new The second item to be checked
804  * @return True if the "systematic name" of both items matches. See description.
805  */
806 static int
807 is_same_street_systematic(struct navigation_itm *old, struct navigation_itm *new)
808 {
809         int slashold,slashnew;
810         if (!old->name2 || !new->name2)
811                 return 1;
812         slashold=strcspn(old->name2, "/");
813         slashnew=strcspn(new->name2, "/");
814         if (slashold != slashnew || strncmp(old->name2, new->name2, slashold))
815                 return 0;
816         return 1;
817 }
818
819
820 /**
821  * @brief Check if there are multiple possibilities to drive from old
822  *
823  * This function checks, if there are multiple streets connected to the exit of "old".
824  * Sometimes it happens that an item on a map is just segmented, without any other streets
825  * being connected there, and it is not useful if navit creates a maneuver there.
826  *
827  * @param new The navigation item we're driving to
828  * @return True if there are multiple streets
829  */
830 static int 
831 maneuver_multiple_streets(struct navigation_itm *new)
832 {
833         if (new->ways) {
834                 return 1;
835         } else {
836                 return 0;
837         }
838 }
839
840
841 /**
842  * @brief Check if the new item is entered "straight"
843  *
844  * This function checks if the new item is entered "straight" from the old item, i.e. if there
845  * is no other street one could take from the old item on with less steering.
846  *
847  * @param new The navigation item we're driving to
848  * @param diff The absolute angle one needs to steer to drive to this item
849  * @return True if the new item is entered "straight"
850  */
851 static int 
852 maneuver_straight(struct navigation_itm *new, int diff)
853 {
854         int curr_diff;
855         struct navigation_way *w;
856
857         w = new->ways;
858         dbg(1,"diff=%d\n", diff);
859         while (w) {
860                 curr_diff=abs(angle_delta(new->prev->angle_end, w->angle2));
861                 dbg(1,"curr_diff=%d\n", curr_diff);
862                 if (curr_diff < diff) {
863                         return 0;
864                 }
865                 w = w->next;
866         }
867         return 1;
868 }
869 #endif
870
871 static int maneuver_category(enum item_type type)
872 {
873         switch (type) {
874         case type_street_0:
875                 return 1;
876         case type_street_1_city:
877                 return 2;
878         case type_street_2_city:
879                 return 3;
880         case type_street_3_city:
881                 return 4;
882         case type_street_4_city:
883                 return 5;
884         case type_highway_city:
885                 return 7;
886         case type_street_1_land:
887                 return 2;
888         case type_street_2_land:
889                 return 3;
890         case type_street_3_land:
891                 return 4;
892         case type_street_4_land:
893                 return 5;
894         case type_street_n_lanes:
895                 return 6;
896         case type_highway_land:
897                 return 7;
898         case type_ramp:
899                 return 0;
900         case type_roundabout:
901                 return 0;
902         case type_ferry:
903                 return 0;
904         default:
905                 return 0;
906         }
907         
908         
909 }
910
911 static int
912 is_way_allowed(struct navigation_way *way)
913 {
914         if (way->dir > 0) {
915                 if (way->flags & AF_ONEWAYREV)
916                         return 0;
917         } else {
918                 if (way->flags & AF_ONEWAY)
919                         return 0;
920         }
921         return 1;
922 }
923
924 /**
925  * @brief Checks if navit has to create a maneuver to drive from old to new
926  *
927  * This function checks if it has to create a "maneuver" - i.e. guide the user - to drive 
928  * from "old" to "new".
929  *
930  * @param old The old navigation item, where we're coming from
931  * @param new The new navigation item, where we're going to
932  * @param delta The angle the user has to steer to navigate from old to new
933  * @param reason A text string explaining how the return value resulted
934  * @return True if navit should guide the user, false otherwise
935  */
936 static int
937 maneuver_required2(struct navigation_itm *old, struct navigation_itm *new, int *delta, char **reason)
938 {
939         int ret=0,d,dw,dlim;
940         char *r=NULL;
941         struct navigation_way *w;
942         int cat,ncat,wcat,maxcat,left=-180,right=180,is_unambigous=0,is_same_street;
943
944         dbg(1,"enter %p %p %p\n",old, new, delta);
945         d=angle_delta(old->angle_end, new->angle_start);
946         if (!new->ways) {
947                 /* No announcement necessary */
948                 r="no: Only one possibility";
949         } else if (!new->ways->next && new->ways->item.type == type_ramp) {
950                 /* If the other way is only a ramp and it is one-way in the wrong direction, no announcement necessary */
951                 /* TODO: check for one way in wrong direction */
952                 r="no: Only ramp";
953         }
954         if (! r) {
955                 if ((old->flags & AF_ROUNDABOUT) && ! (new->flags & AF_ROUNDABOUT)) {
956                         r="yes: leaving roundabout";
957                         ret=1;
958                 } else  if (!(old->flags & AF_ROUNDABOUT) && (new->flags & AF_ROUNDABOUT)) 
959                         r="no: entering roundabout";
960                 else if ((old->flags & AF_ROUNDABOUT) && (new->flags & AF_ROUNDABOUT)) 
961                         r="no: staying in roundabout";
962         }
963         if (!r && abs(d) > 75) {
964                 /* always make an announcement if you have to make a sharp turn */
965                 r="yes: delta over 75";
966                 ret=1;
967         }
968         cat=maneuver_category(old->item.type);
969         ncat=maneuver_category(new->item.type);
970         if (!r) {
971                 /* Check whether the street keeps its name */
972                 is_same_street=is_same_street2(old->name1, old->name2, new->name1, new->name2);
973                 w = new->ways;
974                 maxcat=-1;
975                 while (w) {
976                         dw=angle_delta(old->angle_end, w->angle2);
977                         if (dw < 0) {
978                                 if (dw > left)
979                                         left=dw;
980                         } else {
981                                 if (dw < right)
982                                         right=dw;
983                         }
984                         wcat=maneuver_category(w->item.type);
985                         /* If any other street has the same name but isn't a highway (a highway might split up temporarily), then
986                            we can't use the same name criterium  */
987                         if (is_same_street && is_same_street2(old->name1, old->name2, w->name1, w->name2) && (cat != 7 || wcat != 7) && is_way_allowed(w))
988                                 is_same_street=0;
989                         /* Mark if the street has a higher or the same category */
990                         if (wcat > maxcat)
991                                 maxcat=wcat;
992                         w = w->next;
993                 }
994                 /* get the delta limit for checking for other streets. It is lower if the street has no other
995                    streets of the same or higher category */
996                 if (ncat < cat)
997                         dlim=80;
998                 else
999                         dlim=120;
1000                 /* if the street is really straight, the others might be closer to straight */
1001                 if (abs(d) < 20)
1002                         dlim/=2;
1003                 if ((maxcat == ncat && maxcat == cat) || (ncat == 0 && cat == 0)) 
1004                         dlim=abs(d)*620/256;
1005                 else if (maxcat < ncat && maxcat < cat)
1006                         dlim=abs(d)*128/256;
1007                 if (left < -dlim && right > dlim) 
1008                         is_unambigous=1;
1009                 if (!is_same_street && is_unambigous < 1) {
1010                         ret=1;
1011                         r="yes: not same street or ambigous";
1012                 } else
1013                         r="no: same street and unambigous";
1014 #ifdef DEBUG
1015                 r=g_strdup_printf("yes: d %d left %d right %d dlim=%d cat old:%d new:%d max:%d unambigous=%d same_street=%d", d, left, right, dlim, cat, ncat, maxcat, is_unambigous, is_same_street);
1016 #endif
1017         }
1018         *delta=d;
1019         if (reason)
1020                 *reason=r;
1021         return ret;
1022         
1023
1024 #if 0
1025         if (new->item.type == old->item.type || (new->item.type != type_ramp && old->item.type != type_ramp)) {
1026                 if (is_same_street2(old, new)) {
1027                         if (! entering_straight(new, abs(*delta))) {
1028                                 dbg(1, "maneuver_required: Not driving straight: yes\n");
1029                                 if (reason)
1030                                         *reason="yes: Not driving straight";
1031                                 return 1;
1032                         }
1033
1034                         if (check_multiple_streets(new)) {
1035                                 if (entering_straight(new,abs(*delta)*2)) {
1036                                         if (reason)
1037                                                 *reason="no: delta < ext_limit for same name";
1038                                         return 0;
1039                                 }
1040                                 if (reason)     
1041                                         *reason="yes: delta > ext_limit for same name";
1042                                 return 1;
1043                         } else {
1044                                 dbg(1, "maneuver_required: Staying on the same street: no\n");
1045                                 if (reason)
1046                                         *reason="no: Staying on same street";
1047                                 return 0;
1048                         }
1049                 }
1050         } else
1051                 dbg(1, "maneuver_required: old or new is ramp\n");
1052 #if 0
1053         if (old->item.type == type_ramp && (new->item.type == type_highway_city || new->item.type == type_highway_land)) {
1054                 dbg(1, "no_maneuver_required: old is ramp new is highway\n");
1055                 if (reason)
1056                         *reason="no: old is ramp new is highway";
1057                 return 0;
1058         }
1059 #endif
1060 #if 0
1061         if (old->crossings_end == 2) {
1062                 dbg(1, "maneuver_required: only 2 connections: no\n");
1063                 return 0;
1064         }
1065 #endif
1066         dbg(1,"delta=%d-%d=%d\n", new->angle_start, old->angle_end, *delta);
1067         if ((new->item.type == type_highway_land || new->item.type == type_highway_city || old->item.type == type_highway_land || old->item.type == type_highway_city) && (!is_same_street_systematic(old, new) || (old->name2 != NULL && new->name2 == NULL))) {
1068                 dbg(1, "maneuver_required: highway changed name\n");
1069                 if (reason)
1070                         *reason="yes: highway changed name";
1071                 return 1;
1072         }
1073         if (abs(*delta) < straight_limit) {
1074                 if (! entering_straight(new,abs(*delta))) {
1075                         if (reason)
1076                                 *reason="yes: not straight";
1077                         dbg(1, "maneuver_required: not driving straight: yes\n");
1078                         return 1;
1079                 }
1080
1081                 dbg(1, "maneuver_required: delta(%d) < %d: no\n", *delta, straight_limit);
1082                 if (reason)
1083                         *reason="no: delta < limit";
1084                 return 0;
1085         }
1086         if (abs(*delta) < ext_straight_limit) {
1087                 if (entering_straight(new,abs(*delta)*2)) {
1088                         if (reason)
1089                                 *reason="no: delta < ext_limit";
1090                         return 0;
1091                 }
1092         }
1093
1094         if (! check_multiple_streets(new)) {
1095                 dbg(1, "maneuver_required: only one possibility: no\n");
1096                 if (reason)
1097                         *reason="no: only one possibility";
1098                 return 0;
1099         }
1100
1101         dbg(1, "maneuver_required: delta=%d: yes\n", *delta);
1102         if (reason)
1103                 *reason="yes: delta >= limit";
1104         return 1;
1105 #endif
1106 }
1107
1108 static struct navigation_command *
1109 command_new(struct navigation *this_, struct navigation_itm *itm, int delta)
1110 {
1111         struct navigation_command *ret=g_new0(struct navigation_command, 1);
1112         dbg(1,"enter this_=%p itm=%p delta=%d\n", this_, itm, delta);
1113         ret->delta=delta;
1114         ret->itm=itm;
1115         if (itm && itm->prev && !(itm->flags & AF_ROUNDABOUT) && (itm->prev->flags & AF_ROUNDABOUT)) {
1116                 int len=0;
1117                 int angle;
1118                 struct navigation_itm *itm2=itm->prev;
1119                 while (itm2 && (itm2->flags & AF_ROUNDABOUT)) {
1120                         len+=itm2->length;
1121                         angle=itm2->angle_end;
1122                         itm2=itm2->prev;
1123                 }
1124                 if (itm2) 
1125                         ret->roundabout_delta=angle_delta(itm2->angle_end, itm->angle_start);
1126                 else {
1127                         if (delta > 0) 
1128                                 angle-=90;
1129                         else
1130                                 angle+=90;
1131                         ret->roundabout_delta=angle_delta(angle % 360, itm->angle_start);
1132                 }
1133                 ret->length=len;
1134                 
1135         }
1136         if (this_->cmd_last) {
1137                 this_->cmd_last->next=ret;
1138                 ret->prev = this_->cmd_last;
1139         }
1140         this_->cmd_last=ret;
1141
1142         if (!this_->cmd_first)
1143                 this_->cmd_first=ret;
1144         return ret;
1145 }
1146
1147 static void
1148 make_maneuvers(struct navigation *this_, struct route *route)
1149 {
1150         struct navigation_itm *itm, *last=NULL, *last_itm=NULL;
1151         int delta;
1152         itm=this_->first;
1153         this_->cmd_last=NULL;
1154         this_->cmd_first=NULL;
1155         while (itm) {
1156                 if (last) {
1157                         if (maneuver_required2(last_itm, itm,&delta,NULL)) {
1158                                 command_new(this_, itm, delta);
1159                         }
1160                 } else
1161                         last=itm;
1162                 last_itm=itm;
1163                 itm=itm->next;
1164         }
1165         command_new(this_, last_itm, 0);
1166 }
1167
1168 static int
1169 contains_suffix(char *name, char *suffix)
1170 {
1171         if (!suffix)
1172                 return 0;
1173         if (strlen(name) < strlen(suffix))
1174                 return 0;
1175         return !strcasecmp(name+strlen(name)-strlen(suffix), suffix);
1176 }
1177
1178 static char *
1179 replace_suffix(char *name, char *search, char *replace)
1180 {
1181         int len=strlen(name)-strlen(search);
1182         char *ret=g_malloc(len+strlen(replace)+1);
1183         strncpy(ret, name, len);
1184         strcpy(ret+len, replace);
1185
1186         return ret;
1187 }
1188
1189 static char *
1190 navigation_item_destination(struct navigation_itm *itm, struct navigation_itm *next, char *prefix)
1191 {
1192         char *ret=NULL,*name1,*sep,*name2;
1193         int i,sex;
1194         if (! prefix)
1195                 prefix="";
1196         if(!itm->name1 && !itm->name2 && itm->item.type == type_ramp) {
1197                 dbg(1,">> Next is ramp %lx current is %lx \n", itm->item.type, next->item.type);
1198                          
1199                 if(next->item.type == type_ramp)
1200                         return NULL;
1201                 if(itm->item.type == type_highway_city || itm->item.type == type_highway_land )
1202                         return g_strdup_printf("%s%s",prefix,_("exit"));        /* %FIXME Can this even be reached? */                   
1203                 else
1204                         return g_strdup_printf("%s%s",prefix,_("into the ramp"));
1205                 
1206         }
1207         if (!itm->name1 && !itm->name2)
1208                 return NULL;
1209         if (itm->name1) {
1210                 sex=-1;
1211                 name1=NULL;
1212                 for (i = 0 ; i < sizeof(suffixes)/sizeof(suffixes[0]) ; i++) {
1213                         if (contains_suffix(itm->name1,suffixes[i].fullname)) {
1214                                 sex=suffixes[i].sex;
1215                                 name1=g_strdup(itm->name1);
1216                                 break;
1217                         }
1218                         if (contains_suffix(itm->name1,suffixes[i].abbrev)) {
1219                                 sex=suffixes[i].sex;
1220                                 name1=replace_suffix(itm->name1, suffixes[i].abbrev, suffixes[i].fullname);
1221                                 break;
1222                         }
1223                 }
1224                 if (itm->name2) {
1225                         name2=itm->name2;
1226                         sep=" ";
1227                 } else {
1228                         name2="";
1229                         sep="";
1230                 }
1231                 switch (sex) {
1232                 case -1:
1233                         /* TRANSLATORS: Arguments: 1: Prefix (Space if required) 2: Street Name 3: Separator (Space if required), 4: Systematic Street Name */
1234                         ret=g_strdup_printf(_("%sinto the street %s%s%s"),prefix,itm->name1, sep, name2);
1235                         break;
1236                 case 1:
1237                         /* TRANSLATORS: Arguments: 1: Prefix (Space if required) 2: Street Name 3: Separator (Space if required), 4: Systematic Street Name. Male form. The stuff after | doesn't have to be included */
1238                         ret=g_strdup_printf(_("%sinto the %s%s%s|male form"),prefix,name1, sep, name2);
1239                         break;
1240                 case 2:
1241                         /* TRANSLATORS: Arguments: 1: Prefix (Space if required) 2: Street Name 3: Separator (Space if required), 4: Systematic Street Name. Female form. The stuff after | doesn't have to be included */
1242                         ret=g_strdup_printf(_("%sinto the %s%s%s|female form"),prefix,name1, sep, name2);
1243                         break;
1244                 case 3:
1245                         /* TRANSLATORS: Arguments: 1: Prefix (Space if required) 2: Street Name 3: Separator (Space if required), 4: Systematic Street Name. Neutral form. The stuff after | doesn't have to be included */
1246                         ret=g_strdup_printf(_("%sinto the %s%s%s|neutral form"),prefix,name1, sep, name2);
1247                         break;
1248                 }
1249                 g_free(name1);
1250                         
1251         } else
1252                 /* TRANSLATORS: gives the name of the next road to turn into (into the E17) */
1253                 ret=g_strdup_printf(_("%sinto the %s"),prefix,itm->name2);
1254         name1=ret;
1255         while (*name1) {
1256                 switch (*name1) {
1257                 case '|':
1258                         *name1='\0';
1259                         break;
1260                 case '/':
1261                         *name1++=' ';
1262                         break;
1263                 default:
1264                         name1++;
1265                 }
1266         }
1267         return ret;
1268 }
1269
1270 static char *
1271 show_maneuver(struct navigation *nav, struct navigation_itm *itm, struct navigation_command *cmd, enum attr_type type, int connect)
1272 {
1273         /* TRANSLATORS: right, as in 'Turn right' */
1274         char *dir=_("right"),*strength="";
1275         int distance=itm->dest_length-cmd->itm->dest_length;
1276         char *d,*ret;
1277         int delta=cmd->delta;
1278         int level;
1279         int strength_needed;
1280         int skip_roads;
1281         int count_roundabout;
1282         struct navigation_itm *cur;
1283         struct navigation_way *w;
1284         
1285         if (connect) {
1286                 level = -2; // level = -2 means "connect to another maneuver via 'then ...'"
1287         } else {
1288                 level=1;
1289         }
1290
1291         w = itm->next->ways;
1292         strength_needed = 0;
1293         if ((itm->next->angle_start - itm->angle_end) < 0) {
1294                 while (w) {
1295                         if (w->angle2-itm->next->angle_start < 0) {
1296                                 strength_needed = 1;
1297                                 break;
1298                         }
1299                         w = w->next;
1300                 }
1301         } else {
1302                 while (w) {
1303                         if (w->angle2-itm->next->angle_start > 0) {
1304                                 strength_needed = 1;
1305                                 break;
1306                         }
1307                         w = w->next;
1308                 }
1309         }
1310
1311         if (delta < 0) {
1312                 /* TRANSLATORS: left, as in 'Turn left' */
1313                 dir=_("left");
1314                 delta=-delta;
1315         }
1316
1317         if (strength_needed) {
1318                 if (delta < 45) {
1319                         /* TRANSLATORS: Don't forget the ending space */
1320                         strength=_("easily ");
1321                 } else if (delta < 105) {
1322                         strength="";
1323                 } else if (delta < 165) {
1324                         /* TRANSLATORS: Don't forget the ending space */
1325                         strength=_("strongly ");
1326                 } else {
1327                         dbg(1,"delta=%d\n", delta);
1328                         /* TRANSLATORS: Don't forget the ending space */
1329                         strength=_("unknown ");
1330                 }
1331         }
1332         if (type != attr_navigation_long_exact) 
1333                 distance=round_distance(distance);
1334         if (type == attr_navigation_speech) {
1335                 if (nav->turn_around && nav->turn_around == nav->turn_around_limit) 
1336                         return g_strdup(_("When possible, please turn around"));
1337                 if (!connect) {
1338                         level=navigation_get_announce_level(nav, itm->item.type, distance-cmd->length);
1339                 }
1340                 dbg(1,"distance=%d level=%d type=0x%x\n", distance, level, itm->item.type);
1341         }
1342
1343         if (cmd->itm->prev->flags & AF_ROUNDABOUT) {
1344                 switch (level) {
1345                 case 2:
1346                         return g_strdup(_("Enter the roundabout soon"));
1347                 case 1:
1348                         d = get_distance(distance, type, 1);
1349                         ret = g_strdup_printf(_("In %s, enter the roundabout"), d);
1350                         g_free(d);
1351                         return ret;
1352                 case -2:
1353                 case 0:
1354                         cur = cmd->itm->prev;
1355                         count_roundabout = 0;
1356                         while (cur && (cur->flags & AF_ROUNDABOUT)) {
1357                                 if (cur->next->ways && is_way_allowed(cur->next->ways)) { // If the next segment has no exit or the exit isn't allowed, don't count it
1358                                         count_roundabout++;
1359                                 }
1360                                 cur = cur->prev;
1361                         }
1362                         switch (level) {
1363                         case 0:
1364                                 ret = g_strdup_printf(_("Leave the roundabout at the %s exit"), get_count_str(count_roundabout));
1365                                 break;
1366                         case -2:
1367                                 ret = g_strdup_printf(_("then leave the roundabout at the %s exit"), get_count_str(count_roundabout));
1368                                 break;
1369                         }
1370                         return ret;
1371                 }
1372         }
1373
1374         switch(level) {
1375         case 3:
1376                 d=get_distance(distance, type, 1);
1377                 ret=g_strdup_printf(_("Follow the road for the next %s"), d);
1378                 g_free(d);
1379                 return ret;
1380         case 2:
1381                 d=g_strdup(_("soon"));
1382                 break;
1383         case 1:
1384                 d=get_distance(distance, type, 0);
1385                 break;
1386         case 0:
1387                 skip_roads = count_possible_turns(nav->first,cmd->itm,cmd->delta);
1388                 if (skip_roads > 0) {
1389                         if (get_count_str(skip_roads+1)) {
1390                                 /* TRANSLATORS: First argument is the how manieth street to take, second the direction */ 
1391                                 ret = g_strdup_printf(_("Take the %1$s road to the %2$s"), get_count_str(skip_roads+1), dir);
1392                                 return ret;
1393                         } else {
1394                                 d = g_strdup_printf(_("after %i roads"), skip_roads);
1395                         }
1396                 } else {
1397                         d=g_strdup(_("now"));
1398                 }
1399                 break;
1400         case -2:
1401                 skip_roads = count_possible_turns(cmd->prev->itm,cmd->itm,cmd->delta);
1402                 if (skip_roads > 0) {
1403                         /* TRANSLATORS: First argument is the how manieth street to take, second the direction */ 
1404                         if (get_count_str(skip_roads+1)) {
1405                                 ret = g_strdup_printf(_("then take the %1$s road to the %2$s"), get_count_str(skip_roads+1), dir);
1406                                 return ret;
1407                         } else {
1408                                 d = g_strdup_printf(_("after %i roads"), skip_roads);
1409                         }
1410
1411                 } else {
1412                         d = g_strdup_printf("");
1413                 }
1414                 break;
1415         default:
1416                 d=g_strdup(_("error"));
1417         }
1418         if (cmd->itm->next) {
1419                 int tellstreetname = 0;
1420                 char *destination = NULL; 
1421  
1422                 if(type == attr_navigation_speech) { // In voice mode
1423                         // In Voice Mode only tell the street name in level 1 or in level 0 if level 1
1424                         // was skipped
1425
1426                         if (level == 1) { // we are close to the intersection
1427                                 cmd->itm->streetname_told = 1; // remeber to be checked when we turn
1428                                 tellstreetname = 1; // Ok so we tell the name of the street 
1429                         }
1430
1431                         if (level == 0) {
1432                                 if(cmd->itm->streetname_told == 0) // we are right at the intersection
1433                                         tellstreetname = 1; 
1434                                 else
1435                                         cmd->itm->streetname_told = 0;  // reset just in case we come to the same street again
1436                         }
1437
1438                 }
1439                 else
1440                      tellstreetname = 1;
1441
1442                 if(tellstreetname) 
1443                         destination=navigation_item_destination(cmd->itm, itm, " ");
1444                 if (level != -2) {
1445                         /* TRANSLATORS: The first argument is strength, the second direction, the third distance and the fourth destination Example: 'Turn 'slightly' 'left' in '100 m' 'onto baker street' */
1446                         ret=g_strdup_printf(_("Turn %1$s%2$s %3$s%4$s"), strength, dir, d, destination ? destination:"");
1447                 } else {
1448                         /* TRANSLATORS: First argument is strength, second direction, third how many roads to skip, fourth destination */
1449                         ret=g_strdup_printf(_("then turn %1$s%2$s %3$s%4$s"), strength, dir, d, destination ? destination:"");
1450                 }
1451                 g_free(destination);
1452         } else {
1453                 if (!connect) {
1454                         ret=g_strdup_printf(_("You have reached your destination %s"), d);
1455                 } else {
1456                         ret=g_strdup_printf(_("then you have reached your destination."));
1457                 }
1458         }
1459         g_free(d);
1460         return ret;
1461 }
1462
1463 /**
1464  * @brief Creates announcements for maneuvers, plus maneuvers immediately following the next maneuver
1465  *
1466  * This function does create an announcement for the current maneuver and for maneuvers
1467  * immediately following that maneuver, if these are too close and we're in speech navigation.
1468  *
1469  * @return An announcement that should be made
1470  */
1471 static char *
1472 show_next_maneuvers(struct navigation *nav, struct navigation_itm *itm, struct navigation_command *cmd, enum attr_type type)
1473 {
1474         struct navigation_command *cur,*prev;
1475         int distance=itm->dest_length-cmd->itm->dest_length;
1476         int l0_dist,level,dist,i,time;
1477         int speech_time,time2nav;
1478         char *ret,*old,*buf,*next;
1479
1480         if (type != attr_navigation_speech) {
1481                 return show_maneuver(nav, itm, cmd, type, 0); // We accumulate maneuvers only in speech navigation
1482         }
1483
1484         level=navigation_get_announce_level(nav, itm->item.type, distance-cmd->length);
1485
1486         if (level > 1) {
1487                 return show_maneuver(nav, itm, cmd, type, 0); // We accumulate maneuvers only if they are close
1488         }
1489
1490         if (cmd->itm->told) {
1491                 return "";
1492         }
1493
1494         ret = show_maneuver(nav, itm, cmd, type, 0);
1495         time2nav = navigation_time(itm,cmd->itm->prev);
1496         old = NULL;
1497
1498         cur = cmd->next;
1499         prev = cmd;
1500         i = 0;
1501         while (cur && cur->itm) {
1502                 // We don't merge more than 3 announcements...
1503                 if (i > 1) { // if you change this, please also change the value below, that is used to terminate the loop
1504                         break;
1505                 }
1506                 
1507                 next = show_maneuver(nav,prev->itm, cur, type, 0);
1508                 speech_time = navit_speech_estimate(nav->navit,next);
1509                 g_free(next);
1510
1511                 if (speech_time == -1) { // user didn't set cps
1512                         speech_time = 30; // assume 3 seconds
1513                 }
1514
1515                 dist = prev->itm->dest_length - cur->itm->dest_length;
1516                 time = navigation_time(prev->itm,cur->itm->prev);
1517
1518                 if (time >= (speech_time + 30)) { // 3 seconds for understanding what has been said
1519                         printf("Time: %i, speech_time: %i\n", time, speech_time);
1520                         break;
1521                 }
1522
1523                 old = ret;
1524                 buf = show_maneuver(nav, prev->itm, cur, type, 1);
1525                 ret = g_strdup_printf("%s, %s", old, buf);
1526                 g_free(buf);
1527                 if (navit_speech_estimate(nav->navit,ret) > time2nav) {
1528                         g_free(ret);
1529                         ret = old;
1530                         i = 2; // This will terminate the loop
1531                 } else {
1532                         g_free(old);
1533                 }
1534
1535                 // If the two maneuvers are *really* close, we shouldn't tell the second one again, because TTS won't be fast enough
1536                 if (time <= speech_time) {
1537                         cur->itm->told = 1;
1538                 }
1539
1540                 prev = cur;
1541                 cur = cur->next;
1542                 i++;
1543         }
1544
1545         return ret;
1546 }
1547
1548 static void
1549 navigation_call_callbacks(struct navigation *this_, int force_speech)
1550 {
1551         int distance, level = 0, level2;
1552         void *p=this_;
1553         if (!this_->cmd_first)
1554                 return;
1555         callback_list_call(this_->callback, 1, &p);
1556         dbg(1,"force_speech=%d turn_around=%d turn_around_limit=%d\n", force_speech, this_->turn_around, this_->turn_around_limit);
1557         distance=round_distance(this_->first->dest_length-this_->cmd_first->itm->dest_length);
1558         if (this_->turn_around_limit && this_->turn_around == this_->turn_around_limit) {
1559                 dbg(1,"distance=%d distance_turn=%d\n", distance, this_->distance_turn);
1560                 while (distance > this_->distance_turn) {
1561                         this_->level_last=4;
1562                         level=4;
1563                         force_speech=1;
1564                         if (this_->distance_turn >= 500)
1565                                 this_->distance_turn*=2;
1566                         else
1567                                 this_->distance_turn=500;
1568                 }
1569         } else if (!this_->turn_around_limit || this_->turn_around == -this_->turn_around_limit+1) {
1570                 this_->distance_turn=50;
1571                 distance-=this_->cmd_first->length;
1572                 level=navigation_get_announce_level(this_, this_->first->item.type, distance);
1573                 if (this_->cmd_first->itm->prev) {
1574                         level2=navigation_get_announce_level(this_, this_->cmd_first->itm->prev->item.type, distance);
1575                         if (level2 > level)
1576                                 level=level2;
1577                 }
1578                 if (level < this_->level_last) {
1579                         dbg(1,"level %d < %d\n", level, this_->level_last);
1580                         this_->level_last=level;
1581                         force_speech=1;
1582                 }
1583                 if (!item_is_equal(this_->cmd_first->itm->item, this_->item_last)) {
1584                         dbg(1,"item different\n");
1585                         this_->item_last=this_->cmd_first->itm->item;
1586                         force_speech=1;
1587                 }
1588         }
1589         if (force_speech) {
1590                 this_->level_last=level;
1591                 dbg(1,"distance=%d level=%d type=0x%x\n", distance, level, this_->first->item.type);
1592                 callback_list_call(this_->callback_speech, 1, &p);
1593         }
1594 }
1595
1596 void
1597 navigation_update(struct navigation *this_, struct route *route)
1598 {
1599         struct map *map;
1600         struct map_rect *mr;
1601         struct item *ritem;                     /* Holds an item from the route map */
1602         struct item *sitem;                     /* Holds the corresponding item from the actual map */
1603         struct attr street_item,street_direction;
1604         struct navigation_itm *itm;
1605         int incr=0,first=1;
1606
1607         if (! route)
1608                 return;
1609         map=route_get_map(route);
1610         if (! map)
1611                 return;
1612         mr=map_rect_new(map, NULL);
1613         if (! mr)
1614                 return;
1615         dbg(1,"enter\n");
1616         while ((ritem=map_rect_get_item(mr))) {
1617                 if (first && item_attr_get(ritem, attr_street_item, &street_item)) {
1618                         first=0;
1619                         if (!item_attr_get(ritem, attr_direction, &street_direction))
1620                                 street_direction.u.num=0;
1621                         sitem=street_item.u.item;
1622                         dbg(1,"sitem=%p\n", sitem);
1623                         itm=item_hash_lookup(this_->hash, sitem);
1624                         dbg(2,"itm for item with id (0x%x,0x%x) is %p\n", sitem->id_hi, sitem->id_lo, itm);
1625                         if (itm && itm->direction != street_direction.u.num) {
1626                                 dbg(2,"wrong direction\n");
1627                                 itm=NULL;
1628                         }
1629                         navigation_destroy_itms_cmds(this_, itm);
1630                         if (itm) {
1631                                 navigation_itm_update(itm, ritem);
1632                                 break;
1633                         }
1634                         dbg(1,"not on track\n");
1635                 }
1636                 navigation_itm_new(this_, ritem);
1637         }
1638         if (first) 
1639                 navigation_destroy_itms_cmds(this_, NULL);
1640         else {
1641                 if (! ritem) {
1642                         navigation_itm_new(this_, NULL);
1643                         make_maneuvers(this_,route);
1644                 }
1645                 calculate_dest_distance(this_, incr);
1646                 dbg(2,"destination distance old=%d new=%d\n", this_->distance_last, this_->first->dest_length);
1647                 if (this_->first->dest_length > this_->distance_last && this_->distance_last >= 0) 
1648                         this_->turn_around++;
1649                 else
1650                         this_->turn_around--;
1651                 if (this_->turn_around > this_->turn_around_limit)
1652                         this_->turn_around=this_->turn_around_limit;
1653                 else if (this_->turn_around < -this_->turn_around_limit+1)
1654                         this_->turn_around=-this_->turn_around_limit+1;
1655                 dbg(2,"turn_around=%d\n", this_->turn_around);
1656                 this_->distance_last=this_->first->dest_length;
1657                 profile(0,"end");
1658                 navigation_call_callbacks(this_, FALSE);
1659         }
1660         map_rect_destroy(mr);
1661 }
1662
1663 void
1664 navigation_flush(struct navigation *this_)
1665 {
1666         navigation_destroy_itms_cmds(this_, NULL);
1667 }
1668
1669
1670 void
1671 navigation_destroy(struct navigation *this_)
1672 {
1673         navigation_flush(this_);
1674         item_hash_destroy(this_->hash);
1675         callback_list_destroy(this_->callback);
1676         callback_list_destroy(this_->callback_speech);
1677         g_free(this_);
1678 }
1679
1680 int
1681 navigation_register_callback(struct navigation *this_, enum attr_type type, struct callback *cb)
1682 {
1683         if (type == attr_navigation_speech)
1684                 callback_list_add(this_->callback_speech, cb);
1685         else
1686                 callback_list_add(this_->callback, cb);
1687         return 1;
1688 }
1689
1690 void
1691 navigation_unregister_callback(struct navigation *this_, enum attr_type type, struct callback *cb)
1692 {
1693         if (type == attr_navigation_speech)
1694                 callback_list_remove_destroy(this_->callback_speech, cb);
1695         else
1696                 callback_list_remove_destroy(this_->callback, cb);
1697 }
1698
1699 struct map *
1700 navigation_get_map(struct navigation *this_)
1701 {
1702         if (! this_->map)
1703                 this_->map=map_new(NULL, (struct attr*[]){
1704                         &(struct attr){attr_type,{"navigation"}},
1705                         &(struct attr){attr_navigation,.u.navigation=this_},
1706                         &(struct attr){attr_data,{""}},
1707                         &(struct attr){attr_description,{"Navigation"}},
1708                         NULL});
1709         return this_->map;
1710 }
1711
1712 struct map_priv {
1713         struct navigation *navigation;
1714 };
1715
1716 struct map_rect_priv {
1717         struct navigation *nav;
1718         struct navigation_command *cmd;
1719         struct navigation_command *cmd_next;
1720         struct navigation_itm *itm;
1721         struct navigation_itm *itm_next;
1722         struct navigation_itm *cmd_itm;
1723         struct navigation_itm *cmd_itm_next;
1724         struct item item;
1725         enum attr_type attr_next;
1726         int ccount;
1727         int debug_idx;
1728         struct navigation_way *ways;
1729         int show_all;
1730         char *str;
1731 };
1732
1733 static int
1734 navigation_map_item_coord_get(void *priv_data, struct coord *c, int count)
1735 {
1736         struct map_rect_priv *this=priv_data;
1737         if (this->ccount || ! count)
1738                 return 0;
1739         *c=this->itm->start;
1740         this->ccount=1;
1741         return 1;
1742 }
1743
1744 static int
1745 navigation_map_item_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
1746 {
1747         struct map_rect_priv *this_=priv_data;
1748         attr->type=attr_type;
1749         struct navigation_command *cmd=this_->cmd;
1750         struct navigation_itm *itm=this_->itm;
1751         struct navigation_itm *prev=itm->prev;
1752
1753         if (this_->str) {
1754                 g_free(this_->str);
1755                 this_->str=NULL;
1756         }
1757
1758         if (cmd) {
1759                 if (cmd->itm != itm)
1760                         cmd=NULL;       
1761         }
1762         switch(attr_type) {
1763         case attr_navigation_short:
1764                 this_->attr_next=attr_navigation_long;
1765                 if (cmd) {
1766                         this_->str=attr->u.str=show_next_maneuvers(this_->nav, this_->cmd_itm, cmd, attr_type);
1767                         return 1;
1768                 }
1769                 return 0;
1770         case attr_navigation_long:
1771                 this_->attr_next=attr_navigation_long_exact;
1772                 if (cmd) {
1773                         this_->str=attr->u.str=show_next_maneuvers(this_->nav, this_->cmd_itm, cmd, attr_type);
1774                         return 1;
1775                 }
1776                 return 0;
1777         case attr_navigation_long_exact:
1778                 this_->attr_next=attr_navigation_speech;
1779                 if (cmd) {
1780                         this_->str=attr->u.str=show_next_maneuvers(this_->nav, this_->cmd_itm, cmd, attr_type);
1781                         return 1;
1782                 }
1783                 return 0;
1784         case attr_navigation_speech:
1785                 this_->attr_next=attr_length;
1786                 if (cmd) {
1787                         this_->str=attr->u.str=show_next_maneuvers(this_->nav, this_->cmd_itm, this_->cmd, attr_type);
1788                         return 1;
1789                 }
1790                 return 0;
1791         case attr_length:
1792                 this_->attr_next=attr_time;
1793                 if (cmd) {
1794                         attr->u.num=this_->cmd_itm->dest_length-cmd->itm->dest_length;
1795                         return 1;
1796                 }
1797                 return 0;
1798         case attr_time:
1799                 this_->attr_next=attr_destination_length;
1800                 if (cmd) {
1801                         attr->u.num=this_->cmd_itm->dest_time-cmd->itm->dest_time;
1802                         return 1;
1803                 }
1804                 return 0;
1805         case attr_destination_length:
1806                 attr->u.num=itm->dest_length;
1807                 this_->attr_next=attr_destination_time;
1808                 return 1;
1809         case attr_destination_time:
1810                 attr->u.num=itm->dest_time;
1811                 this_->attr_next=attr_street_name;
1812                 return 1;
1813         case attr_street_name:
1814                 attr->u.str=itm->name1;
1815                 this_->attr_next=attr_street_name_systematic;
1816                 if (attr->u.str)
1817                         return 1;
1818                 return 0;
1819         case attr_street_name_systematic:
1820                 attr->u.str=itm->name2;
1821                 this_->attr_next=attr_debug;
1822                 if (attr->u.str)
1823                         return 1;
1824                 return 0;
1825         case attr_debug:
1826                 switch(this_->debug_idx) {
1827                 case 0:
1828                         this_->debug_idx++;
1829                         this_->str=attr->u.str=g_strdup_printf("angle:%d (- %d)", itm->angle_start, itm->angle_end);
1830                         return 1;
1831                 case 1:
1832                         this_->debug_idx++;
1833                         this_->str=attr->u.str=g_strdup_printf("item type:%s", item_to_name(itm->item.type));
1834                         return 1;
1835                 case 2:
1836                         this_->debug_idx++;
1837                         if (cmd) {
1838                                 this_->str=attr->u.str=g_strdup_printf("delta:%d", cmd->delta);
1839                                 return 1;
1840                         }
1841                 case 3:
1842                         this_->debug_idx++;
1843                         if (prev) {
1844                                 this_->str=attr->u.str=g_strdup_printf("prev street_name:%s", prev->name1);
1845                                 return 1;
1846                         }
1847                 case 4:
1848                         this_->debug_idx++;
1849                         if (prev) {
1850                                 this_->str=attr->u.str=g_strdup_printf("prev street_name_systematic:%s", prev->name2);
1851                                 return 1;
1852                         }
1853                 case 5:
1854                         this_->debug_idx++;
1855                         if (prev) {
1856                                 this_->str=attr->u.str=g_strdup_printf("prev angle:(%d -) %d", prev->angle_start, prev->angle_end);
1857                                 return 1;
1858                         }
1859                 case 6:
1860                         this_->debug_idx++;
1861                         this_->ways=itm->ways;
1862                         if (prev) {
1863                                 this_->str=attr->u.str=g_strdup_printf("prev item type:%s", item_to_name(prev->item.type));
1864                                 return 1;
1865                         }
1866                 case 7:
1867                         if (this_->ways && prev) {
1868                                 this_->str=attr->u.str=g_strdup_printf("other item angle:%d delta:%d flags:%d dir:%d type:%s id:(0x%x,0x%x)", this_->ways->angle2, angle_delta(prev->angle_end, this_->ways->angle2), this_->ways->flags, this_->ways->dir, item_to_name(this_->ways->item.type), this_->ways->item.id_hi, this_->ways->item.id_lo);
1869                                 this_->ways=this_->ways->next;
1870                                 return 1;
1871                         }
1872                         this_->debug_idx++;
1873                 case 8:
1874                         this_->debug_idx++;
1875                         if (prev) {
1876                                 int delta=0;
1877                                 char *reason=NULL;
1878                                 maneuver_required2(prev, itm, &delta, &reason);
1879                                 this_->str=attr->u.str=g_strdup_printf("reason:%s",reason);
1880                                 return 1;
1881                         }
1882                         
1883                 default:
1884                         this_->attr_next=attr_none;
1885                         return 0;
1886                 }
1887         case attr_any:
1888                 while (this_->attr_next != attr_none) {
1889                         if (navigation_map_item_attr_get(priv_data, this_->attr_next, attr))
1890                                 return 1;
1891                 }
1892                 return 0;
1893         default:
1894                 attr->type=attr_none;
1895                 return 0;
1896         }
1897 }
1898
1899 static struct item_methods navigation_map_item_methods = {
1900         NULL,
1901         navigation_map_item_coord_get,
1902         NULL,
1903         navigation_map_item_attr_get,
1904 };
1905
1906
1907 static void
1908 navigation_map_destroy(struct map_priv *priv)
1909 {
1910         g_free(priv);
1911 }
1912
1913 static void
1914 navigation_map_rect_init(struct map_rect_priv *priv)
1915 {
1916         priv->cmd_next=priv->nav->cmd_first;
1917         priv->cmd_itm_next=priv->itm_next=priv->nav->first;
1918 }
1919
1920 static struct map_rect_priv *
1921 navigation_map_rect_new(struct map_priv *priv, struct map_selection *sel)
1922 {
1923         struct navigation *nav=priv->navigation;
1924         struct map_rect_priv *ret=g_new0(struct map_rect_priv, 1);
1925         ret->nav=nav;
1926         navigation_map_rect_init(ret);
1927         ret->item.meth=&navigation_map_item_methods;
1928         ret->item.priv_data=ret;
1929 #ifdef DEBUG
1930         ret->show_all=1;
1931 #endif
1932         return ret;
1933 }
1934
1935 static void
1936 navigation_map_rect_destroy(struct map_rect_priv *priv)
1937 {
1938         g_free(priv);
1939 }
1940
1941 static struct item *
1942 navigation_map_get_item(struct map_rect_priv *priv)
1943 {
1944         struct item *ret=&priv->item;
1945         int delta;
1946         if (!priv->itm_next)
1947                 return NULL;
1948         priv->itm=priv->itm_next;
1949         priv->cmd=priv->cmd_next;
1950         priv->cmd_itm=priv->cmd_itm_next;
1951         if (!priv->cmd)
1952                 return NULL;
1953         if (!priv->show_all && priv->itm->prev != NULL) 
1954                 priv->itm=priv->cmd->itm;
1955         priv->itm_next=priv->itm->next;
1956         if (priv->itm->prev)
1957                 ret->type=type_nav_none;
1958         else
1959                 ret->type=type_nav_position;
1960         if (priv->cmd->itm == priv->itm) {
1961                 priv->cmd_itm_next=priv->cmd->itm;
1962                 priv->cmd_next=priv->cmd->next;
1963                 if (priv->cmd_itm_next && !priv->cmd_itm_next->next)
1964                         ret->type=type_nav_destination;
1965                 else {
1966                         if (priv->itm && priv->itm->prev && !(priv->itm->flags & AF_ROUNDABOUT) && (priv->itm->prev->flags & AF_ROUNDABOUT)) {
1967                                 
1968                                 switch (((180+22)-priv->cmd->roundabout_delta)/45) {
1969                                 case 0:
1970                                 case 1:
1971                                         ret->type=type_nav_roundabout_r1;
1972                                         break;
1973                                 case 2:
1974                                         ret->type=type_nav_roundabout_r2;
1975                                         break;
1976                                 case 3:
1977                                         ret->type=type_nav_roundabout_r3;
1978                                         break;
1979                                 case 4:
1980                                         ret->type=type_nav_roundabout_r4;
1981                                         break;
1982                                 case 5:
1983                                         ret->type=type_nav_roundabout_r5;
1984                                         break;
1985                                 case 6:
1986                                         ret->type=type_nav_roundabout_r6;
1987                                         break;
1988                                 case 7:
1989                                         ret->type=type_nav_roundabout_r7;
1990                                         break;
1991                                 case 8:
1992                                         ret->type=type_nav_roundabout_r8;
1993                                         break;
1994                                 }
1995                         } else {
1996                                 delta=priv->cmd->delta; 
1997                                 if (delta < 0) {
1998                                         delta=-delta;
1999                                         if (delta < 45)
2000                                                 ret->type=type_nav_left_1;
2001                                         else if (delta < 105)
2002                                                 ret->type=type_nav_left_2;
2003                                         else if (delta < 165) 
2004                                                 ret->type=type_nav_left_3;
2005                                         else
2006                                                 ret->type=type_none;
2007                                 } else {
2008                                         if (delta < 45)
2009                                                 ret->type=type_nav_right_1;
2010                                         else if (delta < 105)
2011                                                 ret->type=type_nav_right_2;
2012                                         else if (delta < 165) 
2013                                                 ret->type=type_nav_right_3;
2014                                         else
2015                                                 ret->type=type_none;
2016                                 }
2017                         }
2018                 }
2019         }
2020         priv->ccount=0;
2021         priv->debug_idx=0;
2022         priv->attr_next=attr_navigation_short;
2023
2024         ret->id_lo=priv->itm->dest_count;
2025         dbg(1,"type=%d\n", ret->type);
2026         return ret;
2027 }
2028
2029 static struct item *
2030 navigation_map_get_item_byid(struct map_rect_priv *priv, int id_hi, int id_lo)
2031 {
2032         struct item *ret;
2033         navigation_map_rect_init(priv);
2034         while ((ret=navigation_map_get_item(priv))) {
2035                 if (ret->id_hi == id_hi && ret->id_lo == id_lo) 
2036                         return ret;
2037         }
2038         return NULL;
2039 }
2040
2041 static struct map_methods navigation_map_meth = {
2042         projection_mg,
2043         "utf-8",
2044         navigation_map_destroy,
2045         navigation_map_rect_new,
2046         navigation_map_rect_destroy,
2047         navigation_map_get_item,
2048         navigation_map_get_item_byid,
2049         NULL,
2050         NULL,
2051         NULL,
2052 };
2053
2054 static struct map_priv *
2055 navigation_map_new(struct map_methods *meth, struct attr **attrs)
2056 {
2057         struct map_priv *ret;
2058         struct attr *navigation_attr;
2059
2060         navigation_attr=attr_search(attrs, NULL, attr_navigation);
2061         if (! navigation_attr)
2062                 return NULL;
2063         ret=g_new0(struct map_priv, 1);
2064         *meth=navigation_map_meth;
2065         ret->navigation=navigation_attr->u.navigation;
2066
2067         return ret;
2068 }
2069
2070
2071 void
2072 navigation_init(void)
2073 {
2074         plugin_register_map_type("navigation", navigation_map_new);
2075 }