Skip to content

Commit

Permalink
Implement experimental humanization, rel #40
Browse files Browse the repository at this point in the history
  • Loading branch information
fs-c authored and lw committed May 2, 2020
1 parent 7c13b57 commit 81a8f1b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
31 changes: 30 additions & 1 deletion src/humanization.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,33 @@ static int generate_number(int range, int rounds, double bound) {
}

return rn;
}
}

void humanize_actions_exp(int total, struct action **actions, int regen_time,
int delta) {
if (!delta || !regen_time) {
return;
}

int offset = 0, passed_time = 0;
struct action *cur_action = NULL;
for (int i = 1; i < total; i++) {
cur_action = *actions + i;

int cur_t = cur_action->time;
int prev_t = (*actions + i - 1)->time;
passed_time += cur_t - prev_t;

while (passed_time > regen_time) {
offset -= delta * 4;

passed_time -= regen_time;
}

if (offset < 0) {
offset = 0;
}

offset += delta;
}
}
5 changes: 5 additions & 0 deletions src/humanization.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
*/
void humanize_hitpoints(int total, struct hitpoint **points, int level);

/**
*/
void humanize_actions_exp(int total, struct action **actions, int regen_time,
int delta);

#endif /* HUMANIZATION_H */
50 changes: 46 additions & 4 deletions src/maniac.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ int delay = 0;
int exit_check = 1;
int replay_delta = 0;

int exp_delay = 0;
int exp_regen = 0;

void *time_address;
pid_t game_proc_id;

Expand All @@ -41,6 +44,8 @@ static int play(char *map);

static void play_loop(struct action *actions, int num_actions);

static int parse_key_value(char *string, char **key, char **value);

int main(int argc, char **argv) {
setbuf(stdout, NULL);

Expand All @@ -58,12 +63,14 @@ int main(int argc, char **argv) {
{ "replay", optional_argument, NULL, 'r' },
{ "exit-checks", no_argument, NULL, 'e' },
{ "help", no_argument, NULL, 'h' },
{ "exp-humanization", required_argument, NULL, 'x' },
{ NULL, 0, NULL, 0 },
};

opterr = 0;
char *v1, *v2;
while (true) {
c = getopt_long(argc, argv, ":m:p:a:l:r:he", long_options,
c = getopt_long(argc, argv, ":m:p:a:l:r:x:he", long_options,
&option_index);

if (c == -1)
Expand All @@ -84,6 +91,16 @@ int main(int argc, char **argv) {
break;
case 'l':
delay = strtol(optarg, NULL, 10);
break;
case 'x':
parse_key_value(optarg, &v1, &v2);

exp_delay = strtol(v1, NULL, 10);
exp_regen = strtol(v2, NULL, 10);

printf("enabled experimental humanization with delay = %d"
" and regen = %d\n", exp_delay, exp_regen);

break;
case 'r':
replay = 1;
Expand Down Expand Up @@ -190,8 +207,8 @@ static int standby_loop(char *map, int *search, int replay) {
printf("retrying in %d ms\n", retry_delay);

nanosleep((struct timespec[]) {{
0, (long) (retry_delay * 1000)
}}, NULL);
0, (long) (retry_delay * 1000)
}}, NULL);

return STANDBY_CONTINUE;
}
Expand Down Expand Up @@ -239,7 +256,9 @@ static int play(char *map) {
printf("parsed %d hitpoints of map '%s' ('%s', %d)\n", num_points,
meta->title, meta->version, meta->map_id);

humanize_hitpoints(num_points, &points, delay);
if (delay) {
humanize_hitpoints(num_points, &points, delay);
}

debug("humanized %d hitpoints with delay of %d", num_points, delay);

Expand All @@ -262,6 +281,10 @@ static int play(char *map) {

debug("sorted %d actions", num_actions);

if (exp_delay) {
humanize_actions_exp(num_actions, &actions, exp_regen, exp_delay);
}

printf("playing with delay of %d (delta: %d)\n", delay, replay_delta);

play_loop(actions, num_actions);
Expand Down Expand Up @@ -319,6 +342,25 @@ static void play_loop(struct action *actions, int num_actions) {
free(title);
}

static int parse_key_value(char *string, char **key, char **value) {
int i = 0;
char *token = strtok(string, ":");
while (token != NULL) {
switch (i++) {
case 0:
*key = token;
break;
case 1:
*value = token;
break;
}

token = strtok(NULL, ":");
}

return i;
}

static void print_usage() {
printf(" Usage: ./maniac [options]\n\n");
printf(" Options: \n\n");
Expand Down

0 comments on commit 81a8f1b

Please sign in to comment.