[Wesnoth-commits] r10053 - in /trunk: src/attack_prediction.cpp tools/wesnoth-attack-sim.c (February 11, 2006 - 11:29)

Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Headers

 

Author: rusty
Date: Sat Feb 11 11:29:07 2006
New Revision: 10053

URL: http://svn.gna.org/viewcvs/wesnoth?rev=10053&view=rev
Log:
Enhance attack simulation and prediction file to show problem with "touched" calculation (first step to fixing it).

Modified:
    trunk/src/attack_prediction.cpp
    trunk/tools/wesnoth-attack-sim.c

Modified: trunk/src/attack_prediction.cpp
URL: http://svn.gna.org/viewcvs/wesnoth/trunk/src/attack_prediction.cpp?rev=10053&r1=10052&r2=10053&view=diff
==============================================================================
--- trunk/src/attack_prediction.cpp (original)
+++ trunk/src/attack_prediction.cpp Sat Feb 11 11:29:07 2006
@@ -436,7 +436,6 @@
 					bool swarm, bool firststrike);
 
 	// Set effect against this particular opponent.
-	// FIXME: damage if slowed.
 	void set_effectiveness(unsigned damage, double hit_chance, bool slows);
 
 	// Fight!
@@ -641,11 +640,7 @@
 #if defined(BENCHMARK) || defined(CHECK)
 // We create a significant number of nasty-to-calculate units, and
 // test each one against the others.
-#ifdef BENCHMARK
 #define NUM_UNITS 50
-#else
-#define NUM_UNITS 25
-#endif
 
 // Stolen from glibc headers sys/time.h 
 #define timer_sub(a, b, result)						      \
@@ -693,8 +688,8 @@
 	struct timeval start, end, total;
 
 	for (i = 0; i < NUM_UNITS; i++) {
-		unsigned hp = 1 + i/2 + ((i*2)%40);
-		u[i] = new combatant(hp, hp + (i+7)%20, false);
+		unsigned hp = 1 + ((i*3)%23);
+		u[i] = new combatant(hp, hp + (i+7)%17, false);
 		u[i]->set_weapon((i % 4) + 1, (i % 9) == 0, (i % 5) == 0,
 						 ((i+4) % 4) == 0,
 						 ((i+3) % 5) == 0);

Modified: trunk/tools/wesnoth-attack-sim.c
URL: http://svn.gna.org/viewcvs/wesnoth/trunk/tools/wesnoth-attack-sim.c?rev=10053&r1=10052&r2=10053&view=diff
==============================================================================
--- trunk/tools/wesnoth-attack-sim.c (original)
+++ trunk/tools/wesnoth-attack-sim.c Sat Feb 11 11:29:07 2006
@@ -107,6 +107,7 @@
 static void calculate_attack(const struct unit *defender,
 			     double defender_res[],
 			     double *defender_touched,
+			     double attacker_touched[],
 			     const struct unit *attackers[],
 			     double *attacker_res[],
 			     unsigned num_attackers,
@@ -115,6 +116,9 @@
 	unsigned int i, j;
 
 	*defender_touched = 0;
+
+	for (j = 0; j < num_attackers; j++)
+		attacker_touched[j] = 0;
 
 	for (i = 0; i < num_sims; i++) {
 		struct unit def = *defender;
@@ -135,6 +139,8 @@
 			else
 				simulate_attack(&att, &def);
 			attacker_res[j][att.hp]++;
+			if (att.touched)
+				attacker_touched[j]++;
 		}
 		defender_res[def.hp]++;
 		if (def.touched)
@@ -155,6 +161,7 @@
 		/* Any battle we weren't in, we're unscathed. */
 		attacker_res[i][attackers[i]->hp]
 			+= (1.0*num_sims-battles)/num_sims;
+		attacker_touched[i] /= battles;
 	}
 }
 
