This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
extract_units.cpp
92 lines (83 loc) · 2.54 KB
/
extract_units.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
extern "C" {
#include <TH/TH.h>
#include <luaT.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <glob.h>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
#include <thread>
#include "TorchCraft/include/replayer.h"
using namespace torchcraft::replayer;
inline std::vector<std::string> glob(const std::string& pat){
using namespace std;
glob_t glob_result;
glob(pat.c_str(),GLOB_TILDE,NULL,&glob_result);
vector<string> ret;
for(unsigned int i=0;i<glob_result.gl_pathc;++i){
ret.push_back(string(glob_result.gl_pathv[i]));
}
globfree(&glob_result);
return ret;
}
bool is_file_exist(const char *fileName)
{
std::ifstream infile(fileName);
return infile.good();
}
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cout << "Need one argument, glob of source file path" << std::endl;
return 1;
}
auto dest = "/tmp";
std::vector<std::string> files = glob(std::string(argv[1]));
//std::random_shuffle ( files.begin(), files.end() );
std::cout << files.size() << std::endl;
#pragma omp parallel for
for (int k=0; k<files.size(); k++) {
auto fname = files[k];
auto where = fname.find("sc_uncompressed") + 15;
auto outname = dest + fname.substr(where);
auto tmpname = outname + ".tmp";
if (is_file_exist(outname.c_str()) || is_file_exist(tmpname.c_str()))
continue;
std::cout << "doing file " << fname << '\n';
std::ifstream inRep(fname);
Replayer r;
try {inRep >> r;} catch (const std::exception & e) {continue;}
std::ofstream ofs(tmpname);
auto map = r.getRawMap();
auto x = THByteTensor_size(map, 0);
auto y = THByteTensor_size(map, 1);
auto t = r.size();
ofs << x << " " << y << " " << t << '\n';
for (int i=0; i<r.size(); i++) {
auto f = r.getFrame(i);
auto fn = r.getFrame(i+1);
for (auto team : f->units)
for (auto unit : team.second) {
ofs << team.first << " " << unit.id << " " << unit.type << " " << unit.x << " " << unit.y << " ";
}
ofs << "\n";
}
ofs.close();
if (rename(tmpname.c_str(), outname.c_str()) != 0) {
std::cout << "Rename failed! From " << tmpname << " to " << outname << std::endl;
}
}
return 0;
}