Passed
Pull Request — master (#291)
by Marek
02:06
created

osci.res.OLDgetFFColorCode()   B

Complexity

Conditions 7

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nop 1
dl 0
loc 22
rs 8
c 0
b 0
f 0
1
#
2
#  Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Outer Space.
5
#
6
#  Outer Space is free software; you can redistribute it and/or modify
7
#  it under the terms of the GNU General Public License as published by
8
#  the Free Software Foundation; either version 2 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Outer Space is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  GNU General Public License for more details.
15
#
16
#  You should have received a copy of the GNU General Public License
17
#  along with Outer Space; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
import bisect
22
import glob
23
import math
24
import os
25
import re
26
27
import gdata, client
28
import pygame, pygame.image
29
from osci import gdata
30
import ige.ospace.Const as Const
31
from ige.ospace import Rules
32
from ige import log
33
34
import resources
35
36
whiteShift = 80000
37
redShift = 90000
38
39
smallStarImgs = None
40
techImgs = None
41
bigStarImgs = None
42
planetImgs = None
43
planetImgCnt = None
44
buttonImgs = None
45
cmdInProgressImg = None
46
loginLogoImg = None
47
structProblemImg = None
48
structOffImg = None
49
icons = {}
50
ui_icons = {}
51
52
def initialize():
53
    # needed for progress dlg
54
    global loginLogoImg
55
    loginLogoImg = pygame.image.load(resources.get('logo-login.png')).convert_alpha()
56
57
def updateProgress(curr, progress_dlg):
58
    if not progress_dlg: return
59
    if curr % 30 == 0:
60
        periods = '.' * (curr / 30 % 4)
61
        progress_dlg.setProgress(_('Loading resources' + periods), curr)
62
63
64
65
def loadResources(progress_dlg=None):
66
    curr = 0
67
    max = len(glob.glob(resources.get('galaxy/*.png'))) + len(glob.glob(resources.get('techs/*.png'))) + \
68
        len(glob.glob(resources.get('system/*.png'))) + len(glob.glob(resources.get('icons/*.png'))) + len(glob.glob(resources.get('buttons/*.png')))
69
    if progress_dlg:
70
        progress_dlg.display(_('Loading resources'), 0, max)
71
    # load star imgs
72
    global smallStarImgs
73
    smallStarImgs = {}
74
    for filename in glob.glob(resources.get('galaxy/star_*.png')):
75
        name = re.search("star_([^.]+).png", filename).group(1)
76
        smallStarImgs[name] = pygame.image.load(filename).convert_alpha()
77
        curr += 1
78
        updateProgress(curr, progress_dlg)
79
    # load tech imgs
80
    global techImgs
81
    techImgs = {}
82
    white = pygame.Surface((37,37))
83
    white.fill((255, 255, 255))
84
    white.set_alpha(64)
85
    red = pygame.Surface((37,37))
86
    red.fill((255, 0, 0))
87
    red.set_alpha(64)
88
    for filename in glob.glob(resources.get('techs/????.png')):
89
        name = os.path.splitext(os.path.basename(filename))[0]
90
        imgID = int(name)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable int does not seem to be defined.
Loading history...
91
        techImgs[imgID] = pygame.image.load(filename).convert_alpha()
92
        copyImg = techImgs[imgID].convert_alpha()
93
        copyImg.blit(white, (0,0))
94
        techImgs[imgID + whiteShift] = copyImg
95
        copyImg = techImgs[imgID].convert_alpha()
96
        copyImg.blit(red, (0,0))
97
        techImgs[imgID + redShift] = copyImg
98
        curr += 1
99
        updateProgress(curr, progress_dlg)
100
    # load big star imgs
101
    global bigStarImgs
102
    bigStarImgs = {}
103
    for filename in glob.glob(resources.get('system/star_*.png')):
104
        name = re.search("star_([^.]+).png", filename).group(1)
105
        bigStarImgs[name] = pygame.image.load(filename).convert_alpha()
106
        curr += 1
107
        updateProgress(curr, progress_dlg)
108
    # load planet images
109
    global planetImgs
110
    global planetImgCnt
111
    planetImgs = {}
112
    planetImgCnt = {}
113
    for filename in glob.glob(resources.get('system/planet_*.png')):
114
        matchobj = re.search("planet_((.)[^.]+).png", filename)
115
        name = matchobj.group(1)
116
        pltype = matchobj.group(2)
117
        if pltype in planetImgCnt:
118
            planetImgCnt[pltype] += 1
119
        else:
120
            planetImgCnt[pltype] = 1
121
        planetImgs[name] = pygame.image.load(filename).convert_alpha()
122
        curr += 1
123
        updateProgress(curr, progress_dlg)
124
    # load ship imgs
125
    global shipImgs
126
    shipImgs = {}
127
    for filename in glob.glob(resources.get('ships/??.png')):
128
        name = os.path.splitext(os.path.basename(filename))[0]
129
        shipImgs[int(name)] = pygame.image.load(filename).convert_alpha()
130
        curr += 1
131
        updateProgress(curr, progress_dlg)
132
    # load star imgs
133
    global icons
134
    icons = {}
135
    for filename in glob.glob(resources.get('icons/[!ui_]*.png')):
136
        name = os.path.splitext(os.path.basename(filename))[0]
137
        icons[name] = pygame.image.load(filename).convert_alpha()
138
        curr += 1
139
        updateProgress(curr, progress_dlg)
140
    # load UI icons
141
    global ui_icons
142
    ui_icons = {}
143
    for filename in glob.glob(resources.get('icons/ui_*.png')):
144
        name = os.path.splitext(os.path.basename(filename))[0]
145
        ui_icons[name] = pygame.image.load(filename).convert_alpha()
146
        curr += 1
147
        updateProgress(curr, progress_dlg)
148
    # load buttons
149
    global buttonImgs
150
    buttonImgs = {}
151
    for filename in glob.glob(resources.get('buttons/*.png')):
152
        name = os.path.splitext(os.path.basename(filename))[0]
153
        buttonImgs[name] = pygame.image.load(filename).convert_alpha()
154
        curr += 1
155
        updateProgress(curr, progress_dlg)
156
    # other icons
157
    global cmdInProgressImg
158
    cmdInProgressImg = pygame.image.load(resources.get('cmdInProgress.png')).convert_alpha()
159
    global structProblemImg
160
    structProblemImg = pygame.image.load(resources.get('struct_problem.png')).convert_alpha()
161
    global structOffImg
162
    structOffImg = pygame.image.load(resources.get('struct_off.png')).convert_alpha()
163
164
def prepareUIIcons(color):
165
    for image in ui_icons.values():
166
        image.fill((0, 0, 0, 255), None, pygame.BLEND_RGBA_MULT)
167
        image.fill(color[0:3] + (0,), None, pygame.BLEND_RGBA_ADD)
168
169
def getUIIcon(icon_name):
170
    return ui_icons["ui_" + str(icon_name)]
171
172
def getTechImg(techID):
173
    return techImgs.get(techID, techImgs[0])
174
175
def getShipImg(combatClass, isMilitary):
176
    return shipImgs.get(int(combatClass) * 10 + int(isMilitary), shipImgs[99])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable shipImgs does not seem to be defined.
Loading history...
177
178
def getSmallStarImg(name):
179
    return smallStarImgs[name]
180
181
def getBigStarImg(name):
182
    return bigStarImgs[name]
183
184
def getPlanetImg(pltype,plid):
185
    global planetImgCnt
186
    name = '%s%d' % (pltype,plid % planetImgCnt[pltype])
187
    return planetImgs[name]
188
189
def getButton(name,state):
190
    if state:
191
        name = "%s_%s" % (name,'active')
192
    else:
193
        name = "%s_%s" % (name,'inactive')
194
    return buttonImgs[name]
195
196
def getUnknownName():
197
    return _('[Unknown]')
198
199
def getNA():
200
    return _('N/A')
201
202
def getSystemOverviewProblemColor(owner,problem):
203
    if problem:
204
        return gdata.sevColors[gdata.CRI]
205
    else:
206
        return getPlayerColor(owner)
207
208
def getFFColorCode(relationship):
209
    if relationship == Const.REL_UNITY:
210
        return (0x00, 0xff, 0x00)
211
    relColors = [(0xff, 0x80, 0x80),
212
                 (0xff, 0x90, 0x01),
213
                 (0xff, 0xff, 0x00),
214
                 (0xb0, 0xb0, 0xff),
215
                 (0x80, 0xff, 0xff)]
216
    return relColors[bisect.bisect(Const.REL_BOUNDARIES, relationship)]
217
218
def getHabitabilityColorCode(bio):
219
    if bio < 0:
220
        return (0xff, 0x00, 0xff)
221
    colorCodes = [(255, 5*bio, 0), # end 255, 125, 0
222
                  (255-2*(bio-25), 125+2*(bio-25), 0), # end 155, 225, 0
223
                  (155-3*(bio-75), 225, 0), #end 5, 225, 0
224
                  (0, 255, 2*(bio-125)), #end 0, 225, 250
225
                  (0, 255, 255)]
226
    boundaries = [25,75,125,200]
227
    return colorCodes[bisect.bisect(boundaries, bio)]
228
229
def getPirateColonyColorCode(pirate):
230
    if pirate is False:
231
        return (0xc0, 0xc0, 0xc0)
232
    if pirate <= 0:
233
        return (0xff, 0x00, 0xff)
234
    colorCodes = [(255, 5*pirate, 0), # end 255, 125, 0
235
                  (255-2*(pirate-25), 125+2*(pirate-25), 0), # end 155, 225, 0
236
                  (155-3*(pirate-75), 225, 0), #end 5, 225, 0
237
                  (0, 255, 2*(pirate-125)), #end 0, 225, 250
238
                  (0, 255, 255)]
239
    boundaries = [25,75,125,200]
240
    return colorCodes[bisect.bisect(boundaries, pirate)]
241
242
def getFameColorCode(fame):
243
    if fame > 0 and fame < 200:
244
        #log.debug(fame,(0xff,255 - int(255*(fame/200)),0x00))
245
        return (0xff,255 - int(255*(fame/200.0)),0x00)
246
    if fame == 200:
247
        return (0xff,0x00,0xff) #pirate colony
248
    return (0xc0, 0xc0, 0xc0)
249
250
def getMineralColorCode(minerals):
251
    if minerals >= 0:
252
        return (0xff,max(0,min(255,255 - int(255*(minerals/200.0)))),0x0) #use min, since it we occasionally get 201+ mineral levels, but it is so rare that we can ignore it for colors.
253
    return (0xc0, 0xc0, 0xc0)
254
255
def getSlotColorCode(slots):
256
    if slots > 20:
257
        return (0x0,0xFF,min(255,(slots-20)*10)) #in case sometime in the future we have larger worlds available
258
    if slots > 0:
259
        return (int(255*(slots/20.0)),255 - int(255*(slots/20.0)),0x0)
260
    return (0xc0, 0xc0, 0xc0)
261
262
def getSlotSystemColorCode(slots,planets):
263
    if planets > 0:
264
        slots = int(float(slots)/planets)
265
        if slots > 20:
266
            return (0x0,0xFF,min(255,(slots-20)*10)) #in case sometime in the future we have larger worlds available
267
        if slots > 0:
268
            return (int(255*(slots/20.0)),255 - int(255*(slots/20.0)),0x0)
269
        return (0xc0, 0xc0, 0xc0)
270
    else:
271
        return (0xc0, 0xc0, 0xc0)
272
273
274
def getStargateColorCode(accel):
275
    accel = accel * 100 - 100
276
    if accel < 1:
277
        return (0xc0, 0xc0, 0xc0)
278
    if accel < 50:
279
        return (0xff, 0xff, 0x00)
280
    if accel < 150:
281
        return (0xff, 0xc0, 0x00)
282
    if accel < 250:
283
        return (0xff, 0x60, 0x00)
284
    if accel < 350:
285
        return (0xff, 0x00, 0x00)
286
    if accel < 450:
287
        return (0xff, 0x00, 0x80)
288
    if accel > 449:
289
        return (0xff, 0x00, 0xff)
290
    return (0xc0, 0xc0, 0xc0)
291
292
def getDockColorCode(refuel,upgrades): #refuel is based on best dock; upgrades are based on sum of all docks
293
    #refuel range: 0...6
294
    #upgrade range: 0...100 (cap 100)
295
    if (refuel > 1 and upgrades > 0) or (refuel > 2): #refuel > 2 for other player docks since they always read upgrades of 0; this probably should be corrected for allies
296
        refuelScale = max(0,min(1,(refuel-1)/5.0))
297
        upgradeScale = max(0,min(1,upgrades/50))
298
        return (0xFF,int(refuelScale*(1-upgradeScale)*255),int(refuelScale*(upgradeScale)*255))
299
    if refuel > 0:
300
        refuelScale = max(0,min(1,refuel/2.0))
301
        return (0x00,100+100*int(refuelScale),100+100*int(refuelScale)) # cyan
302
    return (0xc0, 0xc0, 0xc0)
303
304
def getMoraleColors(morale):
305
    if morale >= 0:
306
        return (255-int(255*(morale/100.0)),int(255*(morale/100.0)),0x0)
307
    return (0xc0, 0xc0, 0xc0)
308
309
def getPlayerColor(owner, onlyDiplo = False):
310
    if owner == Const.OID_NONE:
311
        return getFFColorCode(Const.REL_UNDEF)
312
    if not onlyDiplo:
313
        if gdata.config.defaults.highlights == 'yes':
314
            if gdata.playersHighlightColors.has_key(owner):
315
                return gdata.playersHighlightColors[owner]
316
    rel = min(Const.REL_UNDEF,client.getRelationTo(owner))
317
    return getFFColorCode(rel)
318
319
def getControlColor(owner, onlyDiplo = False):
320
    if owner == Const.OID_NONE:
321
        return False
322
    if not onlyDiplo:
323
        if gdata.config.defaults.highlights == 'yes':
324
            if gdata.playersHighlightColors.has_key(owner):
325
                return fadeDarkColor(gdata.playersHighlightColors[owner])
326
    rel = min(Const.REL_UNDEF,client.getRelationTo(owner))
327
    return fadeDarkColor(getFFColorCode(rel))
328
329
def getGateLineWidth(owner):
330
    if owner == Const.OID_NONE:
331
        return 1
332
    rel = min(Const.REL_UNDEF,client.getRelationTo(owner))
333
    if rel == 1250:
334
        return 2
335
    return 1
336
337
def getStarmapWidgetPlanetColor(ownerid,bio,mineral,slot,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate=False):
338
    colors = {}
339
    for datatype in gdata.OVERLAY_TYPES:
340
        colors[datatype] = getStarmapWidgetPlanetColorPerDatatype(datatype,ownerid,bio,mineral,slot,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate)
341
    return colors
342
343
def getStarmapWidgetSystemColor(ownerid,bio,mineral,slot,num_planets,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate=False):
344
    colors = {}
345
    for datatype in gdata.OVERLAY_TYPES:
346
        colors[datatype] = getStarmapWidgetSystemColorPerDatatype(datatype,ownerid,bio,mineral,slot,num_planets,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate)
347
    return colors
348
349
350 View Code Duplication
def getStarmapWidgetPlanetColorPerDatatype(datatype,ownerid,bio,mineral,slot,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate=False):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
351
    if (datatype == gdata.OVERLAY_OWNER):
352
        return getPlayerColor(ownerid)
353
    if (datatype == gdata.OVERLAY_DIPLO):
354
        return getPlayerColor(ownerid,True)
355
    if (datatype == gdata.OVERLAY_BIO):
356
        return getHabitabilityColorCode(bio)
357
    if (datatype == gdata.OVERLAY_MIN):
358
        return getMineralColorCode(mineral)
359
    if (datatype == gdata.OVERLAY_SLOT):
360
        return getSlotColorCode(slot)
361
    if (datatype == gdata.OVERLAY_STARGATE):
362
        return getStargateColorCode(stargate)
363
    if (datatype == gdata.OVERLAY_DOCK):
364
        return getDockColorCode(dockfuel,dockupgrade)
365
    if (datatype == gdata.OVERLAY_FAME):
366
        return getFameColorCode(fame)
367
    if (datatype == gdata.OVERLAY_PIRATECOLONYCOST):
368
        return getPirateColonyColorCode(pirate)
369
#    if (datatype == "stratres"):
370
#        return getMoraleColors(stratres)
371
    if (datatype == gdata.OVERLAY_MORALE):
372
        return getMoraleColors(morale)
373
    return getPlayerColor(ownerid) #default
374
375
376 View Code Duplication
def getStarmapWidgetSystemColorPerDatatype(datatype,ownerid,bio,mineral,slot,num_planets,stargate,dockfuel,dockupgrade,fame,stratres,morale,pirate=False):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
377
    if (datatype == gdata.OVERLAY_OWNER):
378
        return getPlayerColor(ownerid)
379
    if (datatype == gdata.OVERLAY_DIPLO):
380
        return getPlayerColor(ownerid,True)
381
    if (datatype == gdata.OVERLAY_BIO):
382
        return getHabitabilityColorCode(bio)
383
    if (datatype == gdata.OVERLAY_MIN):
384
        return getMineralColorCode(mineral)
385
    if (datatype == gdata.OVERLAY_SLOT):
386
        return getSlotSystemColorCode(slot,num_planets)
387
    if (datatype == gdata.OVERLAY_STARGATE):
388
        return getStargateColorCode(stargate)
389
    if (datatype == gdata.OVERLAY_DOCK):
390
        return getDockColorCode(dockfuel,dockupgrade)
391
    if (datatype == gdata.OVERLAY_FAME):
392
        return getFameColorCode(fame)
393
    if (datatype == gdata.OVERLAY_PIRATECOLONYCOST):
394
        return getPirateColonyColorCode(pirate)
395
#    if (datatype == "stratres"):
396
#        return getMoraleColors(stratres)
397
    if (datatype == gdata.OVERLAY_MORALE):
398
        return getMoraleColors(morale)
399
    return getPlayerColor(ownerid) #default
400
401
def fadeColor(triplet):
402
    return ((triplet[0]+0xc0)/2,(triplet[1]+0xc0)/2,(triplet[2]+0xc0)/2)
403
404
def fadeDarkColor(triplet):
405
    return ((triplet[0]+0x00*2)/3,(triplet[1]+0x00*2)/3,(triplet[2]+0x00*2)/3)
406
407
def formatTime(time,separator=':'):
408
    time = int(math.ceil(time))
409
    sign = ''
410
    if time < 0:
411
        time = - time
412
        sign = '-'
413
    days = time / Rules.turnsPerDay
414
    hours = time % Rules.turnsPerDay
415
    return '%s%d%s%02d' % (sign, days, separator, hours)
416
417
def formatBE(b, e):
418
    return '%d / %d' % (b, e)
419
420
def globalQueueName(index):
421
    return ['Default', 'Red', 'Blue', 'Yellow', 'Violet'][index]
422