@@ -217,6 +224,7 @@
 #endif
 
 static void draw_results(const double res[], const struct unit *u,
+			 double touched,
 			 const char label[])
 {
 	unsigned int i;
@@ -234,6 +242,7 @@
 	if (u->firststrike)
 		printf("firststrike,");
 	printf("maxhp=%u ", u->max_hp);
+	printf("touched:%.2f%% ", touched*100);
 	for (i = 0; i < u->max_hp+1; i++)
 		printf(" %.2f", res[i]*100);
 	printf("\n");
@@ -271,12 +280,9 @@
 		barf("Malformed touched: %s hp %u battle %u", 
 		     label, i, battle);
 
-	/* We only calculate this for defender... */
-	if (def_untouched != 0.0) {
-		if (abs(val - def_untouched)*100 > 1.0)
-			barf("Expected %f touched, got %f battle %u",
-			     def_untouched, val, battle);
-	}
+	if (abs(val - def_untouched)*100 > 1.0)
+		barf("Expected %f touched, got %f battle %u",
+		     def_untouched, val, battle);
 
 	for (i = 0; i < u->max_hp+1; i++) {
 		if (fscanf(f, " %lf", &val) != 1)
@@ -305,7 +311,7 @@
 	assert(sum > 0.999 && sum < 1.001);
 }
 
-#define NUM_UNITS 25
+#define NUM_UNITS 50
 static void check(const char *filename)
 {
 	/* N^2 battles. */
@@ -318,8 +324,8 @@
 
 	printf("Creating %i units...\n", NUM_UNITS);
 	for (i = 0; i < NUM_UNITS; i++) {
-		u[i].hp = 1 + i/2 + ((i*2)%40);
-		u[i].max_hp = u[i].hp + (i+7)%20;
+		u[i].hp = 1 + ((i*3)%23);
+		u[i].max_hp = u[i].hp + (i+7)%17;
 		u[i].damage = (i % 7) + 2;
 		u[i].num_attacks = (i % 4) + 1;
 		u[i].slows = (i % 8) == 0;
@@ -348,6 +354,7 @@
 				double i_touched;
 				double *attacker_res[2];
 				const struct unit *attackers[2];
+				double touched[2];
 
 				memset(i_result, 0, sizeof(i_result));
 				memset(j_result, 0, sizeof(j_result));
@@ -358,6 +365,7 @@
 				attackers[0] = &u[j];
 				attackers[1] = &u[k];
 				calculate_attack(&u[i], i_result, &i_touched,
+						 touched,
 						 attackers, attacker_res, 2,
 						 10000);
 				battle++;
@@ -367,9 +375,9 @@
 				compare_results(i_result, &u[i], "Defender",
 						battle, 1 - i_touched, f);
 				compare_results(j_result, &u[j], "Attacker #1",
-						battle, 0.0, f);
+						battle, 1 - touched[0], f);
 				compare_results(k_result, &u[k], "Attacker #2",
-						battle, 0.0, f);
+						battle, 1 - touched[1], f);
 				if ((battle % percent) == 0) {
 					printf(".");
 					fflush(stdout);
@@ -384,7 +392,7 @@
 int main(int argc, char *argv[])
 {
 	unsigned int i;
-	double *res_def, *res_att[argc / 4], touched;
+	double *res_def, *res_att[argc / 4], def_touched, att_touched[argc/4];
 	const struct unit *def, *attacker[argc / 4 + 1];
 
 	if (argc == 3 && strcmp(argv[1], "--check") == 0)
@@ -404,9 +412,11 @@
 	attacker[i] = NULL;
 
 	srandom(time(NULL));
-	calculate_attack(def, res_def, &touched, attacker, res_att, i, 10000);
-	draw_results(res_def, def, "Defender");
+	calculate_attack(def, res_def, &def_touched, att_touched,
+			 attacker, res_att, i, 10000);
+	draw_results(res_def, def, def_touched, "Defender");
 	for (i = 0; attacker[i]; i++)
-		draw_results(res_att[i], attacker[i], "Attacker");
+		draw_results(res_att[i], attacker[i], att_touched[i],
+			     "Attacker");
 	return 0;
 }



Related Mails




Powered by MHonArc, Updated Sat Feb 11 14:40:17 2006