README.md edited online with Bitbucket
[swg-src.git] / dsrc / sku.0 / sys.server / compiled / game / datatables / ship / balance_space_crafted.pl
blob81a4d9ba5228a85631ed7c39376d1e08a2dde718
1 #!/usr/bin/perl
3 # Constants
5 $balance_sheet = "ship_balance.txt";
6 $comp_sheet = "crafted_component_balance.txt";
8 #$template_path = "C:\Projects\swg\current\exe\win32\TemplateCompiler.exe
9 $server_prefix = "\\swg\\current\\dsrc\\sku.0\\sys.server\\compiled\\game\\object\\draft_schematic\\space";
10 $shared_prefix = "\\swg\\current\\dsrc\\sku.0\\sys.shared\\compiled\\game\\object\\draft_schematic\\space";
11 $shared_bin_prefix = "\\swg\\current\\data\\sku.0\\sys.shared\\compiled\\game\\object\\draft_schematic\\space";
13 ###
14 # main
15 ###
17 # Read our balance sheet.
18 print "\nbalance_space_crafted process started\n";
20 if ( $ARGV[0] ne undef )
22 $argtype = $ARGV[0];
25 # Read balance data.
26 readCoreBalance();
27 calculateCoreBalance();
28 readCraftedChunks();
30 ###
31 # sub readCoreBalance
32 # Overall function for reading the balance sheet.
33 ###
35 sub readCoreBalance
37 open BALANCE, "<$balance_sheet";
39 while ( <BALANCE> )
41 chop;
42 @fields = split /\t/;
43 if ( /Core Balance Data/ )
45 @header = @fields;
47 elsif ( /Player Baseline/ )
49 @baseline = @fields;
51 elsif ( /Player Factor/ )
53 @player_factor = @fields;
57 close BALANCE;
60 ###
61 # sub calculateCoreBalance
62 ###
64 sub calculateCoreBalance
66 for ( my $i=0; $i<@header; $i++ )
68 if ( $i == 0 )
70 next;
73 for ( my $tier=1; $tier<6; $tier++ )
75 $key = $header[$i];
76 my $name = $key . "_tier" . $tier;
78 # Modify by the player tier factor.
79 $f = $baseline[$i];
80 if ( $tier > 1 )
82 @factor = split /!/, $player_factor[$i];
83 $pref = $factor[0];
84 $postf = $factor[1];
86 for ( my $x=1; $x<$tier; $x++ )
88 $f *= $pref;
89 $pref *= $postf;
92 $f = sprintf( "%.2f", $f );
93 $balancedata{$name} = $f;
98 ###
99 # sub readCraftedChunks
102 sub readCraftedChunks
104 # Read balance data from the loot sheet.
105 open CRAFTED_COMPONENTS, "<$comp_sheet";
106 while ( <CRAFTED_COMPONENTS> )
108 $line = $_;
109 for ( $chunk )
111 # Baselines
112 /Mass Distribution/ and do { $massb = readBaseline( $line, $massb, \$massindex ); next; };
113 /Reactor Drain Distribution/ and do { $reactorb = readBaseline( $line, $reactorb, \$reactorindex ); next; };
115 # Chassis mass distribution.
116 /Chassis/ and do { processChassis( $line ); next; };
118 # Component chunks.
119 if ( $chunk ne "" )
121 processComponentChunk( $line );
122 next;
125 @cfields = split /\t/, $line;
126 $chunk = $cfields[0];
127 $directory = $cfields[1];
128 $varmod = $cfields[2];
131 close CRAFTED_COMPONENTS;
135 # sub readBaseline
138 sub readBaseline
140 my ($line, $var, $index) = @_;
142 # Check for end of chunk.
143 if ( $line eq "\n" )
145 # End of this chunk.
146 $chunk = "";
147 return $var;
150 # Read some data.
151 chop($line);
152 @fields = split /\t/, $line;
153 if ( $fields[0] ne "BASELINE" )
155 return $line;
157 else
159 $$index = $line;
164 # sub processChassis
167 sub processChassis
169 $line = $_[0];
171 # Check for end of chunk.
172 if ( $line eq "\n" )
174 # End of this chunk.
175 $chunk = "";
176 return;
178 return;
179 # Write chassis mass values to the chassis draft schematics.
180 chop($line);
181 @fields = split /\t/, $line;
182 if ( $fields[0] eq "Name" )
184 return;
186 else
188 my $filename = $shared_prefix . "\\chassis\\shared_" . $fields[0] . ".tpf";
189 $filename =~ s/\\/\//g;
190 my $p4name = $filename;
191 my $filename = $shared_prefix . "\\chassis\\shared_" . $fields[0] . ".tpf";
193 # Calculate the mass range for this chassis.
194 @mass = split /\t/, $massb;
195 $medmass = $fields[1] * $mass[0];
196 $massvar = $fields[2] * $mass[0];
197 $mass_min = $medmass - ($massvar/2);
198 $mass_max = $medmass + ($massvar/2);
200 # Check out the file to be edited.
201 system( "p4 edit //depot" . $p4name );
203 # Read the schematic.
204 open CHASSIS, "<$filename";
205 my $i = 0;
206 while ( my $z = <CHASSIS> )
208 # Read the file into a buffer.
209 $lines[$i++] = $z;
211 close CHASSIS;
213 # Dump the modified schematic.
214 open CHASSIS, ">$filename";
215 for ( my $i=0; $i<@lines; $i++ )
217 if ( $lines[$i] =~ m/\"massMax\"/ )
219 $lines[$i] =~ s/(\d*)\.\.(\d*)/$mass_min\.\.$mass_max/;
221 print CHASSIS $lines[$i];
223 close CHASSIS;
224 @lines = ();
226 # Check out the .iff.
227 my $sfilename = $shared_bin_prefix . "\\chassis\\shared_" . $fields[0] . ".iff";
228 $sfilename =~ s/\\/\//g;
229 my $sp4name = $sfilename;
230 system( "p4 edit //depot" . $sp4name );
232 # Compile the freaking file.
233 system( "templatecompiler -compile $filename" );
238 # sub processComponentChunk
241 sub processComponentChunk
243 $line = $_[0];
245 # Check for end of chunk.
246 if ( $line =~ m/^\s*\n$/ )
248 # End of this chunk.
249 $header = 0;
250 $chunk = "";
251 return;
253 chop($line);
254 @fields = split /\t/, $line;
256 if ( ($argtype ne undef) && ($directory ne $argtype) )
258 return;
261 # Read the chunk header.
262 if ( $header == 0 )
264 @categories = @fields;
265 $header++;
266 return;
268 elsif ( $header == 1 )
270 @bal_cat_names = @fields;
271 $header++;
272 return;
274 elsif ( $header == 2 )
276 @var_names = @fields;
277 $header++;
278 return;
280 elsif ( $header == 3 )
282 @exp_names = @fields;
283 $header++;
284 return;
286 elsif ( $header == 4 )
288 @baseline = @fields;
289 $header++;
290 return;
293 # Read a component chunk entry.
295 my $filename = "$shared_prefix\\$directory\\shared_$fields[0].tpf";
296 $filename =~ s/\\/\//g;
297 my $p4name = $filename;
298 my $filename = "$shared_prefix\\$directory\\shared_$fields[0].tpf";
300 print $filename . "\n";
302 # Check out the file to be edited.
303 system( "p4 edit //depot" . $p4name );
305 # Read the schematic.
306 open SCHEMATIC, "<$filename";
307 my $i = 0;
308 while ( my $z = <SCHEMATIC> )
310 # Read the file into a buffer.
311 $lines[$i++] = $z;
313 close SCHEMATIC;
315 # Write out our changes.
316 open SCHEMATIC, ">$filename";
317 for ( my $i=0; $i<@lines; $i++ )
319 $lines[$i] =~ m/\"crafting\" \"(\w*)\", exp/;
320 $var = $1;
321 if ( $var eq undef )
323 print SCHEMATIC $lines[$i];
324 next;
326 my $x = 0;
327 foreach $str (@var_names)
329 if ( $var eq $str )
331 # print "var: $var\n";
333 # Find our cert level.
334 if ( $categories[1] eq "Cert" )
336 $cert = $fields[1];
338 else
340 $cert = 1;
343 # Find our tier.
344 $tier = sprintf( "%.0f", $cert/2 );
345 $step = ($fields[1] % 2) == 0;
346 $mod = 1;
347 if ( $step )
349 $mod = 1.25;
352 # We want to calculate the correct range for this crafting variable.
353 # >>>>>> THIS IS THE MEAT OF THIS CODE
354 # print " exp: $exp_names[$x]\n";
356 $balcat = $bal_cat_names[$x];
357 if ( $balcat =~ m/None/ )
359 # Just use this range as stated in the balance line.
360 $range_min = $fields[$x];
361 $range_max = $fields[$x];
363 elsif ( $balcat =~ m/Var/ )
365 # Just use this range as stated in the balance line, with variance.
366 $variance = $fields[$x] * 0.15 * $varmod;
367 $range_min = $fields[$x] - ($variance/2);
368 $range_max = $fields[$x] + ($variance/2);
370 elsif ( $balcat =~ m/Mass/ )
372 # Mass is derived from the mass baseline for this object type.
373 @massindexf = split /\t/, $massindex;
374 @massf = split /\t/, $massb;
375 for ( my $y=0; $y<@massindexf; $y++ )
377 if ( $massindexf[$y] eq $directory )
379 $basemass = $massf[$y] * $massf[0] * $fields[$x];
380 $variance = $basemass * 0.15 * $varmod;
381 $range_min = $basemass - ($variance/2);
382 $range_max = $basemass + ($variance/2);
383 last;
387 elsif ( $balcat =~ m/ReactorDrain/ )
389 # Reactor drain is derived from the reactor baseline for this object type.
390 @drainindexf = split /\t/, $reactorindex;
391 @drainf = split /\t/, $reactorb;
392 for ( my $y=0; $y<@drainindexf; $y++ )
394 if ( $drainindexf[$y] eq $directory )
396 $basedrain = $drainf[$y] * $drainf[0] * $fields[$x];
397 $variance = $basedrain * 0.15 * $varmod;
398 $range_min = $basedrain - ($variance/2);
399 $range_max = $basedrain + ($variance/2);
400 last;
404 elsif ( $balcat =~ m/ReactorGen/ )
406 # Reactor gen is specific to the crafted object balance sheet.
407 $data = $baseline[$x] * $fields[$x];
408 $variance = $data * 0.15 * $varmod;
409 $range_min = $data - ($variance/2);
410 $range_max = $data + ($variance/2);
412 elsif ( $balcat =~ m/CompArmor/ )
414 # Armor on components is half normal component value.
415 $data = $balancedata{"Component_tier".$tier} * $fields[$x] * $mod;
416 $data = $data / 2;
417 $variance = $data * 0.15 * $varmod;
418 $range_min = $data - ($variance/2);
419 $range_max = $data + ($variance/2);
421 elsif( ($balcat =~ m/WeaponRate/) || ($balcat =~ m/WeaponEffShield/) || ($balcat =~ m/WeaponEffArmor/) || ($balcat =~ m/ShieldRegen/) )
423 # These entries must be scaled up.
424 $data = $balancedata{$bal_cat_names[$x]."_tier".$tier} * $fields[$x] * $mod * 1000;
425 $variance = $data * 0.15 * $varmod;
426 $range_min = $data - ($variance/2);
427 $range_max = $data + ($variance/2);
429 else
431 # A normal entry.
432 $data = $balancedata{$bal_cat_names[$x]."_tier".$tier} * $fields[$x] * $mod;
433 $variance = $data * 0.15 * $varmod;
434 $range_min = $data - ($variance/2);
435 $range_max = $data + ($variance/2);
438 # Maintain left-to-right relationship.
439 if ( $range_max < $range_min )
441 my $temp = $range_max;
442 $range_max = $range_min;
443 $range_min = $temp;
446 # Format into integers.
447 $range_min = sprintf( "%.0f", $range_min );
448 $range_max = sprintf( "%.0f", $range_max );
450 # print " range: $range_min..$range_max\n";
452 $lines[$i] =~ m/^(.*=\s*)-?\d+\.\.-?\d+(].*)$/;
453 # print $1 . "\n";
454 # print $2 . "\n";
455 $lines[$i] = $1 . $range_min . ".." . $range_max . $2 . "\n";
456 # print $lines[$i] . "\n";
458 last;
461 $x++;
464 print SCHEMATIC $lines[$i];
466 close SCHEMATIC;
468 @lines = ();
470 # Check out the .iff.
471 my $sfilename = "$shared_bin_prefix\\$directory\\shared_$fields[0].iff";
472 $sfilename =~ s/\\/\//g;
473 my $sp4name = $sfilename;
474 system( "p4 edit //depot" . $sp4name );
476 # Compile the freaking file.
477 system( "templatecompiler -compile $filename" );