1 #include <string>
2 #include "../../dataobj/tabfile.h"
3 #include "obj_node.h"
4 #include "obj_pak_exception.h"
5 #include "../bridge_desc.h"
6 #include "text_writer.h"
7 #include "imagelist_writer.h"
8 #include "skin_writer.h"
9 #include "get_waytype.h"
10 #include "bridge_writer.h"
11
12 using std::string;
13
write_bridge_images(FILE * outfp,obj_node_t & node,tabfileobj_t & obj,int season)14 void write_bridge_images(FILE* outfp, obj_node_t& node, tabfileobj_t& obj, int season)
15 {
16 slist_tpl<string> backkeys;
17 slist_tpl<string> frontkeys;
18
19 static const char* const names[] = {
20 "image",
21 "ns", "ew", NULL,
22 "start",
23 "n", "s", "e", "w", NULL,
24 "ramp",
25 "n", "s", "e", "w", NULL,
26 "pillar",
27 "s", "w", NULL,
28 "image2",
29 "ns", "ew", NULL,
30 "start2",
31 "n", "s", "e", "w", NULL,
32 "ramp2",
33 "n", "s", "e", "w", NULL,
34 "pillar2",
35 "s", "w", NULL,
36 NULL
37 };
38
39 const char* const * ptr = names;
40 const char* keyname = *ptr++;
41 char keybuf[40];
42
43 do {
44 const char* keyindex = *ptr++;
45 do {
46 string value;
47
48 if( season < 0 ) {
49 sprintf( keybuf, "back%s[%s]", keyname, keyindex );
50 value = obj.get( keybuf );
51 backkeys.append( value );
52 //intf("BACK: %s -> %s\n", keybuf, value.chars());
53 sprintf( keybuf, "front%s[%s]", keyname, keyindex );
54 }
55 else {
56 sprintf( keybuf, "back%s[%s][%d]", keyname, keyindex, season );
57 value = obj.get( keybuf );
58 backkeys.append( value );
59 //intf("BACK: %s -> %s\n", keybuf, value.chars());
60 sprintf( keybuf, "front%s[%s][%d]", keyname, keyindex, season );
61 }
62
63 // must append to front keys even if empty to keep order correct (but warn anyway)
64 value = obj.get( keybuf );
65 frontkeys.append( value );
66 //intf("FRNT: %s -> %s\n", keybuf, value.chars());
67 if( value.size() <= 2 ) {
68 dbg->warning( obj_writer_t::last_name, "No %s specified (might still work)", keybuf );
69 }
70
71 keyindex = *ptr++;
72 } while( keyindex );
73 keyname = *ptr++;
74 } while( keyname );
75
76 imagelist_writer_t::instance()->write_obj( outfp, node, backkeys );
77 imagelist_writer_t::instance()->write_obj( outfp, node, frontkeys );
78 if( season <= 0 ) {
79 slist_tpl<string> cursorkeys;
80 cursorkeys.append( string( obj.get("cursor") ) );
81 cursorkeys.append( string( obj.get("icon") ) );
82
83 cursorskin_writer_t::instance()->write_obj( outfp, node, obj, cursorkeys );
84
85 cursorkeys.clear();
86 }
87 backkeys.clear();
88 frontkeys.clear();
89 }
90
write_obj(FILE * outfp,obj_node_t & parent,tabfileobj_t & obj)91 void bridge_writer_t::write_obj(FILE* outfp, obj_node_t& parent, tabfileobj_t& obj)
92 {
93 obj_node_t node(this, 24, &parent);
94
95 uint8 wegtyp = get_waytype(obj.get("waytype"));
96 uint16 topspeed = obj.get_int("topspeed", 999);
97 uint32 price = obj.get_int("cost", 0);
98 uint32 maintenance = obj.get_int("maintenance", 1000);
99 uint8 pillars_every = obj.get_int("pillar_distance",0); // distance==0 is off
100 uint8 pillar_asymmetric = obj.get_int("pillar_asymmetric",0); // middle of tile
101 uint8 max_length = obj.get_int("max_lenght",0); // max_lenght==0: unlimited
102 max_length = obj.get_int("max_length",max_length); // with correct spelling
103 uint8 max_height = obj.get_int("max_height",0); // max_height==0: unlimited
104 uint16 axle_load = obj.get_int("axle_load", 9999);
105
106 // prissi: timeline
107 uint16 intro_date = obj.get_int("intro_year", DEFAULT_INTRO_DATE) * 12;
108 intro_date += obj.get_int("intro_month", 1) - 1;
109
110 uint16 retire_date = obj.get_int("retire_year", DEFAULT_RETIRE_DATE) * 12;
111 retire_date += obj.get_int("retire_month", 1) - 1;
112
113 sint8 number_of_seasons = 0;
114
115 // Hajo: Version needs high bit set as trigger -> this is required
116 // as marker because formerly nodes were unversionend
117 uint16 version = 0x8009;
118 node.write_uint16(outfp, version, 0);
119 node.write_uint16(outfp, topspeed, 2);
120 node.write_uint32(outfp, price, 4);
121 node.write_uint32(outfp, maintenance, 8);
122 node.write_uint8 (outfp, wegtyp, 12);
123 node.write_uint8 (outfp, pillars_every, 13);
124 node.write_uint8 (outfp, max_length, 14);
125 node.write_uint16(outfp, intro_date, 15);
126 node.write_uint16(outfp, retire_date, 17);
127 node.write_uint8 (outfp, pillar_asymmetric, 19);
128 node.write_uint16(outfp, axle_load, 20);
129 node.write_uint8 (outfp, max_height, 22);
130
131 char keybuf[40];
132
133 string str = obj.get("backimage[ns][0]");
134 if (str.empty()) {
135 node.write_data_at(outfp, &number_of_seasons, 23, sizeof(uint8));
136 write_head(outfp, node, obj);
137 write_bridge_images( outfp, node, obj, -1 );
138
139 }
140 else {
141 while(number_of_seasons < 2) {
142 sprintf(keybuf, "backimage[ns][%d]", number_of_seasons+1);
143 string str = obj.get(keybuf);
144 if (!str.empty()) {
145 number_of_seasons++;
146 }
147 else {
148 break;
149 }
150 }
151
152 node.write_data_at(outfp, &number_of_seasons, 23, sizeof(uint8));
153 write_head(outfp, node, obj);
154
155 for(uint8 season = 0 ; season <= number_of_seasons ; season++) {
156 write_bridge_images( outfp, node, obj, season );
157 }
158 }
159
160 // node.write_data(outfp, &desc);
161 node.write(outfp);
162 }
163